Search in sources :

Example 1 with VisualComponent

use of org.terasology.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 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)1 EntityRef (org.terasology.entitySystem.entity.EntityRef)1 OnActivatedComponent (org.terasology.entitySystem.entity.lifecycleEvents.OnActivatedComponent)1 OnChangedComponent (org.terasology.entitySystem.entity.lifecycleEvents.OnChangedComponent)1 CharacterComponent (org.terasology.logic.characters.CharacterComponent)1 CharacterHeldItemComponent (org.terasology.logic.characters.CharacterHeldItemComponent)1 LocationComponent (org.terasology.logic.location.LocationComponent)1 Quat4f (org.terasology.math.geom.Quat4f)1 ClientComponent (org.terasology.network.ClientComponent)1 VisualComponent (org.terasology.rendering.logic.VisualComponent)1