Search in sources :

Example 1 with DropItemRequest

use of org.terasology.logic.inventory.events.DropItemRequest 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)1 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)1 CharacterHeldItemComponent (org.terasology.logic.characters.CharacterHeldItemComponent)1 DropItemRequest (org.terasology.logic.inventory.events.DropItemRequest)1 Vector3f (org.terasology.math.geom.Vector3f)1 HitResult (org.terasology.physics.HitResult)1 InventoryHud (org.terasology.rendering.nui.layers.hud.InventoryHud)1