diff --git a/src/main/java/core/Master.java b/src/main/java/core/Master.java index de6056d..596e1fb 100644 --- a/src/main/java/core/Master.java +++ b/src/main/java/core/Master.java @@ -76,7 +76,7 @@ public class Master extends JPanel { create(bs); create(battleShip); - create(new Submarine(new Vector2D(), new Vector2D(20, 20))); + create(new Submarine(new Vector2D(), new Vector2D(5, 5))); create(new Wall(20, 80, 50, 2)); } @@ -173,8 +173,14 @@ public class Master extends JPanel { for (Collidable c : collidables) { double distance = Vector2D.distance(c.getCenterPos(), col.getCenterPos()); - if (!(distance > c.getSize().magnitude() && distance > col.getSize().magnitude())) { + + if (c != col /*&& !(distance > c.getSize().magnitude() && distance > col.getSize().magnitude())*/) { + + System.out.println("\n\nCOLL POSSIBLE"); + System.out.println("This: " + c.getHitbox() + "\n\nother: " + col.getHitbox()); + if (c.collidesWith(col)) { + System.err.println("COLL"); collides = true; } } diff --git a/src/main/java/core/physics/Hitbox.java b/src/main/java/core/physics/Hitbox.java index 5d0717d..415bfa8 100644 --- a/src/main/java/core/physics/Hitbox.java +++ b/src/main/java/core/physics/Hitbox.java @@ -1,8 +1,13 @@ package core.physics; import core.Drawable; +import core.math.Vector2D; import objects.GameObject; public abstract class Hitbox implements Drawable { + @Override + public String toString() { + return super.toString(); + } } diff --git a/src/main/java/objects/GameObject.java b/src/main/java/objects/GameObject.java index 218b1cb..1d5a402 100644 --- a/src/main/java/objects/GameObject.java +++ b/src/main/java/objects/GameObject.java @@ -79,6 +79,24 @@ public abstract class GameObject implements Drawable { g2d.fillRect(xAbs, yAbs, sizeXAbs, sizeYAbs); } + /** + * This method draws a rectangle at the current position and size + * + * @param g2d The Graphics2D object provided by the master + * @param w The width of the screen + */ + public void drawOval(Graphics2D g2d, int w) { + this.w = w; + h = (int) (this.w / Master.SCREEN_RATIO); + int xAbs = (int) getWorldCoords(position.x, true); + int yAbs = (int) getWorldCoords(position.y, false); + int sizeXAbs = (int) getWorldCoordsSize(size.x, true); + int sizeYAbs = (int) getWorldCoordsSize(size.y, false); + + g2d.setPaint(mainColor); + g2d.fillOval(xAbs, yAbs, sizeXAbs, sizeYAbs); + } + /** * This method draws a rounded rectangle at the current position and size * @@ -107,6 +125,12 @@ public abstract class GameObject implements Drawable { } } + public Vector2D getMapCoordsFromWorld(Vector2D value){ + double x = (value.x / w) * Master.SCREEN_Y_COORDINATES * Master.SCREEN_RATIO; + double y = (value.y / h) * Master.SCREEN_Y_COORDINATES; + return new Vector2D(x, y); + } + public double getWorldCoordsSize(double value, boolean isX) { if (isX) { return (value / Master.SCREEN_Y_COORDINATES * w); @@ -141,6 +165,6 @@ public abstract class GameObject implements Drawable { } public Vector2D getCenterPosition(){ - return new Vector2D(position.x + size.x, position.y + size.y); + return new Vector2D(position.x - size.x/2, position.y - size.y/2); } } diff --git a/src/main/java/objects/ships/Submarine.java b/src/main/java/objects/ships/Submarine.java index 819ef3a..9db5194 100644 --- a/src/main/java/objects/ships/Submarine.java +++ b/src/main/java/objects/ships/Submarine.java @@ -16,18 +16,21 @@ public class Submarine extends GameObject implements Collidable { public Submarine(Vector2D position, Vector2D size) { super(position, size); this.hitbox = new RectHitBox(position, size); + this.mainColor = Color.BLUE; } @Override public void draw(Graphics2D g2d, int w, Master master) { g2d.setPaint(Color.BLUE); - g2d.fillOval((int) (position.x - size.x / 2), (int) (position.y - size.y / 2), (int) size.x, (int) size.y); + drawOval(g2d, w); } @Override public void update(Master master) { Point mouse = master.getMouseLocation(); - moveTo(new Vector2D(mouse.x, mouse.y), master); + Vector2D relPos = getMapCoordsFromWorld(new Vector2D(mouse.x, mouse.y)); + Vector2D centerRelPos = new Vector2D(relPos.x - size.x/2, relPos.y - size.y/2); + moveTo(centerRelPos, master); } @Override diff --git a/target/classes/core/Master.class b/target/classes/core/Master.class index 4b736b9..8cfb839 100644 Binary files a/target/classes/core/Master.class and b/target/classes/core/Master.class differ diff --git a/target/classes/objects/GameObject.class b/target/classes/objects/GameObject.class index 8af8567..3555019 100644 Binary files a/target/classes/objects/GameObject.class and b/target/classes/objects/GameObject.class differ