diff --git a/src/main/java/core/Master.java b/src/main/java/core/Master.java index 0b765c8..799d44c 100644 --- a/src/main/java/core/Master.java +++ b/src/main/java/core/Master.java @@ -129,7 +129,7 @@ public class Master extends JPanel { * @param pos The position */ public void debugPos(Vector2D pos) { - create(new DebugPos(pos, new Vector2D(10, 10))); + create(new DebugPos(pos, new Vector2D(2, 2)), 3); } /** @@ -246,6 +246,7 @@ public class Master extends JPanel { public void destroy(GameObject gameObject) { objects.remove(gameObject); + objectBuffer.remove(gameObject); drawables.get(gameObject.getLayer()).remove(gameObject); if (gameObject instanceof Collidable) { collidables.remove(gameObject); diff --git a/src/main/java/core/math/Vector2D.java b/src/main/java/core/math/Vector2D.java index c5e4398..fa8050c 100644 --- a/src/main/java/core/math/Vector2D.java +++ b/src/main/java/core/math/Vector2D.java @@ -1,6 +1,8 @@ package core.math; +import java.awt.*; + /** * A 2-dimensional Vector that can be used to store position or velocity */ @@ -33,6 +35,15 @@ public class Vector2D { y = 0; } + /** + * Get a new Vector2D from a Point + * @param point The point + * @return The Vector2D + */ + public static Vector2D fromPoint(Point point) { + return new Vector2D(point.x, point.y); + } + /** * Add another Vector to this vector, modifies this object * diff --git a/src/main/java/objects/DebugPos.java b/src/main/java/objects/DebugPos.java index 9775791..a5e31c7 100644 --- a/src/main/java/objects/DebugPos.java +++ b/src/main/java/objects/DebugPos.java @@ -12,12 +12,12 @@ public class DebugPos extends GameObject { public DebugPos(Vector2D position, Vector2D size) { super(position.copy(), size); this.velocity = new Vector2D(); + this.mainColor = Color.GREEN; } @Override public void draw(Graphics2D g2d) { - g2d.setPaint(Color.green); - g2d.drawOval((int) (position.x - size.x / 2), (int) (position.y - size.y / 2), (int) size.x, (int) size.y); + drawOval(g2d); } @Override diff --git a/src/main/java/objects/core/GameObject.java b/src/main/java/objects/core/GameObject.java index 3327e03..1bb293f 100644 --- a/src/main/java/objects/core/GameObject.java +++ b/src/main/java/objects/core/GameObject.java @@ -4,7 +4,6 @@ import core.math.Coords; import core.Drawable; import core.Master; import core.math.Vector2D; -import core.physics.Collidable; import java.awt.*; @@ -74,7 +73,7 @@ public abstract class GameObject implements Drawable { * * @param g2d The Graphics2D object provided by the master */ - public void drawOval(Graphics2D g2d) { + public void fillOval(Graphics2D g2d) { Vector2D abs = Coords.getWorldCoords(position); Vector2D sizeAbs = Coords.getWorldCoordsSize(size); @@ -82,6 +81,19 @@ public abstract class GameObject implements Drawable { 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 + * + * @param g2d The Graphics2D object provided by the master + */ + public void drawOval(Graphics2D g2d) { + Vector2D abs = Coords.getWorldCoords(position); + Vector2D sizeAbs = Coords.getWorldCoordsSize(size); + + g2d.setPaint(mainColor); + g2d.drawOval((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y); + } + /** * This method draws a rounded rectangle at the current position and size * @@ -97,6 +109,7 @@ public abstract class GameObject implements Drawable { g2d.fillRoundRect((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y, arcW, arcH); } + @Deprecated public void destroy() { master.destroy(this); } @@ -124,8 +137,12 @@ public abstract class GameObject implements Drawable { return Coords.getWorldCoordsSize(getMapCoordsSize(value)); } + public Vector2D getCenterPosition(Vector2D position){ + return new Vector2D(position.x + size.x / 2, position.y + size.y / 2); + } + public Vector2D getCenterPosition() { - return new Vector2D(position.x - size.x / 2, position.y - size.y / 2); + return getCenterPosition(position); } public int getLayer() { diff --git a/src/main/java/objects/ships/BattleShip.java b/src/main/java/objects/ships/BattleShip.java index 4799aeb..6848fec 100644 --- a/src/main/java/objects/ships/BattleShip.java +++ b/src/main/java/objects/ships/BattleShip.java @@ -14,6 +14,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, 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 cf2ded7..5110a2b 100644 --- a/src/main/java/objects/ships/Shell.java +++ b/src/main/java/objects/ships/Shell.java @@ -3,6 +3,7 @@ package objects.ships; import core.math.Vector2D; import core.physics.hitboxes.RectHitBox; import objects.core.CollGameObject; +import objects.core.GameObject; import java.awt.*; @@ -10,11 +11,11 @@ import java.awt.*; * A shell fired by a cannon */ //TODO why tf do shells not use map coords... -public class Shell extends CollGameObject { +public class Shell extends GameObject { public Shell(Vector2D position, Vector2D size, Vector2D velocity) { - super(position, size, new RectHitBox(position, size)); + super(position, size/*, new RectHitBox(position, size)*/); this.velocity = velocity; } diff --git a/src/main/java/objects/ships/Submarine.java b/src/main/java/objects/ships/Submarine.java index 2204269..ba4b2ff 100644 --- a/src/main/java/objects/ships/Submarine.java +++ b/src/main/java/objects/ships/Submarine.java @@ -17,7 +17,7 @@ public class Submarine extends CollGameObject { @Override public void draw(Graphics2D g2d) { g2d.setPaint(Color.BLUE); - drawOval(g2d); + fillOval(g2d); } @Override diff --git a/src/main/java/objects/ships/Turret.java b/src/main/java/objects/ships/Turret.java index ed1cbd6..3cd9477 100644 --- a/src/main/java/objects/ships/Turret.java +++ b/src/main/java/objects/ships/Turret.java @@ -1,9 +1,13 @@ 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.*; /** @@ -25,8 +29,6 @@ public class Turret extends GameObject { private long lastShot = 0; - //private double rotation; - public Turret(BattleShip battleShip, double x, double y, double size, int barrelAmount) { super(x, y, size, size); this.battleShip = battleShip; @@ -78,28 +80,38 @@ public class Turret extends GameObject { @Override public void update() { + Point msLoc = master.getMouseLocation(); + Vector2D mouseRel = Coords.getMapCoordsFromWorld(Vector2D.fromPoint(msLoc)); //100 correct + + //TODO getCenter uses the wrong size + Vector2D center = battleShip.getMapCoords(getCenterPosition(position)); + System.out.println(getCenterPosition(position)); + master.debugPos(battleShip.getMapCoords(position)); + master.debugPos(center); + double targetRotation = -Math.atan2(center.x - mouseRel.x, center.y - mouseRel.y); + + //---------------OLD IMPLEMENTATION Vector2D abs = battleShip.getWorldCoordsFromLocal(position); int sizeAbs = (int) battleShip.getWorldCoordsFromLocalSize(size).x; int xCenterAbs = (int) (abs.x + sizeAbs / 2); int yCenterAbs = (int) (abs.y + sizeAbs / 2); - - Point msLoc = master.getMouseLocation(); - double targetRotation = -Math.atan2(xCenterAbs - msLoc.x, yCenterAbs - msLoc.y); + double targetRotationOld = -Math.atan2(xCenterAbs - msLoc.x, yCenterAbs - msLoc.y); + //---------------- rotation = ExMath.angleLerp(rotation, targetRotation, ROTATION_SPEED); - int barrelSpacing = sizeAbs / (barrelAmount + 1); + int barrelSpacing = (int) (size.x / (barrelAmount + 1)); for (int i = 0; i < barrelAmount; i++) { - int barrelX = (int) (abs.x + (i + 1) * barrelSpacing); - int frontPosY = (int) (abs.y - sizeAbs / 2); + int barrelX = (int) (position.x + (i + 1) * barrelSpacing); + int frontPosY = (int) (position.y - size.x / 2); if (master.isMousePressed()) { lastShot = System.currentTimeMillis(); Vector2D shellVel = Vector2D.getUnitVector(rotation).negative().multiply(SHELL_SPEED); - Vector2D pos = Vector2D.rotateAround(new Vector2D(xCenterAbs, yCenterAbs), new Vector2D(barrelX, frontPosY), rotation, Vector2D.COUNTERCLOCKWISE); + 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)); } diff --git a/target/classes/core/Master.class b/target/classes/core/Master.class index 420b9fb..e0c9a96 100644 Binary files a/target/classes/core/Master.class and b/target/classes/core/Master.class differ diff --git a/target/classes/objects/DebugPos.class b/target/classes/objects/DebugPos.class index f2867d2..8613add 100644 Binary files a/target/classes/objects/DebugPos.class and b/target/classes/objects/DebugPos.class differ diff --git a/target/classes/objects/ships/BattleShip.class b/target/classes/objects/ships/BattleShip.class index 7799251..56bb9a3 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/Shell.class b/target/classes/objects/ships/Shell.class index 91994d2..c86ceb8 100644 Binary files a/target/classes/objects/ships/Shell.class and b/target/classes/objects/ships/Shell.class differ diff --git a/target/classes/objects/ships/Turret.class b/target/classes/objects/ships/Turret.class index fddcfd6..59ca635 100644 Binary files a/target/classes/objects/ships/Turret.class and b/target/classes/objects/ships/Turret.class differ diff --git a/target/classes/objects/world/Grid.class b/target/classes/objects/world/Grid.class index 394b5da..c26fd1e 100644 Binary files a/target/classes/objects/world/Grid.class and b/target/classes/objects/world/Grid.class differ