diff --git a/2DGame.zip b/2DGame.zip deleted file mode 100644 index 947ef13..0000000 Binary files a/2DGame.zip and /dev/null differ diff --git a/src/main/java/core/general/Input.java b/src/main/java/core/general/Input.java new file mode 100644 index 0000000..e7060ad --- /dev/null +++ b/src/main/java/core/general/Input.java @@ -0,0 +1,40 @@ +package core.general; + +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.util.ArrayList; + +/** + * Handles easily accessible input for the objects + */ +public class Input { + + private static ArrayList pressedKeys = new ArrayList<>(); + + private static boolean mousePressed; + + public static boolean keyPressed(int keyKey){ + return pressedKeys.contains(keyKey); + } + + public static void addPressedKey(int keyKey){ + pressedKeys.add(keyKey); + } + + public static void frameReset(){ + pressedKeys.clear(); + mousePressed = false; + } + + public static boolean isMousePressed() { + return mousePressed; + } + + /** + * Call this method when the user pressed the left mouse button + */ + public static void mousePressed() { + mousePressed = true; + } + +} diff --git a/src/main/java/Main.java b/src/main/java/core/general/Main.java similarity index 90% rename from src/main/java/Main.java rename to src/main/java/core/general/Main.java index 01c14d8..af7c1c6 100644 --- a/src/main/java/Main.java +++ b/src/main/java/core/general/Main.java @@ -1,4 +1,6 @@ -import core.general.Master; +package core.general; + +import objects.Init; import javax.swing.*; import java.awt.*; @@ -23,6 +25,8 @@ class Main extends JFrame { master = new Master(); add(master); + Init.init(); + setTitle("Points"); int w = 1500; setSize(w, (int) (w / Master.SCREEN_RATIO)); @@ -32,7 +36,7 @@ class Main extends JFrame { addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { - master.mousePressed(); + Input.mousePressed(); } }); } diff --git a/src/main/java/core/general/Master.java b/src/main/java/core/general/Master.java index d4d4429..230153c 100644 --- a/src/main/java/core/general/Master.java +++ b/src/main/java/core/general/Master.java @@ -6,12 +6,7 @@ import core.physics.Collidable; import core.physics.Collision; import core.objects.base.DebugPos; import core.physics.hitboxes.Hitbox; -import objects.ships.BattleShip; import core.objects.core.GameObject; -import objects.ships.Submarine; -import objects.ships.Turret; -import objects.world.Grid; -import objects.world.Wall; import javax.swing.*; import java.awt.*; @@ -64,10 +59,6 @@ public class Master extends JPanel { */ private final ArrayList collidablesBuffer; - /** - * Whether the left mouse button has been pressed since the last frame - */ - private boolean mousePressed = false; /** * The current width and height of the game area @@ -86,22 +77,6 @@ public class Master extends JPanel { collidablesBuffer = new ArrayList<>(); drawables = new ArrayList<>(); drawables.add(new ArrayList<>()); - - create(new Grid()); - - - BattleShip battleShip = new BattleShip(Color.DARK_GRAY); - BattleShip bs = new BattleShip(140, 10, 10, 80, Color.GREEN); - - for (int i = 0; i < 8; i++) { - bs.addTurret(new Turret(bs, 2.5, 10 * i + 1, 5, (i % 5 )+ 1)); - - } - create(bs); - create(battleShip); - - create(new Submarine(new Vector2D(), new Vector2D(5, 5))); - create(new Wall(20, 80, 50, 2)); } public static Master getMaster() { @@ -113,6 +88,7 @@ public class Master extends JPanel { * * @param g */ + @Deprecated private void doDrawing(Graphics g) { if (getWidth() * 9 > getHeight() * 16) { @@ -144,12 +120,7 @@ public class Master extends JPanel { create(new DebugPos(pos, new Vector2D(2, 2)), 3); } - /** - * Call this method when the user pressed the left mouse button - */ - public void mousePressed() { - mousePressed = true; - } + /** * This method is the entry method for each frame. It handles everything about the frame @@ -162,7 +133,7 @@ public class Master extends JPanel { collidables.addAll(collidablesBuffer); objects.forEach(GameObject::startUpdate); long time2 = System.currentTimeMillis(); - mousePressed = false; + Input.frameReset(); repaint(); System.out.println("Frame took " + (System.currentTimeMillis() - time) + "ms, " + (time2 - time) + "ms for update, " + (System.currentTimeMillis() - time2) + "ms for draw"); } @@ -178,9 +149,6 @@ public class Master extends JPanel { return p; } - public boolean isMousePressed() { - return mousePressed; - } /** * This method has to be called for every newly created GameObject @@ -203,7 +171,6 @@ public class Master extends JPanel { collidablesBuffer.add((Collidable) obj); } addDrawable(obj, renderLayer); - } /** diff --git a/src/main/java/core/objects/core/GameObject.java b/src/main/java/core/objects/core/GameObject.java index d0b7eb1..c409255 100644 --- a/src/main/java/core/objects/core/GameObject.java +++ b/src/main/java/core/objects/core/GameObject.java @@ -39,8 +39,11 @@ public abstract class GameObject implements Drawable { this.layer = 0; } + /** + * Gets called at the start of the update method + */ public void startUpdate(){ - if(Coords.outOfBounds(position, size)){ + if(doesDespawn && Coords.outOfBounds(position, size)){ destroy(); } update(); diff --git a/src/main/java/objects/GameObject.java b/src/main/java/objects/GameObject.java deleted file mode 100644 index 6234486..0000000 --- a/src/main/java/objects/GameObject.java +++ /dev/null @@ -1,67 +0,0 @@ -package objects; - -import core.general.Master; -import core.math.Vector2D; - -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 - */ -public abstract class GameObject { - - protected int w; - protected int h; - - protected Vector2D position; - protected Vector2D size; - - protected Vector2D velocity; - - public GameObject(double x, double y, double xSize, double ySize) { - this(new Vector2D(x, y), new Vector2D(xSize, ySize)); - } - - public GameObject(Vector2D position, Vector2D size) { - this.position = position; - this.size = size; - this.velocity = new Vector2D(); - } - - public abstract void draw(Graphics2D g2d, int w, Master master); - public abstract void update(Master master); - - - public double getMapCoords(double value, boolean isX){ - if (isX){ - return (position.x + value / 100 * size.x); - } else { - return (position.y + value / 100 * size.y); - } - } - - public double getMapCoordsSize(double value, boolean useX){ - if (useX){ - return (value / 100 * 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); - } - } - - public int getWorldCoordsFromLocal(double value, boolean isX){ - return (int) getWorldCoords(getMapCoords(value, isX), isX); - } - - public int getWorldCoordsFromLocalSize(double value, boolean useX){ - return (int) getWorldCoords(getMapCoordsSize(value, useX), useX); - } -} diff --git a/src/main/java/objects/Init.java b/src/main/java/objects/Init.java new file mode 100644 index 0000000..e1f5f76 --- /dev/null +++ b/src/main/java/objects/Init.java @@ -0,0 +1,48 @@ +package objects; + +import core.general.Master; +import core.math.Vector2D; +import core.objects.core.GameObject; +import objects.ships.BattleShip; +import objects.ships.Submarine; +import objects.ships.Turret; +import objects.world.Grid; +import objects.world.Wall; + +import java.awt.*; + +/** + * This class contains everything about custom GameObject initialization + */ +public class Init { + + /** + * Create a new GameObject + * @param o The GameObject + */ + public static void create(GameObject o){ + Master.getMaster().create(o); + } + + /** + * This method is called once at the start of the game + */ + public static void init(){ + + //INIT GOES HERE + create(new Grid()); + + BattleShip battleShip = new BattleShip(Color.DARK_GRAY); + BattleShip bs = new BattleShip(140, 10, 10, 80, Color.GREEN); + + for (int i = 0; i < 8; i++) { + bs.addTurret(new Turret(bs, 2.5, 10 * i + 1, 5, (i % 5 )+ 1)); + } + create(bs); + create(battleShip); + + create(new Submarine(new Vector2D(), new Vector2D(5, 5))); + create(new Wall(20, 80, 50, 2)); + } + +} diff --git a/src/main/java/objects/ships/Submarine.java b/src/main/java/objects/ships/Submarine.java index 2b6dc4a..701f1ff 100644 --- a/src/main/java/objects/ships/Submarine.java +++ b/src/main/java/objects/ships/Submarine.java @@ -12,6 +12,7 @@ public class Submarine extends CollGameObject { public Submarine(Vector2D position, Vector2D size) { super(position, size, new RectHitBox(position, size)); this.mainColor = Color.BLUE; + doesDespawn = false; } @Override diff --git a/src/main/java/objects/ships/Turret.java b/src/main/java/objects/ships/Turret.java index 6ca7943..94458d3 100644 --- a/src/main/java/objects/ships/Turret.java +++ b/src/main/java/objects/ships/Turret.java @@ -1,5 +1,6 @@ package objects.ships; +import core.general.Input; import core.math.Coords; import core.math.ExMath; import core.math.Vector2D; @@ -94,7 +95,7 @@ public class Turret extends GameObject { Vector2D spawnPosNR = battleShip.getMapCoords(new Vector2D(barrelX, frontPosY)); - if (master.isMousePressed()) { + if (Input.isMousePressed()) { lastShot = System.currentTimeMillis(); Vector2D shellVel = Vector2D.getUnitVector(rotation).negative().multiply(SHELL_SPEED); diff --git a/src/test/java/core/general/InputTest.java b/src/test/java/core/general/InputTest.java new file mode 100644 index 0000000..f21b30c --- /dev/null +++ b/src/test/java/core/general/InputTest.java @@ -0,0 +1,15 @@ +package core.general; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class InputTest { + + @Test + void simpleTest(){ + Input.addPressedKey(10); + assertTrue(Input.keyPressed(10)); + } + +} \ No newline at end of file diff --git a/target/classes/objects/ships/Turret.class b/target/classes/objects/ships/Turret.class index 8d49257..8c3bf56 100644 Binary files a/target/classes/objects/ships/Turret.class and b/target/classes/objects/ships/Turret.class differ