From b374e9a7368b81231e2512798b355debaea08630 Mon Sep 17 00:00:00 2001 From: opfromthestart Date: Tue, 4 May 2021 20:29:06 -0400 Subject: [PATCH] Added FallingBlock Classes -Added TestEntityDataFallingBlock class -Right now only supports Test:Sand -Kinda just copied from Statie -Added TestEntityRenderFallingBlock -Used BlockRenderTexturedCube and TestEntityRenderStatie for this one -Has a setTexture method, but it needs improving -Added TestEntityLogicFallingBlock class -Allows for a falling block to return to being a block. -The coordinates of the entity never move, even if it does ingame. -In TestContent -Registers Test:FallingBlock -In TestWorldGenerator -Adds listener for a sand block being placed -Listener replaces the block with a FallingBlock entity -It should also replace the block with air but it hasn't been working for me. --- .../windcorp/progressia/test/TestContent.java | 4 + .../test/TestEntityDataFallingBlock.java | 30 +++++ .../test/TestEntityLogicFallingBlock.java | 51 +++++++++ .../test/TestEntityRenderFallingBlock.java | 55 +++++++++ .../test/gen/TestWorldGenerator.java | 104 ++++++++++-------- 5 files changed, 197 insertions(+), 47 deletions(-) create mode 100644 src/main/java/ru/windcorp/progressia/test/TestEntityDataFallingBlock.java create mode 100644 src/main/java/ru/windcorp/progressia/test/TestEntityLogicFallingBlock.java create mode 100644 src/main/java/ru/windcorp/progressia/test/TestEntityRenderFallingBlock.java diff --git a/src/main/java/ru/windcorp/progressia/test/TestContent.java b/src/main/java/ru/windcorp/progressia/test/TestContent.java index dad5496..7f4ed7e 100644 --- a/src/main/java/ru/windcorp/progressia/test/TestContent.java +++ b/src/main/java/ru/windcorp/progressia/test/TestContent.java @@ -245,6 +245,10 @@ public class TestContent { register("Test:Statie", TestEntityDataStatie::new); register(new TestEntityRenderStatie("Test:Statie")); register(new TestEntityLogicStatie("Test:Statie")); + + register("Test:FallingBlock", TestEntityDataFallingBlock::new); + register(new TestEntityRenderFallingBlock("Test:FallingBlock")); + register(new TestEntityLogicFallingBlock("Test:FallingBlock")); } private static void regsiterControls() { diff --git a/src/main/java/ru/windcorp/progressia/test/TestEntityDataFallingBlock.java b/src/main/java/ru/windcorp/progressia/test/TestEntityDataFallingBlock.java new file mode 100644 index 0000000..cdb5060 --- /dev/null +++ b/src/main/java/ru/windcorp/progressia/test/TestEntityDataFallingBlock.java @@ -0,0 +1,30 @@ +package ru.windcorp.progressia.test; + +import ru.windcorp.progressia.common.collision.AABB; +import ru.windcorp.progressia.common.world.block.BlockData; +import ru.windcorp.progressia.common.world.entity.EntityData; + +/** + * Data for Test:FallingBlock + * @author opfromthestart + * + */ +public class TestEntityDataFallingBlock extends EntityData { + + private BlockData block; + + public TestEntityDataFallingBlock() { + this("Test:FallingBlock",new BlockData("Test:Sand")); + } + + protected TestEntityDataFallingBlock(String id, BlockData blockInput) { + super(id); + setCollisionModel(new AABB(0,0,0,1,1,1)); + block = blockInput; + } + + public BlockData getBlock() + { + return block; + } +} diff --git a/src/main/java/ru/windcorp/progressia/test/TestEntityLogicFallingBlock.java b/src/main/java/ru/windcorp/progressia/test/TestEntityLogicFallingBlock.java new file mode 100644 index 0000000..1b8b600 --- /dev/null +++ b/src/main/java/ru/windcorp/progressia/test/TestEntityLogicFallingBlock.java @@ -0,0 +1,51 @@ +package ru.windcorp.progressia.test; + +import org.apache.logging.log4j.LogManager; + +import glm.vec._3.Vec3; +import glm.vec._3.i.Vec3i; +import ru.windcorp.progressia.common.world.entity.EntityData; +import ru.windcorp.progressia.server.world.TickContext; +import ru.windcorp.progressia.server.world.entity.EntityLogic; + +/** + * Logic for Test:FallingBlock + * @author opfromthestart + * + */ +public class TestEntityLogicFallingBlock extends EntityLogic { + + public TestEntityLogicFallingBlock(String id) { + super(id); + } + + @Override + public void tick(EntityData entity, TickContext context) { + super.tick(entity, context); + + //friction + Vec3 vel = entity.getVelocity(); + float friction = .8f; + vel = new Vec3(vel.x*friction,vel.y*friction,vel.z); + entity.setVelocity(vel); + + + TestEntityDataFallingBlock fallBlock = (TestEntityDataFallingBlock) context.getServer().getWorld().getData().getEntity(entity.getEntityId());; + + Vec3i occupiedBlock = entity.getBlockInWorld(null); + Vec3i underBlock = occupiedBlock.sub_(0, 0, 1); + + Vec3i chunkCoords = underBlock.div_(16); + Vec3i inChunkCoords = underBlock.mod_(new Vec3i(16)); + + LogManager.getLogger().info("FallingBlock is at "+String.valueOf(occupiedBlock.x)+" "+String.valueOf(occupiedBlock.y)+" "+String.valueOf(occupiedBlock.z)); + LogManager.getLogger().info("Block is of type " + context.getWorldData().getChunk(chunkCoords).getBlock(inChunkCoords).getId()); + + if (context.getServer().getWorld().getData().getChunk(chunkCoords).getBlock(inChunkCoords) + .getId() != "Test:Air") { + LogManager.getLogger().info("Deleting FallingBlock at " + String.valueOf(occupiedBlock.x)); + context.getServer().getWorldAccessor().setBlock(occupiedBlock, fallBlock.getBlock()); + context.getServer().getWorld().getData().removeEntity(entity); + } + } +} diff --git a/src/main/java/ru/windcorp/progressia/test/TestEntityRenderFallingBlock.java b/src/main/java/ru/windcorp/progressia/test/TestEntityRenderFallingBlock.java new file mode 100644 index 0000000..7fe07da --- /dev/null +++ b/src/main/java/ru/windcorp/progressia/test/TestEntityRenderFallingBlock.java @@ -0,0 +1,55 @@ +package ru.windcorp.progressia.test; + +import ru.windcorp.progressia.client.graphics.model.Renderable; +import ru.windcorp.progressia.client.graphics.model.ShapeRenderHelper; +import ru.windcorp.progressia.client.graphics.model.Shapes; +import ru.windcorp.progressia.client.graphics.texture.Atlases; +import ru.windcorp.progressia.client.graphics.texture.SimpleTexture; +import ru.windcorp.progressia.client.graphics.texture.Texture; +import ru.windcorp.progressia.client.graphics.texture.Atlases.AtlasGroup; +import ru.windcorp.progressia.client.graphics.world.WorldRenderProgram; +import ru.windcorp.progressia.client.world.entity.EntityRender; +import ru.windcorp.progressia.client.world.entity.EntityRenderable; +import ru.windcorp.progressia.common.resource.ResourceManager; +import ru.windcorp.progressia.common.world.entity.EntityData; + +/** + * Renderer for Test:FallingBlock + * @author opfromthestart + * + */ +public class TestEntityRenderFallingBlock extends EntityRender { + private Renderable cube; + + public TestEntityRenderFallingBlock(String id) { + super(id); + cube = new Shapes.PppBuilder( + WorldRenderProgram.getDefault(), + new SimpleTexture( + Atlases.getSprite( + ResourceManager.getTextureResource("blocks/Sand"), new AtlasGroup("Blocks", 1 << 12) + ) + ) + ).create(); + } + + public void setTexture(Texture texture) { // There has to be a better way. + cube = new Shapes.PppBuilder( + WorldRenderProgram.getDefault(), + texture + ) + .create(); + } + + @Override + public EntityRenderable createRenderable(EntityData entity) { + return new EntityRenderable(entity) { + @Override + public void render(ShapeRenderHelper renderer) { + //LogManager.getLogger().info("Rendering FallingBlock"); + cube.render(renderer); + } + }; + } + +} diff --git a/src/main/java/ru/windcorp/progressia/test/gen/TestWorldGenerator.java b/src/main/java/ru/windcorp/progressia/test/gen/TestWorldGenerator.java index 1b35ffc..5898fe3 100644 --- a/src/main/java/ru/windcorp/progressia/test/gen/TestWorldGenerator.java +++ b/src/main/java/ru/windcorp/progressia/test/gen/TestWorldGenerator.java @@ -15,7 +15,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - + package ru.windcorp.progressia.test.gen; import java.io.DataInputStream; @@ -23,10 +23,14 @@ import java.io.DataOutputStream; import java.io.IOException; import java.util.Random; +import org.apache.logging.log4j.LogManager; + +import glm.vec._3.Vec3; import glm.vec._3.i.Vec3i; import ru.windcorp.progressia.common.util.VectorUtil; import ru.windcorp.progressia.common.util.Vectors; import ru.windcorp.progressia.common.world.ChunkData; +import ru.windcorp.progressia.common.world.ChunkDataListener; import ru.windcorp.progressia.common.world.Coordinates; import ru.windcorp.progressia.common.world.DecodingException; import ru.windcorp.progressia.common.world.WorldData; @@ -38,6 +42,7 @@ import ru.windcorp.progressia.common.world.tile.TileData; import ru.windcorp.progressia.common.world.tile.TileDataRegistry; import ru.windcorp.progressia.server.world.WorldLogic; import ru.windcorp.progressia.server.world.generation.AbstractWorldGenerator; +import ru.windcorp.progressia.test.TestEntityDataFallingBlock; public class TestWorldGenerator extends AbstractWorldGenerator { @@ -51,6 +56,41 @@ public class TestWorldGenerator extends AbstractWorldGenerator { @Override public void onChunkLoaded(WorldData world, ChunkData chunk) { findAndPopulate(chunk.getPosition(), world); + chunk.addListener(new ChunkDataListener() { + @Override + public void onChunkBlockChanged(ChunkData chunk, Vec3i blockInChunk, BlockData previous, + BlockData current) { + if (current.getId() != "Test:Sand") { // Replace with + // proper check + // for all + // gravity + // blocks + return; + } + if (chunk.getBlock(blockInChunk.add_(0, 0, -1)).getId() == "Test:Air")// TODO + // Won't + // work + // on + // z + // chunk + // boundaries + { + LogManager.getLogger().info("Inserting FallingBlock"); + + TestEntityDataFallingBlock fallingBlock = new TestEntityDataFallingBlock(); + + Vec3i worldPos = chunk.getPosition().mul_(16).add_(blockInChunk); + Vec3 floatWorldPos = new Vec3(worldPos.x, worldPos.y, worldPos.z); + fallingBlock.setPosition(floatWorldPos); + + fallingBlock.setEntityId(("Test:FallingBlock" + floatWorldPos.toString() + + String.valueOf(new Random().nextFloat())).hashCode()); + + chunk.getWorld().addEntity(fallingBlock); + chunk.setBlock(blockInChunk, BlockDataRegistry.getInstance().get("Test:Air"), true); + } + } + }); } }); } @@ -88,12 +128,10 @@ public class TestWorldGenerator extends AbstractWorldGenerator { BlockData stone = BlockDataRegistry.getInstance().get("Test:Stone"); BlockData air = BlockDataRegistry.getInstance().get("Test:Air"); - BlockData[] granites = new BlockData[] { - BlockDataRegistry.getInstance().get("Test:GraniteGravel"), - BlockDataRegistry.getInstance().get("Test:GraniteGravel"), - BlockDataRegistry.getInstance().get("Test:GraniteCracked"), - BlockDataRegistry.getInstance().get("Test:GraniteMonolith") - }; + BlockData[] granites = new BlockData[] { BlockDataRegistry.getInstance().get("Test:GraniteGravel"), + BlockDataRegistry.getInstance().get("Test:GraniteGravel"), + BlockDataRegistry.getInstance().get("Test:GraniteCracked"), + BlockDataRegistry.getInstance().get("Test:GraniteMonolith") }; double[][] heightMap = new double[bpc][bpc]; double[][] gradMap = new double[bpc][bpc]; @@ -168,7 +206,7 @@ public class TestWorldGenerator extends AbstractWorldGenerator { ChunkData chunk = world.getChunk(chunkPos); assert chunk != null : "Something went wrong when populating chunk at (" + chunkPos.x + "; " + chunkPos.y + "; " - + chunkPos.z + ")"; + + chunkPos.z + ")"; BlockData air = BlockDataRegistry.getInstance().get("Test:Air"); BlockData dirt = BlockDataRegistry.getInstance().get("Test:Dirt"); @@ -203,15 +241,8 @@ public class TestWorldGenerator extends AbstractWorldGenerator { int xic = Coordinates.convertInWorldToInChunk(biw.x); int yic = Coordinates.convertInWorldToInChunk(biw.y); - addTiles( - chunk, - biw, - world, - random, - world.getBlock(biw) == dirt, - heightMap[xic][yic], - gradMap[xic][yic] - ); + addTiles(chunk, biw, world, random, world.getBlock(biw) == dirt, heightMap[xic][yic], + gradMap[xic][yic]); } } @@ -219,15 +250,8 @@ public class TestWorldGenerator extends AbstractWorldGenerator { chunk.setGenerationHint(true); } - private void addTiles( - ChunkData chunk, - Vec3i biw, - WorldData world, - Random random, - boolean isDirt, - double height, - double grad - ) { + private void addTiles(ChunkData chunk, Vec3i biw, WorldData world, Random random, boolean isDirt, double height, + double grad) { if (isDirt) addGrass(chunk, biw, world, random); addDecor(chunk, biw, world, random, isDirt); @@ -257,40 +281,26 @@ public class TestWorldGenerator extends AbstractWorldGenerator { private void addDecor(ChunkData chunk, Vec3i biw, WorldData world, Random random, boolean isDirt) { if (isDirt) { if (random.nextInt(8) == 0) { - world.getTiles(biw, BlockFace.TOP).addFarthest( - TileDataRegistry.getInstance().get("Test:Sand") - ); + world.getTiles(biw, BlockFace.TOP).addFarthest(TileDataRegistry.getInstance().get("Test:Sand")); } if (random.nextInt(8) == 0) { - world.getTiles(biw, BlockFace.TOP).addFarthest( - TileDataRegistry.getInstance().get("Test:Stones") - ); + world.getTiles(biw, BlockFace.TOP).addFarthest(TileDataRegistry.getInstance().get("Test:Stones")); } if (random.nextInt(8) == 0) { - world.getTiles(biw, BlockFace.TOP).addFarthest( - TileDataRegistry.getInstance().get("Test:YellowFlowers") - ); + world.getTiles(biw, BlockFace.TOP) + .addFarthest(TileDataRegistry.getInstance().get("Test:YellowFlowers")); } } else { if (random.nextInt(2) == 0) { - world.getTiles(biw, BlockFace.TOP).addFarthest( - TileDataRegistry.getInstance().get("Test:Stones") - ); + world.getTiles(biw, BlockFace.TOP).addFarthest(TileDataRegistry.getInstance().get("Test:Stones")); } } } - private void addSnow( - ChunkData chunk, - Vec3i biw, - WorldData world, - Random random, - boolean isDirt, - double height, - double grad - ) { + private void addSnow(ChunkData chunk, Vec3i biw, WorldData world, Random random, boolean isDirt, double height, + double grad) { if (height < 1500) return;