Search in sources :

Example 1 with OnItemUseEvent

use of org.terasology.engine.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(new Vector3f());
        Vector3f originPos = gazeLocation.getWorldPosition(new Vector3f());
        if (recordAndReplayCurrentStatus.getStatus() == RecordAndReplayStatus.RECORDING) {
            directionAndOriginPosRecorderList.getAttackEventDirectionAndOriginPosRecorder().add(direction, originPos);
        } else if (recordAndReplayCurrentStatus.getStatus() == RecordAndReplayStatus.REPLAYING) {
            Vector3f[] data = directionAndOriginPosRecorderList.getAttackEventDirectionAndOriginPosRecorder().poll();
            direction = data[0];
            originPos = data[1];
        }
        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.engine.physics.HitResult) OnItemUseEvent(org.terasology.engine.logic.characters.events.OnItemUseEvent) Vector3f(org.joml.Vector3f) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) LocationComponent(org.terasology.engine.logic.location.LocationComponent) AttackEvent(org.terasology.engine.logic.characters.events.AttackEvent) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 2 with OnItemUseEvent

use of org.terasology.engine.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.engine.logic.common.ActivateEvent) OnItemUseEvent(org.terasology.engine.logic.characters.events.OnItemUseEvent) ActivationRequestDenied(org.terasology.engine.logic.characters.events.ActivationRequestDenied) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 3 with OnItemUseEvent

use of org.terasology.engine.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.engine.logic.characters.events.OnItemUseEvent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 4 with OnItemUseEvent

use of org.terasology.engine.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.engine.logic.characters.events.AttackRequest) OnItemUseEvent(org.terasology.engine.logic.characters.events.OnItemUseEvent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Aggregations

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