use of org.terasology.engine.entitySystem.event.ReceiveEvent in project Terasology by MovingBlocks.
the class CharacterSoundSystem method onPlayerDeath.
@ReceiveEvent
public void onPlayerDeath(PlayerDeathEvent event, EntityRef character) {
CharacterSoundComponent characterSounds = character.getComponent(CharacterSoundComponent.class);
if (characterSounds.deathSounds.size() > 0) {
StaticSound sound = random.nextItem(characterSounds.deathSounds);
character.send(new PlaySoundEvent(character, sound, characterSounds.deathVolume));
}
}
use of org.terasology.engine.entitySystem.event.ReceiveEvent in project Terasology by MovingBlocks.
the class CharacterSoundSystem method onFootstep.
@ReceiveEvent
public void onFootstep(FootstepEvent event, EntityRef entity, LocationComponent locationComponent, CharacterSoundComponent characterSounds) {
List<StaticSound> footstepSounds = characterSounds.footstepSounds;
// Check if the block the character is standing on has footstep sounds
Vector3i blockPos = new Vector3i(locationComponent.getLocalPosition(), RoundingMode.FLOOR);
// The block *below* the character's feet is interesting to us
blockPos.y--;
Block block = worldProvider.getBlock(blockPos);
if (block != null) {
if (block.getSounds() == null) {
logger.error("Block '{}' has no sounds", block.getURI());
} else if (!block.getSounds().getStepSounds().isEmpty()) {
footstepSounds = block.getSounds().getStepSounds();
}
}
if (footstepSounds.size() > 0 && characterSounds.lastSoundTime + MIN_TIME < time.getGameTimeInMs()) {
StaticSound sound = random.nextItem(footstepSounds);
entity.send(new PlaySoundEvent(entity, sound, characterSounds.footstepVolume));
characterSounds.lastSoundTime = time.getGameTimeInMs();
entity.saveComponent(characterSounds);
}
}
use of org.terasology.engine.entitySystem.event.ReceiveEvent 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();
}
}
use of org.terasology.engine.entitySystem.event.ReceiveEvent in project Terasology by MovingBlocks.
the class CharacterSystem method onScaleCharacter.
@ReceiveEvent
public void onScaleCharacter(OnScaleEvent event, EntityRef entity, CharacterComponent character, CharacterMovementComponent movement) {
// TODO: We should catch and consume this event somewhere in case there is no space for the character to grow
Prefab parent = entity.getParentPrefab();
// adjust movement parameters based on the default values defined by prefab
CharacterMovementComponent defaultMovement = Optional.ofNullable(parent.getComponent(CharacterMovementComponent.class)).orElse(new CharacterMovementComponent());
final float factor = event.getFactor();
movement.height = factor * movement.height;
movement.jumpSpeed = getJumpSpeed(factor, defaultMovement.jumpSpeed);
movement.stepHeight = factor * movement.stepHeight;
movement.distanceBetweenFootsteps = factor * movement.distanceBetweenFootsteps;
movement.runFactor = getRunFactor(factor, defaultMovement.runFactor);
entity.saveComponent(movement);
// adjust character parameters
CharacterComponent defaultCharacter = Optional.ofNullable(parent.getComponent(CharacterComponent.class)).orElse(new CharacterComponent());
character.interactionRange = getInteractionRange(factor, defaultCharacter.interactionRange);
entity.saveComponent(character);
// refresh the entity collider - by retrieving the character collider after removing it we force recreation
physicsEngine.removeCharacterCollider(entity);
physicsEngine.getCharacterCollider(entity);
// Scaling a character up will grow them into the ground. We would need to adjust the vertical position to be
// safely above ground.
Optional.ofNullable(entity.getComponent(LocationComponent.class)).map(k -> k.getWorldPosition(new Vector3f())).map(location -> location.add(0, (event.getNewValue() - event.getOldValue()) / 2f, 0)).ifPresent(location -> entity.send(new CharacterTeleportEvent(location)));
}
use of org.terasology.engine.entitySystem.event.ReceiveEvent in project Terasology by MovingBlocks.
the class CharacterSystem method onItemUse.
@ReceiveEvent(components = CharacterComponent.class)
public void onItemUse(OnItemUseEvent event, EntityRef entity, CharacterHeldItemComponent characterHeldItemComponent) {
long currentTime = time.getGameTimeInMs();
if (characterHeldItemComponent.nextItemUseTime > currentTime) {
// this character is not yet ready to use another item, they are still cooling down from last use
event.consume();
return;
}
EntityRef selectedItemEntity = characterHeldItemComponent.selectedItem;
characterHeldItemComponent.lastItemUsedTime = currentTime;
characterHeldItemComponent.nextItemUseTime = currentTime;
ItemComponent itemComponent = selectedItemEntity.getComponent(ItemComponent.class);
// Add the cooldown time for the next use of this item.
if (itemComponent != null) {
// Send out this event so other systems can alter the cooldown time.
AffectItemUseCooldownTimeEvent affectItemUseCooldownTimeEvent = new AffectItemUseCooldownTimeEvent(itemComponent.cooldownTime);
entity.send(affectItemUseCooldownTimeEvent);
characterHeldItemComponent.nextItemUseTime += (long) affectItemUseCooldownTimeEvent.getResultValue();
} else {
characterHeldItemComponent.nextItemUseTime += 200;
}
entity.saveComponent(characterHeldItemComponent);
}
Aggregations