Example #1
0
        public static EntityTemplate CreateFromOther(string name, EntityTemplate cloneSource)
        {
            List <Component> newComponents = new List <Component>();

            foreach (Component component in cloneSource.ComponentTemplates)
            {
                Component newComponent = (Component)Activator.CreateInstance(component.GetType());
                newComponent.ApplyFromTemplate(component);
                newComponents.Add(newComponent);
            }
            EntityTemplate foo = new EntityTemplate(name, newComponents.ToArray());

            foo.ParentName = cloneSource.Name;

            // Children! phil
            if ((cloneSource.ChildTemplates != null) && (cloneSource.ChildTemplates.Count > 0))
            {
                foo.AddChildren(cloneSource.ChildTemplates);
            }

            return(foo);
        }
Example #2
0
        // TODO: This has code in common with SerializeEntity a bit.
        private bool IsEntityExactMatchForTemplate(Entity childEntity, EntityTemplate childEntityTemplate)
        {
            bool exactMatch = (childEntity.TemplateId == childEntityTemplate.EntityTemplateId);

            if (exactMatch)
            {
                for (int i = 0; exactMatch && (i < EntityManager.MaxComponentTypes); i++)
                {
                    Component componentCurrent         = GetComponent(childEntity, i);
                    Component templateComponentCurrent = childEntityTemplate.ComponentArray[i];
                    if ((componentCurrent != null) && (templateComponentCurrent != null))
                    {
                        exactMatch = !componentCurrent.IsDifferent(childEntity, templateComponentCurrent);
                    }
                    else
                    {
                        exactMatch = (componentCurrent == null) && (templateComponentCurrent == null);
                    }
                }
            }
            return(exactMatch);
        }
Example #3
0
        public static EntityTemplate NewInheritFrom(string name, string parent)
        {
            EntityTemplate cloneSource = EntityTemplateManager.GetTemplateByName(parent);

            return(CreateFromOther(name, cloneSource));
        }
Example #4
0
        public void SerializeEntity(BinaryWriter writer, Entity entity, List <int> extraIdsToSerialize, EntityEnumeration enumerationType, List <int> liveIdsWeSkippedSerializing)
        {
            Debug.Assert(((enumerationType == EntityEnumeration.Deep) && (extraIdsToSerialize != null)) ||
                         ((enumerationType == EntityEnumeration.Shallow) && (extraIdsToSerialize == null)));

            writer.Write(entity.UniqueId);
            writer.Write(entity.OwnerUniqueId);
            writer.Write(entity.TemplateId);

            EntityTemplate entityTemplate = EntityTemplateManager.GetTemplateById(entity.TemplateId);

            //bool addTransformChildrenToExtraIdsList = false;

            // Now we write the number of components. To know how many we're going to write out,
            // we need to figure out how many are different than the template ones.
            int saveCount = 0;

            for (int i = 0; i < EntityManager.MaxComponentTypes; i++)
            {
                Component componentCurrent = GetComponent(entity, i);
                serializationComponentWorker1[i] = null;
                if (componentCurrent != null)
                {
                    bool exactMatch = false;
                    //if ((i == ComponentTypeIds.TransformChildren) && (enumerationType == EntityEnumeration.Deep))
                    if (enumerationType == EntityEnumeration.Deep)
                    {
                        throw new NotImplementedException();

                        /*
                         * // Big optimization: For TransformChildren, if the set of transform children for this entity matches
                         * // *exactly* what's in the template's children, then we can skip adding this component altogether,
                         * // saving a lot of space. We''ll assume the same order too.
                         * TransformChildren transformChildren = componentCurrent as TransformChildren;
                         * List<int> containedItemUniqueIds = transformChildren.GetContainedItemUniqueIds();
                         * exactMatch = (containedItemUniqueIds.Count == entityTemplate.ChildTemplates.Count);
                         * if (exactMatch)
                         * {
                         *  for (int matchIndex = 0; exactMatch && (matchIndex < containedItemUniqueIds.Count); matchIndex++)
                         *  {
                         *      Entity childEntity = GetEntityByUniqueId(containedItemUniqueIds[matchIndex]);
                         *      EntityTemplate childEntityTemplate = entityTemplate.ChildTemplates[matchIndex];
                         *      exactMatch = IsEntityExactMatchForTemplate(childEntity, childEntityTemplate);
                         *  }
                         * }
                         *
                         * // We'll need to serialize the extra ids if this wasn't an exact match.
                         * addTransformChildrenToExtraIdsList = !exactMatch;
                         *
                         * // If we're not adding them to the "please serialize" list, we need to add them to the
                         * // list of things we skipped serializing (in case they need to be removed)
                         * if (!addTransformChildrenToExtraIdsList)
                         * {
                         *  foreach (int uniqueId in containedItemUniqueIds)
                         *  {
                         *      liveIdsWeSkippedSerializing.Add(GetEntityByUniqueId(uniqueId).LiveId);
                         *  }
                         * }*/
                    }
                    else
                    {
                        Component templateComponent = entityTemplate.ComponentArray[i];
                        exactMatch = (templateComponent != null) && !componentCurrent.IsDifferent(entity, templateComponent);
                    }

                    if (!exactMatch)
                    {
                        // Need to serialize it, because it doesn't match the template.
                        serializationComponentWorker1[i] = componentCurrent;
                        saveCount++;
                    }
                }
            }

            writer.Write((byte)saveCount);

            for (int i = 0; i < EntityManager.MaxComponentTypes; i++)
            {
                Component componentCurrent = serializationComponentWorker1[i];
                if (componentCurrent != null)
                {
                    writer.Write(typeToComponentId[componentCurrent.GetType()]);
                    componentCurrent.Serialize(writer);

                    // Inventory is straightforward, because these are never part of a template, so we don't need to optimize them out.
                    // So always add them to the extra ids to serialize.
                    if (enumerationType == EntityEnumeration.Deep)
                    {
                        throw new NotImplementedException();
                    }

                    /*
                     * if ((i == ComponentTypeIds.Inventory) && (enumerationType == EntityEnumeration.Deep))
                     * {
                     *  Inventory inventory = componentCurrent as Inventory;
                     *  foreach (int uniqueId in inventory.GetContainedItemUniqueIds())
                     *  {
                     *      extraIdsToSerialize.Add(GetEntityByUniqueId(uniqueId).LiveId);
                     *  }
                     * }
                     * else if ((i == ComponentTypeIds.TransformChildren) && addTransformChildrenToExtraIdsList)
                     * {
                     *  TransformChildren transformChildren = componentCurrent as TransformChildren;
                     *  foreach (int uniqueId in transformChildren.GetContainedItemUniqueIds())
                     *  {
                     *      extraIdsToSerialize.Add(GetEntityByUniqueId(uniqueId).LiveId);
                     *  }
                     * }*/
                }
            }
        }