Search in sources :

Example 1 with Side

use of org.terasology.math.Side 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 2 with Side

use of org.terasology.math.Side 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 3 with Side

use of org.terasology.math.Side in project Terasology by MovingBlocks.

the class StandardBatchPropagator method reviewChange.

private void reviewChange(BlockChange blockChange) {
    byte newValue = rules.getFixedValue(blockChange.getTo(), blockChange.getPosition());
    byte existingValue = world.getValueAt(blockChange.getPosition());
    if (newValue > existingValue) {
        increase(blockChange.getPosition(), newValue);
    }
    byte oldValue = rules.getFixedValue(blockChange.getFrom(), blockChange.getPosition());
    if (newValue < oldValue) {
        reduce(blockChange.getPosition(), oldValue);
    }
    for (Side side : Side.values()) {
        PropagationComparison comparison = rules.comparePropagation(blockChange.getTo(), blockChange.getFrom(), side);
        if (comparison.isRestricting() && existingValue > 0) {
            reduce(blockChange.getPosition(), existingValue);
            Vector3i adjPos = side.getAdjacentPos(blockChange.getPosition());
            byte adjValue = world.getValueAt(adjPos);
            if (adjValue == rules.propagateValue(existingValue, side, blockChange.getFrom())) {
                reduce(adjPos, adjValue);
            }
        } else if (comparison.isPermitting()) {
            if (existingValue > 0) {
                queueSpreadValue(blockChange.getPosition(), existingValue);
            }
            Vector3i adjPos = side.getAdjacentPos(blockChange.getPosition());
            byte adjValue = world.getValueAt(adjPos);
            if (adjValue != PropagatorWorldView.UNAVAILABLE) {
                queueSpreadValue(adjPos, adjValue);
            }
        }
    }
}
Also used : Side(org.terasology.math.Side) Vector3i(org.terasology.math.geom.Vector3i)

Example 4 with Side

use of org.terasology.math.Side in project Terasology by MovingBlocks.

the class StandardBatchPropagator method push.

private void push(Vector3i pos, byte value) {
    Block block = world.getBlockAt(pos);
    for (Side side : Side.values()) {
        byte spreadValue = rules.propagateValue(value, side, block);
        Vector3i adjPos = side.getAdjacentPos(pos);
        if (rules.canSpreadOutOf(block, side)) {
            byte adjValue = world.getValueAt(adjPos);
            if (adjValue < spreadValue && adjValue != PropagatorWorldView.UNAVAILABLE) {
                Block adjBlock = world.getBlockAt(adjPos);
                if (rules.canSpreadInto(adjBlock, side.reverse())) {
                    increase(adjPos, spreadValue);
                }
            }
        }
    }
}
Also used : Side(org.terasology.math.Side) Vector3i(org.terasology.math.geom.Vector3i) Block(org.terasology.world.block.Block)

Example 5 with Side

use of org.terasology.math.Side in project Terasology by MovingBlocks.

the class StandardBatchPropagator method purge.

private void purge(Vector3i pos, byte oldValue) {
    Block block = world.getBlockAt(pos);
    increaseQueues[rules.getMaxValue() - oldValue].remove(pos);
    byte fixedValue = rules.getFixedValue(block, pos);
    if (fixedValue > 0) {
        increase(pos, fixedValue);
    } else {
        world.setValueAt(pos, NO_VALUE);
    }
    for (Side side : Side.values()) {
        byte expectedValue = rules.propagateValue(oldValue, side, block);
        Vector3i adjPos = side.getAdjacentPos(pos);
        if (rules.canSpreadOutOf(block, side)) {
            byte adjValue = world.getValueAt(adjPos);
            if (adjValue == expectedValue) {
                Block adjBlock = world.getBlockAt(adjPos);
                if (rules.canSpreadInto(adjBlock, side.reverse())) {
                    reduce(adjPos, expectedValue);
                }
            } else if (adjValue > 0) {
                queueSpreadValue(adjPos, adjValue);
            }
        }
    }
}
Also used : Side(org.terasology.math.Side) Vector3i(org.terasology.math.geom.Vector3i) Block(org.terasology.world.block.Block)

Aggregations

Side (org.terasology.math.Side)18 Block (org.terasology.world.block.Block)11 Vector3i (org.terasology.math.geom.Vector3i)9 PlaySoundEvent (org.terasology.audio.events.PlaySoundEvent)4 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)4 Rotation (org.terasology.math.Rotation)4 EntityRef (org.terasology.entitySystem.entity.EntityRef)3 BlockPart (org.terasology.world.block.BlockPart)3 BlockUri (org.terasology.world.block.BlockUri)3 BlockRegionComponent (org.terasology.world.block.regions.BlockRegionComponent)3 BlockComponent (org.terasology.world.block.BlockComponent)2 PlaceBlocks (org.terasology.world.block.entity.placement.PlaceBlocks)2 BlockFamily (org.terasology.world.block.family.BlockFamily)2 Chunk (org.terasology.world.chunks.Chunk)2 TIntArrayList (gnu.trove.list.array.TIntArrayList)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LocationComponent (org.terasology.logic.location.LocationComponent)1 Vector3f (org.terasology.math.geom.Vector3f)1 Vector4f (org.terasology.math.geom.Vector4f)1