Search in sources :

Example 31 with ReceiveEvent

use of org.terasology.entitySystem.event.ReceiveEvent in project Terasology by MovingBlocks.

the class ExplosionAuthoritySystem method onDelayedExplosion.

@ReceiveEvent
public void onDelayedExplosion(DelayedActionTriggeredEvent event, EntityRef entityRef, ExplosionActionComponent explosionActionComponent) {
    if (event.getActionId().equals(DELAYED_EXPLOSION_ACTION_ID)) {
        // check if the exploding entity is a block or not
        if (entityRef.hasComponent(BlockComponent.class)) {
            BlockComponent blockComponent = entityRef.getComponent(BlockComponent.class);
            // always destroy the block that caused the explosion
            worldProvider.setBlock(blockComponent.getPosition(), blockManager.getBlock(BlockManager.AIR_ID));
            // create the explosion from the block's location
            doExplosion(explosionActionComponent, blockComponent.getPosition().toVector3f(), entityRef);
        } else if (entityRef.hasComponent(LocationComponent.class)) {
            // get the position of the non-block entity to make it explode from there
            Vector3f position = entityRef.getComponent(LocationComponent.class).getWorldPosition();
            // destroy the non-block entity
            entityRef.destroy();
            // create the explosion from the non-block entity location
            doExplosion(explosionActionComponent, position, EntityRef.NULL);
        }
    }
}
Also used : BlockComponent(org.terasology.world.block.BlockComponent) Vector3f(org.terasology.math.geom.Vector3f) LocationComponent(org.terasology.logic.location.LocationComponent) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 32 with ReceiveEvent

use of org.terasology.entitySystem.event.ReceiveEvent in project Terasology by MovingBlocks.

the class ExplosionAuthoritySystem method onActivate.

@ReceiveEvent
public void onActivate(ActivateEvent event, EntityRef entity, ExplosionActionComponent explosionComp) {
    Vector3f origin = null;
    switch(explosionComp.relativeTo) {
        case Self:
            LocationComponent loc = entity.getComponent(LocationComponent.class);
            if (loc != null) {
                origin = loc.getWorldPosition();
            }
            break;
        case Instigator:
            origin = event.getInstigatorLocation();
            break;
        default:
            origin = event.getTargetLocation();
            break;
    }
    if (origin == null) {
        return;
    }
    doExplosion(explosionComp, origin, EntityRef.NULL);
}
Also used : Vector3f(org.terasology.math.geom.Vector3f) LocationComponent(org.terasology.logic.location.LocationComponent) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 33 with ReceiveEvent

use of org.terasology.entitySystem.event.ReceiveEvent in project Terasology by MovingBlocks.

the class TunnelAction method onActivate.

@ReceiveEvent
public void onActivate(ActivateEvent event, EntityRef entity, TunnelActionComponent tunnelActionComponent) {
    Vector3f dir = new Vector3f(event.getDirection());
    dir.scale(4.0f);
    Vector3f origin = new Vector3f(event.getOrigin());
    origin.add(dir);
    Vector3i blockPos = new Vector3i();
    int particleEffects = 0;
    int blockCounter = tunnelActionComponent.maxDestroyedBlocks;
    for (int s = 0; s <= tunnelActionComponent.maxTunnelDepth; s++) {
        origin.add(dir);
        if (!worldProvider.isBlockRelevant(origin)) {
            break;
        }
        for (int i = 0; i < tunnelActionComponent.maxRaysCast; i++) {
            Vector3f direction = random.nextVector3f();
            Vector3f impulse = new Vector3f(direction);
            impulse.scale(tunnelActionComponent.explosiveForce);
            for (int j = 0; j < 3; j++) {
                Vector3f target = new Vector3f(origin);
                target.x += direction.x * j;
                target.y += direction.y * j;
                target.z += direction.z * j;
                blockPos.set((int) target.x, (int) target.y, (int) target.z);
                Block currentBlock = worldProvider.getBlock(blockPos);
                if (currentBlock.isDestructible()) {
                    if (particleEffects < tunnelActionComponent.maxParticalEffects) {
                        EntityBuilder builder = entityManager.newBuilder("Core:smokeExplosion");
                        builder.getComponent(LocationComponent.class).setWorldPosition(target);
                        builder.build();
                        particleEffects++;
                    }
                    if (random.nextFloat() < tunnelActionComponent.thoroughness) {
                        EntityRef blockEntity = blockEntityRegistry.getEntityAt(blockPos);
                        blockEntity.send(new DoDamageEvent(tunnelActionComponent.damageAmount, tunnelActionComponent.damageType));
                    }
                    blockCounter--;
                }
                if (blockCounter <= 0) {
                    return;
                }
            }
        }
    }
    // No blocks were destroyed, so cancel the event
    if (blockCounter == tunnelActionComponent.maxDestroyedBlocks) {
        event.consume();
    }
}
Also used : Vector3f(org.terasology.math.geom.Vector3f) Vector3i(org.terasology.math.geom.Vector3i) Block(org.terasology.world.block.Block) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) DoDamageEvent(org.terasology.logic.health.DoDamageEvent) LocationComponent(org.terasology.logic.location.LocationComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 34 with ReceiveEvent

use of org.terasology.entitySystem.event.ReceiveEvent in project Terasology by MovingBlocks.

the class HealthAuthoritySystem method onDamaged.

@ReceiveEvent
public void onDamaged(OnDamagedEvent event, EntityRef entity, CharacterSoundComponent characterSounds) {
    if (characterSounds.lastSoundTime + CharacterSoundSystem.MIN_TIME < time.getGameTimeInMs()) {
        // play the sound of damage hitting the character for everyone
        DamageSoundComponent damageSounds = event.getType().getComponent(DamageSoundComponent.class);
        if (damageSounds != null && !damageSounds.sounds.isEmpty()) {
            StaticSound sound = random.nextItem(damageSounds.sounds);
            if (sound != null) {
                entity.send(new PlaySoundEvent(sound, 1f));
            }
        }
        // play the sound of a client's character being damaged to the client
        if (!characterSounds.damageSounds.isEmpty()) {
            StaticSound sound = random.nextItem(characterSounds.damageSounds);
            if (sound != null) {
                entity.send(new PlaySoundForOwnerEvent(sound, characterSounds.damageVolume));
            }
        }
        characterSounds.lastSoundTime = time.getGameTimeInMs();
        entity.saveComponent(characterSounds);
    }
}
Also used : StaticSound(org.terasology.audio.StaticSound) PlaySoundEvent(org.terasology.audio.events.PlaySoundEvent) PlaySoundForOwnerEvent(org.terasology.audio.events.PlaySoundForOwnerEvent) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 35 with ReceiveEvent

use of org.terasology.entitySystem.event.ReceiveEvent in project Terasology by MovingBlocks.

the class CharacterInventorySystem method onDropItemRequest.

@ReceiveEvent(components = { CharacterComponent.class, LocationComponent.class }, netFilter = RegisterMode.AUTHORITY)
public void onDropItemRequest(DropItemRequest event, EntityRef character) {
    // make sure we own the item and it exists
    if (!event.getItem().exists() || !networkSystem.getOwnerEntity(event.getItem()).equals(networkSystem.getOwnerEntity(character))) {
        return;
    }
    int count = event.getCount();
    // remove 'count' items from the stack
    EntityRef pickupItem = event.getItem();
    EntityRef owner = pickupItem.getOwner();
    if (owner.hasComponent(InventoryComponent.class)) {
        final EntityRef removedItem = inventoryManager.removeItem(owner, EntityRef.NULL, pickupItem, false, count);
        if (removedItem != null) {
            pickupItem = removedItem;
        }
    }
    pickupItem.send(new DropItemEvent(event.getNewPosition()));
    if (pickupItem.hasComponent(PickupComponent.class)) {
        PickupComponent pickupComponent = pickupItem.getComponent(PickupComponent.class);
        pickupComponent.timeDropped = time.getGameTimeInMs();
        pickupItem.saveComponent(pickupComponent);
    }
    pickupItem.send(new ImpulseEvent(event.getImpulse()));
}
Also used : DropItemEvent(org.terasology.logic.inventory.events.DropItemEvent) ImpulseEvent(org.terasology.physics.events.ImpulseEvent) EntityRef(org.terasology.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Aggregations

ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)98 EntityRef (org.terasology.entitySystem.entity.EntityRef)36 Vector3f (org.terasology.math.geom.Vector3f)26 LocationComponent (org.terasology.logic.location.LocationComponent)17 Vector3i (org.terasology.math.geom.Vector3i)17 PlaySoundEvent (org.terasology.audio.events.PlaySoundEvent)15 StaticSound (org.terasology.audio.StaticSound)13 Block (org.terasology.world.block.Block)11 ClientComponent (org.terasology.network.ClientComponent)7 BlockComponent (org.terasology.world.block.BlockComponent)7 BlockRegionComponent (org.terasology.world.block.regions.BlockRegionComponent)7 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)5 CharacterComponent (org.terasology.logic.characters.CharacterComponent)4 OnItemUseEvent (org.terasology.logic.characters.events.OnItemUseEvent)4 ResourceUrn (org.terasology.assets.ResourceUrn)3 Component (org.terasology.entitySystem.Component)3 ItemComponent (org.terasology.logic.inventory.ItemComponent)3 Side (org.terasology.math.Side)3 BoneHealthChangedEvent (org.terasology.anatomy.AnatomySkeleton.event.BoneHealthChangedEvent)2 PartSkeletalDetails (org.terasology.anatomy.component.PartSkeletalDetails)2