diff --git a/src/main/java/com/github/halotroop/litecraft/types/block/Block.java b/src/main/java/com/github/halotroop/litecraft/types/block/Block.java index c7c9041..04208e5 100644 --- a/src/main/java/com/github/halotroop/litecraft/types/block/Block.java +++ b/src/main/java/com/github/halotroop/litecraft/types/block/Block.java @@ -19,14 +19,15 @@ public class Block public final TexturedModel model; public final boolean visible; + public static final Block AIR = new Block((TexturedModel) null, new Properties().visible(false)); 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; + public Properties visible(boolean visible) { + this.visible = visible; return this; } } diff --git a/src/main/java/com/github/halotroop/litecraft/world/Chunk.java b/src/main/java/com/github/halotroop/litecraft/world/Chunk.java index 3c33b55..d5c42e3 100644 --- a/src/main/java/com/github/halotroop/litecraft/world/Chunk.java +++ b/src/main/java/com/github/halotroop/litecraft/world/Chunk.java @@ -3,25 +3,33 @@ package com.github.halotroop.litecraft.world; import java.util.ArrayList; import java.util.List; -import com.github.halotroop.litecraft.types.block.*; +import com.github.halotroop.litecraft.types.block.Block; +import com.github.halotroop.litecraft.types.block.BlockEntity; import com.github.hydos.ginger.engine.math.vectors.Vector3f; import it.unimi.dsi.fastutil.longs.Long2ObjectArrayMap; import it.unimi.dsi.fastutil.longs.Long2ObjectMap; -public class Chunk +public class Chunk implements TileAccess { public Chunk(int chunkX, int chunkY, int chunkZ) { this.chunkX = chunkX; + this.chunkY = chunkY; + this.chunkZ = chunkZ; + this.chunkStartX = chunkX << 3; + this.chunkStartY = chunkY << 3; + this.chunkStartZ = chunkZ << 3; } private final Long2ObjectMap blocks = new Long2ObjectArrayMap<>(); private final List blockEntities = new ArrayList<>(); private boolean render = false; - - public final int chunkX; + public final int chunkX, chunkY, chunkZ; + private final int chunkStartX, chunkStartY, chunkStartZ; + + @Override public void setBlock(int x, int y, int z, Block block) { if (x > 7) x = 7; @@ -33,21 +41,39 @@ public class Chunk long hash = posHash(x, y, z); this.blocks.put(hash, block); + + if (this.render) { + this.blockEntities.add(new BlockEntity(block, new Vector3f(this.chunkStartX + x, this.chunkStartY + y, this.chunkStartZ + z))); + } } + @Override 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 Vector3f(0,0,0))); - }); + for (int x = 0; x < 8; ++x) + { + for (int y = 0; y < 8; ++y) + { + for (int z = 0; z < 8; ++z) + { + long hash = posHash(x, y, z); + Block block = this.blocks.get(hash); + if (block.visible) this.blockEntities.add(new BlockEntity(block, + new Vector3f( + this.chunkStartX + x, + this.chunkStartY + y, + this.chunkStartZ + z))); + } + } + } } else if (this.render) // else if it has been changed to false { @@ -62,6 +88,14 @@ public class Chunk this.render = render; } + public void render() + { + if (this.render) { + // TODO @hydos pls do this + // TODO @hydos culling good + } + } + public boolean doRender() { return this.render; } diff --git a/src/main/java/com/github/halotroop/litecraft/world/TileAccess.java b/src/main/java/com/github/halotroop/litecraft/world/TileAccess.java new file mode 100644 index 0000000..864292e --- /dev/null +++ b/src/main/java/com/github/halotroop/litecraft/world/TileAccess.java @@ -0,0 +1,9 @@ +package com.github.halotroop.litecraft.world; + +import com.github.halotroop.litecraft.types.block.Block; + +public interface TileAccess +{ + Block getBlock(int x, int y, int z); + void setBlock(int x, int y, int z, Block block); +}