Search in sources :

Example 6 with Component

use of org.terasology.entitySystem.Component in project Terasology by MovingBlocks.

the class NetworkEntitySerializer method deserializeOnto.

public void deserializeOnto(MutableComponentContainer entity, EntityData.PackedEntity entityData, FieldSerializeCheck<Component> fieldCheck) {
    int fieldPos = 0;
    for (int componentIndex = 0; componentIndex < entityData.getComponentIdCount(); ++componentIndex) {
        Integer componentId = entityData.getComponentId(componentIndex);
        Class<? extends Component> componentClass = idTable.inverse().get(componentId);
        ComponentMetadata<?> metadata = componentLibrary.getMetadata(componentClass);
        if (metadata == null) {
            logger.warn("Skipping unknown component {}", componentId);
            fieldPos += UnsignedBytes.toInt(entityData.getComponentFieldCounts().byteAt(componentIndex));
            continue;
        }
        if (!componentSerializeCheck.serialize(metadata)) {
            fieldPos += UnsignedBytes.toInt(entityData.getComponentFieldCounts().byteAt(componentIndex));
            continue;
        }
        Component component = entity.getComponent(metadata.getType());
        boolean createdNewComponent = false;
        if (component == null) {
            createdNewComponent = true;
            component = metadata.newInstance();
        }
        Serializer serializer = typeSerializationLibrary.getSerializerFor(metadata);
        for (int fieldIndex = 0; fieldIndex < UnsignedBytes.toInt(entityData.getComponentFieldCounts().byteAt(componentIndex)); ++fieldIndex) {
            byte fieldId = entityData.getFieldIds().byteAt(fieldPos);
            ReplicatedFieldMetadata fieldMetadata = metadata.getField(fieldId);
            if (fieldMetadata != null && fieldCheck.shouldDeserialize(metadata, fieldMetadata)) {
                logger.trace("Deserializing field {} of component {} as value {}", fieldMetadata, metadata, entityData.getFieldValue(fieldPos));
                serializer.deserializeOnto(component, fieldMetadata, new ProtobufPersistedData(entityData.getFieldValue(fieldPos)), deserializationContext);
            }
            fieldPos++;
        }
        if (createdNewComponent) {
            entity.addComponent(component);
        } else {
            entity.saveComponent(component);
        }
    }
    for (int componentId : entityData.getRemovedComponentList()) {
        Class<? extends Component> componentClass = idTable.inverse().get(componentId);
        ComponentMetadata<?> metadata = componentLibrary.getMetadata(componentClass);
        if (componentSerializeCheck.serialize(metadata)) {
            entity.removeComponent(metadata.getType());
        }
    }
}
Also used : ProtobufPersistedData(org.terasology.persistence.typeHandling.protobuf.ProtobufPersistedData) ReplicatedFieldMetadata(org.terasology.entitySystem.metadata.ReplicatedFieldMetadata) Component(org.terasology.entitySystem.Component) Serializer(org.terasology.persistence.typeHandling.Serializer)

Example 7 with Component

use of org.terasology.entitySystem.Component in project Terasology by MovingBlocks.

the class NetworkEntitySerializer method serialize.

public EntityData.PackedEntity serialize(EntityRef entityRef, Set<Class<? extends Component>> added, Set<Class<? extends Component>> changed, Set<Class<? extends Component>> removed, FieldSerializeCheck<Component> fieldCheck) {
    EntityData.PackedEntity.Builder entity = EntityData.PackedEntity.newBuilder();
    ByteString.Output fieldIds = ByteString.newOutput();
    ByteString.Output componentFieldCounts = ByteString.newOutput();
    for (Class<? extends Component> componentType : added) {
        Component component = entityRef.getComponent(componentType);
        if (component == null) {
            logger.error("Non-existent component marked as added: {}", componentType);
        }
        serializeComponentFull(entityRef.getComponent(componentType), false, fieldCheck, entity, fieldIds, componentFieldCounts, true);
    }
    for (Class<? extends Component> componentType : changed) {
        Component comp = entityRef.getComponent(componentType);
        if (comp != null) {
            serializeComponentFull(comp, true, fieldCheck, entity, fieldIds, componentFieldCounts, false);
        } else {
            logger.error("Non-existent component marked as changed: {}", componentType);
        }
    }
    for (Class<? extends Component> componentType : removed) {
        entity.addRemovedComponent(idTable.get(componentType));
    }
    entity.setFieldIds(fieldIds.toByteString());
    entity.setComponentFieldCounts(componentFieldCounts.toByteString());
    if (entity.getFieldIds().isEmpty() && entity.getRemovedComponentCount() == 0) {
        return null;
    } else {
        return entity.build();
    }
}
Also used : ByteString(com.google.protobuf.ByteString) Component(org.terasology.entitySystem.Component)

Example 8 with Component

use of org.terasology.entitySystem.Component in project Terasology by MovingBlocks.

the class PrefabSerializer method serialize.

/**
 * @param prefab
 * @return The serialized prefab
 */
public EntityData.Prefab serialize(Prefab prefab) {
    EntityData.Prefab.Builder prefabData = EntityData.Prefab.newBuilder();
    prefabData.setName(prefab.getName());
    if (prefab.getParent() != null) {
        prefabData.setParentName(prefab.getParent().getName());
    }
    prefabData.setAlwaysRelevant(prefab.isAlwaysRelevant());
    prefabData.setPersisted(prefab.isPersisted());
    // Delta off the parent
    for (Component component : prefab.iterateComponents()) {
        if (prefab.getParent() != null && prefab.getParent().hasComponent(component.getClass())) {
            EntityData.Component serializedComponent = componentSerializer.serialize(prefab.getParent().getComponent(component.getClass()), component);
            if (serializedComponent != null) {
                prefabData.addComponent(serializedComponent);
            }
        } else {
            prefabData.addComponent(componentSerializer.serialize(component));
        }
    }
    if (prefab.getParent() != null) {
        for (Component parentComp : prefab.getParent().iterateComponents()) {
            if (!prefab.hasComponent(parentComp.getClass())) {
                prefabData.addRemovedComponent(componentLibrary.getMetadata(parentComp).getUri().toString());
            }
        }
    }
    return prefabData.build();
}
Also used : EntityData(org.terasology.protobuf.EntityData) Component(org.terasology.entitySystem.Component) Prefab(org.terasology.entitySystem.prefab.Prefab)

Example 9 with Component

use of org.terasology.entitySystem.Component in project Terasology by MovingBlocks.

the class ItemPickupAuthoritySystem method onBumpGiveItemToEntity.

@ReceiveEvent
public void onBumpGiveItemToEntity(CollideEvent event, EntityRef entity, PickupComponent pickupComponent) {
    if (pickupComponent.timeDropped + pickupComponent.timeToPickUp < time.getGameTimeInMs()) {
        GiveItemEvent giveItemEvent = new GiveItemEvent(event.getOtherEntity());
        entity.send(giveItemEvent);
        if (giveItemEvent.isHandled()) {
            // remove all the components added from the pickup prefab
            ItemComponent itemComponent = entity.getComponent(ItemComponent.class);
            if (itemComponent != null) {
                for (Component component : itemComponent.pickupPrefab.iterateComponents()) {
                    entity.removeComponent(component.getClass());
                }
            }
        }
    }
}
Also used : BlockItemComponent(org.terasology.world.block.items.BlockItemComponent) GiveItemEvent(org.terasology.logic.inventory.events.GiveItemEvent) LocationComponent(org.terasology.logic.location.LocationComponent) RigidBodyComponent(org.terasology.physics.components.RigidBodyComponent) OnAddedComponent(org.terasology.entitySystem.entity.lifecycleEvents.OnAddedComponent) Component(org.terasology.entitySystem.Component) BoxShapeComponent(org.terasology.physics.components.shapes.BoxShapeComponent) BlockItemComponent(org.terasology.world.block.items.BlockItemComponent) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 10 with Component

use of org.terasology.entitySystem.Component in project Terasology by MovingBlocks.

the class FirstPersonClientSystem method linkHeldItemLocationForLocalPlayer.

/**
 * Changes held item entity.
 *
 * <p>Detaches old held item and removes it's components. Adds components to new held item and
 * attaches it to the mount point entity.</p>
 */
private void linkHeldItemLocationForLocalPlayer(EntityRef newItem) {
    if (!newItem.equals(currentHeldItem)) {
        EntityRef camera = localPlayer.getCameraEntity();
        FirstPersonHeldItemMountPointComponent mountPointComponent = camera.getComponent(FirstPersonHeldItemMountPointComponent.class);
        if (mountPointComponent != null) {
            // currentHeldItem is at this point the old item
            if (currentHeldItem != EntityRef.NULL) {
                currentHeldItem.destroy();
            }
            // use the hand if there is no new item
            EntityRef newHeldItem;
            if (newItem == EntityRef.NULL) {
                newHeldItem = getHandEntity();
            } else {
                newHeldItem = newItem;
            }
            // create client side held item entity
            currentHeldItem = entityManager.create();
            // add the visually relevant components
            for (Component component : newHeldItem.iterateComponents()) {
                if (component instanceof VisualComponent) {
                    currentHeldItem.addComponent(component);
                }
            }
            // ensure world location is set
            currentHeldItem.addComponent(new LocationComponent());
            currentHeldItem.addComponent(new ItemIsHeldComponent());
            FirstPersonHeldItemTransformComponent heldItemTransformComponent = currentHeldItem.getComponent(FirstPersonHeldItemTransformComponent.class);
            if (heldItemTransformComponent == null) {
                heldItemTransformComponent = new FirstPersonHeldItemTransformComponent();
                currentHeldItem.addComponent(heldItemTransformComponent);
            }
            Location.attachChild(mountPointComponent.mountPointEntity, currentHeldItem, heldItemTransformComponent.translate, new Quat4f(TeraMath.DEG_TO_RAD * heldItemTransformComponent.rotateDegrees.y, TeraMath.DEG_TO_RAD * heldItemTransformComponent.rotateDegrees.x, TeraMath.DEG_TO_RAD * heldItemTransformComponent.rotateDegrees.z), heldItemTransformComponent.scale);
        }
    }
}
Also used : VisualComponent(org.terasology.rendering.logic.VisualComponent) VisualComponent(org.terasology.rendering.logic.VisualComponent) CharacterHeldItemComponent(org.terasology.logic.characters.CharacterHeldItemComponent) OnActivatedComponent(org.terasology.entitySystem.entity.lifecycleEvents.OnActivatedComponent) OnChangedComponent(org.terasology.entitySystem.entity.lifecycleEvents.OnChangedComponent) CharacterComponent(org.terasology.logic.characters.CharacterComponent) ClientComponent(org.terasology.network.ClientComponent) LocationComponent(org.terasology.logic.location.LocationComponent) Component(org.terasology.entitySystem.Component) EntityRef(org.terasology.entitySystem.entity.EntityRef) LocationComponent(org.terasology.logic.location.LocationComponent) Quat4f(org.terasology.math.geom.Quat4f)

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