From 058d7e99443f00f956561795b3a22635e963a7e4 Mon Sep 17 00:00:00 2001 From: Collin Smith Date: Thu, 2 May 2019 21:23:38 -0700 Subject: [PATCH] Integrated EngineUtils into MapRenderer --- core/src/com/riiablo/map/MapRenderer.java | 30 +++++++++------------- core/src/com/riiablo/util/EngineUtils.java | 19 ++++++++++++++ 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/core/src/com/riiablo/map/MapRenderer.java b/core/src/com/riiablo/map/MapRenderer.java index 8bd90853..80af8ab3 100644 --- a/core/src/com/riiablo/map/MapRenderer.java +++ b/core/src/com/riiablo/map/MapRenderer.java @@ -21,6 +21,7 @@ import com.riiablo.entity.Object; import com.riiablo.graphics.BlendMode; import com.riiablo.graphics.PaletteIndexedBatch; import com.riiablo.map.DT1.Tile; +import com.riiablo.util.EngineUtils; import java.util.Arrays; import java.util.Comparator; @@ -194,9 +195,7 @@ public class MapRenderer { } public Vector2 project(float x, float y, Vector2 dst) { - dst.x = +(x * Tile.SUBTILE_WIDTH50) - (y * Tile.SUBTILE_WIDTH50); - dst.y = -(x * Tile.SUBTILE_HEIGHT50) - (y * Tile.SUBTILE_HEIGHT50); - return dst; + return EngineUtils.worldToScreenCoords(x, y, dst); } public Vector2 unproject(Vector2 dst) { @@ -306,11 +305,8 @@ public class MapRenderer { currentPos.set(pos); this.x = Map.round(pos.x);//(int) pos.x; this.y = Map.round(pos.y);//(int) pos.y; - spx = (x * Tile.SUBTILE_WIDTH50) - (y * Tile.SUBTILE_WIDTH50); - spy = -(x * Tile.SUBTILE_HEIGHT50) - (y * Tile.SUBTILE_HEIGHT50); - float spxf = (pos.x * Tile.SUBTILE_WIDTH50) - (pos.y * Tile.SUBTILE_WIDTH50); - float spyf = -(pos.x * Tile.SUBTILE_HEIGHT50) - (pos.y * Tile.SUBTILE_HEIGHT50); - camera.position.set(spxf, spyf, 0); + EngineUtils.worldToScreenCoords(pos, tmpVec2a); + camera.position.set(tmpVec2a, 0); camera.update(); // subtile index in tile-space @@ -323,8 +319,9 @@ public class MapRenderer { t = Tile.SUBTILE_INDEX[stx][sty]; // pixel offset of subtile in world-space - spx = -Tile.SUBTILE_WIDTH50 + (x * Tile.SUBTILE_WIDTH50) - (y * Tile.SUBTILE_WIDTH50); - spy = -Tile.SUBTILE_HEIGHT50 - (x * Tile.SUBTILE_HEIGHT50) - (y * Tile.SUBTILE_HEIGHT50); + EngineUtils.worldToScreenCoords(x, y, tmpVec2a).sub(Tile.SUBTILE_WIDTH50, Tile.SUBTILE_HEIGHT50); + spx = (int) tmpVec2a.x; + spy = (int) tmpVec2a.y; // tile index in world-space tx = x < 0 @@ -771,11 +768,10 @@ public class MapRenderer { if (DEBUG_MOUSE) { coords(tmpVec2i); - int mx = -Tile.SUBTILE_WIDTH50 + (tmpVec2i.x * Tile.SUBTILE_WIDTH50) - (tmpVec2i.y * Tile.SUBTILE_WIDTH50); - int my = -Tile.SUBTILE_HEIGHT50 - (tmpVec2i.x * Tile.SUBTILE_HEIGHT50) - (tmpVec2i.y * Tile.SUBTILE_HEIGHT50); + EngineUtils.worldToScreenCoords(tmpVec2i.x, tmpVec2i.y, tmpVec2i).sub(Tile.SUBTILE_WIDTH50, Tile.SUBTILE_HEIGHT50); shapes.setColor(Color.VIOLET); - drawDiamond(shapes, mx, my, Tile.SUBTILE_WIDTH, Tile.SUBTILE_HEIGHT); + drawDiamond(shapes, tmpVec2i.x, tmpVec2i.y, Tile.SUBTILE_WIDTH, Tile.SUBTILE_HEIGHT); } } @@ -1238,11 +1234,9 @@ public class MapRenderer { MapGraph.Point2 src = it.next(); for (MapGraph.Point2 dst; it.hasNext(); src = dst) { dst = it.next(); - float px1 = +(src.x * Tile.SUBTILE_WIDTH50) - (src.y * Tile.SUBTILE_WIDTH50); - float py1 = -(src.x * Tile.SUBTILE_HEIGHT50) - (src.y * Tile.SUBTILE_HEIGHT50); - float px2 = +(dst.x * Tile.SUBTILE_WIDTH50) - (dst.y * Tile.SUBTILE_WIDTH50); - float py2 = -(dst.x * Tile.SUBTILE_HEIGHT50) - (dst.y * Tile.SUBTILE_HEIGHT50); - shapes.line(px1, py1, px2, py2); + EngineUtils.worldToScreenCoords(src.x, src.y, tmpVec2a); + EngineUtils.worldToScreenCoords(dst.x, dst.y, tmpVec2b); + shapes.line(tmpVec2a.x, tmpVec2a.y, tmpVec2b.x, tmpVec2b.y); } } diff --git a/core/src/com/riiablo/util/EngineUtils.java b/core/src/com/riiablo/util/EngineUtils.java index 1169ebb4..00538d44 100644 --- a/core/src/com/riiablo/util/EngineUtils.java +++ b/core/src/com/riiablo/util/EngineUtils.java @@ -1,5 +1,6 @@ package com.riiablo.util; +import com.badlogic.gdx.math.GridPoint2; import com.badlogic.gdx.math.Vector2; import com.riiablo.map.DT1; import com.riiablo.map.DT1.Tile; @@ -12,4 +13,22 @@ public class EngineUtils { dst.y = -(world.x * Tile.SUBTILE_HEIGHT50) - (world.y * Tile.SUBTILE_HEIGHT50); return dst; } + + public static Vector2 worldToScreenCoords(float x, float y, Vector2 dst) { + dst.x = +(x * Tile.SUBTILE_WIDTH50) - (y * DT1.Tile.SUBTILE_WIDTH50); + dst.y = -(x * Tile.SUBTILE_HEIGHT50) - (y * Tile.SUBTILE_HEIGHT50); + return dst; + } + + public static GridPoint2 worldToScreenCoords(GridPoint2 world, GridPoint2 dst) { + dst.x = +(world.x * Tile.SUBTILE_WIDTH50) - (world.y * Tile.SUBTILE_WIDTH50); + dst.y = -(world.x * Tile.SUBTILE_HEIGHT50) - (world.y * Tile.SUBTILE_HEIGHT50); + return dst; + } + + public static GridPoint2 worldToScreenCoords(int x, int y, GridPoint2 dst) { + dst.x = +(x * Tile.SUBTILE_WIDTH50) - (y * Tile.SUBTILE_WIDTH50); + dst.y = -(x * Tile.SUBTILE_HEIGHT50) - (y * Tile.SUBTILE_HEIGHT50); + return dst; + } }