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