Search in sources :

Example 51 with ReceiveEvent

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));
    }
}
Also used : StaticSound(org.terasology.engine.audio.StaticSound) PlaySoundEvent(org.terasology.engine.audio.events.PlaySoundEvent) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 52 with ReceiveEvent

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);
    }
}
Also used : StaticSound(org.terasology.engine.audio.StaticSound) PlaySoundEvent(org.terasology.engine.audio.events.PlaySoundEvent) Vector3i(org.joml.Vector3i) Block(org.terasology.engine.world.block.Block) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 53 with ReceiveEvent

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

Example 54 with ReceiveEvent

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)));
}
Also used : ActivateEvent(org.terasology.engine.logic.common.ActivateEvent) In(org.terasology.engine.registry.In) RegisterMode(org.terasology.engine.entitySystem.systems.RegisterMode) DestroyEvent(org.terasology.engine.logic.health.DestroyEvent) LoggerFactory(org.slf4j.LoggerFactory) BeforeDestroyEvent(org.terasology.engine.logic.health.BeforeDestroyEvent) PhysicsEngine(org.terasology.engine.physics.engine.PhysicsEngine) UpdateSubscriberSystem(org.terasology.engine.entitySystem.systems.UpdateSubscriberSystem) Physics(org.terasology.engine.physics.Physics) ActAsBlockComponent(org.terasology.engine.world.block.regions.ActAsBlockComponent) InteractionUtil(org.terasology.engine.logic.characters.interactions.InteractionUtil) EngineDamageTypes(org.terasology.engine.logic.health.EngineDamageTypes) DisplayNameComponent(org.terasology.engine.logic.common.DisplayNameComponent) Time(org.terasology.engine.core.Time) OnScaleEvent(org.terasology.engine.logic.characters.events.OnScaleEvent) Vector3f(org.joml.Vector3f) RangeComponent(org.terasology.engine.logic.common.RangeComponent) AttackEvent(org.terasology.engine.logic.characters.events.AttackEvent) OnItemUseEvent(org.terasology.engine.logic.characters.events.OnItemUseEvent) ActivationRequest(org.terasology.engine.logic.characters.events.ActivationRequest) Logger(org.slf4j.Logger) DeathEvent(org.terasology.engine.logic.characters.events.DeathEvent) HitResult(org.terasology.engine.physics.HitResult) LocationComponent(org.terasology.engine.logic.location.LocationComponent) ClientComponent(org.terasology.engine.network.ClientComponent) AttackRequest(org.terasology.engine.logic.characters.events.AttackRequest) EntityManager(org.terasology.engine.entitySystem.entity.EntityManager) StandardCollisionGroup(org.terasology.engine.physics.StandardCollisionGroup) Prefab(org.terasology.engine.entitySystem.prefab.Prefab) Sets(com.google.common.collect.Sets) RecordAndReplayCurrentStatus(org.terasology.engine.recording.RecordAndReplayCurrentStatus) BaseComponentSystem(org.terasology.engine.entitySystem.systems.BaseComponentSystem) PlayerDeathEvent(org.terasology.engine.logic.characters.events.PlayerDeathEvent) ItemComponent(org.terasology.engine.logic.inventory.ItemComponent) ActivationRequestDenied(org.terasology.engine.logic.characters.events.ActivationRequestDenied) RecordAndReplayStatus(org.terasology.engine.recording.RecordAndReplayStatus) AttackButton(org.terasology.engine.input.binds.interaction.AttackButton) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) EventPriority(org.terasology.engine.entitySystem.event.EventPriority) RegisterSystem(org.terasology.engine.entitySystem.systems.RegisterSystem) BlockComponent(org.terasology.engine.world.block.BlockComponent) PlayerCharacterComponent(org.terasology.engine.logic.players.PlayerCharacterComponent) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent) Optional(java.util.Optional) DirectionAndOriginPosRecorderList(org.terasology.engine.recording.DirectionAndOriginPosRecorderList) CollisionGroup(org.terasology.engine.physics.CollisionGroup) NetworkSystem(org.terasology.engine.network.NetworkSystem) Vector3f(org.joml.Vector3f) PlayerCharacterComponent(org.terasology.engine.logic.players.PlayerCharacterComponent) Prefab(org.terasology.engine.entitySystem.prefab.Prefab) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 55 with ReceiveEvent

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);
}
Also used : ItemComponent(org.terasology.engine.logic.inventory.ItemComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Aggregations

ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)83 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)27 Vector3f (org.joml.Vector3f)18 LocationComponent (org.terasology.engine.logic.location.LocationComponent)12 Vector3i (org.joml.Vector3i)10 StaticSound (org.terasology.engine.audio.StaticSound)10 Vector3ic (org.joml.Vector3ic)9 PlaySoundEvent (org.terasology.engine.audio.events.PlaySoundEvent)9 ClientComponent (org.terasology.engine.network.ClientComponent)8 Quaternionf (org.joml.Quaternionf)6 BlockComponent (org.terasology.engine.world.block.BlockComponent)6 OnItemUseEvent (org.terasology.engine.logic.characters.events.OnItemUseEvent)5 Block (org.terasology.engine.world.block.Block)5 CharacterComponent (org.terasology.engine.logic.characters.CharacterComponent)4 BlockRegion (org.terasology.engine.world.block.BlockRegion)4 EntityBuilder (org.terasology.engine.entitySystem.entity.EntityBuilder)3 BlockRegionComponent (org.terasology.engine.world.block.regions.BlockRegionComponent)3 Component (org.terasology.gestalt.entitysystem.component.Component)3 Method (java.lang.reflect.Method)2 Vector3fc (org.joml.Vector3fc)2