コード例 #1
0
        private bool SetCharacter(Entity entity, int id, bool isLoadingPlayer, Entity world, int metaID, int classID, int clanID, float3 position, int creatorID = 0)
        {
            if (!World.EntityManager.Exists(world))
            {
                Debug.LogError("Cannot spawn character into a non existing world.");
                return(false);
            }
            if (!World.EntityManager.HasComponent <Voxels.World>(world))
            {
                Debug.LogError("Cannot spawn character into a world without a component.");
                return(false);
            }
            if (characters.ContainsKey(id) == true)
            {
                return(false);
            }
            characters.Add(id, entity);
            if (!meta.ContainsKey(metaID))
            {
                Debug.LogError("Meta not contained: " + metaID);
                return(false);
            }
            CharacterDatam characterDatam = meta[metaID];

            // ZOXID
            World.EntityManager.SetComponentData(entity,
                                                 new ZoxID
            {
                id        = id,
                clanID    = clanID,
                creatorID = creatorID
            });
            World.EntityManager.SetComponentData(entity,
                                                 new Character
            {
                metaID = metaID
            });
            // WORLD BINDING
            int3 voxelDimensions = new int3(16, 64, 16); // float3.zero;

            if (worldSpawnSystem != null)
            {
                voxelDimensions = World.EntityManager.GetComponentData <Voxels.World>(world).voxelDimensions;
            }
            // TRANSFORMS
            World.EntityManager.SetComponentData(entity, new Translation {
                Value = position
            });
            World.EntityManager.SetComponentData(entity, new Rotation {
                Value = quaternion.identity
            });
            World.EntityManager.SetComponentData(entity, new NonUniformScale
            {
                Value = new float3(1, 1, 1)
            });
            // RENDERING
            World.EntityManager.SetSharedComponentData(entity, new RenderMesh {
                material       = Bootstrap.GetVoxelMaterial(),
                mesh           = new Mesh(),
                castShadows    = UnityEngine.Rendering.ShadowCastingMode.On,
                receiveShadows = true
            });
            float3 bodySize = new float3(0.1f, 0.1f, 0.1f);

            if (characterDatam.vox != null && !isLoadingPlayer)
            {
                bodySize = characterDatam.vox.data.GetSize();
                World.EntityManager.SetComponentData(entity, new Body {
                    size = bodySize
                });
                // this can be done in equip system
                WorldSpawnSystem.QueueUpdateModel(World.EntityManager, entity, id, characterDatam.vox.data);
            }
            World.EntityManager.SetComponentData(entity, new WorldBound {
                size            = bodySize,
                world           = world,
                voxelDimensions = voxelDimensions
            });
            World.EntityManager.SetComponentData(entity, characterDatam.stats.Clone());
            // Physics
            World.EntityManager.SetComponentData(entity, new BodyInnerForce
            {
                movementForce  = characterDatam.movementSpeed,
                movementTorque = characterDatam.turnSpeed,
                maxVelocity    = characterDatam.maxVelocity
            });
            // combat stuff
            World.EntityManager.SetComponentData(entity,
                                                 new Targeter
            {
                Value = characterDatam.behaviour.Value.seek
            });

            if (!isLoadingPlayer)
            {
                if (classID != 0)
                {
                    GiveClassSkills(id, classMeta[classID]);
                }
                else if (characterDatam.defaultClass)
                {
                    GiveClassSkills(id, characterDatam.defaultClass);
                }
            }
            Equipment equipment = new Equipment {
            };

            if (!isLoadingPlayer)
            {
                equipment.EquipBody(characterDatam.body);
                equipment.EquipGear(characterDatam.gear);
            }
            World.EntityManager.SetComponentData(entity, equipment);
            return(true);
        }
コード例 #2
0
ファイル: EquipmentSystem.cs プロジェクト: Deus0/zoxel
        // real positions
        // first take away center offset - half bounds of mesh
        //float3 halfBounds = 0.5f * model.size.ToFloat3();
        // Debug.LogError("model.size: " + model.size + ", halfBounds: " + (halfBounds / 16f));
        // Debug.LogError("min: " + min + ", max: " + max + ", addition: " + addition);

        /*for (int i = 0; i < bodyLayer.realPositions.Count; i++)
         * {
         *  bodyLayer.realPositions[i] += addition.ToFloat3();
         *  bodyLayer.realPositions[i] -= halfBounds;
         *  bodyLayer.realPositions[i] = new float3(
         *      bodyLayer.realPositions[i].x / ((float)model.size.x),
         *      bodyLayer.realPositions[i].y / ((float)model.size.y),
         *      bodyLayer.realPositions[i].z / ((float)model.size.z)
         *  );
         *  //Debug.LogError("Offset is: " + bodyLayer.positions[i] + " and real: " + bodyLayer.realPositions[i]);
         * }*/

        bool AddChildren(ref Equipment equipment,
                         ref VoxBuildLayer bodyLayer, ref VoxBuildLayer gearLayer,
                         VoxOperation lastOperation, Item parentBodyPart, int bodyIndex, int3 position)
        {
            // for all female slots, find an empty one if it is the slot of our male one
            var parentModel = meta[parentBodyPart.id].model.data;   // get the part im attached to

            for (int j = 0; j < parentBodyPart.femaleSlots.Length; j++)
            {
                var  femaleSlot     = parentBodyPart.femaleSlots[j];
                byte femaleModifier = 0;
                if (j < parentBodyPart.femaleModifiers.Length)
                {
                    femaleModifier = parentBodyPart.femaleModifiers[j];
                }
                int3 femaleOffset = int3.Zero();
                if (j < parentBodyPart.femaleOffsets.Length)
                {
                    femaleOffset = parentBodyPart.femaleOffsets[j];
                }
                // find child index - bodyIndex is childIndex
                for (int k = 0; k < equipment.body.Length; k++)
                {
                    var attachedBodyPart = equipment.body[k];
                    if (bodyIndex != k && attachedBodyPart.bodyIndex == bodyIndex && attachedBodyPart.slotIndex == j && meta.ContainsKey(attachedBodyPart.data.id))
                    {
                        var attachItem = attachedBodyPart.data;
                        var maleSlot   = attachItem.maleSlot;
                        if (maleSlot.id == femaleSlot.id)
                        {
                            var attachedModel = meta[attachItem.id].model.data;       // new part adding to body
                            var operation     = lastOperation;
                            if (((VoxOperation)femaleModifier) != VoxOperation.None)
                            {
                                operation = ((VoxOperation)femaleModifier);
                            }
                            int3 offset = position;

                            // using axis, calculate position offset

                            if (femaleSlot.axis == ((byte)SlotAxis.Bottom))
                            {
                                offset   += new int3(0, -attachedModel.size.y, 0);
                                offset.x += (parentModel.size.x - attachedModel.size.x) / 2;
                                offset.z += (parentModel.size.z - attachedModel.size.z) / 2;
                            }
                            else if (femaleSlot.axis == ((byte)SlotAxis.Top))
                            {
                                offset   += new int3(0, parentModel.size.y, 0);
                                offset.x += (parentModel.size.x - attachedModel.size.x) / 2;
                                offset.z += (parentModel.size.z - attachedModel.size.z) / 2;
                            }
                            else if (femaleSlot.axis == ((byte)SlotAxis.Left))
                            {
                                offset.y += (parentModel.size.y - attachedModel.size.y) / 2;
                                offset.z += (parentModel.size.z - attachedModel.size.z) / 2;
                                if (operation == VoxOperation.FlipX)
                                {
                                    offset += new int3(parentModel.size.x, 0, 0);
                                }
                                else
                                {
                                    offset += new int3(-attachedModel.size.x, 0, 0);
                                }
                            }
                            else if (femaleSlot.axis == ((byte)SlotAxis.Right))
                            {
                                offset.y += (parentModel.size.y - attachedModel.size.y) / 2;
                                offset.z += (parentModel.size.z - attachedModel.size.z) / 2;
                                if (operation == VoxOperation.FlipX)
                                {
                                    offset += new int3(-attachedModel.size.x, 0, 0);
                                }
                                else
                                {
                                    offset += new int3(parentModel.size.x, 0, 0);
                                }
                            }
                            // done
                            if (operation == VoxOperation.FlipX)
                            {
                                offset += new int3(-attachItem.offset.x, attachItem.offset.y, attachItem.offset.z);
                                offset += new int3(-femaleOffset.x, femaleOffset.y, femaleOffset.z);
                            }
                            else
                            {
                                offset += attachItem.offset;
                                offset += femaleOffset;
                                //Debug.LogError("Norm: Offsetting model by: " + (femaleOffset.x));
                                if (femaleSlot.axis == ((byte)SlotAxis.Bottom) || femaleSlot.axis == ((byte)SlotAxis.Top))
                                {
                                    offset += new int3((attachedModel.size.x % 2), 0, (attachedModel.size.z % 2));
                                    offset += new int3((parentModel.size.x % 2), 0, (parentModel.size.z % 2));
                                }
                            }
                            int3 bonePosition = offset;
                            if (femaleSlot.axis == ((byte)SlotAxis.Bottom) || femaleSlot.axis == ((byte)SlotAxis.Top))
                            {
                                bonePosition.x += attachedModel.size.x / 2;
                                bonePosition.z += attachedModel.size.z / 2;
                            }
                            else if (femaleSlot.axis == ((byte)SlotAxis.Left) || femaleSlot.axis == ((byte)SlotAxis.Right))
                            {
                                bonePosition.y += attachedModel.size.y / 2;
                                bonePosition.z += attachedModel.size.z / 2;
                                if (operation == VoxOperation.FlipX)
                                {
                                    bonePosition.x += attachedModel.size.x;
                                }
                            }
                            if (femaleSlot.layer == 0)
                            {
                                bodyLayer.voxes.Add(attachedModel);
                                bodyLayer.positions.Add(offset);
                                bodyLayer.operations.Add(operation);
                                bodyLayer.axes.Add(femaleSlot.axis);
                                bodyLayer.parents.Add(bodyIndex);
                                bodyLayer.bonePositions.Add(bonePosition);
                            }
                            else
                            {
                                gearLayer.voxes.Add(attachedModel);
                                gearLayer.positions.Add(offset);
                                gearLayer.operations.Add(operation);
                                gearLayer.axes.Add(femaleSlot.axis);
                                gearLayer.parents.Add(bodyIndex);
                                gearLayer.bonePositions.Add(bonePosition);
                            }
                            if (!AddChildren(ref equipment, ref bodyLayer, ref gearLayer, operation, attachItem, k, offset))
                            {
                                return(false);
                            }
                            break;
                        }
                    }
                }
            }
            return(true);
        }
コード例 #3
0
        private void SaveComponentData <T>(Entity e, string typeName, string entityName) where T : struct, IComponentData
        {
            string filePath = GetFolderPath(typeName, entityName) + typeof(T).Name.ToString() + ".txt";
            //Debug.LogError("Saving [" + entityName + "] [" + typeof(T).Name.ToString() + "] to File at: " + filePath);
            string json;

            /*if (typeof(BlitzSerializeable<T>) == typeof(T))
             * {
             *  BlitzSerializeable<T> component = EntityManager.GetComponentData<BlitzSerializeable<T>>(e);
             *  json = component.GetJson();
             * }*/
            if (typeof(T) == typeof(Inventory))
            {
                if (EntityManager.HasComponent <Inventory>(e) == false)
                {
                    Debug.LogError("Character has no inventory..");
                    json = "";
                }
                else
                {
                    Inventory component = EntityManager.GetComponentData <Inventory>(e);
                    json = component.GetJson();
                }
            }
            else if (typeof(T) == typeof(Stats))
            {
                if (World.EntityManager.HasComponent <Stats>(e))
                {
                    Stats component = EntityManager.GetComponentData <Stats>(e);
                    json = component.GetJson();
                }
                else
                {
                    json = "";
                }
            }
            else if (typeof(T) == typeof(Skills))
            {
                if (World.EntityManager.HasComponent <Skills>(e))
                {
                    Skills component = EntityManager.GetComponentData <Skills>(e);
                    json = component.GetJson();
                }
                else
                {
                    json = "";
                }
            }
            else if (typeof(T) == typeof(Equipment))
            {
                if (World.EntityManager.HasComponent <Equipment>(e))
                {
                    Equipment component = EntityManager.GetComponentData <Equipment>(e);
                    json = component.GetJson();
                }
                else
                {
                    json = "";
                }
            }
            else if (typeof(T) == typeof(QuestLog))
            {
                if (World.EntityManager.HasComponent <QuestLog>(e))
                {
                    QuestLog component = EntityManager.GetComponentData <QuestLog>(e);
                    json = component.GetJson();
                }
                else
                {
                    json = "";
                }
            }
            else
            {
                T component = EntityManager.GetComponentData <T>(e);
                json = ToJson(component);
            }
            //Debug.Log("Saving Json to: " + filePath + " of type " + typeof(T).ToString() + "\n" + json);
            System.IO.File.WriteAllText(filePath, json);
        }