chunk stuff

pull/2/head
valoeghese 2020-02-24 19:09:14 +13:00
parent 804ae4ad45
commit 6f09c99252
2 changed files with 79 additions and 2 deletions

View File

@ -13,14 +13,21 @@ public class Block
protected Block(TexturedModel model, Properties properties)
{
this.model = model;
this.visible = properties.visible;
}
public final TexturedModel model;
public final boolean visible;
public static final Block GRASS = new Block("block/cubes/soil/gravel.png", new Properties());
public static final Block DIRT = new Block("block/cubes/soil/dirt.png", new Properties());
public static class Properties { // add properties to this builder!
private boolean visible = false;
public Properties visible(boolean invisible) {
this.visible = invisible;
return this;
}
}
}

View File

@ -1,5 +1,75 @@
package io.github.halotroop.litecraft.world;
public class Chunk {
import java.util.ArrayList;
import java.util.List;
import io.github.halotroop.litecraft.types.block.Block;
import io.github.halotroop.litecraft.types.block.BlockEntity;
import it.unimi.dsi.fastutil.longs.Long2ObjectArrayMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
public class Chunk
{
public Chunk(int chunkX, int chunkY, int chunkZ)
{
this.chunkX = chunkX;
}
private final Long2ObjectMap<Block> blocks = new Long2ObjectArrayMap<>();
private final List<BlockEntity> blockEntities = new ArrayList<>();
private boolean render = false;
public final int chunkX;
public void setBlock(int x, int y, int z, Block block)
{
if (x > 7) x = 7;
else if (x < 0) x = 0;
if (y > 7) y = 7;
else if (y < 0) y = 0;
if (z > 7) z = 7;
else if (z < 0) z = 0;
long hash = posHash(x, y, z);
this.blocks.put(hash, block);
}
public Block getBlock(int x, int y, int z)
{
long hash = posHash(x, y, z);
return this.blocks.get(hash);
}
public void setRender(boolean render)
{
if (render && !this.render) // if it has been changed to true
{
this.blocks.forEach((coord, block) -> {
if (block.visible) this.blockEntities.add(new BlockEntity(block, new))
});
}
else if (this.render) // else if it has been changed to false
{
int length = blockEntities.size();
for (int i = length; i >= 0; --i)
{
this.blockEntities.remove(i);
}
}
this.render = render;
}
public boolean doRender()
{ return this.render; }
/**
* @param x in-chunk x coordinate.
* @param y in-chunk y coordinate.
* @param z in-chunk z coordinate.
* @return creates a long that represents a coordinate, for use as a key in maps.
*/
private static long posHash(int x, int y, int z)
{ return ((long) x & 0b111) | (((long) y & 0b111) << 3) | (((long) z & 0b111) << 6); }
}