use of org.terasology.entitySystem.Component in project Terasology by MovingBlocks.
the class EntitySerializer method createInitialComponents.
/**
* Creates the components for the entity being deserialized based on its prefab (if any)
*
* @param entityData
* @return The mapping of components
*/
private Map<Class<? extends Component>, Component> createInitialComponents(EntityData.Entity entityData) {
Set<ComponentMetadata<?>> removedComponents = Sets.newHashSet();
for (String removedComp : entityData.getRemovedComponentList()) {
ComponentMetadata<?> removedMetadata = componentLibrary.resolve(removedComp);
if (removedMetadata != null) {
removedComponents.add(removedMetadata);
}
}
Map<Class<? extends Component>, Component> componentMap = Maps.newHashMap();
if (entityData.hasParentPrefab() && !entityData.getParentPrefab().isEmpty() && prefabManager.exists(entityData.getParentPrefab())) {
Prefab prefab = prefabManager.getPrefab(entityData.getParentPrefab());
for (Component component : prefab.iterateComponents()) {
ComponentMetadata<?> metadata = componentLibrary.getMetadata(component);
if (!removedComponents.contains(metadata)) {
componentMap.put(component.getClass(), componentLibrary.copy(component));
}
}
componentMap.put(EntityInfoComponent.class, new EntityInfoComponent(prefab, true, prefab.isAlwaysRelevant()));
}
return componentMap;
}
use of org.terasology.entitySystem.Component in project Terasology by MovingBlocks.
the class EntitySerializer method deserializeOntoComponents.
/**
* Deserializes the components from an EntityData onto a map of components
*
* @param entityData
* @param componentMap
*/
private void deserializeOntoComponents(EntityData.Entity entityData, Map<Class<? extends Component>, Component> componentMap) {
EntityInfoComponent entityInfo = (EntityInfoComponent) componentMap.get(EntityInfoComponent.class);
if (entityInfo == null) {
entityInfo = new EntityInfoComponent();
componentMap.put(EntityInfoComponent.class, entityInfo);
}
if (entityData.hasOwner()) {
entityInfo.owner = entityManager.getEntity(entityData.getOwner());
}
if (entityData.hasAlwaysRelevant()) {
entityInfo.alwaysRelevant = entityData.getAlwaysRelevant();
}
switch(entityData.getScope()) {
case GLOBAL:
entityInfo.scope = EntityScope.GLOBAL;
break;
case SECTOR:
entityInfo.scope = EntityScope.SECTOR;
break;
case CHUNK:
entityInfo.scope = EntityScope.CHUNK;
break;
}
for (EntityData.Component componentData : entityData.getComponentList()) {
ComponentMetadata<? extends Component> metadata = componentSerializer.getComponentMetadata(componentData);
if (metadata == null || !componentSerializeCheck.serialize(metadata)) {
continue;
}
Component existingComponent = componentMap.get(metadata.getType());
if (existingComponent == null) {
Component newComponent = componentSerializer.deserialize(componentData);
componentMap.put(metadata.getType(), newComponent);
} else {
componentSerializer.deserializeOnto(existingComponent, componentData, FieldSerializeCheck.NullCheck.<Component>newInstance());
}
}
}
use of org.terasology.entitySystem.Component in project Terasology by MovingBlocks.
the class NetworkEntitySerializer method serializeEntityDelta.
private EntityData.PackedEntity.Builder serializeEntityDelta(EntityRef entityRef, Prefab prefab, FieldSerializeCheck<Component> fieldCheck) {
EntityData.PackedEntity.Builder entity = EntityData.PackedEntity.newBuilder();
entity.setParentPrefabUri(prefab.getName());
Set<Class<? extends Component>> presentClasses = Sets.newHashSet();
ByteString.Output fieldIds = ByteString.newOutput();
ByteString.Output componentFieldCounts = ByteString.newOutput();
for (Component component : entityRef.iterateComponents()) {
if (!componentSerializeCheck.serialize(componentLibrary.getMetadata(component.getClass()))) {
continue;
}
presentClasses.add(component.getClass());
Component prefabComponent = prefab.getComponent(component.getClass());
if (prefabComponent == null) {
serializeComponentFull(component, false, fieldCheck, entity, fieldIds, componentFieldCounts, true);
} else {
serializeComponentDelta(prefabComponent, component, fieldCheck, entity, fieldIds, componentFieldCounts, true);
}
}
entity.setFieldIds(fieldIds.toByteString());
entity.setComponentFieldCounts(componentFieldCounts.toByteString());
for (Component prefabComponent : prefab.iterateComponents()) {
if (!presentClasses.contains(prefabComponent.getClass()) && componentSerializeCheck.serialize(componentLibrary.getMetadata(prefabComponent.getClass()))) {
entity.addRemovedComponent(idTable.get(prefabComponent.getClass()));
}
}
return entity;
}
use of org.terasology.entitySystem.Component in project Terasology by MovingBlocks.
the class LocalChunkProvider method generateQueuedEntities.
private void generateQueuedEntities(EntityStore store) {
Prefab prefab = store.getPrefab();
EntityRef entity;
if (prefab != null) {
entity = entityManager.create(prefab);
} else {
entity = entityManager.create();
}
for (Component component : store.iterateComponents()) {
entity.addComponent(component);
}
}
use of org.terasology.entitySystem.Component in project Terasology by MovingBlocks.
the class SaveTransaction method applyDeltaToPrivateEntityManager.
private void applyDeltaToPrivateEntityManager() {
deltaToSave.getEntityDeltas().forEachEntry((entityId, delta) -> {
if (entityId >= privateEntityManager.getNextId()) {
privateEntityManager.setNextId(entityId + 1);
}
return true;
});
deltaToSave.getDestroyedEntities().forEach(entityId -> {
if (entityId >= privateEntityManager.getNextId()) {
privateEntityManager.setNextId(entityId + 1);
}
return true;
});
deltaToSave.getEntityDeltas().forEachEntry((entityId, delta) -> {
if (privateEntityManager.isActiveEntity(entityId)) {
EntityRef entity = privateEntityManager.getEntity(entityId);
for (Component changedComponent : delta.getChangedComponents().values()) {
entity.removeComponent(changedComponent.getClass());
entity.addComponent(changedComponent);
}
delta.getRemovedComponents().forEach(entity::removeComponent);
} else {
privateEntityManager.createEntityWithId(entityId, delta.getChangedComponents().values());
}
return true;
});
final List<EntityRef> entitiesToDestroy = Lists.newArrayList();
deltaToSave.getDestroyedEntities().forEach(entityId -> {
EntityRef entityToDestroy;
if (privateEntityManager.isActiveEntity(entityId)) {
entityToDestroy = privateEntityManager.getEntity(entityId);
} else {
/**
* Create the entity as theere could be a component that references a {@link DelayedEntityRef}
* with the specified id. It is important that the {@link DelayedEntityRef} will reference
* a destroyed {@link EntityRef} instance. That is why a entity will be created, potentially
* bound to one or more {@link DelayedEntityRef}s and then destroyed.
*/
entityToDestroy = privateEntityManager.createEntityWithId(entityId, Collections.<Component>emptyList());
}
entitiesToDestroy.add(entityToDestroy);
return true;
});
/*
* Bind the delayed entities refs, before destroying the entities:
*
* That way delayed entity refs will reference the enttiy refs that got marked as destroyed and now new
* unloaded ones.
*/
deltaToSave.bindAllDelayedEntityRefsTo(privateEntityManager);
entitiesToDestroy.forEach(EntityRef::destroy);
deltaToSave.getDeactivatedEntities().forEach(entityId -> {
EntityRef entityRef = privateEntityManager.getEntity(entityId);
privateEntityManager.deactivateForStorage(entityRef);
return true;
});
}
Aggregations