From 5bb0282443b0745d37d7d50bad544ead4c73adc1 Mon Sep 17 00:00:00 2001 From: Nilstrieb Date: Thu, 24 Dec 2020 14:34:39 +0100 Subject: [PATCH] coordinate stuff --- .../java/core/objects/core/GameObject.java | 22 ++++++++++++++++++- src/main/java/objects/ships/Turret.java | 13 +++++------ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/main/java/core/objects/core/GameObject.java b/src/main/java/core/objects/core/GameObject.java index 2c57b83..7e606fd 100644 --- a/src/main/java/core/objects/core/GameObject.java +++ b/src/main/java/core/objects/core/GameObject.java @@ -26,6 +26,8 @@ public abstract class GameObject implements Drawable { protected int layer; + protected GameObject parent; + public GameObject(double x, double y, double xSize, double ySize) { this(new Vector2D(x, y), new Vector2D(xSize, ySize)); } @@ -150,17 +152,35 @@ public abstract class GameObject implements Drawable { } + /** + * Returns the value as map coords (only called on a parent) + * @param value + * @return + */ public Vector2D getMapCoords(Vector2D value) { double x = position.x + value.x; double y = position.y + value.y; return new Vector2D(x, y); - } + /** + * Returns the value as world coordinates (only called on a parent) + * @param value + * @return + */ public Vector2D getWorldCoordsFromLocal(Vector2D value) { return Coordinates.getWorldCoordinates(getMapCoords(value)); } + /** + * Get world coords of a value (called on itself) + * @param value + * @return + */ + public Vector2D getWorldCoords(Vector2D value){ + return parent.getWorldCoordsFromLocal(value); + } + public Vector2D getCenterPosition(Vector2D position){ return new Vector2D(position.x + size.x / 2, position.y + size.y / 2); diff --git a/src/main/java/objects/ships/Turret.java b/src/main/java/objects/ships/Turret.java index 49932af..c46e778 100644 --- a/src/main/java/objects/ships/Turret.java +++ b/src/main/java/objects/ships/Turret.java @@ -18,9 +18,6 @@ public class Turret extends GameObject { private static final int SHELL_SPEED = 1; public static final double SHELL_SIZE = 2; private static final double BARREL_THICKNESS = 1.7; - - BattleShip battleShip; - private int barrelAmount = 3; private final Color mainColor; @@ -29,21 +26,21 @@ public class Turret extends GameObject { public Turret(BattleShip battleShip, double x, double y, double size, int barrelAmount) { super(x, y, size, size); - this.battleShip = battleShip; + this.parent = battleShip; this.barrelAmount = barrelAmount; mainColor = Color.GRAY; } public Turret(BattleShip battleShip) { super(25, 50, 1.25, 0.5); - this.battleShip = battleShip; + this.parent = battleShip; mainColor = Color.GRAY; } @Override public void draw(Graphics2D g2d) { g2d.setPaint(mainColor); - Vector2D abs = battleShip.getWorldCoordsFromLocal(position); + Vector2D abs = parent.getWorldCoordsFromLocal(position); int sizeAbs = (int) Coordinates.getWorldCoordinates(size).x; int xCenterAbs = (int) (abs.x + sizeAbs / 2); int yCenterAbs = (int) (abs.y + sizeAbs / 2); @@ -80,7 +77,7 @@ public class Turret extends GameObject { Point msLoc = master.getMouseLocation(); Vector2D mouseRel = Coordinates.getMapCoordinatesFromWorld(Vector2D.fromPoint(msLoc)); //100 correct - Vector2D centerMap = battleShip.getMapCoords(getCenterPosition()); + Vector2D centerMap = parent.getMapCoords(getCenterPosition()); double targetRotation = -Math.atan2(centerMap.x - mouseRel.x, centerMap.y - mouseRel.y); rotation = ExMath.angleLerp(rotation, targetRotation, ROTATION_SPEED); @@ -91,7 +88,7 @@ public class Turret extends GameObject { int barrelX = (int) (position.x + (i + 1) * barrelSpacing); int frontPosY = (int) (position.y - size.x / 2); - Vector2D spawnPosNR = battleShip.getMapCoords(new Vector2D(barrelX, frontPosY)); + Vector2D spawnPosNR = parent.getMapCoords(new Vector2D(barrelX, frontPosY)); if (Input.isMousePressed()) { lastShot = System.currentTimeMillis();