diff --git a/src/main/java/core/Coords.java b/src/main/java/core/Coords.java index 57aaa50..ca031eb 100644 --- a/src/main/java/core/Coords.java +++ b/src/main/java/core/Coords.java @@ -41,7 +41,6 @@ public class Coords { } public static Vector2D getWorldCoordsSize(Vector2D value) { - //TODO h not w 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/Master.java b/src/main/java/core/Master.java index 6aa2585..02a91d5 100644 --- a/src/main/java/core/Master.java +++ b/src/main/java/core/Master.java @@ -41,8 +41,9 @@ public class Master extends JPanel { /** * All GameObjects that can be drawn + * Has different render layers, 0 is on the bottom */ - private final ArrayList drawables; + private final ArrayList> drawables; /** * All physics objects that exist @@ -74,6 +75,7 @@ public class Master extends JPanel { objectBuffer = new ArrayList<>(); collidables = new ArrayList<>(); drawables = new ArrayList<>(); + drawables.add(new ArrayList<>()); create(new Grid()); @@ -111,7 +113,7 @@ public class Master extends JPanel { Graphics2D g2d = (Graphics2D) g.create(); - drawables.forEach(o -> o.draw(g2d)); + drawables.forEach(l -> l.forEach(o -> o.draw(g2d))); } @@ -169,16 +171,45 @@ public class Master extends JPanel { * @param obj The new object */ public void create(GameObject obj) { + create(obj, 0); + } + + /** + * This method has to be called for every newly created GameObject + * + * @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) { objectBuffer.add(obj); if (obj instanceof Collidable) { collidables.add((Collidable) obj); } - drawables.add(obj); + addDrawable(obj, renderLayer); } - public void addDrawable(Drawable d) { - drawables.add(d); + /** + * Add a new Drawable to the render list + * @param d The drawable + * @param layer The layer it should be put on (>=0) + */ + public void addDrawable(Drawable d, int layer) { + + if (layer < 0) { + + throw new IllegalArgumentException("Layer must be at least 9"); + } + + //layer exists check + int layerDif = layer - (drawables.size()-1); + if (layerDif > 0) { + for (int i = 0; i < layerDif; i++) { + drawables.add(new ArrayList<>()); + } + } + + drawables.get(layer).add(d); } diff --git a/src/main/java/core/physics/Collision.java b/src/main/java/core/physics/Collision.java index ecf23a9..491755d 100644 --- a/src/main/java/core/physics/Collision.java +++ b/src/main/java/core/physics/Collision.java @@ -1,5 +1,8 @@ package core.physics; +/** + * This class stores information about a collision + */ public class Collision { Collidable a; @@ -13,10 +16,18 @@ public class Collision { this.haveCollided = haveCollided; } + /** + * Get the Collidable a, the one that called the collision testing + * @return The Collidable a + */ public Collidable getA() { return a; } + /** + * Get the Collidable b, the one that a collided width + * @return The Collidable a + */ public Collidable getB() { return b; } diff --git a/src/main/java/core/physics/Hitbox.java b/src/main/java/core/physics/Hitbox.java index 415bfa8..e3264a6 100644 --- a/src/main/java/core/physics/Hitbox.java +++ b/src/main/java/core/physics/Hitbox.java @@ -2,10 +2,11 @@ package core.physics; import core.Drawable; import core.math.Vector2D; -import objects.GameObject; public abstract class Hitbox implements Drawable { + public abstract void moveTo(Vector2D x1, Vector2D size); + @Override public String toString() { return super.toString(); diff --git a/src/main/java/core/physics/RectHitBox.java b/src/main/java/core/physics/RectHitBox.java index 4f68345..eb7d3a2 100644 --- a/src/main/java/core/physics/RectHitBox.java +++ b/src/main/java/core/physics/RectHitBox.java @@ -1,5 +1,6 @@ package core.physics; +import core.Coords; import core.Master; import core.math.Vector2D; @@ -10,8 +11,6 @@ import java.awt.*; */ public class RectHitBox extends Hitbox { - int w,h; - /** * The corners of the rectangle like this: *

x1 x2

@@ -30,14 +29,16 @@ public class RectHitBox extends Hitbox { 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); } /** * Move the hitbox to a new position * - * @param x1 - * @param size + * @param x1 The new position + * @param size The new size */ + @Override public void moveTo(Vector2D x1, Vector2D size) { this.x1 = x1.copy(); this.x2 = Vector2D.add(x1, new Vector2D(size.x, 0)); @@ -105,8 +106,7 @@ public class RectHitBox extends Hitbox { } } - boolean coll = !(allAbove || allBelow || allLeft || allRight); - return coll; + return !(allAbove || allBelow || allLeft || allRight); } /** @@ -151,31 +151,10 @@ public class RectHitBox extends Hitbox { @Override public void draw(Graphics2D g2d) { - this.w = w; - int h = (int) (w / Master.SCREEN_RATIO); - int xAbs = (int) getWorldCoords(x1.x, true); - int yAbs = (int) getWorldCoords(x1.y, false); - int sizeXAbs = (int) getWorldCoordsSize(x2.x - x1.x, true); - int sizeYAbs = (int) getWorldCoordsSize(y1.y - x1.y, false); + Vector2D abs = Coords.getWorldCoords(x1); + Vector2D sizeAbs = Coords.getWorldCoordsSize(Vector2D.subtract(y2, x1)); - g2d.fillRect(xAbs, yAbs, sizeXAbs, sizeYAbs); + g2d.drawRect((int)abs.x, (int)abs.y, (int)sizeAbs.y, (int)sizeAbs.y); g2d.setPaint(Color.MAGENTA); } - - - public double getWorldCoords(double value, boolean isX) { - if (isX) { - return (value / (Master.SCREEN_Y_COORDINATES * Master.SCREEN_RATIO) * w); - } else { - return (value / Master.SCREEN_Y_COORDINATES * h); - } - } - - public double getWorldCoordsSize(double value, boolean isX) { - if (isX) { - return (value / Master.SCREEN_Y_COORDINATES * w); - } else { - return (value / Master.SCREEN_Y_COORDINATES * h); - } - } } diff --git a/src/main/java/objects/GameObject.java b/src/main/java/objects/GameObject.java index bb13948..d9451c4 100644 --- a/src/main/java/objects/GameObject.java +++ b/src/main/java/objects/GameObject.java @@ -37,7 +37,7 @@ public abstract class GameObject implements Drawable { /** - *

The update method is called every frame before the {@link #draw(Graphics2D, int, Master)} method by the master object on each object. Everything + *

The update method is called every frame before the {@link #draw(Graphics2D)} method by the master object on each object. Everything * that is needed for the game to work should be here in this method.

*

No drawing should be made in this method. The {@code debug} method can be called on the master.

*

This function is NOT intended to be called manually.

@@ -50,12 +50,14 @@ public abstract class GameObject implements Drawable { * @param target The target position */ public void moveTo(Vector2D target) { - Vector2D oldPos = position; + Vector2D oldPos = position.copy(); this.position = target; if (this instanceof Collidable) { + ((Collidable) this).getHitbox().moveTo(position, size); if (master.doesCollide((Collidable) this)) { this.position = oldPos; + ((Collidable) this).getHitbox().moveTo(oldPos, size); } } } diff --git a/target/classes/core/Master.class b/target/classes/core/Master.class index dde115f..a199731 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 967cd7a..58420b5 100644 Binary files a/target/classes/objects/GameObject.class and b/target/classes/objects/GameObject.class differ