Search in sources :

Example 36 with Component

use of org.terasology.gestalt.entitysystem.component.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 Quaternionf().rotationYXZ(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.engine.rendering.logic.VisualComponent) Quaternionf(org.joml.Quaternionf) CharacterComponent(org.terasology.engine.logic.characters.CharacterComponent) CharacterHeldItemComponent(org.terasology.engine.logic.characters.CharacterHeldItemComponent) Component(org.terasology.gestalt.entitysystem.component.Component) VisualComponent(org.terasology.engine.rendering.logic.VisualComponent) LocationComponent(org.terasology.engine.logic.location.LocationComponent) ClientComponent(org.terasology.engine.network.ClientComponent) OnActivatedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnActivatedComponent) OnChangedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnChangedComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) LocationComponent(org.terasology.engine.logic.location.LocationComponent)

Example 37 with Component

use of org.terasology.gestalt.entitysystem.component.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.engine.world.block.items.BlockItemComponent) GiveItemEvent(org.terasology.engine.logic.inventory.events.GiveItemEvent) Component(org.terasology.gestalt.entitysystem.component.Component) RigidBodyComponent(org.terasology.engine.physics.components.RigidBodyComponent) LocationComponent(org.terasology.engine.logic.location.LocationComponent) OnAddedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnAddedComponent) BlockItemComponent(org.terasology.engine.world.block.items.BlockItemComponent) BoxShapeComponent(org.terasology.engine.physics.components.shapes.BoxShapeComponent) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 38 with Component

use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.

the class GlobalStoreBuilder method build.

public EntityData.GlobalStore build(EngineEntityManager entityManager, Iterable<EntityRef> entities) {
    EntityData.GlobalStore.Builder store = EntityData.GlobalStore.newBuilder();
    Map<Class<? extends Component>, Integer> componentIdTable = Maps.newHashMap();
    for (ComponentMetadata<?> componentMetadata : entityManager.getComponentLibrary().iterateComponentMetadata()) {
        store.addComponentClass(componentMetadata.getId().toString());
        componentIdTable.put(componentMetadata.getType(), componentIdTable.size());
    }
    prefabSerializer.setComponentIdMapping(componentIdTable);
    /*
         * The prefabs can't be obtained from  entityManager.getPrefabManager().listPrefabs() as that might not
         * be thread save.
         */
    Set<Prefab> prefabsRequiredForEntityStorage = new HashSet<>();
    for (EntityRef entityRef : entityManager.getAllEntities()) {
        Prefab prefab = entityRef.getParentPrefab();
        if (prefab != null) {
            prefabsRequiredForEntityStorage.add(prefab);
        }
    }
    for (Prefab prefab : prefabsRequiredForEntityStorage) {
        store.addPrefab(prefabSerializer.serialize(prefab));
    }
    EntitySerializer entitySerializer = new EntitySerializer(entityManager);
    entitySerializer.setComponentSerializeCheck(new PersistenceComponentSerializeCheck());
    entitySerializer.setComponentIdMapping(componentIdTable);
    for (EntityRef entity : entities) {
        if (entity.isPersistent()) {
            store.addEntity(entitySerializer.serialize(entity));
        }
    }
    store.setNextEntityId(nextEntityId);
    return store.build();
}
Also used : PersistenceComponentSerializeCheck(org.terasology.engine.persistence.serializers.PersistenceComponentSerializeCheck) EntitySerializer(org.terasology.engine.persistence.serializers.EntitySerializer) Component(org.terasology.gestalt.entitysystem.component.Component) Prefab(org.terasology.engine.entitySystem.prefab.Prefab) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) HashSet(java.util.HashSet)

Example 39 with Component

use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.

the class ServerImpl method sendEntities.

private void sendEntities(NetData.NetMessage.Builder message) {
    TIntIterator dirtyIterator = netDirty.iterator();
    while (dirtyIterator.hasNext()) {
        int netId = dirtyIterator.next();
        EntityRef entity = networkSystem.getEntity(netId);
        if (isOwned(entity)) {
            Set<Class<? extends Component>> emptyComponentClassSet = Collections.emptySet();
            EntityData.PackedEntity entityData = entitySerializer.serialize(entity, emptyComponentClassSet, changedComponents.get(netId), emptyComponentClassSet, new ClientComponentFieldCheck());
            if (entityData != null) {
                message.addUpdateEntity(NetData.UpdateEntityMessage.newBuilder().setEntity(entityData).setNetId(netId));
            }
        }
    }
    netDirty.clear();
}
Also used : TIntIterator(gnu.trove.iterator.TIntIterator) EntityData(org.terasology.protobuf.EntityData) Component(org.terasology.gestalt.entitysystem.component.Component) NetworkComponent(org.terasology.engine.network.NetworkComponent) BlockComponent(org.terasology.engine.world.block.BlockComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) ClientComponentFieldCheck(org.terasology.engine.network.serialization.ClientComponentFieldCheck)

Example 40 with Component

use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.

the class NetworkSystemImpl method updateOwnership.

public void updateOwnership(EntityRef entity) {
    NetworkComponent netComponent = entity.getComponent(NetworkComponent.class);
    if (netComponent == null || netComponent.getNetworkId() == NULL_NET_ID) {
        return;
    }
    EntityRef lastOwnerEntity = ownerLookup.get(entity);
    if (lastOwnerEntity == null) {
        lastOwnerEntity = EntityRef.NULL;
    }
    EntityRef newOwnerEntity = entity.getOwner();
    if (!Objects.equal(lastOwnerEntity, newOwnerEntity)) {
        NetClient lastOwner = getNetOwner(lastOwnerEntity);
        NetClient newOwner = getNetOwner(newOwnerEntity);
        if (!Objects.equal(lastOwner, newOwner)) {
            recursiveUpdateOwnership(entity, lastOwner, newOwner);
            if (newOwner != null) {
                int id = netComponent.getNetworkId();
                for (Component component : entity.iterateComponents()) {
                    if (componentLibrary.getMetadata(component.getClass()).isReplicated()) {
                        newOwner.setComponentDirty(id, component.getClass());
                    }
                }
            }
        }
        if (lastOwnerEntity.exists()) {
            ownedLookup.remove(lastOwnerEntity, entity);
        }
        if (newOwnerEntity.exists()) {
            ownerLookup.put(entity, newOwnerEntity);
            ownedLookup.put(newOwnerEntity, entity);
        } else {
            ownerLookup.remove(entity);
        }
    }
}
Also used : NetworkComponent(org.terasology.engine.network.NetworkComponent) Component(org.terasology.gestalt.entitysystem.component.Component) NetworkComponent(org.terasology.engine.network.NetworkComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Aggregations

Component (org.terasology.gestalt.entitysystem.component.Component)57 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)20 Prefab (org.terasology.engine.entitySystem.prefab.Prefab)12 LocationComponent (org.terasology.engine.logic.location.LocationComponent)12 OnActivatedComponent (org.terasology.engine.entitySystem.entity.lifecycleEvents.OnActivatedComponent)11 NetworkComponent (org.terasology.engine.network.NetworkComponent)10 OnChangedComponent (org.terasology.engine.entitySystem.entity.lifecycleEvents.OnChangedComponent)9 EntityData (org.terasology.protobuf.EntityData)8 BeforeDeactivateComponent (org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent)7 OnAddedComponent (org.terasology.engine.entitySystem.entity.lifecycleEvents.OnAddedComponent)7 BlockComponent (org.terasology.engine.world.block.BlockComponent)7 EntityInfoComponent (org.terasology.engine.entitySystem.entity.internal.EntityInfoComponent)6 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)6 EntityManager (org.terasology.engine.entitySystem.entity.EntityManager)5 ClientComponent (org.terasology.engine.network.ClientComponent)5 List (java.util.List)4 Map (java.util.Map)4 Optional (java.util.Optional)4 FieldMetadata (org.terasology.reflection.metadata.FieldMetadata)4 ByteString (com.google.protobuf.ByteString)3