diff --git a/src/main/java/core/general/Master.java b/src/main/java/core/general/Master.java index c964354..5d198c1 100644 --- a/src/main/java/core/general/Master.java +++ b/src/main/java/core/general/Master.java @@ -122,6 +122,16 @@ public class Master extends JPanel { create(new DebugPos(pos, new Vector2D(2, 2)), 3); } + /** + * Debug a position, creates a green dot at the position + * + * @param pos The position + * @param lifeTime The lifetime of the {@code DebugPos} in ms + */ + public void debugPos(Vector2D pos, long lifeTime) { + create(new DebugPos(pos, new Vector2D(2, 2), lifeTime), 3); + } + /** * This method is the entry method for each frame. It handles everything about the frame diff --git a/src/main/java/core/math/Vector2D.java b/src/main/java/core/math/Vector2D.java index caa05ab..2aa5844 100644 --- a/src/main/java/core/math/Vector2D.java +++ b/src/main/java/core/math/Vector2D.java @@ -31,8 +31,7 @@ public class Vector2D { * Create a new empty Vector2D object */ public Vector2D() { - x = 0; - y = 0; + this(0, 0); } /** @@ -44,7 +43,6 @@ public class Vector2D { return new Vector2D(point.x, point.y); } - /** * Add another Vector to this vector, modifies this object * @@ -58,9 +56,9 @@ public class Vector2D { } /** - * Multiply this vector with a simple scalar, modifies this object + * Multiply this vector with a simple factor, modifies this object * - * @param a The scalar + * @param a The multiplying factor * @return this after the multiplication */ public Vector2D multiply(double a) { @@ -101,6 +99,14 @@ public class Vector2D { return new Vector2D(-x, -y); } + /** + * Get the rotation of the {@code Vector2D} in radians + * @return the rotation in radians + */ + public double getRotation() { + return Math.atan2(y, x); + } + /** * Add two Vectors @@ -183,11 +189,21 @@ public class Vector2D { return rotateAround(new Vector2D(), new Vector2D(0, 1), direction); } + /** + * Get the distance between two {@code Vector2D} + * @param a The first {@code Vector2D} + * @param b The second {@code Vector2D} + * @return The distance between the 2 {@code Vector2D} + */ public static double distance(Vector2D a, Vector2D b) { Vector2D dif = subtract(a, b); return Math.sqrt(dif.x * dif.x + dif.y + dif.y); } + public static Vector2D zero() { + return new Vector2D(0, 0); + } + /** * Copy this object @@ -222,8 +238,4 @@ public class Vector2D { if (Double.compare(vector2D.x, x) != 0) return false; return Double.compare(vector2D.y, y) == 0; } - - public double getRotation() { - return Math.atan2(y, x); - } } \ No newline at end of file diff --git a/src/main/java/core/objects/base/DebugPos.java b/src/main/java/core/objects/base/DebugPos.java index 5dd25e1..f6b39c3 100644 --- a/src/main/java/core/objects/base/DebugPos.java +++ b/src/main/java/core/objects/base/DebugPos.java @@ -1,7 +1,9 @@ package core.objects.base; +import core.math.Coordinates; import core.math.Vector2D; import core.objects.core.GameObject; +import core.renderer.CustomRenderer; import java.awt.*; @@ -22,11 +24,20 @@ public class DebugPos extends GameObject { this.velocity = new Vector2D(); this.mainColor = Color.GREEN; this.lifeTime = lifeTime; - } + this.spawnTime = System.currentTimeMillis(); + this.layer = 3; - @Override - public void draw(Graphics2D g2d) { - drawOval(g2d, "center"); + this.setRenderer(new CustomRenderer(Color.GREEN, this) { + @Override + public void draw(Graphics2D g2d) { + Vector2D positionAbs = Coordinates.getWorldCoordinates(getMapPosition()); + g2d.setColor(color); + g2d.setStroke(new BasicStroke((int) Coordinates.getWorldCoordinates(new Vector2D(0.5, 0)).x, BasicStroke.CAP_BUTT, + BasicStroke.JOIN_BEVEL)); + g2d.drawLine((int)positionAbs.x - 20, (int)positionAbs.y, (int)positionAbs.x + 20, (int)positionAbs.y); + g2d.drawLine((int)positionAbs.x, (int)positionAbs.y-20, (int)positionAbs.x, (int)positionAbs.y+20); + } + }); } @Override diff --git a/src/main/java/core/objects/core/CollGameObject.java b/src/main/java/core/objects/core/CollGameObject.java index b345994..7ec6ae5 100644 --- a/src/main/java/core/objects/core/CollGameObject.java +++ b/src/main/java/core/objects/core/CollGameObject.java @@ -5,6 +5,7 @@ import core.objects.core.GameObject; import core.physics.Collidable; import core.physics.Collision; import core.physics.hitboxes.Hitbox; +import core.renderer.Renderer; import java.util.ArrayList; diff --git a/src/main/java/core/objects/core/GameObject.java b/src/main/java/core/objects/core/GameObject.java index 3b26b43..3ebf025 100644 --- a/src/main/java/core/objects/core/GameObject.java +++ b/src/main/java/core/objects/core/GameObject.java @@ -4,6 +4,7 @@ import core.math.Coordinates; import core.renderer.Drawable; import core.general.Master; import core.math.Vector2D; +import core.renderer.Renderer; import java.awt.*; @@ -28,6 +29,9 @@ public abstract class GameObject implements Drawable { protected GameObject parent; + private Renderer renderer; + + @Deprecated public GameObject(double x, double y, double xSize, double ySize) { this(new Vector2D(x, y), new Vector2D(xSize, ySize)); } @@ -60,6 +64,11 @@ public abstract class GameObject implements Drawable { */ public abstract void update(); + @Override + public void draw(Graphics2D g2d) { + renderer.draw(g2d); + } + /** * A simple method to move the object to a Vector2D. This method should be called instead of doing it manually. * @@ -95,15 +104,6 @@ public abstract class GameObject implements Drawable { g2d.fillOval((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y); } - /** - * 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) { - drawOval(g2d, ""); - } - /** * This method draws an oval at the current position and size with arguments * @@ -126,29 +126,6 @@ public abstract class GameObject implements Drawable { 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 - * - * @param g2d The Graphics2D object provided by the master - * @param arcW The arc width of the rectangle - * @param arcH The arc height of the rectangle - */ - public void drawRoundRect(Graphics2D g2d, int arcW, int arcH) { - - Vector2D abs = Coordinates.getWorldCoordinates(position); - Vector2D sizeAbs = Coordinates.getWorldCoordinates(size); - - int xCenterAbs = (int) (abs.x + sizeAbs.x / 2); - int yCenterAbs = (int) (abs.y + sizeAbs.y / 2); - - g2d.setPaint(mainColor); - - g2d.rotate(rotation, xCenterAbs, yCenterAbs); - g2d.fillRoundRect((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y, arcW, arcH); - - g2d.rotate(-rotation, xCenterAbs, yCenterAbs); - } - /** * Destroy this {@code GameObject} */ @@ -156,15 +133,15 @@ public abstract class GameObject implements Drawable { master.destroy(this); } - /** * Returns the value as map coords * @param value The value relative to the parent * @return The value in global map coordinates */ public Vector2D getMapCoords(Vector2D value) { - double x = parent.position.x + value.x; - double y = parent.position.y + value.y; + Vector2D rotated = Vector2D.rotateAround(parent.getCenterPositionLocal(), value, parent.rotation); + double x = parent.position.x + rotated.x; + double y = parent.position.y + rotated.y; return parent.getMapCoords(new Vector2D(x, y)); } @@ -186,6 +163,14 @@ public abstract class GameObject implements Drawable { return new Vector2D(position.x + size.x / 2, position.y + size.y / 2); } + /** + * Get the local center position of the object + * @return The center position + */ + public Vector2D getCenterPositionLocal(){ + return new Vector2D(size.x / 2, size.y / 2); + } + /** * Get the render layer of the object * @return The render layer @@ -201,4 +186,26 @@ public abstract class GameObject implements Drawable { protected Vector2D getV2DRotation(){ return Vector2D.getUnitVector(rotation); } + + //----------- Getters and setters ----------------- + + public Vector2D getMapPosition(){ + return getMapCoords(position); + } + + public Vector2D getPosition() { + return position; + } + + public double getRotation() { + return rotation; + } + + public Vector2D getSize() { + return size; + } + + protected void setRenderer(Renderer renderer){ + this.renderer = renderer; + } } diff --git a/src/main/java/core/objects/core/MapAnchor.java b/src/main/java/core/objects/core/MapAnchor.java index 168da7b..7b78961 100644 --- a/src/main/java/core/objects/core/MapAnchor.java +++ b/src/main/java/core/objects/core/MapAnchor.java @@ -15,12 +15,10 @@ public class MapAnchor extends GameObject { @Override public void draw(Graphics2D g2d) { - } @Override public void update() { - } @Override diff --git a/src/main/java/core/renderer/CircleRenderer.java b/src/main/java/core/renderer/CircleRenderer.java index a7af285..76aa7c7 100644 --- a/src/main/java/core/renderer/CircleRenderer.java +++ b/src/main/java/core/renderer/CircleRenderer.java @@ -1,14 +1,46 @@ package core.renderer; +import core.math.Coordinates; +import core.math.Vector2D; +import core.objects.core.GameObject; + import java.awt.*; -public class CircleRenderer extends Renderer{ +/** + * A simple {@code Renderer} that renders a filled circle + */ +public class CircleRenderer extends Renderer { + /** + * The radius of the circle + */ private double radius; - private Color color; + + /** + * The constructor for the circle + * @param object The {@code GameObject} the {@code Renderer} is assigned to + * @param color The {@code Color} of the circle + * @param radius The radius of the circle + */ + public CircleRenderer(GameObject object, Color color, double radius) { + super(color, object); + this.radius = radius; + } @Override public void draw(Graphics2D g2d) { + Vector2D abs = Coordinates.getWorldCoordinates(object.getMapPosition()); + Vector2D sizeAbs = Coordinates.getWorldCoordinates(new Vector2D(radius * 2, radius * 2)); + g2d.setPaint(color); + g2d.fillOval((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y); + } + + public double getRadius() { + return radius; + } + + public void setRadius(double radius) { + this.radius = radius; } } diff --git a/src/main/java/core/renderer/ComplexRenderer.java b/src/main/java/core/renderer/ComplexRenderer.java index 9f42080..d4c5ce8 100644 --- a/src/main/java/core/renderer/ComplexRenderer.java +++ b/src/main/java/core/renderer/ComplexRenderer.java @@ -1,14 +1,26 @@ package core.renderer; +import core.objects.core.GameObject; + import java.awt.*; import java.util.ArrayList; +import java.util.Arrays; +/** + * The {@code ComplexRenderer} consists of multiple {@code Renderers} + *
The {@code Renderer} at index 0 gets rendered first
+ */ public class ComplexRenderer extends Renderer{ private ArrayList