Search in sources :

Example 1 with Block

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

the class ChunkTessellator method calcLightingValuesForVertexPos.

private void calcLightingValuesForVertexPos(ChunkView chunkView, Vector3f vertexPos, float[] output, Vector3f normal) {
    PerformanceMonitor.startActivity("calcLighting");
    float[] lights = new float[8];
    float[] blockLights = new float[8];
    Block[] blocks = new Block[4];
    PerformanceMonitor.startActivity("gatherLightInfo");
    Direction dir = Direction.inDirection(normal);
    switch(dir) {
        case LEFT:
        case RIGHT:
            blocks[0] = chunkView.getBlock((vertexPos.x + 0.8f * normal.x), (vertexPos.y + 0.1f), (vertexPos.z + 0.1f));
            blocks[1] = chunkView.getBlock((vertexPos.x + 0.8f * normal.x), (vertexPos.y + 0.1f), (vertexPos.z - 0.1f));
            blocks[2] = chunkView.getBlock((vertexPos.x + 0.8f * normal.x), (vertexPos.y - 0.1f), (vertexPos.z - 0.1f));
            blocks[3] = chunkView.getBlock((vertexPos.x + 0.8f * normal.x), (vertexPos.y - 0.1f), (vertexPos.z + 0.1f));
            break;
        case FORWARD:
        case BACKWARD:
            blocks[0] = chunkView.getBlock((vertexPos.x + 0.1f), (vertexPos.y + 0.1f), (vertexPos.z + 0.8f * normal.z));
            blocks[1] = chunkView.getBlock((vertexPos.x + 0.1f), (vertexPos.y - 0.1f), (vertexPos.z + 0.8f * normal.z));
            blocks[2] = chunkView.getBlock((vertexPos.x - 0.1f), (vertexPos.y - 0.1f), (vertexPos.z + 0.8f * normal.z));
            blocks[3] = chunkView.getBlock((vertexPos.x - 0.1f), (vertexPos.y + 0.1f), (vertexPos.z + 0.8f * normal.z));
            break;
        default:
            blocks[0] = chunkView.getBlock((vertexPos.x + 0.1f), (vertexPos.y + 0.8f * normal.y), (vertexPos.z + 0.1f));
            blocks[1] = chunkView.getBlock((vertexPos.x + 0.1f), (vertexPos.y + 0.8f * normal.y), (vertexPos.z - 0.1f));
            blocks[2] = chunkView.getBlock((vertexPos.x - 0.1f), (vertexPos.y + 0.8f * normal.y), (vertexPos.z - 0.1f));
            blocks[3] = chunkView.getBlock((vertexPos.x - 0.1f), (vertexPos.y + 0.8f * normal.y), (vertexPos.z + 0.1f));
    }
    lights[0] = chunkView.getSunlight((vertexPos.x + 0.1f), (vertexPos.y + 0.8f), (vertexPos.z + 0.1f));
    lights[1] = chunkView.getSunlight((vertexPos.x + 0.1f), (vertexPos.y + 0.8f), (vertexPos.z - 0.1f));
    lights[2] = chunkView.getSunlight((vertexPos.x - 0.1f), (vertexPos.y + 0.8f), (vertexPos.z - 0.1f));
    lights[3] = chunkView.getSunlight((vertexPos.x - 0.1f), (vertexPos.y + 0.8f), (vertexPos.z + 0.1f));
    lights[4] = chunkView.getSunlight((vertexPos.x + 0.1f), (vertexPos.y - 0.1f), (vertexPos.z + 0.1f));
    lights[5] = chunkView.getSunlight((vertexPos.x + 0.1f), (vertexPos.y - 0.1f), (vertexPos.z - 0.1f));
    lights[6] = chunkView.getSunlight((vertexPos.x - 0.1f), (vertexPos.y - 0.1f), (vertexPos.z - 0.1f));
    lights[7] = chunkView.getSunlight((vertexPos.x - 0.1f), (vertexPos.y - 0.1f), (vertexPos.z + 0.1f));
    blockLights[0] = chunkView.getLight((vertexPos.x + 0.1f), (vertexPos.y + 0.8f), (vertexPos.z + 0.1f));
    blockLights[1] = chunkView.getLight((vertexPos.x + 0.1f), (vertexPos.y + 0.8f), (vertexPos.z - 0.1f));
    blockLights[2] = chunkView.getLight((vertexPos.x - 0.1f), (vertexPos.y + 0.8f), (vertexPos.z - 0.1f));
    blockLights[3] = chunkView.getLight((vertexPos.x - 0.1f), (vertexPos.y + 0.8f), (vertexPos.z + 0.1f));
    blockLights[4] = chunkView.getLight((vertexPos.x + 0.1f), (vertexPos.y - 0.1f), (vertexPos.z + 0.1f));
    blockLights[5] = chunkView.getLight((vertexPos.x + 0.1f), (vertexPos.y - 0.1f), (vertexPos.z - 0.1f));
    blockLights[6] = chunkView.getLight((vertexPos.x - 0.1f), (vertexPos.y - 0.1f), (vertexPos.z - 0.1f));
    blockLights[7] = chunkView.getLight((vertexPos.x - 0.1f), (vertexPos.y - 0.1f), (vertexPos.z + 0.1f));
    PerformanceMonitor.endActivity();
    float resultLight = 0;
    float resultBlockLight = 0;
    int counterLight = 0;
    int counterBlockLight = 0;
    int occCounter = 0;
    int occCounterBillboard = 0;
    for (int i = 0; i < 8; i++) {
        if (lights[i] > 0) {
            resultLight += lights[i];
            counterLight++;
        }
        if (blockLights[i] > 0) {
            resultBlockLight += blockLights[i];
            counterBlockLight++;
        }
        if (i < 4) {
            Block b = blocks[i];
            if (b.isShadowCasting() && !b.isTranslucent()) {
                occCounter++;
            } else if (b.isShadowCasting()) {
                occCounterBillboard++;
            }
        }
    }
    double resultAmbientOcclusion = (TeraMath.pow(0.40, occCounter) + TeraMath.pow(0.80, occCounterBillboard)) / 2.0;
    if (counterLight == 0) {
        output[0] = 0;
    } else {
        output[0] = resultLight / counterLight / 15f;
    }
    if (counterBlockLight == 0) {
        output[1] = 0;
    } else {
        output[1] = resultBlockLight / counterBlockLight / 15f;
    }
    output[2] = (float) resultAmbientOcclusion;
    PerformanceMonitor.endActivity();
}
Also used : Block(org.terasology.world.block.Block) Direction(org.terasology.math.Direction)

Example 2 with Block

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

the class PhysicsSystem method update.

@Override
public void update(float delta) {
    PerformanceMonitor.startActivity("Physics Renderer");
    physics.update(time.getGameDelta());
    PerformanceMonitor.endActivity();
    // Update the velocity from physics engine bodies to Components:
    Iterator<EntityRef> iter = physics.physicsEntitiesIterator();
    while (iter.hasNext()) {
        EntityRef entity = iter.next();
        RigidBodyComponent comp = entity.getComponent(RigidBodyComponent.class);
        RigidBody body = physics.getRigidBody(entity);
        if (body.isActive()) {
            body.getLinearVelocity(comp.velocity);
            body.getAngularVelocity(comp.angularVelocity);
            Vector3f vLocation = Vector3f.zero();
            body.getLocation(vLocation);
            Vector3f vDirection = new Vector3f(comp.velocity);
            float fDistanceThisFrame = vDirection.length();
            vDirection.normalize();
            fDistanceThisFrame = fDistanceThisFrame * delta;
            while (true) {
                HitResult hitInfo = physics.rayTrace(vLocation, vDirection, fDistanceThisFrame + 0.5f, DEFAULT_COLLISION_GROUP);
                if (hitInfo.isHit()) {
                    Block hitBlock = worldProvider.getBlock(hitInfo.getBlockPosition());
                    if (hitBlock != null) {
                        Vector3f vTravelledDistance = vLocation.sub(hitInfo.getHitPoint());
                        float fTravelledDistance = vTravelledDistance.length();
                        if (fTravelledDistance > fDistanceThisFrame) {
                            break;
                        }
                        if (hitBlock.isPenetrable()) {
                            if (!hitInfo.getEntity().hasComponent(BlockComponent.class)) {
                                entity.send(new EntityImpactEvent(hitInfo.getHitPoint(), hitInfo.getHitNormal(), comp.velocity, fDistanceThisFrame, hitInfo.getEntity()));
                                break;
                            }
                            // decrease the remaining distance to check if we hit a block
                            fDistanceThisFrame = fDistanceThisFrame - fTravelledDistance;
                            vLocation = hitInfo.getHitPoint();
                        } else {
                            entity.send(new BlockImpactEvent(hitInfo.getHitPoint(), hitInfo.getHitNormal(), comp.velocity, fDistanceThisFrame, hitInfo.getEntity()));
                            break;
                        }
                    } else {
                        break;
                    }
                } else {
                    break;
                }
            }
        }
    }
    if (networkSystem.getMode().isServer() && time.getGameTimeInMs() - TIME_BETWEEN_NETSYNCS > lastNetsync) {
        sendSyncMessages();
        lastNetsync = time.getGameTimeInMs();
    }
    List<CollisionPair> collisionPairs = physics.getCollisionPairs();
    for (CollisionPair pair : collisionPairs) {
        if (pair.b.exists()) {
            short bCollisionGroup = getCollisionGroupFlag(pair.b);
            short aCollidesWith = getCollidesWithGroupFlag(pair.a);
            if ((bCollisionGroup & aCollidesWith) != 0 || (pair.b.hasComponent(BlockComponent.class) && !pair.a.hasComponent(BlockComponent.class))) {
                pair.a.send(new CollideEvent(pair.b, pair.pointA, pair.pointB, pair.distance, pair.normal));
            }
        }
        if (pair.a.exists()) {
            short aCollisionGroup = getCollisionGroupFlag(pair.a);
            short bCollidesWith = getCollidesWithGroupFlag(pair.b);
            if ((aCollisionGroup & bCollidesWith) != 0 || (pair.a.hasComponent(BlockComponent.class) && !pair.b.hasComponent(BlockComponent.class))) {
                pair.b.send(new CollideEvent(pair.a, pair.pointB, pair.pointA, pair.distance, new Vector3f(pair.normal).invert()));
            }
        }
    }
}
Also used : RigidBodyComponent(org.terasology.physics.components.RigidBodyComponent) HitResult(org.terasology.physics.HitResult) BlockComponent(org.terasology.world.block.BlockComponent) CollideEvent(org.terasology.physics.events.CollideEvent) Vector3f(org.terasology.math.geom.Vector3f) BlockImpactEvent(org.terasology.physics.events.BlockImpactEvent) Block(org.terasology.world.block.Block) OnChangedBlock(org.terasology.world.OnChangedBlock) EntityImpactEvent(org.terasology.physics.events.EntityImpactEvent) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 3 with Block

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

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

the class CharacterSoundSystem method onFootstep.

@ReceiveEvent
public void onFootstep(FootstepEvent event, EntityRef entity, LocationComponent locationComponent, CharacterSoundComponent characterSounds) {
    List<StaticSound> footstepSounds = characterSounds.footstepSounds;
    // Check if the block the character is standing on has footstep sounds
    Vector3i blockPos = new Vector3i(locationComponent.getLocalPosition());
    // The block *below* the character's feet is interesting to us
    blockPos.y--;
    Block block = worldProvider.getBlock(blockPos);
    if (block != null) {
        if (block.getSounds() == null) {
            logger.error("Block '{}' has no sounds", block.getURI());
        } else if (!block.getSounds().getStepSounds().isEmpty()) {
            footstepSounds = block.getSounds().getStepSounds();
        }
    }
    if (footstepSounds.size() > 0 && characterSounds.lastSoundTime + MIN_TIME < time.getGameTimeInMs()) {
        StaticSound sound = random.nextItem(footstepSounds);
        entity.send(new PlaySoundEvent(entity, sound, characterSounds.footstepVolume));
        characterSounds.lastSoundTime = time.getGameTimeInMs();
        entity.saveComponent(characterSounds);
    }
}
Also used : StaticSound(org.terasology.audio.StaticSound) PlaySoundEvent(org.terasology.audio.events.PlaySoundEvent) Vector3i(org.terasology.math.geom.Vector3i) Block(org.terasology.world.block.Block) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 5 with Block

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

the class ServerImpl method processBlockChanges.

private void processBlockChanges(NetData.NetMessage message) {
    for (NetData.BlockChangeMessage blockChange : message.getBlockChangeList()) {
        Block newBlock = blockManager.getBlock((short) blockChange.getNewBlock());
        logger.debug("Received block change to {}", newBlock);
        // TODO: Store changes to blocks that aren't ready to be modified (the surrounding chunks aren't available)
        WorldProvider worldProvider = CoreRegistry.get(WorldProvider.class);
        Vector3i pos = NetMessageUtil.convert(blockChange.getPos());
        if (worldProvider.isBlockRelevant(pos)) {
            worldProvider.setBlock(pos, newBlock);
        } else {
            awaitingChunkReadyBlockUpdates.put(ChunkMath.calcChunkPos(pos), blockChange);
        }
    }
}
Also used : NetData(org.terasology.protobuf.NetData) WorldProvider(org.terasology.world.WorldProvider) Vector3i(org.terasology.math.geom.Vector3i) Block(org.terasology.world.block.Block)

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