@Override public void deserializeWorld(EntityData.World world) { entityManager.setNextId(world.getNextEntityId()); for (Integer deadId : world.getFreedEntityIdList()) { entityManager.getFreedIds().add(deadId); } for (EntityData.Prefab prefabData : world.getPrefabList()) { if (!prefabManager.exists(prefabData.getName())) { deserializePrefab(prefabData); } } for (int index = 0; index < world.getComponentClassCount(); ++index) { ComponentMetadata componentMetadata = componentLibrary.getMetadata(world.getComponentClass(index)); if (componentMetadata != null) { componentIdTable.put(index, componentMetadata.getType()); } } for (EntityData.Entity entityData : world.getEntityList()) { deserializeEntity(entityData); } }
private void writeIdInfo(final EntityData.World.Builder world) { world.setNextEntityId(entityManager.getNextId()); entityManager .getFreedIds() .forEach( new TIntProcedure() { public boolean execute(int i) { world.addFreedEntityId(i); return true; } }); }
@Override public EntityRef deserializeEntity(EntityData.Entity entityData) { EntityRef entity = entityManager.createEntityRefWithId(entityData.getId()); if (entityData.hasParentPrefab() && !entityData.getParentPrefab().isEmpty() && prefabManager.exists(entityData.getParentPrefab())) { Prefab prefab = prefabManager.getPrefab(entityData.getParentPrefab()); for (Component component : prefab.listComponents()) { String componentName = ComponentUtil.getComponentClassName(component.getClass()); if (!containsIgnoreCase(componentName, entityData.getRemovedComponentList())) { entity.addComponent(componentLibrary.copy(component)); } } entity.addComponent(new EntityInfoComponent(entityData.getParentPrefab())); } for (EntityData.Component componentData : entityData.getComponentList()) { Class<? extends Component> componentClass = getComponentClass(componentData); if (componentClass == null) continue; if (!entity.hasComponent(componentClass)) { entity.addComponent(deserializeComponent(componentData)); } else { deserializeComponentOnto(entity.getComponent(componentClass), componentData); } } return entity; }
@Override public EntityData.World serializeWorld() { final EntityData.World.Builder world = EntityData.World.newBuilder(); writeIdInfo(world); if (isUsingLookupTables()) { writeComponentTypeTable(world); } for (Prefab prefab : prefabManager.listPrefabs()) { world.addPrefab(serializePrefab(prefab)); } for (EntityRef entity : entityManager.iteratorEntities()) { world.addEntity(serializeEntity(entity)); } return world.build(); }
public EntityPersisterHelperImpl(PersistableEntityManager entityManager) { this.componentLibrary = entityManager.getComponentLibrary(); this.entityManager = entityManager; this.prefabManager = entityManager.getPrefabManager(); }