Fixed init of grounds and fixed tests.

This commit is contained in:
Ziver Koc 2020-02-16 00:31:58 +01:00
parent 3b9422346a
commit f8353d4354
14 changed files with 178 additions and 96 deletions

View file

@ -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"

View file

@ -1,5 +1,6 @@
{
"1": {
"name": "Grass"
"0": {
"name": "Grass",
"texture": "texture/ground/grass.png"
}
}

View file

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

View file

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

View file

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

View file

@ -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<Integer, GroundType> 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<String> 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);
}
}

View file

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

View file

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

View file

@ -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() {
}
}

View file

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