Search in sources :

Example 1 with CharacterHeldItemComponent

use of org.terasology.logic.characters.CharacterHeldItemComponent in project Terasology by MovingBlocks.

the class FirstPersonClientSystem method update.

/**
 * modifies the held item mount point to move the held item in first person view
 */
@Override
public void update(float delta) {
    // ensure empty hand is shown if no item is hold at the moment
    if (!currentHeldItem.exists() && currentHeldItem != getHandEntity()) {
        linkHeldItemLocationForLocalPlayer(getHandEntity());
    }
    // ensure that there are no lingering items that are marked as still held. This situation happens with client side predicted items
    for (EntityRef entityRef : entityManager.getEntitiesWith(ItemIsHeldComponent.class)) {
        if (!entityRef.equals(currentHeldItem) && !entityRef.equals(handEntity)) {
            entityRef.destroy();
        }
    }
    // get the first person mount point and rotate it away from the camera
    CharacterHeldItemComponent characterHeldItemComponent = localPlayer.getCharacterEntity().getComponent(CharacterHeldItemComponent.class);
    FirstPersonHeldItemMountPointComponent mountPointComponent = localPlayer.getCameraEntity().getComponent(FirstPersonHeldItemMountPointComponent.class);
    if (characterHeldItemComponent == null || mountPointComponent == null) {
        return;
    }
    LocationComponent locationComponent = mountPointComponent.mountPointEntity.getComponent(LocationComponent.class);
    if (locationComponent == null) {
        return;
    }
    long timeElapsedSinceLastUsed = time.getGameTimeInMs() - characterHeldItemComponent.lastItemUsedTime;
    float animateAmount = 0f;
    if (timeElapsedSinceLastUsed < USEANIMATIONLENGTH) {
        // half way through the animation will be the maximum extent of rotation and translation
        animateAmount = 1f - Math.abs(((float) timeElapsedSinceLastUsed / (float) USEANIMATIONLENGTH) - 0.5f);
    }
    float addPitch = 15f * animateAmount;
    float addYaw = 10f * animateAmount;
    locationComponent.setLocalRotation(new Quat4f(TeraMath.DEG_TO_RAD * (mountPointComponent.rotateDegrees.y + addYaw), TeraMath.DEG_TO_RAD * (mountPointComponent.rotateDegrees.x + addPitch), TeraMath.DEG_TO_RAD * mountPointComponent.rotateDegrees.z));
    Vector3f offset = new Vector3f(0.25f * animateAmount, -0.12f * animateAmount, 0f);
    offset.add(mountPointComponent.translate);
    locationComponent.setLocalPosition(offset);
    mountPointComponent.mountPointEntity.saveComponent(locationComponent);
}
Also used : Vector3f(org.terasology.math.geom.Vector3f) CharacterHeldItemComponent(org.terasology.logic.characters.CharacterHeldItemComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) LocationComponent(org.terasology.logic.location.LocationComponent) Quat4f(org.terasology.math.geom.Quat4f)

Example 2 with CharacterHeldItemComponent

use of org.terasology.logic.characters.CharacterHeldItemComponent in project Terasology by MovingBlocks.

the class CharacterInventorySystem method onDropItemRequest.

@ReceiveEvent(components = { CharacterComponent.class, InventoryComponent.class }, netFilter = RegisterMode.CLIENT)
public void onDropItemRequest(DropItemButton event, EntityRef entity) {
    CharacterHeldItemComponent characterHeldItemComponent = entity.getComponent(CharacterHeldItemComponent.class);
    EntityRef selectedItemEntity = characterHeldItemComponent.selectedItem;
    if (selectedItemEntity.equals(EntityRef.NULL)) {
        return;
    }
    // this is a repeating event.
    if (event.isDown() && lastTimeThrowInteraction == 0) {
        lastTimeThrowInteraction = time.getGameTimeInMs();
        return;
    }
    // resize the crosshair
    InventoryHud toolbar = nuiManager.getHUD().getHUDElement("core:InventoryHud", InventoryHud.class);
    if (toolbar != null) {
        toolbar.setChargeAmount(getDropPower());
    }
    float dropPower = getDropPower();
    // handle when we finally let go
    if (!event.isDown()) {
        // Compute new position
        dropPower *= 150f;
        Vector3f position = localPlayer.getViewPosition();
        Vector3f direction = localPlayer.getViewDirection();
        Vector3f maxAllowedDistanceInDirection = direction.mul(1.5f);
        HitResult hitResult = physics.rayTrace(position, direction, 1.5f, StandardCollisionGroup.CHARACTER, StandardCollisionGroup.WORLD);
        if (hitResult.isHit()) {
            Vector3f possibleNewPosition = hitResult.getHitPoint();
            maxAllowedDistanceInDirection = possibleNewPosition.sub(position);
        }
        Vector3f newPosition = position;
        newPosition.add(maxAllowedDistanceInDirection.mul(0.9f));
        // send DropItemRequest
        Vector3f impulseVector = new Vector3f(direction);
        impulseVector.scale(dropPower);
        entity.send(new DropItemRequest(selectedItemEntity, entity, impulseVector, newPosition));
        characterHeldItemComponent.lastItemUsedTime = time.getGameTimeInMs();
        entity.saveComponent(characterHeldItemComponent);
        resetDropMark();
    }
    event.consume();
}
Also used : HitResult(org.terasology.physics.HitResult) DropItemRequest(org.terasology.logic.inventory.events.DropItemRequest) Vector3f(org.terasology.math.geom.Vector3f) InventoryHud(org.terasology.rendering.nui.layers.hud.InventoryHud) CharacterHeldItemComponent(org.terasology.logic.characters.CharacterHeldItemComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Aggregations

EntityRef (org.terasology.entitySystem.entity.EntityRef)2 CharacterHeldItemComponent (org.terasology.logic.characters.CharacterHeldItemComponent)2 Vector3f (org.terasology.math.geom.Vector3f)2 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)1 DropItemRequest (org.terasology.logic.inventory.events.DropItemRequest)1 LocationComponent (org.terasology.logic.location.LocationComponent)1 Quat4f (org.terasology.math.geom.Quat4f)1 HitResult (org.terasology.physics.HitResult)1 InventoryHud (org.terasology.rendering.nui.layers.hud.InventoryHud)1