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.
This commit is contained in:
opfromthestart 2021-05-04 20:29:06 -04:00
parent 531a8c99c3
commit b374e9a736
5 changed files with 197 additions and 47 deletions

View File

@ -245,6 +245,10 @@ public class TestContent {
register("Test:Statie", TestEntityDataStatie::new); register("Test:Statie", TestEntityDataStatie::new);
register(new TestEntityRenderStatie("Test:Statie")); register(new TestEntityRenderStatie("Test:Statie"));
register(new TestEntityLogicStatie("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() { private static void regsiterControls() {

View File

@ -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;
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
};
}
}

View File

@ -23,10 +23,14 @@ import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Random; import java.util.Random;
import org.apache.logging.log4j.LogManager;
import glm.vec._3.Vec3;
import glm.vec._3.i.Vec3i; import glm.vec._3.i.Vec3i;
import ru.windcorp.progressia.common.util.VectorUtil; import ru.windcorp.progressia.common.util.VectorUtil;
import ru.windcorp.progressia.common.util.Vectors; import ru.windcorp.progressia.common.util.Vectors;
import ru.windcorp.progressia.common.world.ChunkData; 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.Coordinates;
import ru.windcorp.progressia.common.world.DecodingException; import ru.windcorp.progressia.common.world.DecodingException;
import ru.windcorp.progressia.common.world.WorldData; 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.common.world.tile.TileDataRegistry;
import ru.windcorp.progressia.server.world.WorldLogic; import ru.windcorp.progressia.server.world.WorldLogic;
import ru.windcorp.progressia.server.world.generation.AbstractWorldGenerator; import ru.windcorp.progressia.server.world.generation.AbstractWorldGenerator;
import ru.windcorp.progressia.test.TestEntityDataFallingBlock;
public class TestWorldGenerator extends AbstractWorldGenerator<Boolean> { public class TestWorldGenerator extends AbstractWorldGenerator<Boolean> {
@ -51,6 +56,41 @@ public class TestWorldGenerator extends AbstractWorldGenerator<Boolean> {
@Override @Override
public void onChunkLoaded(WorldData world, ChunkData chunk) { public void onChunkLoaded(WorldData world, ChunkData chunk) {
findAndPopulate(chunk.getPosition(), world); 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<Boolean> {
BlockData stone = BlockDataRegistry.getInstance().get("Test:Stone"); BlockData stone = BlockDataRegistry.getInstance().get("Test:Stone");
BlockData air = BlockDataRegistry.getInstance().get("Test:Air"); BlockData air = BlockDataRegistry.getInstance().get("Test:Air");
BlockData[] granites = new BlockData[] { BlockData[] granites = new BlockData[] { BlockDataRegistry.getInstance().get("Test:GraniteGravel"),
BlockDataRegistry.getInstance().get("Test:GraniteGravel"),
BlockDataRegistry.getInstance().get("Test:GraniteGravel"), BlockDataRegistry.getInstance().get("Test:GraniteGravel"),
BlockDataRegistry.getInstance().get("Test:GraniteCracked"), BlockDataRegistry.getInstance().get("Test:GraniteCracked"),
BlockDataRegistry.getInstance().get("Test:GraniteMonolith") BlockDataRegistry.getInstance().get("Test:GraniteMonolith") };
};
double[][] heightMap = new double[bpc][bpc]; double[][] heightMap = new double[bpc][bpc];
double[][] gradMap = new double[bpc][bpc]; double[][] gradMap = new double[bpc][bpc];
@ -203,15 +241,8 @@ public class TestWorldGenerator extends AbstractWorldGenerator<Boolean> {
int xic = Coordinates.convertInWorldToInChunk(biw.x); int xic = Coordinates.convertInWorldToInChunk(biw.x);
int yic = Coordinates.convertInWorldToInChunk(biw.y); int yic = Coordinates.convertInWorldToInChunk(biw.y);
addTiles( addTiles(chunk, biw, world, random, world.getBlock(biw) == dirt, heightMap[xic][yic],
chunk, gradMap[xic][yic]);
biw,
world,
random,
world.getBlock(biw) == dirt,
heightMap[xic][yic],
gradMap[xic][yic]
);
} }
} }
@ -219,15 +250,8 @@ public class TestWorldGenerator extends AbstractWorldGenerator<Boolean> {
chunk.setGenerationHint(true); chunk.setGenerationHint(true);
} }
private void addTiles( private void addTiles(ChunkData chunk, Vec3i biw, WorldData world, Random random, boolean isDirt, double height,
ChunkData chunk, double grad) {
Vec3i biw,
WorldData world,
Random random,
boolean isDirt,
double height,
double grad
) {
if (isDirt) if (isDirt)
addGrass(chunk, biw, world, random); addGrass(chunk, biw, world, random);
addDecor(chunk, biw, world, random, isDirt); addDecor(chunk, biw, world, random, isDirt);
@ -257,40 +281,26 @@ public class TestWorldGenerator extends AbstractWorldGenerator<Boolean> {
private void addDecor(ChunkData chunk, Vec3i biw, WorldData world, Random random, boolean isDirt) { private void addDecor(ChunkData chunk, Vec3i biw, WorldData world, Random random, boolean isDirt) {
if (isDirt) { if (isDirt) {
if (random.nextInt(8) == 0) { if (random.nextInt(8) == 0) {
world.getTiles(biw, BlockFace.TOP).addFarthest( world.getTiles(biw, BlockFace.TOP).addFarthest(TileDataRegistry.getInstance().get("Test:Sand"));
TileDataRegistry.getInstance().get("Test:Sand")
);
} }
if (random.nextInt(8) == 0) { if (random.nextInt(8) == 0) {
world.getTiles(biw, BlockFace.TOP).addFarthest( world.getTiles(biw, BlockFace.TOP).addFarthest(TileDataRegistry.getInstance().get("Test:Stones"));
TileDataRegistry.getInstance().get("Test:Stones")
);
} }
if (random.nextInt(8) == 0) { if (random.nextInt(8) == 0) {
world.getTiles(biw, BlockFace.TOP).addFarthest( world.getTiles(biw, BlockFace.TOP)
TileDataRegistry.getInstance().get("Test:YellowFlowers") .addFarthest(TileDataRegistry.getInstance().get("Test:YellowFlowers"));
);
} }
} else { } else {
if (random.nextInt(2) == 0) { if (random.nextInt(2) == 0) {
world.getTiles(biw, BlockFace.TOP).addFarthest( world.getTiles(biw, BlockFace.TOP).addFarthest(TileDataRegistry.getInstance().get("Test:Stones"));
TileDataRegistry.getInstance().get("Test:Stones")
);
} }
} }
} }
private void addSnow( private void addSnow(ChunkData chunk, Vec3i biw, WorldData world, Random random, boolean isDirt, double height,
ChunkData chunk, double grad) {
Vec3i biw,
WorldData world,
Random random,
boolean isDirt,
double height,
double grad
) {
if (height < 1500) if (height < 1500)
return; return;