use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class SaveTransaction method createChunkPosToUnsavedOwnerLessEntitiesMap.
private Map<Vector3i, Collection<EntityRef>> createChunkPosToUnsavedOwnerLessEntitiesMap() {
Map<Vector3i, Collection<EntityRef>> chunkPosToEntitiesMap = Maps.newHashMap();
for (EntityRef entity : privateEntityManager.getEntitiesWith(LocationComponent.class)) {
/*
* Note: Entities with owners get saved with the owner. Entities that are always relevant don't get stored
* in chunk as the chunk is not always loaded
*/
if (entity.isPersistent() && !entity.getOwner().exists() && !entity.hasComponent(ClientComponent.class) && !entity.isAlwaysRelevant()) {
LocationComponent locationComponent = entity.getComponent(LocationComponent.class);
if (locationComponent != null) {
Vector3f loc = locationComponent.getWorldPosition();
Vector3i chunkPos = ChunkMath.calcChunkPos((int) loc.x, (int) loc.y, (int) loc.z);
Collection<EntityRef> collection = chunkPosToEntitiesMap.get(chunkPos);
if (collection == null) {
collection = Lists.newArrayList();
chunkPosToEntitiesMap.put(chunkPos, collection);
}
collection.add(entity);
}
}
}
return chunkPosToEntitiesMap;
}
use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class EntitySerializer method serializeEntityFull.
private EntityData.Entity serializeEntityFull(EntityRef entityRef, FieldSerializeCheck<Component> fieldCheck) {
EntityData.Entity.Builder entity = EntityData.Entity.newBuilder();
if (!ignoringEntityId) {
entity.setId(entityRef.getId());
}
entity.setAlwaysRelevant(entityRef.isAlwaysRelevant());
EntityRef owner = entityRef.getOwner();
if (owner.exists()) {
entity.setOwner(owner.getId());
}
EntityScope scope = entityRef.getScope();
if (scope != null) {
switch(scope) {
case GLOBAL:
entity.setScope(GLOBAL);
break;
case SECTOR:
entity.setScope(SECTOR);
break;
case CHUNK:
entity.setScope(CHUNK);
break;
}
}
for (Component component : entityRef.iterateComponents()) {
if (!componentSerializeCheck.serialize(componentLibrary.getMetadata(component.getClass()))) {
continue;
}
EntityData.Component componentData = componentSerializer.serialize(component, fieldCheck);
if (componentData != null) {
entity.addComponent(componentData);
}
}
return entity.build();
}
use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class EntitySerializer method serializeEntityDelta.
private EntityData.Entity serializeEntityDelta(EntityRef entityRef, Prefab prefab, FieldSerializeCheck<Component> fieldCheck) {
EntityData.Entity.Builder entity = EntityData.Entity.newBuilder();
if (!ignoringEntityId) {
entity.setId(entityRef.getId());
}
entity.setParentPrefab(prefab.getName());
if (entityRef.isAlwaysRelevant() != prefab.isAlwaysRelevant()) {
entity.setAlwaysRelevant(entityRef.isAlwaysRelevant());
}
EntityRef owner = entityRef.getOwner();
if (owner.exists()) {
entity.setOwner(owner.getId());
}
EntityScope scope = entityRef.getScope();
if (scope != null) {
switch(scope) {
case GLOBAL:
entity.setScope(GLOBAL);
break;
case SECTOR:
entity.setScope(SECTOR);
break;
case CHUNK:
entity.setScope(CHUNK);
break;
}
}
Set<Class<? extends Component>> presentClasses = Sets.newHashSet();
for (Component component : entityRef.iterateComponents()) {
if (!componentSerializeCheck.serialize(componentLibrary.getMetadata(component.getClass()))) {
continue;
}
presentClasses.add(component.getClass());
Component prefabComponent = prefab.getComponent(component.getClass());
EntityData.Component componentData;
if (prefabComponent == null) {
componentData = componentSerializer.serialize(component, fieldCheck);
} else {
componentData = componentSerializer.serialize(prefabComponent, component, fieldCheck);
}
if (componentData != null) {
entity.addComponent(componentData);
}
}
for (Component prefabComponent : prefab.iterateComponents()) {
ComponentMetadata<?> metadata = componentLibrary.getMetadata(prefabComponent.getClass());
if (!presentClasses.contains(prefabComponent.getClass()) && componentSerializeCheck.serialize(metadata)) {
// TODO: Use component ids here
entity.addRemovedComponent(metadata.getUri().toString());
}
}
return entity.build();
}
use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class WorldSerializerImpl method serializeWorld.
@Override
public EntityData.GlobalStore serializeWorld(boolean verbose) {
final EntityData.GlobalStore.Builder world = EntityData.GlobalStore.newBuilder();
if (!verbose) {
writeComponentTypeTable(world);
}
for (Prefab prefab : prefabManager.listPrefabs()) {
world.addPrefab(prefabSerializer.serialize(prefab));
}
for (EntityRef entity : entityManager.getAllEntities()) {
if (verbose || entity.isPersistent()) {
world.addEntity(entitySerializer.serialize(entity));
}
}
writeIdInfo(world);
entitySerializer.removeComponentIdMapping();
prefabSerializer.removeComponentIdMapping();
return world.build();
}
use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class EntityRefTypeHandler method deserializeCollection.
@Override
public List<EntityRef> deserializeCollection(PersistedData data, DeserializationContext context) {
PersistedDataArray array = data.getAsArray();
List<EntityRef> result = Lists.newArrayListWithCapacity(array.size());
addEntitiesFromLongArray(result, array);
return result;
}
Aggregations