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); * } * }*/ } } }
public void EnumerateEntitiesOwnedBy(int ownerUniqueId, ICollection <int> liveIds, EntityEnumeration enumerationType) { //ComponentManager<Inventory> inventoryComponentManager = (ComponentManager<Inventory>)componentManagers[ComponentTypeIds.Inventory]; //ComponentManager<TransformChildren> transformChildrenComponentManager = (ComponentManager<TransformChildren>)componentManagers[ComponentTypeIds.TransformChildren]; Debug.Assert(liveIds.Count == 0); for (int i = 0; i < partitionIndex; i++) { int liveId = liveIdRoster[i]; Entity e = entityPool[liveId]; Debug.Assert(e.LiveId == liveId); if (e.OwnerUniqueId == ownerUniqueId) { liveIds.Add(liveId); if (enumerationType == EntityEnumeration.Deep) { throw new NotImplementedException(); /* * // Check to see if it has children. This is a little strange, because the entity * // manager now has knowledge of the Inventory component. We could move this out somewhere. * // For TransformChildren, on the other hand, it makes sense for EntityManager to know about. * int componentLookupId = GetComponentLookupIdForEntity(e, ComponentTypeIds.Inventory); * if (componentLookupId != -1) * { * Inventory inventory = inventoryComponentManager.GetComponentAt<Inventory>(componentLookupId); * foreach (int uniqueId in inventory.GetContainedItemUniqueIds()) * { * liveIds.Add(GetEntityByUniqueId(uniqueId).LiveId); * } * } * * componentLookupId = GetComponentLookupIdForEntity(e, ComponentTypeIds.TransformChildren); * if (componentLookupId != -1) * { * TransformChildren transformChildren = transformChildrenComponentManager.GetComponentAt<TransformChildren>(componentLookupId); * foreach (int uniqueId in transformChildren.GetContainedItemUniqueIds()) * { * liveIds.Add(GetEntityByUniqueId(uniqueId).LiveId); * } * }*/ } } } }