Search in sources :

Example 81 with ReceiveEvent

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

the class PlayerSystem method setSpawnLocationOnRespawnRequest.

@ReceiveEvent(priority = EventPriority.PRIORITY_CRITICAL, components = { ClientComponent.class })
public void setSpawnLocationOnRespawnRequest(RespawnRequestEvent event, EntityRef entity) {
    EntityRef clientInfo = entity.getComponent(ClientComponent.class).clientInfo;
    Vector3f spawnPosition;
    if (clientInfo.hasComponent(StaticSpawnLocationComponent.class)) {
        spawnPosition = clientInfo.getComponent(StaticSpawnLocationComponent.class).position;
    } else {
        spawnPosition = worldGenerator.getSpawnPosition(entity);
    }
    LocationComponent loc = entity.getComponent(LocationComponent.class);
    loc.setWorldPosition(spawnPosition);
    // reset rotation
    loc.setLocalRotation(new Quat4f());
    entity.saveComponent(loc);
}
Also used : Vector3f(org.terasology.math.geom.Vector3f) EntityRef(org.terasology.entitySystem.entity.EntityRef) ClientComponent(org.terasology.network.ClientComponent) LocationComponent(org.terasology.logic.location.LocationComponent) Quat4f(org.terasology.math.geom.Quat4f) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 82 with ReceiveEvent

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

the class PlayerSystem method onConnect.

@ReceiveEvent(components = ClientComponent.class)
public void onConnect(ConnectedEvent connected, EntityRef entity) {
    LocationComponent loc = entity.getComponent(LocationComponent.class);
    // for new clients, the player store will return default values
    PlayerStore playerStore = connected.getPlayerStore();
    Client owner = networkSystem.getOwner(entity);
    Vector3i minViewDist = ViewDistance.LEGALLY_BLIND.getChunkDistance();
    if (playerStore.hasCharacter()) {
        Vector3f storedLocation = playerStore.getRelevanceLocation();
        loc.setWorldPosition(storedLocation);
        entity.saveComponent(loc);
        if (worldProvider.isBlockRelevant(storedLocation)) {
            // chunk for spawning location is ready, so spawn right now
            playerStore.restoreEntities();
            EntityRef character = playerStore.getCharacter();
            Vector3i viewDist = owner.getViewDistance().getChunkDistance();
            addRelevanceEntity(entity, viewDist, owner);
            restoreCharacter(entity, character);
        } else {
            // otherwise wait until chunk is ready
            addRelevanceEntity(entity, minViewDist, owner);
            clientsPreparingToSpawn.add(new SpawningClientInfo(entity, storedLocation, playerStore));
        }
    } else {
        Vector3f spawnPosition = worldGenerator.getSpawnPosition(entity);
        loc.setWorldPosition(spawnPosition);
        entity.saveComponent(loc);
        addRelevanceEntity(entity, minViewDist, owner);
        if (worldProvider.isBlockRelevant(spawnPosition)) {
            spawnPlayer(entity);
        } else {
            clientsPreparingToSpawn.add(new SpawningClientInfo(entity, spawnPosition));
        }
    }
}
Also used : PlayerStore(org.terasology.persistence.PlayerStore) Vector3f(org.terasology.math.geom.Vector3f) Vector3i(org.terasology.math.geom.Vector3i) Client(org.terasology.network.Client) LocationComponent(org.terasology.logic.location.LocationComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 83 with ReceiveEvent

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

the class BuilderHeldItemAuthoritySystem method onGiveItemToCharacterHoldItem.

@ReceiveEvent
public void onGiveItemToCharacterHoldItem(GiveItemEvent event, EntityRef item, ItemComponent itemComponent) {
    if (event.getTargetEntity().hasComponent(CharacterHeldItemComponent.class)) {
        event.getTargetEntity().addOrSaveComponent(new CharacterOwnedItemComponent(item));
        event.getTargetEntity().send(new ChangeHeldItemRequest(item));
        event.setHandled(true);
    }
}
Also used : ChangeHeldItemRequest(org.terasology.logic.characters.events.ChangeHeldItemRequest) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 84 with ReceiveEvent

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

the class BlockDropGrammarSystem method onDestroyed.

@ReceiveEvent
public void onDestroyed(DoDestroyEvent event, EntityRef entity, BlockDropGrammarComponent blockDrop, LocationComponent locationComp) {
    BlockDamageModifierComponent blockDamageModifierComponent = event.getDamageType().getComponent(BlockDamageModifierComponent.class);
    float chanceOfBlockDrop = 1;
    if (blockDamageModifierComponent != null) {
        chanceOfBlockDrop = 1 - blockDamageModifierComponent.blockAnnihilationChance;
    }
    if (random.nextFloat() < chanceOfBlockDrop) {
        List<String> blockDrops = blockDrop.blockDrops;
        List<String> itemDrops = blockDrop.itemDrops;
        if (blockDamageModifierComponent != null && blockDrop.droppedWithTool != null) {
            for (String toolType : blockDamageModifierComponent.materialDamageMultiplier.keySet()) {
                if (blockDrop.droppedWithTool.containsKey(toolType)) {
                    BlockDropGrammarComponent.DropDefinition dropDefinition = blockDrop.droppedWithTool.get(toolType);
                    blockDrops = dropDefinition.blockDrops;
                    itemDrops = dropDefinition.itemDrops;
                    break;
                }
            }
        }
        if (blockDrops != null) {
            for (String drop : blockDrops) {
                String dropResult = drop;
                boolean dropping = true;
                int pipeIndex = dropResult.indexOf('|');
                if (pipeIndex > -1) {
                    float chance = Float.parseFloat(dropResult.substring(0, pipeIndex));
                    if (random.nextFloat() >= chance) {
                        dropping = false;
                    }
                    dropResult = dropResult.substring(pipeIndex + 1);
                }
                if (dropping) {
                    DropParser dropParser = new DropParser(random, dropResult).invoke();
                    EntityRef dropItem = blockItemFactory.newInstance(blockManager.getBlockFamily(dropParser.getDrop()), dropParser.getCount());
                    if (shouldDropToWorld(event, blockDamageModifierComponent, dropItem)) {
                        createDrop(dropItem, locationComp.getWorldPosition(), true);
                    }
                }
            }
        }
        if (itemDrops != null) {
            for (String drop : itemDrops) {
                String dropResult = drop;
                boolean dropping = true;
                int pipeIndex = dropResult.indexOf('|');
                if (pipeIndex > -1) {
                    float chance = Float.parseFloat(dropResult.substring(0, pipeIndex));
                    if (random.nextFloat() >= chance) {
                        dropping = false;
                    }
                    dropResult = dropResult.substring(pipeIndex + 1);
                }
                if (dropping) {
                    DropParser dropParser = new DropParser(random, dropResult).invoke();
                    EntityBuilder dropEntity = entityManager.newBuilder(dropParser.getDrop());
                    if (dropParser.getCount() > 1) {
                        ItemComponent itemComponent = dropEntity.getComponent(ItemComponent.class);
                        itemComponent.stackCount = (byte) dropParser.getCount();
                    }
                    EntityRef dropItem = dropEntity.build();
                    if (shouldDropToWorld(event, blockDamageModifierComponent, dropItem)) {
                        createDrop(dropItem, locationComp.getWorldPosition(), false);
                    }
                }
            }
        }
    }
}
Also used : ItemComponent(org.terasology.logic.inventory.ItemComponent) BlockDamageModifierComponent(org.terasology.world.block.entity.damage.BlockDamageModifierComponent) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) EntityRef(org.terasology.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 85 with ReceiveEvent

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

the class DoorSystem method openDoor.

@ReceiveEvent
public void openDoor(OpenDoorEvent event, EntityRef player) {
    EntityRef entity = event.getDoorEntity();
    DoorComponent door = entity.getComponent(DoorComponent.class);
    Side newSide = door.openSide;
    BlockRegionComponent regionComp = entity.getComponent(BlockRegionComponent.class);
    Block bottomBlock = door.bottomBlockFamily.getBlockForPlacement(worldProvider, blockEntityRegistry, regionComp.region.min(), newSide, Side.TOP);
    worldProvider.setBlock(regionComp.region.min(), bottomBlock);
    Block topBlock = door.topBlockFamily.getBlockForPlacement(worldProvider, blockEntityRegistry, regionComp.region.max(), newSide, Side.TOP);
    worldProvider.setBlock(regionComp.region.max(), topBlock);
    if (door.openSound != null) {
        entity.send(new PlaySoundEvent(door.openSound, 1f));
    }
    door.isOpen = true;
}
Also used : Side(org.terasology.math.Side) BlockRegionComponent(org.terasology.world.block.regions.BlockRegionComponent) PlaySoundEvent(org.terasology.audio.events.PlaySoundEvent) Block(org.terasology.world.block.Block) 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