summaryrefslogtreecommitdiff
path: root/src/main/java/net/tylermurphy/Minecraft/Chunk/Cube.java
diff options
context:
space:
mode:
authorTyler Murphy <tylerm@tylerm.dev>2023-04-17 12:12:01 -0400
committerTyler Murphy <tylerm@tylerm.dev>2023-04-17 12:12:01 -0400
commit180aad05decc7eefa87e4e45d6747c48f40e5361 (patch)
tree51545197f7c94b4022acab880772c9f4fc65db0e /src/main/java/net/tylermurphy/Minecraft/Chunk/Cube.java
downloadminecraftjava-180aad05decc7eefa87e4e45d6747c48f40e5361.tar.gz
minecraftjava-180aad05decc7eefa87e4e45d6747c48f40e5361.tar.bz2
minecraftjava-180aad05decc7eefa87e4e45d6747c48f40e5361.zip
Diffstat (limited to 'src/main/java/net/tylermurphy/Minecraft/Chunk/Cube.java')
-rwxr-xr-xsrc/main/java/net/tylermurphy/Minecraft/Chunk/Cube.java156
1 files changed, 156 insertions, 0 deletions
diff --git a/src/main/java/net/tylermurphy/Minecraft/Chunk/Cube.java b/src/main/java/net/tylermurphy/Minecraft/Chunk/Cube.java
new file mode 100755
index 0000000..1b59393
--- /dev/null
+++ b/src/main/java/net/tylermurphy/Minecraft/Chunk/Cube.java
@@ -0,0 +1,156 @@
+package net.tylermurphy.Minecraft.Chunk;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.HashMap;
+import java.util.stream.Stream;
+
+import net.tylermurphy.Minecraft.Render.Data.Texture;
+import org.joml.Vector2f;
+import org.joml.Vector3f;
+
+import org.json.JSONObject;
+
+public class Cube{
+
+ public static final byte NULL = -2;
+ public static final byte AIR = -1;
+ public static int TEXTURE;
+
+ public int tex_px, tex_nx, tex_py, tex_ny, tex_pz, tex_nz;
+ public boolean transparent;
+ public boolean alwaysRenderNeighbors;
+ public boolean placeable;
+ public String name;
+ public long tick_update_delay;
+ public JSONObject positions;
+
+ public static HashMap<String,JSONObject> models = new HashMap<>();
+
+ public static HashMap<Integer, Texture> textures = new HashMap<>();
+ public static HashMap<String, Integer> texturePairings = new HashMap<>();
+
+ public static HashMap<Integer,Cube> blocks = new HashMap<>();
+ public static HashMap<String,Integer> blockPairings = new HashMap<>();
+
+ public static Cube getBlock(byte b){
+ return blocks.get((int) b);
+ }
+
+ public static Cube getBlock(String s){
+ return blocks.get(blockPairings.get(s));
+ }
+
+ private static JSONObject loadJsonFile(File file){
+ StringBuilder contentBuilder = new StringBuilder();
+ try (Stream<String> stream = Files.lines( Paths.get(file.getPath()), StandardCharsets.UTF_8)){
+ stream.forEach(s -> contentBuilder.append(s).append("\n"));
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return new JSONObject(contentBuilder.toString());
+ }
+
+ private static void loadModels(){
+ models = new HashMap<>();
+ File dir = new File("assets/models");
+ File[] directoryListing = dir.listFiles();
+ assert directoryListing != null;
+ for(File file : directoryListing){
+ JSONObject json = loadJsonFile(file);
+ models.put(file.getName().replaceFirst("[.][^.]+$", ""), json);
+ }
+ }
+
+ private static void loadTextures(){
+ textures = new HashMap<>();
+ texturePairings = new HashMap<>();
+ File dir = new File("assets/textures/blocks");
+ File[] directoryListing = dir.listFiles();
+ int i = 0;
+ assert directoryListing != null;
+ for(File file : directoryListing){
+ String name = file.getName().replaceFirst("[.][^.]+$", "");
+ textures.put(i, Texture.loadRawTextureData("blocks/"+name));
+ texturePairings.put(name, i);
+ i++;
+ }
+ TEXTURE = Texture.loadTexture2DArray(textures);
+ }
+
+ private static void loadBlocks(){
+ blocks = new HashMap<>();
+ blockPairings = new HashMap<>();
+ File file = new File("assets/blocks/blocks.json");
+ JSONObject json = loadJsonFile(file);
+ for(String key : json.keySet()){
+ String name = json.getString(key);
+ int id = Integer.parseInt(key);
+ blockPairings.put(name, id);
+ File block_file = new File("assets/blocks/" + name + ".json");
+ JSONObject block = loadJsonFile(block_file);
+ blocks.put(id, createCube(name, block));
+ }
+ }
+
+ private static Cube createCube(String name, JSONObject json){
+ Cube cube = new Cube();
+ cube.positions = models.get(json.getString("model"));
+ cube.transparent = json.getBoolean("transparent");
+ cube.alwaysRenderNeighbors = json.getBoolean("renderNeighbors");
+ cube.placeable = json.getBoolean("placeable");
+ cube.name = name;
+ cube.tick_update_delay = 20;
+ JSONObject textureList = json.getJSONObject("textures");
+ if(json.getString("texture").equals("all")){
+ cube.tex_px = texturePairings.get(textureList.getString("face"));
+ cube.tex_nx = texturePairings.get(textureList.getString("face"));
+ cube.tex_py = texturePairings.get(textureList.getString("face"));
+ cube.tex_ny = texturePairings.get(textureList.getString("face"));
+ cube.tex_pz = texturePairings.get(textureList.getString("face"));
+ cube.tex_nz = texturePairings.get(textureList.getString("face"));
+ } else if(json.getString("texture").equals("side")){
+ cube.tex_px = texturePairings.get(textureList.getString("side"));
+ cube.tex_nx = texturePairings.get(textureList.getString("side"));
+ cube.tex_py = texturePairings.get(textureList.getString("top"));
+ cube.tex_ny = texturePairings.get(textureList.getString("bottom"));
+ cube.tex_pz = texturePairings.get(textureList.getString("side"));
+ cube.tex_nz = texturePairings.get(textureList.getString("side"));
+ }
+ return cube;
+ }
+
+ public static void init() {
+
+ loadModels();
+ loadTextures();
+ loadBlocks();
+
+ }
+
+ public static Vector3f[] NORMALS = {
+
+ new Vector3f(0.f, 0.f, 0.f),
+ new Vector3f(0.f, 0.f, 0.f),
+ new Vector3f(0.f, 0.f, 0.f),
+ new Vector3f(0.f, 0.f, 0.f),
+ new Vector3f(0.f, 0.f, 0.f),
+ new Vector3f(0.f, 0.f, 0.f)
+
+ };
+
+ public static Vector2f[] TEXTURE_CORDS = {
+
+ new Vector2f(0.f, 0.f),
+ new Vector2f(0.f, 1.f),
+ new Vector2f(1.f, 1.f),
+ new Vector2f(1.f, 1.f),
+ new Vector2f(1.f, 0.f),
+ new Vector2f(0.f, 0.f)
+
+ };
+
+}