diff --git a/src/main/java/core/Coords.java b/src/main/java/core/Coords.java new file mode 100644 index 0000000..6d77a44 --- /dev/null +++ b/src/main/java/core/Coords.java @@ -0,0 +1,50 @@ +package core; + +import core.math.Vector2D; + +/** + * This class provides everything about the local coordinate system the game uses + *
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 { + + /** + * This utility class should not be instantiated + */ + private Coords() { + } + + /** + * The master object used to get the screen size + */ + public static Master master = Master.getMaster(); + + + public static Vector2D getWorldCoords(Vector2D value) { + double x = (value.x / (Master.SCREEN_Y_COORDINATES * Master.SCREEN_RATIO) * master.getW()); + double y = (value.y / Master.SCREEN_Y_COORDINATES * master.getH()); + return new Vector2D(x, y); + } + + /** + * Get local map coordinates (h=100) from the absolute Java 2D coordinates + * + * @param value The Vector2D position in Java 2D coordinates + * @return The Vector2D position in map coordinates + */ + public static Vector2D getMapCoordsFromWorld(Vector2D value) { + double x = (value.x / master.getW()) * Master.SCREEN_Y_COORDINATES * Master.SCREEN_RATIO; + double y = (value.y / master.getH()) * Master.SCREEN_Y_COORDINATES; + return new Vector2D(x, y); + } + + public static Vector2D getWorldCoordsSize(Vector2D value) { + //TODO h not w + double x = (value.x / Master.SCREEN_Y_COORDINATES * master.getW()); + double y = (value.y / Master.SCREEN_Y_COORDINATES * master.getH()); + return new Vector2D(x, y); + } + +} diff --git a/src/main/java/core/Drawable.java b/src/main/java/core/Drawable.java index 97e36e3..ddef1bf 100644 --- a/src/main/java/core/Drawable.java +++ b/src/main/java/core/Drawable.java @@ -3,6 +3,7 @@ package core; import java.awt.*; public interface Drawable { + /** *The draw method is called every frame after the update by the master object on each object. Everything * about drawing should be handled here in this method.
diff --git a/src/main/java/core/Master.java b/src/main/java/core/Master.java index 596e1fb..22203db 100644 --- a/src/main/java/core/Master.java +++ b/src/main/java/core/Master.java @@ -14,8 +14,6 @@ import java.awt.*; import java.util.ArrayList; import java.util.concurrent.atomic.AtomicBoolean; -//TODO master better accessible or something - /** * The main object that controls everything */ @@ -31,8 +29,10 @@ public class Master extends JPanel { */ public static final double SCREEN_Y_COORDINATES = 100d; - @Deprecated - public static final double SCREEN_X_COORDINATES = 100d * SCREEN_RATIO; + /** + * The master object + */ + private static Master master; /** * All GameObjects that exist @@ -54,12 +54,22 @@ public class Master extends JPanel { */ private final ArrayList* This method can now be trusted * - * @param center The center of the rotation - * @param value The point to be rotated, absolut to the center - * @param rotation The rotation angle in radians + * @param center The center of the rotation + * @param value The point to be rotated, absolut to the center + * @param rotation The rotation angle in radians * @param rotationDirection The direction, -1 for clockwise, 1 for counterclockwise * @return The rotated point */ - public static Vector2D rotateAround(Vector2D center, Vector2D value, double rotation, int rotationDirection){ + public static Vector2D rotateAround(Vector2D center, Vector2D value, double rotation, int rotationDirection) { Vector2D dif = Vector2D.subtract(value, center); @@ -135,29 +137,30 @@ public class Vector2D { /** * Rotate a point around another point - * + *
* This method can now be trusted * - * @param center The center of the rotation - * @param value The point to be rotated, absolut to the center + * @param center The center of the rotation + * @param value The point to be rotated, absolut to the center * @param rotation The rotation angle in radians * @return The rotated point */ - public static Vector2D rotateAround(Vector2D center, Vector2D value, double rotation){ + public static Vector2D rotateAround(Vector2D center, Vector2D value, double rotation) { return rotateAround(center, value, rotation, COUNTERCLOCKWISE); } /** * Get a unit vector with magnitude 1 and the direction + * * @param direction The direction of the vector * @return The unit vector */ - public static Vector2D getUnitVector(double direction){ + public static Vector2D getUnitVector(double direction) { return rotateAround(new Vector2D(), new Vector2D(0, 1), direction); } - public static double distance(Vector2D a, Vector2D b){ + public static double distance(Vector2D a, Vector2D b) { Vector2D dif = subtract(a, b); return Math.sqrt(dif.x * dif.x + dif.y + dif.y); } @@ -165,9 +168,10 @@ public class Vector2D { /** * Copy this object + * * @return A copy of this object */ - public Vector2D copy(){ + public Vector2D copy() { return new Vector2D(x, y); } diff --git a/src/main/java/core/physics/Collision.java b/src/main/java/core/physics/Collision.java new file mode 100644 index 0000000..ecf23a9 --- /dev/null +++ b/src/main/java/core/physics/Collision.java @@ -0,0 +1,27 @@ +package core.physics; + +public class Collision { + + Collidable a; + Collidable b; + + boolean haveCollided; + + public Collision(Collidable a, Collidable b, boolean haveCollided) { + this.a = a; + this.b = b; + this.haveCollided = haveCollided; + } + + public Collidable getA() { + return a; + } + + public Collidable getB() { + return b; + } + + public boolean isHaveCollided() { + return haveCollided; + } +} diff --git a/src/main/java/core/physics/RectHitBox.java b/src/main/java/core/physics/RectHitBox.java index b295090..0b4cc14 100644 --- a/src/main/java/core/physics/RectHitBox.java +++ b/src/main/java/core/physics/RectHitBox.java @@ -10,6 +10,8 @@ import java.awt.*; */ public class RectHitBox extends Hitbox { + int w,h; + /** * The corners of the rectangle like this: *
x1 x2
@@ -148,7 +150,32 @@ public class RectHitBox extends Hitbox { @Override public void draw(Graphics2D g2d, int w, Master master) { - g2d.setPaint(Color.BLUE); - g2d.fillRect((int) x1.y, (int) x1.y, (int) (x2.x - x1.x), (int) (y1.y - x1.y)); + + 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); + + g2d.fillRect(xAbs, yAbs, sizeXAbs, sizeYAbs); + 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/DebugPos.java b/src/main/java/objects/DebugPos.java index 4591fb2..e731685 100644 --- a/src/main/java/objects/DebugPos.java +++ b/src/main/java/objects/DebugPos.java @@ -21,6 +21,6 @@ public class DebugPos extends GameObject { } @Override - public void update(Master master) { + public void update() { } } \ No newline at end of file diff --git a/src/main/java/objects/GameObject.java b/src/main/java/objects/GameObject.java index 1d5a402..b6420f6 100644 --- a/src/main/java/objects/GameObject.java +++ b/src/main/java/objects/GameObject.java @@ -1,5 +1,6 @@ package objects; +import core.Coords; import core.Drawable; import core.Master; import core.math.Vector2D; @@ -9,7 +10,7 @@ import java.awt.*; /** * The GameObject class is the superclass of every GameObject that can be displayed on screen. It has the 2 - * {@link #update(Master)} and {@link #draw(Graphics2D, int, Master)} methods that have to be + * {@link #update()} and {@link #draw(Graphics2D, int, Master)} methods that have to be */ public abstract class GameObject implements Drawable { @@ -23,6 +24,8 @@ public abstract class GameObject implements Drawable { protected Color mainColor; + protected Master master; + public GameObject(double x, double y, double xSize, double ySize) { this(new Vector2D(x, y), new Vector2D(xSize, ySize)); } @@ -32,6 +35,7 @@ public abstract class GameObject implements Drawable { this.size = size; this.velocity = new Vector2D(); mainColor = Color.BLACK; + this.master = Master.getMaster(); } @@ -40,17 +44,15 @@ public abstract class GameObject implements Drawable { * 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.
- * - * @param master The master object itself */ - public abstract void update(Master master); + public abstract void update(); /** * A simple method to move the object to a Vector2D. This method should be called instead of doing it manually. * * @param target The target position */ - public void moveTo(Vector2D target, Master master) { + public void moveTo(Vector2D target) { Vector2D oldPos = position; this.position = target; @@ -70,13 +72,11 @@ public abstract class GameObject implements Drawable { public void drawRect(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); + Vector2D abs = Coords.getWorldCoords(position); + Vector2D sizeAbs = Coords.getWorldCoordsSize(size); g2d.setPaint(mainColor); - g2d.fillRect(xAbs, yAbs, sizeXAbs, sizeYAbs); + g2d.fillRect((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y); } /** @@ -88,13 +88,11 @@ public abstract class GameObject implements Drawable { 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); + Vector2D abs = Coords.getWorldCoords(position); + Vector2D sizeAbs = Coords.getWorldCoordsSize(size); g2d.setPaint(mainColor); - g2d.fillOval(xAbs, yAbs, sizeXAbs, sizeYAbs); + g2d.fillOval((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y); } /** @@ -108,63 +106,41 @@ public abstract class GameObject implements Drawable { public void drawRoundRect(Graphics2D g2d, int w, int arcW, int arcH) { this.w = w; h = (int) (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); + Vector2D abs = Coords.getWorldCoords(position); + Vector2D sizeAbs = Coords.getWorldCoordsSize(size); g2d.setPaint(mainColor); - g2d.fillRoundRect(xAbs, yAbs, sizeXAbs, sizeYAbs, arcW, arcH); + g2d.fillRoundRect((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y, arcW, arcH); } - 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 void destroy() { + master.destroy(this); } - 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; + + public Vector2D getMapCoords(Vector2D value) { + double x = (position.x + value.x / 100d * size.x); + double y = (position.y + value.y / 100d * size.y); return new Vector2D(x, y); + } - public double getWorldCoordsSize(double value, boolean isX) { - if (isX) { - return (value / Master.SCREEN_Y_COORDINATES * w); - } else { - return (value / Master.SCREEN_Y_COORDINATES * h); - } + public Vector2D getMapCoordsSize(Vector2D value) { + double x = (value.x / 100d * size.x); + double y = (value.y / 100d * size.y); + return new Vector2D(x, y); + } - - public double getMapCoords(double value, boolean isX) { - if (isX) { - return (position.x + value / 100d * size.x); - } else { - return (position.y + value / 100d * size.y); - } + public Vector2D getWorldCoordsFromLocal(Vector2D value) { + return Coords.getWorldCoords(getMapCoords(value)); } - public double getMapCoordsSize(double value, boolean useX) { - if (useX) { - return (value / 100d * size.x); - } else { - return (value / 100d * size.y); - } + public Vector2D getWorldCoordsFromLocalSize(Vector2D value) { + return Coords.getWorldCoordsSize(getMapCoordsSize(value)); } - public int getWorldCoordsFromLocal(double value, boolean isX) { - return (int) getWorldCoords(getMapCoords(value, isX), isX); - } - - public int getWorldCoordsFromLocalSize(double value, boolean useX) { - return (int) getWorldCoordsSize(getMapCoordsSize(value, useX), useX); - } - - public Vector2D getCenterPosition(){ - 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); } } diff --git a/src/main/java/objects/ships/BattleShip.java b/src/main/java/objects/ships/BattleShip.java index cbcb6b2..55328d4 100644 --- a/src/main/java/objects/ships/BattleShip.java +++ b/src/main/java/objects/ships/BattleShip.java @@ -35,8 +35,8 @@ public class BattleShip extends GameObject { } @Override - public void update(Master master) { - turrets.forEach((turret -> turret.update(master))); + public void update() { + turrets.forEach(Turret::update); } public void addTurret(Turret turret){ diff --git a/src/main/java/objects/ships/Shell.java b/src/main/java/objects/ships/Shell.java index d6344c8..830f7a9 100644 --- a/src/main/java/objects/ships/Shell.java +++ b/src/main/java/objects/ships/Shell.java @@ -21,7 +21,7 @@ public class Shell extends GameObject { } @Override - public void update(Master master) { + public void update() { position.add(velocity); } } diff --git a/src/main/java/objects/ships/Submarine.java b/src/main/java/objects/ships/Submarine.java index 9db5194..80215a0 100644 --- a/src/main/java/objects/ships/Submarine.java +++ b/src/main/java/objects/ships/Submarine.java @@ -1,5 +1,6 @@ package objects.ships; +import core.Coords; import core.Master; import core.math.Vector2D; import core.physics.Collidable; @@ -26,11 +27,11 @@ public class Submarine extends GameObject implements Collidable { } @Override - public void update(Master master) { + public void update() { Point mouse = master.getMouseLocation(); - Vector2D relPos = getMapCoordsFromWorld(new Vector2D(mouse.x, mouse.y)); + Vector2D relPos = Coords.getMapCoordsFromWorld(new Vector2D(mouse.x, mouse.y)); Vector2D centerRelPos = new Vector2D(relPos.x - size.x/2, relPos.y - size.y/2); - moveTo(centerRelPos, master); + moveTo(centerRelPos); } @Override diff --git a/src/main/java/objects/ships/Turret.java b/src/main/java/objects/ships/Turret.java index 6db9e39..acd4d1c 100644 --- a/src/main/java/objects/ships/Turret.java +++ b/src/main/java/objects/ships/Turret.java @@ -45,26 +45,25 @@ public class Turret extends GameObject { public void draw(Graphics2D g2d, int w, Master master) { h = w / 16 * 9; g2d.setPaint(mainColor); - int xAbs = battleShip.getWorldCoordsFromLocal(position.x, true); - int yAbs = battleShip.getWorldCoordsFromLocal(position.y, false); - int sizeAbs = battleShip.getWorldCoordsFromLocalSize(size.x, true); - int xCenterAbs = xAbs + sizeAbs / 2; - int yCenterAbs = yAbs + sizeAbs / 2; + 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); - g2d.fillOval(xAbs, yAbs, sizeAbs, sizeAbs); + g2d.fillOval((int) abs.x, (int) abs.y, (int) sizeAbs, (int) sizeAbs); - g2d.setStroke(new BasicStroke(battleShip.getWorldCoordsFromLocalSize(10, true), BasicStroke.CAP_BUTT, + g2d.setStroke(new BasicStroke((int) battleShip.getWorldCoordsFromLocalSize(new Vector2D(10, 0)).x, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL)); //BARRELS--------------------------------------- g2d.setPaint(Color.BLACK); - int barrelSpacing = sizeAbs / (barrelAmount + 1); + int barrelSpacing = (int) (sizeAbs / (barrelAmount + 1)); g2d.rotate(rotation, xCenterAbs, yCenterAbs); for (int i = 0; i < barrelAmount; i++) { - int barrelX = xAbs + (i + 1) * barrelSpacing; - int frontPosY = yAbs - sizeAbs / 2; + int barrelX = (int) (abs.x + (i + 1) * barrelSpacing); + int frontPosY = (int) (abs.y - sizeAbs / 2); g2d.drawLine(barrelX, yCenterAbs, barrelX, frontPosY); if (lastShot + SHOT_EFFECT_TIME > System.currentTimeMillis()) { @@ -79,13 +78,12 @@ public class Turret extends GameObject { } @Override - public void update(Master master) { + public void update() { - int xAbs = battleShip.getWorldCoordsFromLocal(position.x, true); - int yAbs = battleShip.getWorldCoordsFromLocal(position.y, false); - int sizeAbs = battleShip.getWorldCoordsFromLocalSize(size.x, true); - int xCenterAbs = xAbs + sizeAbs / 2; - int yCenterAbs = yAbs + sizeAbs / 2; + 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); @@ -93,11 +91,11 @@ public class Turret extends GameObject { rotation = ExMath.angleLerp(rotation, targetRotation, ROTATION_SPEED); - int barrelSpacing = sizeAbs / (barrelAmount + 1); + int barrelSpacing = (int) (sizeAbs / (barrelAmount + 1)); for (int i = 0; i < barrelAmount; i++) { - int barrelX = xAbs + (i + 1) * barrelSpacing; - int frontPosY = yAbs - sizeAbs / 2; + int barrelX = (int) (abs.x + (i + 1) * barrelSpacing); + int frontPosY = (int) (abs.y - sizeAbs / 2); if (master.isMousePressed()) { lastShot = System.currentTimeMillis(); diff --git a/src/main/java/objects/world/Grid.java b/src/main/java/objects/world/Grid.java index 083ad96..744a8bc 100644 --- a/src/main/java/objects/world/Grid.java +++ b/src/main/java/objects/world/Grid.java @@ -41,7 +41,7 @@ public class Grid extends GameObject { } @Override - public void update(Master master) { + public void update() { } } diff --git a/src/main/java/objects/world/Wall.java b/src/main/java/objects/world/Wall.java index 58c5ca5..fb0818f 100644 --- a/src/main/java/objects/world/Wall.java +++ b/src/main/java/objects/world/Wall.java @@ -24,7 +24,7 @@ public class Wall extends GameObject implements Collidable { } @Override - public void update(Master master) { + public void update() { } @Override diff --git a/target/classes/core/Master.class b/target/classes/core/Master.class index 8cfb839..efeaa03 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 faaca3e..608784c 100644 Binary files a/target/classes/objects/DebugPos.class and b/target/classes/objects/DebugPos.class differ diff --git a/target/classes/objects/GameObject.class b/target/classes/objects/GameObject.class index 3555019..b0b4774 100644 Binary files a/target/classes/objects/GameObject.class and b/target/classes/objects/GameObject.class differ diff --git a/target/classes/objects/ships/BattleShip.class b/target/classes/objects/ships/BattleShip.class index 9ddd1a5..16f9474 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 3d78565..95ddbfe 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 d9d3099..54be3db 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 52de6aa..9dff9cf 100644 Binary files a/target/classes/objects/world/Grid.class and b/target/classes/objects/world/Grid.class differ