From e82b5fe02f613346aa828ace01941f239ec855bb Mon Sep 17 00:00:00 2001 From: Nilstrieb Date: Fri, 25 Dec 2020 12:14:18 +0100 Subject: [PATCH] general parenting --- .idea/inspectionProfiles/Project_Default.xml | 36 +++++++++++++ src/main/java/core/math/Coordinates.java | 4 ++ src/main/java/core/objects/base/Empty.java | 21 ++++++++ .../java/core/objects/core/GameObject.java | 50 ++++++++++-------- .../java/core/objects/core/MapAnchor.java | 30 +++++++++++ src/main/java/objects/ships/Turret.java | 6 +-- target/classes/objects/ships/BattleShip.class | Bin 3330 -> 3157 bytes target/classes/objects/ships/Turret.class | Bin 3881 -> 3872 bytes 8 files changed, 121 insertions(+), 26 deletions(-) create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 src/main/java/core/objects/base/Empty.java create mode 100644 src/main/java/core/objects/core/MapAnchor.java 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 8f154ff38a78e362c08aa4d378563fcf65b8e798..200598cda150cb2b60a7bd8c25f9daf68a51f25d 100644 GIT binary patch delta 1232 zcmZpYx+=kS>ff$?3=9m`3>!9bWikeEF=#XBuruiLFmN;IF*3*`=NF~w=O<;QCYKcJ zgE{VrxvBnO0Y(OP4Hp*|7tJs(23ZCJkQPHm24)S-Fir*z24fxu69&`CxlGD(<~$4* z44RA#+$s5~#V)DE1&QT(ASG5j4Au-blh-jRbK5c4vokpGFgP+eO}@jV&gR0y;L6}O znUmR&&4Y)*lfi4U1G5X84-bPcgWu#XW<$0B9)>`MpvecB)!0IK7(yAsCckCY;f`R4 zWM_!tVTfjknXJO1uMo!&&(4s*!;r|3#K<57c7JYSNrrw{YH~?_k&(;fXcl$uIEEA+ z1_g#xMh3gd4_QPfKV=l1yq!gYEuDuUgCTSBRTg#mY#xRjhFnGlj-vdM#FEVXJVpja z7cPbfhI}4|0*08$(yWTyB_MZ}GBU_(XilENEX&2kAjnY8!%)FcxjBzjmWgjR7sDKe zx$F${co^n0EZ97gorRHc(d2m?(uPa87&sV~@-R$gm?pq5fuEIO1xW8o9)?~9Lq-N3 z&%C_UBIlgM;^Nd|c7`>K462hava7PO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z~1Kq znXbK!K_(pH!FrI{Twp0%22KV$uz3*Qalw7Z#W0g$7T9;Q8RkF@)Ccn!7+68RTLX_% zHU>s;1c0Is;w)B(F#=qmWDnJB1$7OZcDmL!hB}B-KrVr3*Fkax8#rV^sge!q3TB4+ sVCxnzEQF@rMGVXgD;ZWZtYd(%)-$YQ*u=oVz{SAGu$f^i!*&Kq0G+`CxBvhE diff --git a/target/classes/objects/ships/Turret.class b/target/classes/objects/ships/Turret.class index 62c93bac3c9759e54d6f93b63d3ba8c0e896630d..ea08041480554c6d6de5b8873ddf2aa62803a1a8 100644 GIT binary patch delta 527 zcmZ1}w?J;gMn*=C&6^l4nHf1YzhO~h6XIv!WZ+_8U=-2T+QJ~cjX_OovpTl|W4$1Q z4udR%E`uh69)l@^0fQ@p5koP9IYSkL1w#viCBq~JYle9YHVo?+Y#DYj*fAVqaA3H~ z;K*=?!HMAwgEPZt25W|&3~r1Z3?7Wa44w>!7#JAR7`PbLF}O0EV&GxmVA#cA$#9y1 zhk=t}Dnkmx83rB(c91(54l^8KU~mUB&oZ22U}O+vWM(+epwGa>Aj|Nb;Q|8>12cmr z!)u0%3_J`h45keC87?vKFt9STFic>$%)rCI#&Cs!fkBpm`9H(d$-KO(QqLH;7zLxZo@WPe^?#@@+ud9@~=;*Do4n5@MoRWHd9$Y972%;3ln z$`H&D&JfQK!BE5y$bZZh0rU}O+rxW#ZA8~~CG7a8s_=rb@g7&7c+xXa+kz`_vBFdfO;av*PqF|e~U kaIiDnV`s2sXRu{vWMp9Y&Cv6Uft7)g;Q<2!0~5nT0Ek^|qW}N^ delta 537 zcmZ1=w^DAyMn*=a&6^l4nHiZjzhO~h6BT0MWZ+_8U=-KZ+QJ~cjX_Oo3xnWh6>bH_ zdJzU41_cIP1|0@H1`7rQ1`h@!hB5|ohFS&-hIR%^hN%qJ3=0`-7&bE4GVEorV>rp+ zz;J`Xk>Ngr6T^E3XNGSK)(n3b+!(nTJQ&3oJQ)r#FfgPsa4~FTaAi2fz{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