Search in sources :

Example 1 with BlockSelectionComponent

use of org.terasology.engine.world.selection.BlockSelectionComponent 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.engine.world.selection.event.SetBlockSelectionEndingPointEvent) BlockSelectionComponent(org.terasology.engine.world.selection.BlockSelectionComponent) SetBlockSelectionStartingPointEvent(org.terasology.engine.world.selection.event.SetBlockSelectionStartingPointEvent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 2 with BlockSelectionComponent

use of org.terasology.engine.world.selection.BlockSelectionComponent in project Terasology by MovingBlocks.

the class LocalPlayerBlockSelectionByItemSystem method onCamTargetChanged.

@ReceiveEvent(components = LocationComponent.class)
public void onCamTargetChanged(CameraTargetChangedEvent event, EntityRef entity) {
    // This method will update the block selection to whatever block is targeted in the players view
    if (null == blockSelectionComponentEntity) {
        return;
    }
    BlockSelectionComponent blockSelectionComponent = blockSelectionComponentEntity.getComponent(BlockSelectionComponent.class);
    if (blockSelectionComponent == null) {
        return;
    }
    EntityRef target = event.getNewTarget();
    LocationComponent locationComponent = target.getComponent(LocationComponent.class);
    if (locationComponent == null) {
        return;
    }
    Vector3f targetLocation = locationComponent.getWorldPosition(new Vector3f());
    if (blockSelectionComponent.isMovable) {
        Vector3i pos = new Vector3i(targetLocation, RoundingMode.FLOOR);
        Vector3i size = blockSelectionComponent.currentSelection.getSize(new Vector3i());
        blockSelectionComponent.currentSelection.set(pos, pos).expand(size.x() / 2, 0, size.z() / 2);
        blockSelectionComponentEntity.saveComponent(blockSelectionComponent);
        return;
    }
    if (blockSelectionComponent.startPosition == null) {
        return;
    }
    target.send(new SetBlockSelectionEndingPointEvent(blockSelectionComponentEntity));
}
Also used : SetBlockSelectionEndingPointEvent(org.terasology.engine.world.selection.event.SetBlockSelectionEndingPointEvent) BlockSelectionComponent(org.terasology.engine.world.selection.BlockSelectionComponent) Vector3f(org.joml.Vector3f) Vector3i(org.joml.Vector3i) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) LocationComponent(org.terasology.engine.logic.location.LocationComponent) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 3 with BlockSelectionComponent

use of org.terasology.engine.world.selection.BlockSelectionComponent in project Terasology by MovingBlocks.

the class LocalPlayerBlockSelectionByItemSystem method onLeftMouseButtonDown.

/**
 * This marks the end of the camera position binding with the region position.
 * @param event LeftMouseButtonDownEvent
 * @param entity Entity sending the event
 */
@ReceiveEvent
public void onLeftMouseButtonDown(LeftMouseDownButtonEvent event, EntityRef entity) {
    if (this.blockSelectionComponentEntity != null && this.blockSelectionComponentEntity != EntityRef.NULL) {
        BlockSelectionComponent blockSelectionComponent = blockSelectionComponentEntity.getComponent(BlockSelectionComponent.class);
        if (blockSelectionComponent != null && blockSelectionComponent.isMovable) {
            blockSelectionComponentEntity.send(new MovableSelectionEndEvent(blockSelectionComponent.currentSelection));
            blockSelectionComponentEntity.destroy();
        }
    }
}
Also used : BlockSelectionComponent(org.terasology.engine.world.selection.BlockSelectionComponent) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 4 with BlockSelectionComponent

use of org.terasology.engine.world.selection.BlockSelectionComponent in project Terasology by MovingBlocks.

the class BlockSelectionRenderSystem method renderOverlay.

@Override
public void renderOverlay() {
    for (EntityRef entity : entityManager.getEntitiesWith(BlockSelectionComponent.class)) {
        BlockSelectionComponent blockSelectionComponent = entity.getComponent(BlockSelectionComponent.class);
        if (blockSelectionComponent.shouldRender) {
            Texture texture = blockSelectionComponent.texture;
            if (null == texture) {
                texture = Assets.getTexture("engine:selection").get();
            }
            Vector2i textureDimensions = new Vector2i(texture.getWidth(), texture.getHeight());
            BlockSelectionRenderer selectionRenderer = cachedBlockSelectionRendererByTextureDimensionsMap.get(textureDimensions);
            if (null == selectionRenderer) {
                selectionRenderer = new BlockSelectionRenderer(texture);
                cachedBlockSelectionRendererByTextureDimensionsMap.put(textureDimensions, selectionRenderer);
            } else {
                selectionRenderer.setEffectsTexture(texture);
            }
            renderOverlayForOneBlockSelection(blockSelectionComponent, selectionRenderer);
        }
    }
}
Also used : BlockSelectionComponent(org.terasology.engine.world.selection.BlockSelectionComponent) Vector2i(org.joml.Vector2i) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) Texture(org.terasology.engine.rendering.assets.texture.Texture)

Aggregations

BlockSelectionComponent (org.terasology.engine.world.selection.BlockSelectionComponent)4 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)3 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)3 SetBlockSelectionEndingPointEvent (org.terasology.engine.world.selection.event.SetBlockSelectionEndingPointEvent)2 Vector2i (org.joml.Vector2i)1 Vector3f (org.joml.Vector3f)1 Vector3i (org.joml.Vector3i)1 LocationComponent (org.terasology.engine.logic.location.LocationComponent)1 Texture (org.terasology.engine.rendering.assets.texture.Texture)1 SetBlockSelectionStartingPointEvent (org.terasology.engine.world.selection.event.SetBlockSelectionStartingPointEvent)1