From f8353d435438566e222dc8b4c9f4a213aa271d4d Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Sun, 16 Feb 2020 00:31:58 +0100 Subject: [PATCH] Fixed init of grounds and fixed tests. --- .idea/misc.xml | 7 +++ .idea/vcs.xml | 1 + android/android.iml | 23 -------- core/build.gradle | 2 +- core/resources/data/ground_types.json | 5 +- core/src/se/cookery/world/Block.java | 39 +++++++++---- core/src/se/cookery/world/GroundType.java | 17 ++++-- .../se/cookery/world/GroundTypeFactory.java | 28 --------- .../se/cookery/world/GroundTypeManager.java | 58 +++++++++++++++++++ core/src/se/cookery/world/World.java | 11 +++- .../world/gen/GrassLandWorldGenerator.java | 11 +++- core/test/se/cookery/world/BlockTest.java | 37 ++++++++++++ ...ryTest.java => GroundTypeManagerTest.java} | 17 ++++-- ios/ios.iml | 18 ------ 14 files changed, 178 insertions(+), 96 deletions(-) delete mode 100644 android/android.iml delete mode 100644 core/src/se/cookery/world/GroundTypeFactory.java create mode 100644 core/src/se/cookery/world/GroundTypeManager.java create mode 100644 core/test/se/cookery/world/BlockTest.java rename core/test/se/cookery/world/{GroundTypeFactoryTest.java => GroundTypeManagerTest.java} (53%) delete mode 100644 ios/ios.iml diff --git a/.idea/misc.xml b/.idea/misc.xml index 25d34a4..140105b 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,12 @@ + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 94a25f7..bf3be2a 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -2,5 +2,6 @@ + \ No newline at end of file diff --git a/android/android.iml b/android/android.iml deleted file mode 100644 index 217c060..0000000 --- a/android/android.iml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/core/build.gradle b/core/build.gradle index c5099a0..6320837 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -16,7 +16,7 @@ sourceSets { dependencies { api "com.badlogicgames.gdx:gdx:$gdxVersion" - implementation 'se.koc:zutil:1.0.236' + implementation 'se.koc:zutil:1.0.0-SNAPSHOT' testImplementation "org.junit.jupiter:junit-jupiter-engine:5.5.2" testImplementation "org.junit.platform:junit-platform-runner:1.5.2" diff --git a/core/resources/data/ground_types.json b/core/resources/data/ground_types.json index 25ec708..46280a3 100644 --- a/core/resources/data/ground_types.json +++ b/core/resources/data/ground_types.json @@ -1,5 +1,6 @@ { - "1": { - "name": "Grass" + "0": { + "name": "Grass", + "texture": "texture/ground/grass.png" } } \ No newline at end of file diff --git a/core/src/se/cookery/world/Block.java b/core/src/se/cookery/world/Block.java index 8911b28..cf4df43 100644 --- a/core/src/se/cookery/world/Block.java +++ b/core/src/se/cookery/world/Block.java @@ -17,31 +17,50 @@ public class Block { private long offset_x = 0; private long offset_y = 0; - private byte[][] height = new byte[BLOCK_SIZE][BLOCK_SIZE]; - private byte[][] landType = new byte[BLOCK_SIZE][BLOCK_SIZE]; + private int[][] height = new int[BLOCK_SIZE][BLOCK_SIZE]; + private int[][] groundType = new int[BLOCK_SIZE][BLOCK_SIZE]; + public Block(long x, long y) { + offset_x = x; + offset_y = y; + } public long getBlockStartX(){ return offset_x; } - public long getBlockStartY(){ return offset_y; } - public long getBlockEndX(){ - return offset_x + BLOCK_SIZE; + return offset_x + BLOCK_SIZE - 1; } - public long getBlockEndY(){ - return offset_y + BLOCK_SIZE; + return offset_y + BLOCK_SIZE - 1; } + protected int getLocalX(long world_x) { + return (int) Math.abs(world_x-offset_x); + } + protected int getLocalY(long world_y) { + return (int) Math.abs(world_y-offset_y); + } + + + public void setHeight(long x, long y, int height) { + this.height[getLocalX(x)][getLocalY(y)] = height; + } public int getHeight(long x, long y) { - return height[(int) Math.abs(x-offset_x)][(int) Math.abs(y-offset_y)]; + return height[getLocalX(x)][getLocalY(y)]; } - public int getGround(long x, long y) { - return landType[(int) Math.abs(x-offset_x)][(int) Math.abs(y-offset_y)]; + + public void setGroundType(long x, long y, GroundType groundType) { + setGroundType(x, y, groundType.getId()); + } + public void setGroundType(long x, long y, int groundTypeId) { + this.groundType[getLocalX(x)][getLocalY(y)] = groundTypeId; + } + public int getGroundType(long x, long y) { + return groundType[getLocalX(x)][getLocalY(y)]; } } diff --git a/core/src/se/cookery/world/GroundType.java b/core/src/se/cookery/world/GroundType.java index 32069eb..b0896f5 100644 --- a/core/src/se/cookery/world/GroundType.java +++ b/core/src/se/cookery/world/GroundType.java @@ -3,16 +3,23 @@ package se.cookery.world; /** * Class representing a ground material type. */ -public class GroundType { +public final class GroundType { // Variables - private int id; - private String name; + private final int id; + private final String name; - protected GroundType() {} + protected GroundType(int id, String name) { + this.id = id; + this.name = name; + } + + public int getId() { + return id; + } public String getName() { - return null; + return name; } } diff --git a/core/src/se/cookery/world/GroundTypeFactory.java b/core/src/se/cookery/world/GroundTypeFactory.java deleted file mode 100644 index e88bcd6..0000000 --- a/core/src/se/cookery/world/GroundTypeFactory.java +++ /dev/null @@ -1,28 +0,0 @@ -package se.cookery.world; - -import com.badlogic.gdx.utils.JsonReader; - -import java.security.InvalidParameterException; -import java.util.HashMap; -import java.util.Map; - -public class GroundTypeFactory { - // Constants - - public static final String RESOURCE_GROUND_TYPE = "data/ground_types.json"; - - // Variables - - public static Map groundTypes = new HashMap<>(); - - - public static void initialize() { - - } - - public static GroundType get(int id) { - if (0 > id || id > groundTypes.size()) - throw new InvalidParameterException("Provided id " + id + " is not associated with a ground type."); - return groundTypes.get(id); - } -} diff --git a/core/src/se/cookery/world/GroundTypeManager.java b/core/src/se/cookery/world/GroundTypeManager.java new file mode 100644 index 0000000..02bb8aa --- /dev/null +++ b/core/src/se/cookery/world/GroundTypeManager.java @@ -0,0 +1,58 @@ +package se.cookery.world; + +import com.badlogic.gdx.utils.JsonReader; +import zutil.io.file.FileUtil; +import zutil.log.LogUtil; +import zutil.parser.DataNode; +import zutil.parser.json.JSONParser; + +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.security.InvalidParameterException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.logging.Logger; + +public class GroundTypeManager { + public static final Logger logger = LogUtil.getLogger(); + + // Constants + + public static final String RESOURCE_GROUND_TYPE = "data/ground_types.json"; + + // Variables + + public static Map groundTypes = new HashMap<>(); + + + public static void initialize() throws IOException { + FileReader reader = new FileReader(FileUtil.find(RESOURCE_GROUND_TYPE)); + JSONParser parser = new JSONParser(reader); + DataNode json = parser.read(); + reader.close(); + + + for (Iterator keys=json.keyIterator(); keys.hasNext();) { + int id = Integer.parseInt(keys.next()); + DataNode groundJson = json.get(id); + + registerGroundType(new GroundType( + id, + groundJson.getString("name") + )); + } + } + + protected static void registerGroundType(GroundType groundType) { + logger.finest("Registering new GroundType: id=" + groundType.getId() + ", name=" + groundType.getName()); + groundTypes.put(groundType.getId(), groundType); + } + + public static GroundType get(int id) { + if (0 > id || id > groundTypes.size()) + throw new InvalidParameterException("Provided id " + id + " is not associated with a ground type."); + return groundTypes.get(id); + } +} diff --git a/core/src/se/cookery/world/World.java b/core/src/se/cookery/world/World.java index 0143ac7..cc417c2 100644 --- a/core/src/se/cookery/world/World.java +++ b/core/src/se/cookery/world/World.java @@ -2,6 +2,7 @@ package se.cookery.world; import se.cookery.world.gen.WorldGenerator; +import java.util.HashMap; import java.util.Map; import java.util.TreeMap; @@ -28,8 +29,12 @@ public class World { * not exist then a new block will be generated for the requested coordinate */ public Block getBlock(long x, long y) { - if (!blockExists(x, y)) - generator.generateBlock(x, y); - return null; + if (!blockExists(x, y)) { + if (!worldBlocks.containsKey(x)) + worldBlocks.put(x, new HashMap<>()); + + worldBlocks.get(x).put(y, generator.generateBlock(x, y)); + } + return worldBlocks.get(x).get(y); } } diff --git a/core/src/se/cookery/world/gen/GrassLandWorldGenerator.java b/core/src/se/cookery/world/gen/GrassLandWorldGenerator.java index a6275c4..2e888db 100644 --- a/core/src/se/cookery/world/gen/GrassLandWorldGenerator.java +++ b/core/src/se/cookery/world/gen/GrassLandWorldGenerator.java @@ -6,6 +6,15 @@ public class GrassLandWorldGenerator implements WorldGenerator { @Override public Block generateBlock(long x, long y) { - return null; + Block b = new Block(x, y); + + for (long i = x; i < x+Block.BLOCK_SIZE; i++) { + for (long j = y; j < y+Block.BLOCK_SIZE; j++) { + b.setGroundType(i, j, 0); + b.setHeight(i, j, 0); + } + } + + return b; } } diff --git a/core/test/se/cookery/world/BlockTest.java b/core/test/se/cookery/world/BlockTest.java new file mode 100644 index 0000000..15ad3cc --- /dev/null +++ b/core/test/se/cookery/world/BlockTest.java @@ -0,0 +1,37 @@ +package se.cookery.world; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class BlockTest { + + @Test + void getBlockStartX() { + Block block = new Block(0, 0); + assertEquals(0, block.getBlockStartX()); + assertEquals(0, block.getBlockStartY()); + assertEquals(19, block.getBlockEndX()); + assertEquals(19, block.getBlockEndY()); + } + + @Test + void getBlockStartY() { + } + + @Test + void getLocalX() { + } + + @Test + void getLocalY() { + } + + @Test + void getHeight() { + } + + @Test + void getGroundType() { + } +} \ No newline at end of file diff --git a/core/test/se/cookery/world/GroundTypeFactoryTest.java b/core/test/se/cookery/world/GroundTypeManagerTest.java similarity index 53% rename from core/test/se/cookery/world/GroundTypeFactoryTest.java rename to core/test/se/cookery/world/GroundTypeManagerTest.java index 57358a6..4816eb0 100644 --- a/core/test/se/cookery/world/GroundTypeFactoryTest.java +++ b/core/test/se/cookery/world/GroundTypeManagerTest.java @@ -1,29 +1,36 @@ package se.cookery.world; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import java.io.IOException; import java.security.InvalidParameterException; import static org.junit.jupiter.api.Assertions.*; -class GroundTypeFactoryTest { +class GroundTypeManagerTest { + + @BeforeAll + public static void initialize() throws IOException { + GroundTypeManager.initialize(); + } @Test void invalidIDTest() { assertThrows(InvalidParameterException.class, () -> { - GroundTypeFactory.get(-1); + GroundTypeManager.get(-1); }); assertThrows(InvalidParameterException.class, () -> { - GroundTypeFactory.get(500); + GroundTypeManager.get(500); }); } @Test void get() { - GroundType ground = GroundTypeFactory.get(0); + GroundType ground = GroundTypeManager.get(0); assertNotNull(ground); - assertSame(ground, GroundTypeFactory.get(0)); + assertSame(ground, GroundTypeManager.get(0)); assertEquals("Grass", ground.getName()); } } \ No newline at end of file diff --git a/ios/ios.iml b/ios/ios.iml deleted file mode 100644 index 1c9dfe8..0000000 --- a/ios/ios.iml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file