Search in sources :

Example 1 with VisualComponent

use of org.terasology.engine.rendering.logic.VisualComponent in project Terasology by MovingBlocks.

the class ThirdPersonRemoteClientSystem method linkHeldItemLocationForRemotePlayer.

/**
 * Changes held item entity.
 * <p>
 * Detaches old held item and removes its components. Adds components to new held item and attaches it to the mount
 * point entity.
 */
private void linkHeldItemLocationForRemotePlayer(EntityRef newItem, EntityRef player) {
    if (relatesToLocalPlayer(player)) {
        logger.debug("linkHeldItemLocationForRemotePlayer called with an entity that relates to the local player," + " ignoring{}", player);
        return;
    }
    // Find out if there is a current held item that maps to this player
    EntityRef currentHeldItem = EntityRef.NULL;
    for (EntityRef heldItemCandidate : entityManager.getEntitiesWith(ItemIsRemotelyHeldComponent.class)) {
        EntityRef remotePlayerCandidate = heldItemCandidate.getComponent(ItemIsRemotelyHeldComponent.class).remotePlayer;
        logger.debug("For held item candidate {} got its player candidate as {}", heldItemCandidate, remotePlayerCandidate);
        if (remotePlayerCandidate.equals(player)) {
            logger.debug("Thinking we found a match with player {} so counting this held item as relevant for " + "processing", player);
            currentHeldItem = heldItemCandidate;
            // need to remove the old item
            if (newItem.equals(EntityRef.NULL)) {
                logger.debug("Found an existing held item but the new request was to no longer hold anything so " + "destroying {}", currentHeldItem);
                currentHeldItem.destroy();
                return;
            }
            break;
        }
    }
    // In the case of an actual change of item other than an empty hand we need to hook up a new held item entity
    if (newItem != null && !newItem.equals(EntityRef.NULL) && !newItem.equals(currentHeldItem)) {
        RemotePersonHeldItemMountPointComponent mountPointComponent = player.getComponent(RemotePersonHeldItemMountPointComponent.class);
        if (mountPointComponent != null) {
            // currentHeldItem is at this point the old item
            if (currentHeldItem != EntityRef.NULL) {
                currentHeldItem.destroy();
            }
            currentHeldItem = entityManager.create();
            logger.debug("linkHeldItemLocationForRemotePlayer is now creating a new held item {}", currentHeldItem);
            // add the visually relevant components
            for (Component component : newItem.iterateComponents()) {
                if (component instanceof VisualComponent && !(component instanceof FirstPersonHeldItemTransformComponent)) {
                    currentHeldItem.addComponent(component);
                }
            }
            // ensure world location is set
            currentHeldItem.addComponent(new LocationComponent());
            // Map this held item to the player it is held by
            ItemIsRemotelyHeldComponent itemIsRemotelyHeldComponent = new ItemIsRemotelyHeldComponent();
            itemIsRemotelyHeldComponent.remotePlayer = player;
            currentHeldItem.addComponent(itemIsRemotelyHeldComponent);
            RemotePersonHeldItemTransformComponent heldItemTransformComponent = currentHeldItem.getComponent(RemotePersonHeldItemTransformComponent.class);
            if (heldItemTransformComponent == null) {
                heldItemTransformComponent = new RemotePersonHeldItemTransformComponent();
                currentHeldItem.addComponent(heldItemTransformComponent);
            }
            Location.attachChild(mountPointComponent.mountPointEntity, currentHeldItem, heldItemTransformComponent.translate, new Quaternionf().rotationYXZ(Math.toRadians(heldItemTransformComponent.rotateDegrees.y), Math.toRadians(heldItemTransformComponent.rotateDegrees.x), Math.toRadians(heldItemTransformComponent.rotateDegrees.z)), heldItemTransformComponent.scale);
        }
    } else {
        logger.info("Somehow ended up in the else during linkHeldItemLocationForRemotePlayer - current item was " + "{} and new item {}", currentHeldItem, newItem);
    }
}
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 2 with VisualComponent

use of org.terasology.engine.rendering.logic.VisualComponent 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)

Aggregations

Quaternionf (org.joml.Quaternionf)2 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)2 OnActivatedComponent (org.terasology.engine.entitySystem.entity.lifecycleEvents.OnActivatedComponent)2 OnChangedComponent (org.terasology.engine.entitySystem.entity.lifecycleEvents.OnChangedComponent)2 CharacterComponent (org.terasology.engine.logic.characters.CharacterComponent)2 CharacterHeldItemComponent (org.terasology.engine.logic.characters.CharacterHeldItemComponent)2 LocationComponent (org.terasology.engine.logic.location.LocationComponent)2 ClientComponent (org.terasology.engine.network.ClientComponent)2 VisualComponent (org.terasology.engine.rendering.logic.VisualComponent)2 Component (org.terasology.gestalt.entitysystem.component.Component)2