Search in sources :

Example 6 with Block

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

use of org.terasology.world.block.Block 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 8 with Block

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

the class FloraRasterizer method generateChunk.

@Override
public void generateChunk(CoreChunk chunk, Region chunkRegion) {
    FloraFacet facet = chunkRegion.getFacet(FloraFacet.class);
    WhiteNoise noise = new WhiteNoise(chunk.getPosition().hashCode());
    Map<BaseVector3i, FloraType> entries = facet.getRelativeEntries();
    // check if some other rasterizer has already placed something here
    entries.keySet().stream().filter(pos -> chunk.getBlock(pos).equals(air)).forEach(pos -> {
        FloraType type = entries.get(pos);
        List<Block> list = flora.get(type);
        int blockIdx = Math.abs(noise.intNoise(pos.x(), pos.y(), pos.z())) % list.size();
        Block block = list.get(blockIdx);
        chunk.setBlock(pos, block);
    });
}
Also used : BlockManager(org.terasology.world.block.BlockManager) Region(org.terasology.world.generation.Region) WorldRasterizer(org.terasology.world.generation.WorldRasterizer) Maps(com.google.common.collect.Maps) FloraFacet(org.terasology.core.world.generator.facets.FloraFacet) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) BaseVector3i(org.terasology.math.geom.BaseVector3i) Block(org.terasology.world.block.Block) CoreChunk(org.terasology.world.chunks.CoreChunk) Map(java.util.Map) CoreRegistry(org.terasology.registry.CoreRegistry) WhiteNoise(org.terasology.utilities.procedural.WhiteNoise) FloraFacet(org.terasology.core.world.generator.facets.FloraFacet) WhiteNoise(org.terasology.utilities.procedural.WhiteNoise) BaseVector3i(org.terasology.math.geom.BaseVector3i) Block(org.terasology.world.block.Block)

Example 9 with Block

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

the class SolidRasterizer method generateChunk.

@Override
public void generateChunk(CoreChunk chunk, Region chunkRegion) {
    LiquidData waterLiquid = new LiquidData(LiquidType.WATER, LiquidData.MAX_LIQUID_DEPTH);
    DensityFacet solidityFacet = chunkRegion.getFacet(DensityFacet.class);
    SurfaceHeightFacet surfaceFacet = chunkRegion.getFacet(SurfaceHeightFacet.class);
    SurfaceDepthFacet surfaceDepthFacet = chunkRegion.getFacet(SurfaceDepthFacet.class);
    BiomeFacet biomeFacet = chunkRegion.getFacet(BiomeFacet.class);
    SeaLevelFacet seaLevelFacet = chunkRegion.getFacet(SeaLevelFacet.class);
    int seaLevel = seaLevelFacet.getSeaLevel();
    Vector2i pos2d = new Vector2i();
    for (Vector3i pos : ChunkConstants.CHUNK_REGION) {
        pos2d.set(pos.x, pos.z);
        int posY = pos.y + chunk.getChunkWorldOffsetY();
        // Check for an optional depth for this layer - if defined stop generating below that level
        if (surfaceDepthFacet != null && posY < surfaceDepthFacet.get(pos2d)) {
            continue;
        }
        Biome biome = biomeFacet.get(pos2d);
        chunk.setBiome(pos.x, pos.y, pos.z, biome);
        float density = solidityFacet.get(pos);
        if (density >= 32) {
            chunk.setBlock(pos, stone);
        } else if (density >= 0) {
            int depth = TeraMath.floorToInt(surfaceFacet.get(pos2d)) - posY;
            Block block = getSurfaceBlock(depth, posY, biome, seaLevel);
            chunk.setBlock(pos, block);
        } else {
            // fill up terrain up to sealevel height with water or ice
            if (posY == seaLevel && CoreBiome.SNOW == biome) {
                chunk.setBlock(pos, ice);
            } else if (posY <= seaLevel) {
                // either OCEAN or SNOW
                chunk.setBlock(pos, water);
                chunk.setLiquid(pos, waterLiquid);
            }
        }
    }
}
Also used : SurfaceHeightFacet(org.terasology.world.generation.facets.SurfaceHeightFacet) Biome(org.terasology.world.biomes.Biome) CoreBiome(org.terasology.core.world.CoreBiome) DensityFacet(org.terasology.world.generation.facets.DensityFacet) Vector3i(org.terasology.math.geom.Vector3i) Block(org.terasology.world.block.Block) SeaLevelFacet(org.terasology.world.generation.facets.SeaLevelFacet) Vector2i(org.terasology.math.geom.Vector2i) BiomeFacet(org.terasology.core.world.generator.facets.BiomeFacet) LiquidData(org.terasology.world.liquid.LiquidData) SurfaceDepthFacet(org.terasology.world.generation.facets.SurfaceDepthFacet)

Example 10 with Block

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

the class TreeGeneratorLSystem method generate.

@Override
public void generate(BlockManager blockManager, CoreChunk view, Random rand, int posX, int posY, int posZ) {
    Vector3f position = new Vector3f(0f, 0f, 0f);
    Matrix4f rotation = new Matrix4f(new Quat4f(new Vector3f(0f, 0f, 1f), (float) Math.PI / 2f), Vector3f.ZERO, 1.0f);
    float angleOffset = rand.nextFloat(-MAX_ANGLE_OFFSET, MAX_ANGLE_OFFSET);
    Block bark = blockManager.getBlock(barkType);
    Block leaf = blockManager.getBlock(leafType);
    recursiveGenerator.recurse(view, rand, posX, posY, posZ, angleOffset, new CharSequenceIterator(initialAxiom), position, rotation, bark, leaf, 0, this);
}
Also used : Matrix4f(org.terasology.math.geom.Matrix4f) Vector3f(org.terasology.math.geom.Vector3f) Block(org.terasology.world.block.Block) Quat4f(org.terasology.math.geom.Quat4f) CharSequenceIterator(org.terasology.utilities.collection.CharSequenceIterator)

Aggregations

Block (org.terasology.world.block.Block)67 Vector3i (org.terasology.math.geom.Vector3i)32 EntityRef (org.terasology.entitySystem.entity.EntityRef)16 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)11 Side (org.terasology.math.Side)11 Vector3f (org.terasology.math.geom.Vector3f)9 LocationComponent (org.terasology.logic.location.LocationComponent)7 BlockUri (org.terasology.world.block.BlockUri)7 BlockFamily (org.terasology.world.block.family.BlockFamily)7 PlaySoundEvent (org.terasology.audio.events.PlaySoundEvent)6 OnChangedBlock (org.terasology.world.OnChangedBlock)6 BlockComponent (org.terasology.world.block.BlockComponent)5 BlockManager (org.terasology.world.block.BlockManager)5 BlockRegionComponent (org.terasology.world.block.regions.BlockRegionComponent)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Before (org.junit.Before)3 ResourceUrn (org.terasology.assets.ResourceUrn)3 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)3 DoDamageEvent (org.terasology.logic.health.DoDamageEvent)3