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();
}
Aggregations