From b530a5d3a56ee8d82becada865ec6e7d7c0dd8dd Mon Sep 17 00:00:00 2001 From: Nilstrieb Date: Mon, 14 Dec 2020 08:24:26 +0100 Subject: [PATCH] coordinates --- 2DGame.iml | 2 -- src/main/java/core/math/Coords.java | 14 ++++---- .../core/physics/hitboxes/RectHitBox.java | 2 +- src/main/java/objects/DebugPos.java | 2 +- src/main/java/objects/core/GameObject.java | 30 ++++++++++++------ src/main/java/objects/ships/BattleShip.java | 2 +- src/main/java/objects/ships/Shell.java | 5 ++- src/main/java/objects/ships/Turret.java | 9 +++--- target/classes/objects/ships/BattleShip.class | Bin 2304 -> 2304 bytes target/classes/objects/ships/Turret.class | Bin 4135 -> 4175 bytes 10 files changed, 36 insertions(+), 30 deletions(-) delete mode 100644 2DGame.iml diff --git a/2DGame.iml b/2DGame.iml deleted file mode 100644 index 78b2cc5..0000000 --- a/2DGame.iml +++ /dev/null @@ -1,2 +0,0 @@ - - \ No newline at end of file diff --git a/src/main/java/core/math/Coords.java b/src/main/java/core/math/Coords.java index ad3e83d..858f04e 100644 --- a/src/main/java/core/math/Coords.java +++ b/src/main/java/core/math/Coords.java @@ -22,8 +22,13 @@ public class Coords { public static Master master = Master.getMaster(); + /** + * Get the world coordinates of a point in map coordinates + * @param value A point in map coordinates + * @return The point in world coordinates + */ public static Vector2D getWorldCoords(Vector2D value) { - double x = (value.x / (Master.SCREEN_Y_COORDINATES * Master.SCREEN_RATIO) * master.getW()); + double x = (value.x / Master.SCREEN_Y_COORDINATES / Master.SCREEN_RATIO * master.getW()); double y = (value.y / Master.SCREEN_Y_COORDINATES * master.getH()); return new Vector2D(x, y); } @@ -39,11 +44,4 @@ public class Coords { double y = (value.y / master.getH()) * Master.SCREEN_Y_COORDINATES; return new Vector2D(x, y); } - - public static Vector2D getWorldCoordsSize(Vector2D value) { - double x = (value.x / Master.SCREEN_Y_COORDINATES * master.getH()); - double y = (value.y / Master.SCREEN_Y_COORDINATES * master.getH()); - return new Vector2D(x, y); - } - } diff --git a/src/main/java/core/physics/hitboxes/RectHitBox.java b/src/main/java/core/physics/hitboxes/RectHitBox.java index 2cae2b9..bab67d0 100644 --- a/src/main/java/core/physics/hitboxes/RectHitBox.java +++ b/src/main/java/core/physics/hitboxes/RectHitBox.java @@ -175,7 +175,7 @@ public class RectHitBox extends Hitbox { public void draw(Graphics2D g2d) { Vector2D abs = Coords.getWorldCoords(x1); - Vector2D sizeAbs = Coords.getWorldCoordsSize(Vector2D.subtract(y2, x1)); + Vector2D sizeAbs = Coords.getWorldCoords(Vector2D.subtract(y2, x1)); g2d.drawRect((int)abs.x, (int)abs.y, (int)sizeAbs.x, (int)sizeAbs.y); g2d.setPaint(Color.MAGENTA); diff --git a/src/main/java/objects/DebugPos.java b/src/main/java/objects/DebugPos.java index a5e31c7..cfc4559 100644 --- a/src/main/java/objects/DebugPos.java +++ b/src/main/java/objects/DebugPos.java @@ -17,7 +17,7 @@ public class DebugPos extends GameObject { @Override public void draw(Graphics2D g2d) { - drawOval(g2d); + drawOval(g2d, "center"); } @Override diff --git a/src/main/java/objects/core/GameObject.java b/src/main/java/objects/core/GameObject.java index 1bb293f..5bac81e 100644 --- a/src/main/java/objects/core/GameObject.java +++ b/src/main/java/objects/core/GameObject.java @@ -62,7 +62,7 @@ public abstract class GameObject implements Drawable { */ public void drawRect(Graphics2D g2d) { Vector2D abs = Coords.getWorldCoords(position); - Vector2D sizeAbs = Coords.getWorldCoordsSize(size); + Vector2D sizeAbs = Coords.getWorldCoords(size); g2d.setPaint(mainColor); g2d.fillRect((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y); @@ -75,20 +75,32 @@ public abstract class GameObject implements Drawable { */ public void fillOval(Graphics2D g2d) { Vector2D abs = Coords.getWorldCoords(position); - Vector2D sizeAbs = Coords.getWorldCoordsSize(size); + Vector2D sizeAbs = Coords.getWorldCoords(size); g2d.setPaint(mainColor); g2d.fillOval((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y); } /** - * This method draws a rectangle at the current position and size + * This method draws an oval at the current position and size * * @param g2d The Graphics2D object provided by the master */ public void drawOval(Graphics2D g2d) { - Vector2D abs = Coords.getWorldCoords(position); - Vector2D sizeAbs = Coords.getWorldCoordsSize(size); + drawOval(g2d, ""); + } + + /** + * This method draws an oval at the current position and size with arguments + * + * @param g2d The Graphics2D object provided by the master + */ + public void drawOval(Graphics2D g2d, String arg) { + + Vector2D abs; + + abs = (arg.contains("center")) ? Coords.getWorldCoords(getCenterPosition()) : Coords.getWorldCoords(position); + Vector2D sizeAbs = Coords.getWorldCoords(size); g2d.setPaint(mainColor); g2d.drawOval((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y); @@ -103,7 +115,7 @@ public abstract class GameObject implements Drawable { */ public void drawRoundRect(Graphics2D g2d, int arcW, int arcH) { Vector2D abs = Coords.getWorldCoords(position); - Vector2D sizeAbs = Coords.getWorldCoordsSize(size); + Vector2D sizeAbs = Coords.getWorldCoords(size); g2d.setPaint(mainColor); g2d.fillRoundRect((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y, arcW, arcH); @@ -116,8 +128,8 @@ public abstract class GameObject implements Drawable { public Vector2D getMapCoords(Vector2D value) { - double x = (position.x + value.x / 100d * size.x); - double y = (position.y + value.y / 100d * size.y); + double x = position.x + value.x; + double y = position.y + value.y; return new Vector2D(x, y); } @@ -134,7 +146,7 @@ public abstract class GameObject implements Drawable { } public Vector2D getWorldCoordsFromLocalSize(Vector2D value) { - return Coords.getWorldCoordsSize(getMapCoordsSize(value)); + return Coords.getWorldCoords(getMapCoordsSize(value)); } public Vector2D getCenterPosition(Vector2D position){ diff --git a/src/main/java/objects/ships/BattleShip.java b/src/main/java/objects/ships/BattleShip.java index 6848fec..9f10068 100644 --- a/src/main/java/objects/ships/BattleShip.java +++ b/src/main/java/objects/ships/BattleShip.java @@ -15,7 +15,7 @@ public class BattleShip extends GameObject { public BattleShip(Color mainColor) { this(20, 20, 10, 40, mainColor); //TODO turret size should use w and h but correct just like with world coords - turrets.add(new Turret(this, 25, 25, 50, 3)); + turrets.add(new Turret(this, 2.5, 10, 50, 3)); //turrets.add(new Turret(this, 25, 10, 50, 2)); //turrets.add(new Turret(this, 25, 70, 50, 2)); } diff --git a/src/main/java/objects/ships/Shell.java b/src/main/java/objects/ships/Shell.java index 5110a2b..4aaa38d 100644 --- a/src/main/java/objects/ships/Shell.java +++ b/src/main/java/objects/ships/Shell.java @@ -10,19 +10,18 @@ import java.awt.*; /** * A shell fired by a cannon */ -//TODO why tf do shells not use map coords... public class Shell extends GameObject { public Shell(Vector2D position, Vector2D size, Vector2D velocity) { super(position, size/*, new RectHitBox(position, size)*/); this.velocity = velocity; + this.mainColor = Color.ORANGE; } @Override public void draw(Graphics2D g2d) { - g2d.setPaint(Color.orange); - g2d.fillOval((int) position.x, (int) position.y, (int) size.x, (int) size.y); + fillOval(g2d); } @Override diff --git a/src/main/java/objects/ships/Turret.java b/src/main/java/objects/ships/Turret.java index 3cd9477..18f1dc7 100644 --- a/src/main/java/objects/ships/Turret.java +++ b/src/main/java/objects/ships/Turret.java @@ -1,13 +1,10 @@ package objects.ships; -import core.Master; import core.math.Coords; import core.math.ExMath; import core.math.Vector2D; -import jdk.swing.interop.SwingInterOpUtils; import objects.core.GameObject; -import javax.swing.*; import java.awt.*; /** @@ -17,7 +14,8 @@ public class Turret extends GameObject { private static final double ROTATION_SPEED = 0.05; private static final int SHOT_EFFECT_TIME = 300; - private static final int SHELL_SPEED = 10; + private static final int SHELL_SPEED = 1; + public static final double SHELL_SIZE = 2; BattleShip battleShip; @@ -113,7 +111,8 @@ public class Turret extends GameObject { Vector2D shellVel = Vector2D.getUnitVector(rotation).negative().multiply(SHELL_SPEED); Vector2D pos = Vector2D.rotateAround(new Vector2D(center.x, center.y), new Vector2D(barrelX, frontPosY), rotation, Vector2D.COUNTERCLOCKWISE); - master.create(new Shell(pos, new Vector2D(10, 10), shellVel)); + master.debugPos(pos); + master.create(new Shell(pos, new Vector2D(SHELL_SIZE, SHELL_SIZE), shellVel)); } } } diff --git a/target/classes/objects/ships/BattleShip.class b/target/classes/objects/ships/BattleShip.class index 56bb9a370cd82208833b1e1497558f0388d70a99..431889524f545f62be0d0109726e01c7ede5cc46 100644 GIT binary patch delta 86 zcmZn=Y7m;RgOO$9&Nr-#%#)X~$+HSEXfudTKE)QxE5X3UAju%gAjKffAU)ZHeFCQf rgCv6@gBpX<8E7&sZUC#P|^Gb&DA#}NboI!_UE delta 86 zcmZn=Y7m;RgVA#1&Nr-#!jqS=$+HSFXfudRKE)QxE6%{hAi*HYAju%jAT`;9eFCRE rgCv6jgBpY46u88{iVCZ}<@Gb&78#}NboO*Rp# diff --git a/target/classes/objects/ships/Turret.class b/target/classes/objects/ships/Turret.class index 59ca635c4038be46f30aae1ea4d18a12f5b4d2e9..4849663a0bdab90408233c0d0a70bb8613250c68 100644 GIT binary patch delta 828 zcmZ3ka9)Ay)W2Q(7#J9=7~XE=dd$wp$jHDI?BVL;6Cdmu<+}MD`w2!y&dJ+34f!P* z&NA3CFfyEAU|?WkSULGQr#|aB21bVSlcl*VjV~}TFvu~mGcYhHF>o_TG6*opGl()M zGe|M0FeosnGN?1CFQi2}ijsTtOyCfn_xr1Q;Y4 zL>c56q!_dq!=TN;#Gu0P zmfPaGuu_C!<#q;7NrCMQ2evUBZsBEI{y%0T0|Nsy11R9q7$g}O z7(y607(y8Y7$O)H7$O<;7@`;)7@`?G7-AR_7~&an84?)k7!nzJ7?K!fF(fl=U`S^; zz>vvsi6NWe9zzbpM}}NRc82_VMnQ%GMhS)@Mh%8yMn8s9#&Cu*#xjO%#u|o7a40Tf z5Mb0`@MCz)pv@q_=*OVJ@Pt8|L6G4iLn}CB`9Q{iLzbW6D#LPc!197T4-Hrj#!`l- z49^%C83Y(}8J>d!R)H~=;RS;>12cnr0;4R$O9op87KU6#0dTmpG1M{q2ZuX5Ll478 zaJX|Y%wl*94tGw54Gd=)UNP7*a4}qBSPhN=9)^dLzwoQ_E%?R2%D~9*8l3#!007~l Bt(5=( delta 795 zcmX@Fuv~%b)W2Q(7#J9=7~X8;dd$wk#mJyIxqw4@vn0nVMn;awM>q{9vvTRNo@HQU zI5*jX%hLEf0|SE`13Lo)gCYYrg9L*BgB*h>gA#)jgEE5xg9?K>gDS%X1_lN@1~vu; z1`URb43{P^;j(1C3^w5rmxU|H1SzPj0D}aBD1#h>6oV#%JcAa48iO`iw-^Hx0|SFP zNH+rrs51yNh%hiRFfv?WxC%09b3C^aV?D^IC)0|sLT z8wOJb9|m)V0tOp~at2$5Mg}{Eeg+4I*$j>hs~Maawlg?09A=B8w@<4 zuwWE~xs*|m;U>c^21W)EhQAEA8MGOg7*rVEGTdR{VPIx3V0g%Imw|_Yg~5j5D#JYn z9tKv1MurIt_ZfH?*ccv6_TyC%e#npu@*e{O(_RKH1|bFp#3?F z!H~3-fmdq_L)Z?6_1hRWi!iuPUcskdznfuSq@>_(2Fpkh26qt#_w5Xxk^vytfFYgX5<@1#J%%iXj||z2>