Search in sources :

Example 36 with EntityRef

use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class FirstPersonClientSystem method onHeldItemChanged.

@ReceiveEvent
public void onHeldItemChanged(OnChangedComponent event, EntityRef character, CharacterHeldItemComponent heldItemComponent, CharacterComponent characterComponents) {
    if (localPlayer.getCharacterEntity().equals(character)) {
        EntityRef newItem = heldItemComponent.selectedItem;
        linkHeldItemLocationForLocalPlayer(newItem);
    }
}
Also used : EntityRef(org.terasology.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 37 with EntityRef

use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class LocalPlayerSystem method onTargetChanged.

@ReceiveEvent
public void onTargetChanged(PlayerTargetChangedEvent event, EntityRef entity) {
    EntityRef target = event.getNewTarget();
    if (target.exists()) {
        LocationComponent location = target.getComponent(LocationComponent.class);
        if (location != null) {
            BlockComponent blockComp = target.getComponent(BlockComponent.class);
            BlockRegionComponent blockRegion = target.getComponent(BlockRegionComponent.class);
            if (blockComp != null || blockRegion != null) {
                Vector3f blockPos = location.getWorldPosition();
                Block block = worldProvider.getBlock(blockPos);
                aabb = block.getBounds(blockPos);
            } else {
                MeshComponent mesh = target.getComponent(MeshComponent.class);
                if (mesh != null && mesh.mesh != null) {
                    aabb = mesh.mesh.getAABB();
                    aabb = aabb.transform(location.getWorldRotation(), location.getWorldPosition(), location.getWorldScale());
                }
            }
        }
    } else {
        aabb = null;
    }
}
Also used : BlockComponent(org.terasology.world.block.BlockComponent) MeshComponent(org.terasology.rendering.logic.MeshComponent) BlockRegionComponent(org.terasology.world.block.regions.BlockRegionComponent) Vector3f(org.terasology.math.geom.Vector3f) Block(org.terasology.world.block.Block) EntityRef(org.terasology.entitySystem.entity.EntityRef) LocationComponent(org.terasology.logic.location.LocationComponent) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 38 with EntityRef

use of org.terasology.entitySystem.entity.EntityRef 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();
    }
}
Also used : OnItemUseEvent(org.terasology.logic.characters.events.OnItemUseEvent) EntityRef(org.terasology.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 39 with EntityRef

use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class PlayerSystem method respawnPlayer.

private void respawnPlayer(EntityRef clientEntity) {
    ClientComponent client = clientEntity.getComponent(ClientComponent.class);
    EntityRef playerCharacter = client.character;
    LocationComponent location = clientEntity.getComponent(LocationComponent.class);
    PlayerFactory playerFactory = new PlayerFactory(entityManager, worldProvider);
    Vector3f spawnPosition = playerFactory.findSpawnPositionFromLocationComponent(location);
    location.setWorldPosition(spawnPosition);
    clientEntity.saveComponent(location);
    playerCharacter.addComponent(new AliveCharacterComponent());
    playerCharacter.send(new CharacterTeleportEvent(spawnPosition));
    logger.debug("Re-spawing player at: {}", spawnPosition);
    Client clientListener = networkSystem.getOwner(clientEntity);
    Vector3i distance = clientListener.getViewDistance().getChunkDistance();
    updateRelevanceEntity(clientEntity, distance);
    playerCharacter.send(new OnPlayerRespawnedEvent());
}
Also used : CharacterTeleportEvent(org.terasology.logic.characters.CharacterTeleportEvent) Vector3f(org.terasology.math.geom.Vector3f) Vector3i(org.terasology.math.geom.Vector3i) Client(org.terasology.network.Client) ClientComponent(org.terasology.network.ClientComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) LocationComponent(org.terasology.logic.location.LocationComponent) OnPlayerRespawnedEvent(org.terasology.logic.players.event.OnPlayerRespawnedEvent) AliveCharacterComponent(org.terasology.logic.characters.AliveCharacterComponent)

Example 40 with EntityRef

use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class LocalPlayerBlockSelectionByItemSystem method onPlaced.

@ReceiveEvent(components = { OnItemActivateSelectionComponent.class })
public void onPlaced(ActivateEvent event, EntityRef itemEntity) {
    if (event.getTargetLocation() == null) {
        return;
    }
    EntityRef targetLocationEntity = event.getTarget();
    this.blockSelectionComponentEntity = itemEntity;
    BlockSelectionComponent blockSelectionComponent = itemEntity.getComponent(BlockSelectionComponent.class);
    if (null == blockSelectionComponent.startPosition) {
        // on the first item click, we start selecting blocks
        targetLocationEntity.send(new SetBlockSelectionStartingPointEvent(itemEntity));
        blockSelectionComponent.shouldRender = true;
    } else {
        // on the second item click, we will set the ending selection point and send an ApplyBlockSelectionEvent
        targetLocationEntity.send(new SetBlockSelectionEndingPointEvent(itemEntity));
        localPlayer.getCharacterEntity().send(new ApplyBlockSelectionEvent(itemEntity, blockSelectionComponent.currentSelection));
        blockSelectionComponent.shouldRender = false;
        blockSelectionComponent.currentSelection = null;
        blockSelectionComponent.startPosition = null;
    }
}
Also used : SetBlockSelectionEndingPointEvent(org.terasology.world.selection.event.SetBlockSelectionEndingPointEvent) BlockSelectionComponent(org.terasology.world.selection.BlockSelectionComponent) SetBlockSelectionStartingPointEvent(org.terasology.world.selection.event.SetBlockSelectionStartingPointEvent) EntityRef(org.terasology.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Aggregations

EntityRef (org.terasology.entitySystem.entity.EntityRef)337 Test (org.junit.Test)106 ClientComponent (org.terasology.network.ClientComponent)49 LocationComponent (org.terasology.logic.location.LocationComponent)45 Vector3f (org.terasology.math.geom.Vector3f)44 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)36 Vector3i (org.terasology.math.geom.Vector3i)34 Command (org.terasology.logic.console.commandSystem.annotations.Command)28 StringComponent (org.terasology.entitySystem.stubs.StringComponent)26 NetworkComponent (org.terasology.network.NetworkComponent)21 EntityData (org.terasology.protobuf.EntityData)21 DisplayNameComponent (org.terasology.logic.common.DisplayNameComponent)17 Block (org.terasology.world.block.Block)16 Component (org.terasology.entitySystem.Component)15 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)15 CharacterComponent (org.terasology.logic.characters.CharacterComponent)14 Quat4f (org.terasology.math.geom.Quat4f)14 BlockComponent (org.terasology.world.block.BlockComponent)13 Map (java.util.Map)11 LocalPlayer (org.terasology.logic.players.LocalPlayer)11