diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..6a0d4d1 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,36 @@ + + + + \ No newline at end of file diff --git a/src/main/java/core/math/Coordinates.java b/src/main/java/core/math/Coordinates.java index 2f3a83f..62297d1 100644 --- a/src/main/java/core/math/Coordinates.java +++ b/src/main/java/core/math/Coordinates.java @@ -1,6 +1,8 @@ package core.math; import core.general.Master; +import core.objects.core.MapAnchor; +import core.objects.core.GameObject; /** * This class provides everything about the local coordinate system the game uses @@ -9,6 +11,8 @@ import core.general.Master; */ public class Coordinates { + public static final GameObject MAP_ANCHOR = new MapAnchor(); + /** * This utility class should not be instantiated */ diff --git a/src/main/java/core/objects/base/Empty.java b/src/main/java/core/objects/base/Empty.java new file mode 100644 index 0000000..467b182 --- /dev/null +++ b/src/main/java/core/objects/base/Empty.java @@ -0,0 +1,21 @@ +package core.objects.base; + +import core.math.Vector2D; +import core.objects.core.GameObject; + +import java.awt.*; + +public class Empty extends GameObject { + + public Empty(Vector2D position) { + super(position, new Vector2D(0, 0)); + } + + @Override + public void draw(Graphics2D g2d) { + } + + @Override + public void update() { + } +} diff --git a/src/main/java/core/objects/core/GameObject.java b/src/main/java/core/objects/core/GameObject.java index 7e606fd..984984f 100644 --- a/src/main/java/core/objects/core/GameObject.java +++ b/src/main/java/core/objects/core/GameObject.java @@ -8,7 +8,7 @@ import core.math.Vector2D; import java.awt.*; /** - * The GameObject class is the superclass of every GameObject that can be displayed on screen. It has the 2 + * The GameObject class is the superclass of every {@code GameObject} that can be displayed on screen. It has the 2 * {@link #update()} and {@link #draw(Graphics2D)} methods that have to be overridden */ public abstract class GameObject implements Drawable { @@ -39,6 +39,7 @@ public abstract class GameObject implements Drawable { this.mainColor = Color.BLACK; this.master = Master.getMaster(); this.layer = 0; + this.parent = Coordinates.MAP_ANCHOR; } /** @@ -107,13 +108,14 @@ public abstract class GameObject implements Drawable { * This method draws an oval at the current position and size with arguments * * @param g2d The Graphics2D object provided by the master + * @param arg Arguments like "center" for the object being drawn in the center */ public void drawOval(Graphics2D g2d, String arg) { Vector2D abs; if(arg.contains("center")){ - abs = Coordinates.getWorldCoordinates(new Vector2D(position.x - size.x / 2, position.y - size.y / 2)); + abs = Coordinates.getWorldCoordinates(getCenterPosition()); } else { abs = Coordinates.getWorldCoordinates(position); } @@ -147,53 +149,55 @@ public abstract class GameObject implements Drawable { g2d.rotate(-rotation, xCenterAbs, yCenterAbs); } + /** + * Destroy this {@code GameObject} + */ public void destroy() { master.destroy(this); } /** - * Returns the value as map coords (only called on a parent) - * @param value - * @return + * Returns the value as map coords + * @param value The value relative to the parent + * @return The value in global map coordinates */ public Vector2D getMapCoords(Vector2D value) { - double x = position.x + value.x; - double y = position.y + value.y; - return new Vector2D(x, y); + double x = parent.position.x + value.x; + double y = parent.position.y + value.y; + return parent.getMapCoords(new Vector2D(x, y)); } + /** * Returns the value as world coordinates (only called on a parent) - * @param value - * @return + * @param value The value relative to the parent + * @return The absolute world coordinates */ public Vector2D getWorldCoordsFromLocal(Vector2D value) { return Coordinates.getWorldCoordinates(getMapCoords(value)); } /** - * Get world coords of a value (called on itself) - * @param value - * @return + * Get the center position of the object + * @return The center position */ - public Vector2D getWorldCoords(Vector2D value){ - return parent.getWorldCoordsFromLocal(value); - } - - - public Vector2D getCenterPosition(Vector2D position){ + public Vector2D getCenterPosition(){ return new Vector2D(position.x + size.x / 2, position.y + size.y / 2); } - public Vector2D getCenterPosition() { - return getCenterPosition(position); - } - + /** + * Get the render layer of the object + * @return The render layer + */ public int getLayer() { return layer; } + /** + * Get the rotation of the object as a Vector2D + * @return The rotation + */ protected Vector2D getV2DRotation(){ return Vector2D.getUnitVector(rotation); } diff --git a/src/main/java/core/objects/core/MapAnchor.java b/src/main/java/core/objects/core/MapAnchor.java new file mode 100644 index 0000000..168da7b --- /dev/null +++ b/src/main/java/core/objects/core/MapAnchor.java @@ -0,0 +1,30 @@ +package core.objects.core; + +import core.math.Vector2D; + +import java.awt.*; + +/** + * The {@code mapAnchor} objects is the default parent of every new {@code GameObject}. + */ +public class MapAnchor extends GameObject { + + public MapAnchor() { + super(new Vector2D(0, 0), new Vector2D(0, 0)); + } + + @Override + public void draw(Graphics2D g2d) { + + } + + @Override + public void update() { + + } + + @Override + public Vector2D getMapCoords(Vector2D value) { + return value.copy(); + } +} diff --git a/src/main/java/objects/ships/Turret.java b/src/main/java/objects/ships/Turret.java index c46e778..4e49ca6 100644 --- a/src/main/java/objects/ships/Turret.java +++ b/src/main/java/objects/ships/Turret.java @@ -40,7 +40,7 @@ public class Turret extends GameObject { @Override public void draw(Graphics2D g2d) { g2d.setPaint(mainColor); - Vector2D abs = parent.getWorldCoordsFromLocal(position); + Vector2D abs = getWorldCoordsFromLocal(position); int sizeAbs = (int) Coordinates.getWorldCoordinates(size).x; int xCenterAbs = (int) (abs.x + sizeAbs / 2); int yCenterAbs = (int) (abs.y + sizeAbs / 2); @@ -77,7 +77,7 @@ public class Turret extends GameObject { Point msLoc = master.getMouseLocation(); Vector2D mouseRel = Coordinates.getMapCoordinatesFromWorld(Vector2D.fromPoint(msLoc)); //100 correct - Vector2D centerMap = parent.getMapCoords(getCenterPosition()); + Vector2D centerMap = getMapCoords(getCenterPosition()); double targetRotation = -Math.atan2(centerMap.x - mouseRel.x, centerMap.y - mouseRel.y); rotation = ExMath.angleLerp(rotation, targetRotation, ROTATION_SPEED); @@ -88,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 = parent.getMapCoords(new Vector2D(barrelX, frontPosY)); + Vector2D spawnPosNR = getMapCoords(new Vector2D(barrelX, frontPosY)); if (Input.isMousePressed()) { lastShot = System.currentTimeMillis(); diff --git a/target/classes/objects/ships/BattleShip.class b/target/classes/objects/ships/BattleShip.class index 8f154ff..200598c 100644 Binary files a/target/classes/objects/ships/BattleShip.class and b/target/classes/objects/ships/BattleShip.class differ diff --git a/target/classes/objects/ships/Turret.class b/target/classes/objects/ships/Turret.class index 62c93ba..ea08041 100644 Binary files a/target/classes/objects/ships/Turret.class and b/target/classes/objects/ships/Turret.class differ