From b7991cdb740d25475d1aed1876b9879daaaad30f Mon Sep 17 00:00:00 2001 From: Ivory Date: Fri, 20 Jan 2023 07:26:06 -0500 Subject: [PATCH] selection box now tied to world not screen --- .../xyz/valnet/engine/graphics/Tile9.java | 7 ++++- .../java/xyz/valnet/engine/math/Vector4i.java | 19 +++++++++++++ .../xyz/valnet/hadean/gameobjects/Camera.java | 8 ++++-- .../inputlayer/SelectionLayer.java | 27 +++++++++++++------ .../worldobjects/agents/Agent.java | 2 +- 5 files changed, 51 insertions(+), 12 deletions(-) diff --git a/src/main/java/xyz/valnet/engine/graphics/Tile9.java b/src/main/java/xyz/valnet/engine/graphics/Tile9.java index d2ed331..f41fb63 100644 --- a/src/main/java/xyz/valnet/engine/graphics/Tile9.java +++ b/src/main/java/xyz/valnet/engine/graphics/Tile9.java @@ -1,6 +1,7 @@ package xyz.valnet.engine.graphics; import xyz.valnet.engine.math.Vector4f; +import xyz.valnet.engine.math.Vector4i; public class Tile9 { @@ -37,7 +38,11 @@ public class Tile9 { } public void draw(Vector4f box) { - draw((int)box.x, (int)box.y, (int)box.z, (int)box.w); + draw(box.asInt()); + } + + public void draw(Vector4i box) { + draw(box.x, box.y, box.z, box.w); } public void draw(int x, int y, int w, int h) { diff --git a/src/main/java/xyz/valnet/engine/math/Vector4i.java b/src/main/java/xyz/valnet/engine/math/Vector4i.java index f1847bd..c772a7a 100644 --- a/src/main/java/xyz/valnet/engine/math/Vector4i.java +++ b/src/main/java/xyz/valnet/engine/math/Vector4i.java @@ -56,4 +56,23 @@ public class Vector4i implements Serializable { return vecs; } + + + public Vector4i toAABB() { + return new Vector4i( + x, + y, + x + z, + y + w + ); + } + + public Vector4i toXYWH() { + return new Vector4i( + x, + y, + z - x, + w - y + ); + } } diff --git a/src/main/java/xyz/valnet/hadean/gameobjects/Camera.java b/src/main/java/xyz/valnet/hadean/gameobjects/Camera.java index b52a255..961eace 100644 --- a/src/main/java/xyz/valnet/hadean/gameobjects/Camera.java +++ b/src/main/java/xyz/valnet/hadean/gameobjects/Camera.java @@ -79,7 +79,11 @@ public class Camera extends GameObject implements ITransient, IMouseCaptureArea return new Vector2f((x - screenWidth / 2 + focus.x * tileWidth) / tileWidth, (y - screenHeight / 2 + focus.y * tileWidth) / tileWidth); } - public Vector4f world2Screen(Vector4f input) { + public Vector2f screen2world(Vector2f pos) { + return screen2world(pos.x, pos.y); + } + + public Vector4f world2screen(Vector4f input) { return new Vector4f( input.x * tileWidth + screenWidth / 2 - focus.x * tileWidth, input.y * tileWidth + screenHeight / 2 - focus.y * tileWidth, @@ -120,7 +124,7 @@ public class Camera extends GameObject implements ITransient, IMouseCaptureArea public void drawProgressBar(float progress, Vector4f worldBox) { int h = 6; - Vector4i box = world2Screen(worldBox).toXYWH().asInt(); + Vector4i box = world2screen(worldBox).toXYWH().asInt(); Drawing.setLayer(Layers.GENERAL_UI); Assets.flat.pushColor(new Vector4f(0, 0, 0, 1)); Assets.uiFrame.draw(box.x - h, box.y + box.w / 2 - h / 2, box.z + h * 2, h); diff --git a/src/main/java/xyz/valnet/hadean/gameobjects/inputlayer/SelectionLayer.java b/src/main/java/xyz/valnet/hadean/gameobjects/inputlayer/SelectionLayer.java index 88c6fee..17bbef7 100644 --- a/src/main/java/xyz/valnet/hadean/gameobjects/inputlayer/SelectionLayer.java +++ b/src/main/java/xyz/valnet/hadean/gameobjects/inputlayer/SelectionLayer.java @@ -10,6 +10,7 @@ import xyz.valnet.engine.graphics.Drawing; import xyz.valnet.engine.math.Vector2f; import xyz.valnet.engine.math.Vector2i; import xyz.valnet.engine.math.Vector4f; +import xyz.valnet.engine.math.Vector4i; import xyz.valnet.engine.scenegraph.GameObject; import xyz.valnet.engine.scenegraph.IMouseCaptureArea; import xyz.valnet.engine.scenegraph.ITransient; @@ -91,7 +92,13 @@ public class SelectionLayer extends GameObject implements IMouseCaptureArea, ITr } if(initialCoords != null) { - Assets.selectionFrame.draw((int) initialCoords.x, (int) initialCoords.y, (int) (App.mouseX - initialCoords.x), (int) (App.mouseY - initialCoords.y)); + Vector2i screenPos = camera.world2screen(initialCoords); + Assets.selectionFrame.draw(new Vector4i( + screenPos.x, + screenPos.y, + App.mouseX, + App.mouseY + ).toXYWH()); } } @@ -111,8 +118,8 @@ public class SelectionLayer extends GameObject implements IMouseCaptureArea, ITr List newSelection = new ArrayList(); Vector4f normalizedSelectionBoxScreen = sortVector(a); - Vector2f selectionBoxWorldMin = camera.screen2world(normalizedSelectionBoxScreen.x, normalizedSelectionBoxScreen.y); - Vector2f selectionBoxWorldMax = camera.screen2world(normalizedSelectionBoxScreen.z, normalizedSelectionBoxScreen.w); + Vector2f selectionBoxWorldMin = new Vector2f(normalizedSelectionBoxScreen.x, normalizedSelectionBoxScreen.y); + Vector2f selectionBoxWorldMax = new Vector2f(normalizedSelectionBoxScreen.z, normalizedSelectionBoxScreen.w); List selectables = getAll(ISelectable.class); @@ -197,7 +204,7 @@ public class SelectionLayer extends GameObject implements IMouseCaptureArea, ITr if(button == 0) { if(initialCoords == null) { - initialCoords = new Vector2f(App.mouseX, App.mouseY); + initialCoords = camera.screen2world(new Vector2f(App.mouseX, App.mouseY)); } } else if (button == 1) { if(selected.size() == 0) { @@ -218,12 +225,16 @@ public class SelectionLayer extends GameObject implements IMouseCaptureArea, ITr public void mouseUp(int button) { if(initialCoords != null && button == 0) { - makeSelection(new Vector4f( + Vector2f worldMouse = camera.screen2world(App.mouseX, App.mouseY); + + Vector4f worldBox = new Vector4f( initialCoords.x, initialCoords.y, - App.mouseX, - App.mouseY - )); + worldMouse.x, + worldMouse.y + ); + + makeSelection(worldBox); initialCoords = null; } diff --git a/src/main/java/xyz/valnet/hadean/gameobjects/worldobjects/agents/Agent.java b/src/main/java/xyz/valnet/hadean/gameobjects/worldobjects/agents/Agent.java index b8bd7de..0b594e2 100644 --- a/src/main/java/xyz/valnet/hadean/gameobjects/worldobjects/agents/Agent.java +++ b/src/main/java/xyz/valnet/hadean/gameobjects/worldobjects/agents/Agent.java @@ -185,7 +185,7 @@ public abstract class Agent extends WorldObject implements ISelectable { } Assets.selectionFrame.draw( - camera.world2Screen( + camera.world2screen( terrain.getTile( path.getDestination().getPosition() )