diff --git a/src/main/java/core/Master.java b/src/main/java/core/Master.java index c4e77e9..06042e7 100644 --- a/src/main/java/core/Master.java +++ b/src/main/java/core/Master.java @@ -17,6 +17,19 @@ import java.util.ArrayList; */ public class Master extends JPanel { + /** + * The ratio of height to width. + */ + public static double SCREEN_RATIO = 16f / 9f; + + /** + * The height of the relative coordinates shown on the screen. + */ + public static final double SCREEN_Y_COORDINATES = 100d; + + @Deprecated + public static final double SCREEN_X_COORDINATES = 100d * SCREEN_RATIO; + /** * All GameObjects that exist */ @@ -41,9 +54,9 @@ public class Master extends JPanel { BattleShip battleShip = new BattleShip(Color.DARK_GRAY); BattleShip bs = new BattleShip(70, 10, 5, 80, Color.GREEN); - for (int i = 0; i < 10; i++) { + /*for (int i = 0; i < 10; i++) { bs.addTurret(new Turret(bs, 25, 10 * i + 1, 50, i % 5)); - } + }*/ objects.add(bs); objects.add(battleShip); @@ -57,7 +70,6 @@ public class Master extends JPanel { */ private void doDrawing(Graphics g) { - //TODO w/h fields int w, h; if (getWidth() * 9 > getHeight() * 16) { h = getHeight(); @@ -85,6 +97,7 @@ public class Master extends JPanel { */ public void debugPos(Vector2D pos){ create(new DebugPos(pos, new Vector2D(10, 10))); + System.out.println(pos); } /** diff --git a/src/main/java/core/Collidable.java b/src/main/java/core/physics/Collidable.java similarity index 60% rename from src/main/java/core/Collidable.java rename to src/main/java/core/physics/Collidable.java index 7b14855..3336ffb 100644 --- a/src/main/java/core/Collidable.java +++ b/src/main/java/core/physics/Collidable.java @@ -1,4 +1,4 @@ -package core; +package core.physics; public interface Collidable { diff --git a/src/main/java/objects/GameObject.java b/src/main/java/objects/GameObject.java index 6e2ae52..33cb6ab 100644 --- a/src/main/java/objects/GameObject.java +++ b/src/main/java/objects/GameObject.java @@ -4,6 +4,8 @@ import core.Master; import core.Vector2D; import java.awt.*; +import java.util.function.Consumer; +import java.util.function.Function; /** * The GameObject class is the superclass of every GameObject that can be displayed on screen. It has the 2 @@ -19,6 +21,8 @@ public abstract class GameObject { protected Vector2D velocity; + protected Color mainColor; + public GameObject(double x, double y, double xSize, double ySize) { this(new Vector2D(x, y), new Vector2D(xSize, ySize)); } @@ -27,37 +31,104 @@ public abstract class GameObject { this.position = position; this.size = size; this.velocity = new Vector2D(); + mainColor = Color.BLACK; } + /** + *

The draw method is called every frame after the {@link #update(Master)} by the master object on each object. Everything + * about drawing should be handled here in this method.

+ *

No general calculations should be made in this method. The game should be able to work just + * fine even without this method being called.

+ *

This function is NOT intended to be called manually.

+ * @param g2d The {@code Graphics2D} object given by the master + * @param w The width of the screen + * @param master The master object itself + */ public abstract void draw(Graphics2D g2d, int w, Master master); + + /** + *

The update method is called every frame before the {@link #draw(Graphics2D, int, Master)} 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.

+ * @param master The master object itself + */ public abstract void update(Master master); + /** + * 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){ this.position = target; } + /** + * 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 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); + + g2d.setPaint(mainColor); + g2d.fillRect(xAbs, yAbs, sizeXAbs, sizeYAbs); + } + + /** + * This method draws a rounded rectangle at the current position and size + * @param g2d The Graphics2D object provided by the master + * @param w The width of the screen + * @param arcW The arc width of the rectangle + * @param arcH The arc height of the rectangle + */ + 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); + + g2d.setPaint(mainColor); + g2d.fillRoundRect(xAbs, yAbs, sizeXAbs, sizeYAbs, 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 double getWorldCoordsSize(double value, boolean isX){ + if (isX){ + return (value / Master.SCREEN_Y_COORDINATES * w); + } else { + return (value / Master.SCREEN_Y_COORDINATES * h); + } + } + public double getMapCoords(double value, boolean isX){ if (isX){ - return (position.x + value / 100 * size.x); + return (position.x + value / 100d * size.x); } else { - return (position.y + value / 100 * size.y); + return (position.y + value / 100d * size.y); } } public double getMapCoordsSize(double value, boolean useX){ if (useX){ - return (value / 100 * size.x); + return (value / 100d * size.x); } else { - return (value / 100 * size.y); - } - } - - public double getWorldCoords(double value, boolean isX){ - if (isX){ - return (value / 100 * w); - } else { - return (value / 100 * h); + return (value / 100d * size.y); } } @@ -66,6 +137,6 @@ public abstract class GameObject { } public int getWorldCoordsFromLocalSize(double value, boolean useX){ - return (int) getWorldCoords(getMapCoordsSize(value, useX), useX); + return (int) getWorldCoordsSize(getMapCoordsSize(value, useX), useX); } } diff --git a/src/main/java/objects/ships/BattleShip.java b/src/main/java/objects/ships/BattleShip.java index 107f4aa..cbcb6b2 100644 --- a/src/main/java/objects/ships/BattleShip.java +++ b/src/main/java/objects/ships/BattleShip.java @@ -11,16 +11,14 @@ import java.util.ArrayList; */ public class BattleShip extends GameObject { - private Color mainColor; private ArrayList turrets; public BattleShip(Color mainColor) { - super(20, 20, 5, 40); - turrets = new ArrayList<>(); - turrets.add(new Turret(this, 25, 25, 50, 3)); + this(20, 20, 5, 40, mainColor); + //TODO child x coords 100 not 100*16/9 + turrets.add(new Turret(this, 100*16/9d/4, 25, 50, 3)); //turrets.add(new Turret(this, 25, 10, 50, 2)); //turrets.add(new Turret(this, 25, 70, 50, 2)); - this.mainColor = mainColor; } public BattleShip(double x, double y, double xSize, double ySize, Color mainColor) { @@ -31,15 +29,8 @@ public class BattleShip extends GameObject { @Override public void draw(Graphics2D g2d, int w, Master master) { - this.w = w; - h = w/16*9; - g2d.setPaint(mainColor); - int xAbs = (int) getWorldCoords(position.x, true); - int yAbs = (int) getWorldCoords(position.y, false); - int sizeXAbs = (int) getWorldCoords(size.x, true); - int sizeYAbs = (int) getWorldCoords(size.y, false); - g2d.fillRoundRect(xAbs, yAbs, sizeXAbs, sizeYAbs, w/10, w/10); + drawRoundRect(g2d, w, w/10, w/10); turrets.forEach((turret -> turret.draw(g2d, w, master))); } diff --git a/src/main/java/objects/ships/Turret.java b/src/main/java/objects/ships/Turret.java index c115c15..9689e57 100644 --- a/src/main/java/objects/ships/Turret.java +++ b/src/main/java/objects/ships/Turret.java @@ -43,7 +43,6 @@ public class Turret extends GameObject { @Override public void draw(Graphics2D g2d, int w, Master master) { - //TODO draw should be draw only for better everything h = w / 16 * 9; g2d.setPaint(mainColor); int xAbs = battleShip.getWorldCoordsFromLocal(position.x, true); @@ -75,6 +74,8 @@ public class Turret extends GameObject { } } g2d.rotate(-rotation, xCenterAbs, yCenterAbs); + + g2d.setStroke(new BasicStroke()); } @Override diff --git a/src/main/java/objects/world/Wall.java b/src/main/java/objects/world/Wall.java index 5b2e1a9..4ea16d0 100644 --- a/src/main/java/objects/world/Wall.java +++ b/src/main/java/objects/world/Wall.java @@ -13,13 +13,7 @@ public class Wall extends GameObject { @Override public void draw(Graphics2D g2d, int w, Master master) { - int xAbs = (int) getWorldCoords(position.x, true); - int yAbs = (int) getWorldCoords(position.y, false); - int sizeXAbs = (int) getWorldCoords(size.x, true); - int sizeYAbs = (int) getWorldCoords(size.y, false); - - g2d.setPaint(Color.BLACK); - g2d.fillRect(xAbs, yAbs, sizeXAbs, sizeYAbs); + drawRect(g2d, w); } @Override diff --git a/target/classes/core/Master.class b/target/classes/core/Master.class index 271caab..933f437 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 5a0ef20..ffcdc6e 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 961b066..34aa79b 100644 Binary files a/target/classes/objects/ships/BattleShip.class and b/target/classes/objects/ships/BattleShip.class differ