Search in sources :

Example 1 with BlockRegionComponent

use of org.terasology.world.block.regions.BlockRegionComponent 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 2 with BlockRegionComponent

use of org.terasology.world.block.regions.BlockRegionComponent in project Terasology by MovingBlocks.

the class DoorSystem method placeDoor.

@ReceiveEvent(components = { DoorComponent.class, ItemComponent.class })
public void placeDoor(ActivateEvent event, EntityRef entity) {
    DoorComponent door = entity.getComponent(DoorComponent.class);
    BlockComponent targetBlockComp = event.getTarget().getComponent(BlockComponent.class);
    if (targetBlockComp == null) {
        event.consume();
        return;
    }
    Vector3f horizDir = new Vector3f(event.getDirection());
    horizDir.y = 0;
    Side facingDir = Side.inDirection(horizDir);
    if (!facingDir.isHorizontal()) {
        event.consume();
        return;
    }
    Vector3f offset = new Vector3f(event.getHitPosition());
    offset.sub(targetBlockComp.getPosition().toVector3f());
    Side offsetDir = Side.inDirection(offset);
    Vector3i primePos = new Vector3i(targetBlockComp.getPosition());
    primePos.add(offsetDir.getVector3i());
    Block primeBlock = worldProvider.getBlock(primePos);
    if (!primeBlock.isReplacementAllowed()) {
        event.consume();
        return;
    }
    Block belowBlock = worldProvider.getBlock(primePos.x, primePos.y - 1, primePos.z);
    Block aboveBlock = worldProvider.getBlock(primePos.x, primePos.y + 1, primePos.z);
    // Determine top and bottom blocks
    Vector3i bottomBlockPos;
    Vector3i topBlockPos;
    if (belowBlock.isReplacementAllowed()) {
        bottomBlockPos = new Vector3i(primePos.x, primePos.y - 1, primePos.z);
        topBlockPos = primePos;
    } else if (aboveBlock.isReplacementAllowed()) {
        bottomBlockPos = primePos;
        topBlockPos = new Vector3i(primePos.x, primePos.y + 1, primePos.z);
    } else {
        event.consume();
        return;
    }
    Side attachSide = determineAttachSide(facingDir, offsetDir, bottomBlockPos, topBlockPos);
    if (attachSide == null) {
        event.consume();
        return;
    }
    Side closedSide = facingDir.reverse();
    if (closedSide == attachSide || closedSide.reverse() == attachSide) {
        closedSide = attachSide.yawClockwise(1);
    }
    Block newBottomBlock = door.bottomBlockFamily.getBlockForPlacement(worldProvider, blockEntityRegistry, bottomBlockPos, closedSide, Side.TOP);
    Block newTopBlock = door.topBlockFamily.getBlockForPlacement(worldProvider, blockEntityRegistry, bottomBlockPos, closedSide, Side.TOP);
    Map<Vector3i, Block> blockMap = new HashMap<>();
    blockMap.put(bottomBlockPos, newBottomBlock);
    blockMap.put(topBlockPos, newTopBlock);
    PlaceBlocks blockEvent = new PlaceBlocks(blockMap, event.getInstigator());
    worldProvider.getWorldEntity().send(blockEvent);
    if (!blockEvent.isConsumed()) {
        EntityRef newDoor = entityManager.create(door.doorRegionPrefab);
        entity.removeComponent(MeshComponent.class);
        newDoor.addComponent(new BlockRegionComponent(Region3i.createBounded(bottomBlockPos, topBlockPos)));
        Vector3f doorCenter = bottomBlockPos.toVector3f();
        doorCenter.y += 0.5f;
        newDoor.addComponent(new LocationComponent(doorCenter));
        DoorComponent newDoorComp = newDoor.getComponent(DoorComponent.class);
        newDoorComp.closedSide = closedSide;
        newDoorComp.openSide = attachSide.reverse();
        newDoorComp.isOpen = false;
        newDoor.saveComponent(newDoorComp);
        newDoor.send(new PlaySoundEvent(Assets.getSound("engine:PlaceBlock").get(), 0.5f));
        logger.info("Closed Side: {}", newDoorComp.closedSide);
        logger.info("Open Side: {}", newDoorComp.openSide);
        newDoor.send(new DoorPlacedEvent(event.getInstigator()));
    }
}
Also used : HashMap(java.util.HashMap) BlockRegionComponent(org.terasology.world.block.regions.BlockRegionComponent) PlaySoundEvent(org.terasology.audio.events.PlaySoundEvent) LocationComponent(org.terasology.logic.location.LocationComponent) BlockComponent(org.terasology.world.block.BlockComponent) Side(org.terasology.math.Side) Vector3f(org.terasology.math.geom.Vector3f) Vector3i(org.terasology.math.geom.Vector3i) Block(org.terasology.world.block.Block) PlaceBlocks(org.terasology.world.block.entity.placement.PlaceBlocks) EntityRef(org.terasology.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 3 with BlockRegionComponent

use of org.terasology.world.block.regions.BlockRegionComponent in project Terasology by MovingBlocks.

the class DoorSystem method closeDoor.

@ReceiveEvent
public void closeDoor(CloseDoorEvent event, EntityRef player) {
    EntityRef entity = event.getDoorEntity();
    DoorComponent door = entity.getComponent(DoorComponent.class);
    Side newSide = door.closedSide;
    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.closeSound != null) {
        entity.send(new PlaySoundEvent(door.closeSound, 1f));
    }
    door.isOpen = false;
    entity.saveComponent(door);
}
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)

Example 4 with BlockRegionComponent

use of org.terasology.world.block.regions.BlockRegionComponent in project Terasology by MovingBlocks.

the class BlockEntitySystem method defaultDropsHandling.

@ReceiveEvent(priority = EventPriority.PRIORITY_TRIVIAL)
public void defaultDropsHandling(CreateBlockDropsEvent event, EntityRef entity, ActAsBlockComponent blockComponent) {
    if (blockComponent.block != null) {
        if (entity.hasComponent(BlockRegionComponent.class)) {
            BlockRegionComponent blockRegion = entity.getComponent(BlockRegionComponent.class);
            if (blockComponent.dropBlocksInRegion) {
                // loop through all the blocks in this region and drop them
                for (Vector3i location : blockRegion.region) {
                    Block blockInWorld = worldProvider.getBlock(location);
                    commonDefaultDropsHandling(event, entity, location, blockInWorld.getBlockFamily().getArchetypeBlock());
                }
            } else {
                // just drop the ActAsBlock block
                Vector3i location = new Vector3i(blockRegion.region.center(), RoundingMode.HALF_UP);
                commonDefaultDropsHandling(event, entity, location, blockComponent.block.getArchetypeBlock());
            }
        } else if (entity.hasComponent(LocationComponent.class)) {
            LocationComponent locationComponent = entity.getComponent(LocationComponent.class);
            Vector3i location = new Vector3i(locationComponent.getWorldPosition(), RoundingMode.HALF_UP);
            commonDefaultDropsHandling(event, entity, location, blockComponent.block.getArchetypeBlock());
        }
    }
}
Also used : BlockRegionComponent(org.terasology.world.block.regions.BlockRegionComponent) Vector3i(org.terasology.math.geom.Vector3i) Block(org.terasology.world.block.Block) LocationComponent(org.terasology.logic.location.LocationComponent) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 5 with BlockRegionComponent

use of org.terasology.world.block.regions.BlockRegionComponent in project Terasology by MovingBlocks.

the class EntityAwareWorldProvider method onBlockRegionActivated.

@ReceiveEvent(components = { BlockRegionComponent.class })
public void onBlockRegionActivated(OnActivatedComponent event, EntityRef entity) {
    BlockRegionComponent regionComp = entity.getComponent(BlockRegionComponent.class);
    blockRegions.put(entity, regionComp.region);
    for (Vector3i pos : regionComp.region) {
        blockRegionLookup.put(pos, entity);
    }
}
Also used : BlockRegionComponent(org.terasology.world.block.regions.BlockRegionComponent) Vector3i(org.terasology.math.geom.Vector3i) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Aggregations

BlockRegionComponent (org.terasology.world.block.regions.BlockRegionComponent)8 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)7 EntityRef (org.terasology.entitySystem.entity.EntityRef)5 Vector3i (org.terasology.math.geom.Vector3i)5 Block (org.terasology.world.block.Block)5 PlaySoundEvent (org.terasology.audio.events.PlaySoundEvent)3 LocationComponent (org.terasology.logic.location.LocationComponent)3 Side (org.terasology.math.Side)3 BlockComponent (org.terasology.world.block.BlockComponent)3 Vector3f (org.terasology.math.geom.Vector3f)2 HashMap (java.util.HashMap)1 Region3i (org.terasology.math.Region3i)1 Texture (org.terasology.rendering.assets.texture.Texture)1 TextureRegionAsset (org.terasology.rendering.assets.texture.TextureRegionAsset)1 MeshComponent (org.terasology.rendering.logic.MeshComponent)1 BlockSelectionRenderer (org.terasology.rendering.world.selection.BlockSelectionRenderer)1 PlaceBlocks (org.terasology.world.block.entity.placement.PlaceBlocks)1