Added conversion from world coordinates to utilities.
Respective tests also added.
This commit is contained in:
parent
16618ef439
commit
4f411c42fb
@ -4,7 +4,11 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import ca.recrown.islandsurvivalcraft.Types.Point2;
|
||||
|
||||
public class Utilities {
|
||||
public final static int CHUNK_SIZE = 16;
|
||||
|
||||
public static <K, V> HashMap<V, ArrayList<K>> invertHashMap(HashMap<K, V> hashMap) {
|
||||
HashMap<V, ArrayList<K>> res = new HashMap<>();
|
||||
for (Entry<K, V> entry : hashMap.entrySet()) {
|
||||
@ -25,4 +29,44 @@ public class Utilities {
|
||||
int res = Math.abs(value) + add;
|
||||
return negative ? - res : res;
|
||||
}
|
||||
|
||||
public static Point2 worldToChunkCoordinates(int x, int y) {
|
||||
int xRes = 0;
|
||||
if (x < 0) {
|
||||
xRes = (int) Math.floor((float) x / CHUNK_SIZE);
|
||||
} else {
|
||||
xRes = x / CHUNK_SIZE;
|
||||
}
|
||||
int yRes = 0;
|
||||
if (x < 0) {
|
||||
yRes = (int) Math.floor((float) y / CHUNK_SIZE);
|
||||
} else {
|
||||
yRes = y / CHUNK_SIZE;
|
||||
}
|
||||
return new Point2(xRes, yRes);
|
||||
}
|
||||
|
||||
public static Point2 worldToChunkCoordinates(Point2 worldCoordinates) {
|
||||
return worldToChunkCoordinates(worldCoordinates.x, worldCoordinates.y);
|
||||
}
|
||||
|
||||
public static Point2 worldToLocalChunkCoordinates(int x, int y) {
|
||||
int xRes = 0;
|
||||
if (x < 0) {
|
||||
xRes = CHUNK_SIZE + (x % CHUNK_SIZE);
|
||||
} else {
|
||||
xRes = x % CHUNK_SIZE;
|
||||
}
|
||||
int yRes = 0;
|
||||
if (y < 0) {
|
||||
yRes = CHUNK_SIZE + (y % CHUNK_SIZE);
|
||||
} else {
|
||||
yRes = y % CHUNK_SIZE;
|
||||
}
|
||||
return new Point2(xRes, yRes);
|
||||
}
|
||||
|
||||
public static Point2 worldToLocalChunkCoordinates(Point2 worldCoordinates) {
|
||||
return worldToChunkCoordinates(worldCoordinates.x, worldCoordinates.y);
|
||||
}
|
||||
}
|
@ -9,6 +9,8 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||
|
||||
import ca.recrown.islandsurvivalcraft.Types.Point2;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
@ -48,4 +50,34 @@ public class UtilitiesTest {
|
||||
public void testMagnitudeAddNegative() {
|
||||
assertEquals(-2, Utilities.addMagnitude(-1, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWorldToChunkCoordinatesNegativePerfect() {
|
||||
assertEquals(new Point2(-4, -3), Utilities.worldToChunkCoordinates(-64, -48));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWorldToChunkCoordinatesNegativeOff() {
|
||||
assertEquals(new Point2(-4, -3), Utilities.worldToChunkCoordinates(-55, -33));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWorldToChunkCoordinatesPositivePerfect() {
|
||||
assertEquals(new Point2(4, 5), Utilities.worldToChunkCoordinates(64, 80));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWorldToChunkCoordinatesPositiveOff() {
|
||||
assertEquals(new Point2(4, 5), Utilities.worldToChunkCoordinates(67, 84));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWorldToLocalCoordinatesNegative() {
|
||||
assertEquals(new Point2(2, 5), Utilities.worldToLocalChunkCoordinates(-62, -27));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWorldToLocalCoordinatesPositive() {
|
||||
assertEquals(new Point2(7, 3), Utilities.worldToLocalChunkCoordinates(39, 83));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user