Search in sources :

Example 26 with Component

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;
}
Also used : ComponentMetadata(org.terasology.entitySystem.metadata.ComponentMetadata) EntityInfoComponent(org.terasology.entitySystem.entity.internal.EntityInfoComponent) Component(org.terasology.entitySystem.Component) Prefab(org.terasology.entitySystem.prefab.Prefab) EntityInfoComponent(org.terasology.entitySystem.entity.internal.EntityInfoComponent)

Example 27 with Component

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());
        }
    }
}
Also used : EntityData(org.terasology.protobuf.EntityData) EntityInfoComponent(org.terasology.entitySystem.entity.internal.EntityInfoComponent) Component(org.terasology.entitySystem.Component) EntityInfoComponent(org.terasology.entitySystem.entity.internal.EntityInfoComponent)

Example 28 with Component

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;
}
Also used : ByteString(com.google.protobuf.ByteString) Component(org.terasology.entitySystem.Component)

Example 29 with Component

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);
    }
}
Also used : Component(org.terasology.entitySystem.Component) Prefab(org.terasology.entitySystem.prefab.Prefab) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 30 with 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;
    });
}
Also used : ClientComponent(org.terasology.network.ClientComponent) LocationComponent(org.terasology.logic.location.LocationComponent) Component(org.terasology.entitySystem.Component) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Aggregations

Component (org.terasology.entitySystem.Component)49 EntityRef (org.terasology.entitySystem.entity.EntityRef)15 Prefab (org.terasology.entitySystem.prefab.Prefab)9 LocationComponent (org.terasology.logic.location.LocationComponent)9 EntityData (org.terasology.protobuf.EntityData)8 OnActivatedComponent (org.terasology.entitySystem.entity.lifecycleEvents.OnActivatedComponent)7 NetworkComponent (org.terasology.network.NetworkComponent)7 BlockComponent (org.terasology.world.block.BlockComponent)7 OnAddedComponent (org.terasology.entitySystem.entity.lifecycleEvents.OnAddedComponent)6 OnChangedComponent (org.terasology.entitySystem.entity.lifecycleEvents.OnChangedComponent)6 EntityInfoComponent (org.terasology.entitySystem.entity.internal.EntityInfoComponent)5 BeforeDeactivateComponent (org.terasology.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent)5 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)4 ByteString (com.google.protobuf.ByteString)3 Test (org.junit.Test)3 BeforeRemoveComponent (org.terasology.entitySystem.entity.lifecycleEvents.BeforeRemoveComponent)3 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)3 SectorSimulationComponent (org.terasology.entitySystem.sectors.SectorSimulationComponent)3 ParticleEmitterComponent (org.terasology.particles.components.ParticleEmitterComponent)3 WorldConfigurator (org.terasology.world.generator.WorldConfigurator)3