Switched to java 8, added ground factory

This commit is contained in:
Ziver Koc 2020-01-04 00:01:45 +01:00
parent a3503e3e5c
commit c51a473120
14 changed files with 154 additions and 25 deletions

View file

@ -1,6 +1,6 @@
apply plugin: "java"
sourceCompatibility = 1.7
sourceCompatibility = 1.8
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
sourceSets {

View file

@ -1,4 +1,43 @@
package se.cookery.world;
/**
* Class representing a block of the world.
*
* The class contains items, land type and world height information
*/
public class Block {
/**
* The size of a block in units of squares. The block is a square with size BLOCK_SIZE x BLOCK_SIZE.
**/
public static final int BLOCK_SIZE = 20;
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];
public long getBlockStartX(){
return offset_x;
}
public long getBlockStartY(){
return offset_y;
}
public long getBlockEndX(){
return offset_x + BLOCK_SIZE;
}
public long getBlockEndY(){
return offset_y + BLOCK_SIZE;
}
public int getHeight(long x, long y) {
return height[(int) Math.abs(x-offset_x)][(int) Math.abs(y-offset_y)];
}
public int getGround(long x, long y) {
return landType[(int) Math.abs(x-offset_x)][(int) Math.abs(y-offset_y)];
}
}

View file

@ -0,0 +1,8 @@
package se.cookery.world;
/**
* Class representing a ground material type.
*/
public class Ground {
private int id;
}

View file

@ -0,0 +1,18 @@
package se.cookery.world;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class GroundFactory {
public static Map<Integer, Ground> groundTypes = new HashMap<>();
public static Ground get(int id) {
if (0 > id || id > groundTypes.size())
throw new InvalidParameterException("Provided id " + id + " is not associated dwith a ground type.");
return groundTypes.get(id);
}
}

View file

@ -1,32 +1,21 @@
package se.cookery.world;
import se.cookery.world.gen.WorldGenerator;
import java.util.Map;
import java.util.TreeMap;
public class World {
// Constants
/**
* The size in a virtual coordinate system unit. The square is a square with size BLOCK_SIZE x BLOCK_SIZE.
**/
public static final int SQUARE_SIZE = 1;
/**
* The size of a block in units of squares. The block is a square with size BLOCK_SIZE x BLOCK_SIZE.
**/
public static final int BLOCK_SIZE = 20;
// Variables
private Map<Long, Map<Long, Block>> worldBlocks = new TreeMap<>();
private WorldGenerator generator;
public World() {
public World(WorldGenerator generator) {
this.generator = generator;
}
/**
* @return true if the block coordiante exists, otherwise false
* @return true if the block coordinate exists, otherwise false
*/
public boolean blockExists(long x, long y) {
return worldBlocks.get(x) != null && worldBlocks.get(x).get(y) != null;
@ -37,6 +26,8 @@ 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;
}
}

View file

@ -0,0 +1,11 @@
package se.cookery.world.gen;
import se.cookery.world.Block;
public class GrassLandWorldGenerator implements WorldGenerator {
@Override
public Block generateBlock(long x, long y) {
return null;
}
}

View file

@ -0,0 +1,19 @@
package se.cookery.world.gen;
import se.cookery.world.Block;
/**
* Interface representing a world generation class.
*
* A class that implements this interface can be used
* to generate a static or dynamic world.
*/
public interface WorldGenerator {
/**
* Method to generate a block with the provided coordinates,
* calling this method with the same parameters will always
* return a new Block object but with the same values.
*/
Block generateBlock(long x, long y);
}

View file

@ -0,0 +1,21 @@
package se.cookery.world;
import org.junit.jupiter.api.Test;
import java.security.InvalidParameterException;
import static org.junit.jupiter.api.Assertions.*;
class GroundFactoryTest {
@Test
void get() {
assertThrows(InvalidParameterException.class, () -> {
GroundFactory.get(-1);
});
assertThrows(InvalidParameterException.class, () -> {
GroundFactory.get(500);
});
}
}

View file

@ -1,18 +1,27 @@
package se.cookery.world;
import org.junit.jupiter.api.Test;
import se.cookery.world.gen.GrassLandWorldGenerator;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.*;
public class WorldTest {
@Test
public void blockExists() {
World w = new World();
assertNull(w.getBlock(0, 0));
World w = new World(new GrassLandWorldGenerator());
assertFalse(w.blockExists(0, 0));
assertFalse(w.blockExists(Long.MAX_VALUE, Long.MAX_VALUE));
assertFalse(w.blockExists(Long.MIN_VALUE, Long.MAX_VALUE));
assertFalse(w.blockExists(Long.MAX_VALUE, Long.MIN_VALUE));
assertFalse(w.blockExists(Long.MIN_VALUE, Long.MIN_VALUE));
}
@Test
public void getBlock() {
World w = new World(new GrassLandWorldGenerator());
assertFalse(w.blockExists(0, 0));
assertNotNull(w.getBlock(0,0));
assertTrue(w.blockExists(0, 0));
}
}

View file

@ -0,0 +1,13 @@
package se.cookery.world.gen;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class GrassLandWorldGeneratorTest {
@Test
void generateBlock() {
}
}