Search in sources :

Example 1 with OnItemUseEvent

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

the class LocalPlayerSystem method onUseItemButton.

@ReceiveEvent(components = { CharacterComponent.class })
public void onUseItemButton(UseItemButton event, EntityRef entity, CharacterHeldItemComponent characterHeldItemComponent) {
    if (!event.isDown()) {
        return;
    }
    EntityRef selectedItemEntity = characterHeldItemComponent.selectedItem;
    if (!selectedItemEntity.exists()) {
        return;
    }
    boolean requestIsValid;
    if (networkSystem.getMode().isAuthority()) {
        // Let the ActivationRequest handler trigger the OnItemUseEvent if this is a local client
        requestIsValid = true;
    } else {
        OnItemUseEvent onItemUseEvent = new OnItemUseEvent();
        entity.send(onItemUseEvent);
        requestIsValid = !onItemUseEvent.isConsumed();
    }
    if (requestIsValid) {
        localPlayer.activateOwnedEntityAsClient(selectedItemEntity);
        entity.saveComponent(characterHeldItemComponent);
        event.consume();
    }
}
Also used : OnItemUseEvent(org.terasology.logic.characters.events.OnItemUseEvent) EntityRef(org.terasology.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 2 with OnItemUseEvent

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

the class CharacterSystem method onAttackRequest.

@ReceiveEvent(components = LocationComponent.class, netFilter = RegisterMode.AUTHORITY)
public void onAttackRequest(AttackRequest event, EntityRef character, CharacterComponent characterComponent) {
    // if an item is used,  make sure this entity is allowed to attack with it
    if (event.getItem().exists()) {
        if (!character.equals(event.getItem().getOwner())) {
            return;
        }
    }
    OnItemUseEvent onItemUseEvent = new OnItemUseEvent();
    character.send(onItemUseEvent);
    if (!onItemUseEvent.isConsumed()) {
        EntityRef gazeEntity = GazeAuthoritySystem.getGazeEntityForCharacter(character);
        LocationComponent gazeLocation = gazeEntity.getComponent(LocationComponent.class);
        Vector3f direction = gazeLocation.getWorldDirection();
        Vector3f originPos = gazeLocation.getWorldPosition();
        HitResult result = physics.rayTrace(originPos, direction, characterComponent.interactionRange, Sets.newHashSet(character), DEFAULTPHYSICSFILTER);
        if (result.isHit()) {
            result.getEntity().send(new AttackEvent(character, event.getItem()));
        }
    }
}
Also used : HitResult(org.terasology.physics.HitResult) OnItemUseEvent(org.terasology.logic.characters.events.OnItemUseEvent) Vector3f(org.terasology.math.geom.Vector3f) EntityRef(org.terasology.entitySystem.entity.EntityRef) LocationComponent(org.terasology.logic.location.LocationComponent) AttackEvent(org.terasology.logic.characters.events.AttackEvent) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 3 with OnItemUseEvent

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

the class CharacterSystem method onActivationRequest.

@ReceiveEvent(components = { CharacterComponent.class, LocationComponent.class }, netFilter = RegisterMode.AUTHORITY)
public void onActivationRequest(ActivationRequest event, EntityRef character) {
    if (isPredictionOfEventCorrect(character, event)) {
        OnItemUseEvent onItemUseEvent = new OnItemUseEvent();
        event.getInstigator().send(onItemUseEvent);
        if (!onItemUseEvent.isConsumed()) {
            if (event.getUsedOwnedEntity().exists()) {
                event.getUsedOwnedEntity().send(new ActivateEvent(event));
            } else {
                event.getTarget().send(new ActivateEvent(event));
            }
        }
    } else {
        character.send(new ActivationRequestDenied(event.getActivationId()));
    }
}
Also used : ActivateEvent(org.terasology.logic.common.ActivateEvent) OnItemUseEvent(org.terasology.logic.characters.events.OnItemUseEvent) ActivationRequestDenied(org.terasology.logic.characters.events.ActivationRequestDenied) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 4 with OnItemUseEvent

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

the class CharacterSystem method onAttackRequest.

@ReceiveEvent(components = { CharacterComponent.class }, netFilter = RegisterMode.CLIENT)
public void onAttackRequest(AttackButton event, EntityRef entity, CharacterHeldItemComponent characterHeldItemComponent) {
    if (!event.isDown()) {
        return;
    }
    boolean attackRequestIsValid;
    if (networkSystem.getMode().isAuthority()) {
        // Let the AttackRequest handler trigger the OnItemUseEvent if this is a local client
        attackRequestIsValid = true;
    } else {
        OnItemUseEvent onItemUseEvent = new OnItemUseEvent();
        entity.send(onItemUseEvent);
        attackRequestIsValid = !onItemUseEvent.isConsumed();
    }
    if (attackRequestIsValid) {
        EntityRef selectedItemEntity = characterHeldItemComponent.selectedItem;
        entity.send(new AttackRequest(selectedItemEntity));
        event.consume();
    }
}
Also used : AttackRequest(org.terasology.logic.characters.events.AttackRequest) OnItemUseEvent(org.terasology.logic.characters.events.OnItemUseEvent) EntityRef(org.terasology.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Aggregations

ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)4 OnItemUseEvent (org.terasology.logic.characters.events.OnItemUseEvent)4 EntityRef (org.terasology.entitySystem.entity.EntityRef)3 ActivationRequestDenied (org.terasology.logic.characters.events.ActivationRequestDenied)1 AttackEvent (org.terasology.logic.characters.events.AttackEvent)1 AttackRequest (org.terasology.logic.characters.events.AttackRequest)1 ActivateEvent (org.terasology.logic.common.ActivateEvent)1 LocationComponent (org.terasology.logic.location.LocationComponent)1 Vector3f (org.terasology.math.geom.Vector3f)1 HitResult (org.terasology.physics.HitResult)1