diff --git a/src/main/java/core/Master.java b/src/main/java/core/Master.java index 02a91d5..516228c 100644 --- a/src/main/java/core/Master.java +++ b/src/main/java/core/Master.java @@ -63,7 +63,7 @@ public class Master extends JPanel { /** * The current width and height of the game area */ - private int w,h; + private int w, h; /** * Create a new master object @@ -171,13 +171,13 @@ public class Master extends JPanel { * @param obj The new object */ public void create(GameObject obj) { - create(obj, 0); + create(obj, obj.getLayer()); } /** * This method has to be called for every newly created GameObject * - * @param obj The new object + * @param obj The new object * @param renderLayer The render layer the object will be put on, 0 is below everything */ public void create(GameObject obj, int renderLayer) { @@ -191,7 +191,8 @@ public class Master extends JPanel { /** * Add a new Drawable to the render list - * @param d The drawable + * + * @param d The drawable * @param layer The layer it should be put on (>=0) */ public void addDrawable(Drawable d, int layer) { @@ -202,7 +203,7 @@ public class Master extends JPanel { } //layer exists check - int layerDif = layer - (drawables.size()-1); + int layerDif = layer - (drawables.size() - 1); if (layerDif > 0) { for (int i = 0; i < layerDif; i++) { drawables.add(new ArrayList<>()); @@ -227,13 +228,9 @@ public class Master extends JPanel { for (Collidable c : collidables) { double distance = Vector2D.distance(c.getCenterPos(), col.getCenterPos()); - 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 != col && (distance < c.getHitbox().getSize() + col.getHitbox().getSize())) { if (c.collidesWith(col)) { - System.err.println("COLL"); collides = true; } } @@ -251,8 +248,8 @@ public class Master extends JPanel { public void destroy(GameObject gameObject) { objects.remove(gameObject); - drawables.remove(gameObject); - if(gameObject instanceof Collidable){ + drawables.get(gameObject.getLayer()).remove(gameObject); + if (gameObject instanceof Collidable) { collidables.remove(gameObject); } } diff --git a/src/main/java/core/Coords.java b/src/main/java/core/math/Coords.java similarity index 95% rename from src/main/java/core/Coords.java rename to src/main/java/core/math/Coords.java index ca031eb..ad3e83d 100644 --- a/src/main/java/core/Coords.java +++ b/src/main/java/core/math/Coords.java @@ -1,5 +1,6 @@ -package core; +package core.math; +import core.Master; import core.math.Vector2D; /** @@ -7,7 +8,6 @@ import core.math.Vector2D; *

In this system, the screen is always 100 high and 100 * the screen ration wide. * This class is the used to convert these coordinates into the true Java 2D coordinates for the drawing

*/ -//TODO make this return a vector2d instead of the isX public class Coords { /** diff --git a/src/main/java/core/physics/Collidable.java b/src/main/java/core/physics/Collidable.java index 8d1c4cd..e1e173c 100644 --- a/src/main/java/core/physics/Collidable.java +++ b/src/main/java/core/physics/Collidable.java @@ -1,6 +1,7 @@ package core.physics; import core.math.Vector2D; +import core.physics.hitboxes.Hitbox; public interface Collidable { diff --git a/src/main/java/core/physics/Hitbox.java b/src/main/java/core/physics/hitboxes/Hitbox.java similarity index 56% rename from src/main/java/core/physics/Hitbox.java rename to src/main/java/core/physics/hitboxes/Hitbox.java index e3264a6..14f6028 100644 --- a/src/main/java/core/physics/Hitbox.java +++ b/src/main/java/core/physics/hitboxes/Hitbox.java @@ -1,12 +1,20 @@ -package core.physics; +package core.physics.hitboxes; import core.Drawable; import core.math.Vector2D; public abstract class Hitbox implements Drawable { + private final boolean isTrigger; + + protected Hitbox(boolean isTrigger) { + this.isTrigger = isTrigger; + } + public abstract void moveTo(Vector2D x1, Vector2D size); + public abstract double getSize(); + @Override public String toString() { return super.toString(); diff --git a/src/main/java/core/physics/RectHitBox.java b/src/main/java/core/physics/hitboxes/RectHitBox.java similarity index 81% rename from src/main/java/core/physics/RectHitBox.java rename to src/main/java/core/physics/hitboxes/RectHitBox.java index eb7d3a2..2cae2b9 100644 --- a/src/main/java/core/physics/RectHitBox.java +++ b/src/main/java/core/physics/hitboxes/RectHitBox.java @@ -1,6 +1,6 @@ -package core.physics; +package core.physics.hitboxes; -import core.Coords; +import core.math.Coords; import core.Master; import core.math.Vector2D; @@ -18,6 +18,22 @@ public class RectHitBox extends Hitbox { */ private Vector2D x1, y1, x2, y2; + /** + * Create a new RectHitbox with the position of the top left point {@code x1} and the size + * + * @param x1 The top left point + * @param size the size + * @param isTrigger Whether the hitbox is a trigger (only collision detection, not restricting movement + */ + public RectHitBox(Vector2D x1, Vector2D size, boolean isTrigger) { + super(isTrigger); + this.x1 = x1; + this.x2 = Vector2D.add(x1, new Vector2D(size.x, 0)); + this.y1 = Vector2D.add(x1, new Vector2D(0, size.y)); + this.y2 = Vector2D.add(x1, new Vector2D(size.x, size.y)); + Master.getMaster().addDrawable(this, 1); + } + /** * Create a new RectHitbox with the position of the top left point {@code x1} and the size * @@ -25,6 +41,7 @@ public class RectHitBox extends Hitbox { * @param size the size */ public RectHitBox(Vector2D x1, Vector2D size) { + this(x1, size, false); this.x1 = x1; this.x2 = Vector2D.add(x1, new Vector2D(size.x, 0)); this.y1 = Vector2D.add(x1, new Vector2D(0, size.y)); @@ -46,6 +63,12 @@ public class RectHitBox extends Hitbox { this.y2 = Vector2D.add(x1, new Vector2D(size.x, size.y)); } + @Override + public double getSize() { + Vector2D size = Vector2D.subtract(y2, x1); + return size.magnitude(); + } + /** * Check whether two hitboxes collide with each other * @@ -154,7 +177,7 @@ public class RectHitBox extends Hitbox { Vector2D abs = Coords.getWorldCoords(x1); Vector2D sizeAbs = Coords.getWorldCoordsSize(Vector2D.subtract(y2, x1)); - g2d.drawRect((int)abs.x, (int)abs.y, (int)sizeAbs.y, (int)sizeAbs.y); + 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/GameObject.java b/src/main/java/objects/GameObject.java index d9451c4..1d3320b 100644 --- a/src/main/java/objects/GameObject.java +++ b/src/main/java/objects/GameObject.java @@ -1,6 +1,6 @@ package objects; -import core.Coords; +import core.math.Coords; import core.Drawable; import core.Master; import core.math.Vector2D; @@ -23,6 +23,8 @@ public abstract class GameObject implements Drawable { protected Master master; + protected int layer; + public GameObject(double x, double y, double xSize, double ySize) { this(new Vector2D(x, y), new Vector2D(xSize, ySize)); } @@ -33,6 +35,7 @@ public abstract class GameObject implements Drawable { this.velocity = new Vector2D(); mainColor = Color.BLACK; this.master = Master.getMaster(); + this.layer = 0; } @@ -133,4 +136,8 @@ public abstract class GameObject implements Drawable { public Vector2D getCenterPosition() { return new Vector2D(position.x - size.x / 2, position.y - size.y / 2); } + + public int getLayer() { + return layer; + } } diff --git a/src/main/java/objects/ships/Submarine.java b/src/main/java/objects/ships/Submarine.java index 7bb43fa..57178a8 100644 --- a/src/main/java/objects/ships/Submarine.java +++ b/src/main/java/objects/ships/Submarine.java @@ -1,10 +1,10 @@ package objects.ships; -import core.Coords; +import core.math.Coords; import core.math.Vector2D; import core.physics.Collidable; -import core.physics.Hitbox; -import core.physics.RectHitBox; +import core.physics.hitboxes.Hitbox; +import core.physics.hitboxes.RectHitBox; import objects.GameObject; import java.awt.*; diff --git a/src/main/java/objects/world/Wall.java b/src/main/java/objects/world/Wall.java index 61e68c5..c42ea97 100644 --- a/src/main/java/objects/world/Wall.java +++ b/src/main/java/objects/world/Wall.java @@ -2,8 +2,8 @@ package objects.world; import core.math.Vector2D; import core.physics.Collidable; -import core.physics.Hitbox; -import core.physics.RectHitBox; +import core.physics.hitboxes.Hitbox; +import core.physics.hitboxes.RectHitBox; import objects.GameObject; import java.awt.*; diff --git a/src/test/java/core/physics/RectHitBoxTest.java b/src/test/java/core/physics/RectHitBoxTest.java index b31a5c5..3170cd7 100644 --- a/src/test/java/core/physics/RectHitBoxTest.java +++ b/src/test/java/core/physics/RectHitBoxTest.java @@ -1,6 +1,7 @@ package core.physics; import core.math.Vector2D; +import core.physics.hitboxes.RectHitBox; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; diff --git a/target/classes/core/Master.class b/target/classes/core/Master.class index a199731..6f26be7 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 58420b5..ef7541f 100644 Binary files a/target/classes/objects/GameObject.class and b/target/classes/objects/GameObject.class differ