diff --git a/src/main/java/core/general/Input.java b/src/main/java/core/general/Input.java index e7060ad..27d9bf2 100644 --- a/src/main/java/core/general/Input.java +++ b/src/main/java/core/general/Input.java @@ -1,6 +1,5 @@ package core.general; -import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.util.ArrayList; @@ -9,23 +8,76 @@ import java.util.ArrayList; */ public class Input { - private static ArrayList pressedKeys = new ArrayList<>(); + public static final int AXIS_HORIZONTAL = 0; + public static final int AXIS_VERTICAL = 1; + + public static final int KEY_HORIZONTAL_HIGH = KeyEvent.VK_D; + public static final int KEY_HORIZONTAL_LOW = KeyEvent.VK_A; + public static final int KEY_VERTICAL_HIGH = KeyEvent.VK_S; + public static final int KEY_VERTICAL_LOW = KeyEvent.VK_W; + + /** + * Array of all possible pressed keys (not best way to do this but ok way) + */ + private static boolean[] presses = new boolean[0x0FFF]; private static boolean mousePressed; - public static boolean keyPressed(int keyKey){ - return pressedKeys.contains(keyKey); + private static double horizontalAxis = 0; + private static double verticalAxis = 0; + + public static boolean keyPressed(int keyKey) { + return presses[keyKey]; } - public static void addPressedKey(int keyKey){ - pressedKeys.add(keyKey); + public static void addPressedKey(int keyCode) { + if (keyCode < presses.length) { + presses[keyCode] = true; + } } - public static void frameReset(){ - pressedKeys.clear(); + public static void removePressKey(int keyCode) { + if (keyCode < presses.length) { + presses[keyCode] = false; + } + } + + /** + * To be called before the frames are calculated + */ + public static void calculate() { + if (presses[KEY_HORIZONTAL_HIGH]) { + horizontalAxis = 1; + } else if (presses[KEY_HORIZONTAL_LOW]) { + horizontalAxis = -1; + } else { + horizontalAxis = 0; + } + + if (presses[KEY_VERTICAL_HIGH]) { + verticalAxis = 1; + } else if (presses[KEY_VERTICAL_LOW]) { + verticalAxis = -1; + } else { + verticalAxis = 0; + } + } + + /** + * Is called after every frame + */ + public static void frameReset() { mousePressed = false; } + public static double getHorizontalAxis() { + return horizontalAxis; + } + + public static double getVerticalAxis() { + return verticalAxis; + } + public static boolean isMousePressed() { return mousePressed; } @@ -36,5 +88,4 @@ public class Input { public static void mousePressed() { mousePressed = true; } - } diff --git a/src/main/java/core/general/Main.java b/src/main/java/core/general/Main.java index af7c1c6..80ff629 100644 --- a/src/main/java/core/general/Main.java +++ b/src/main/java/core/general/Main.java @@ -4,6 +4,8 @@ import objects.Init; import javax.swing.*; import java.awt.*; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; @@ -39,10 +41,24 @@ class Main extends JFrame { Input.mousePressed(); } }); + + addKeyListener(new KeyAdapter() { + @Override + public void keyPressed(KeyEvent e) { + Input.addPressedKey(e.getKeyCode()); + } + + @Override + public void keyReleased(KeyEvent e) { + System.err.println("released"); + Input.removePressKey(e.getKeyCode()); + } + }); } public static void main(String[] args) { + EventQueue.invokeLater(() -> { Main ex = new Main(); diff --git a/src/main/java/core/general/Master.java b/src/main/java/core/general/Master.java index 230153c..7c7ecf8 100644 --- a/src/main/java/core/general/Master.java +++ b/src/main/java/core/general/Master.java @@ -7,6 +7,7 @@ import core.physics.Collision; import core.objects.base.DebugPos; import core.physics.hitboxes.Hitbox; import core.objects.core.GameObject; +import objects.Init; import javax.swing.*; import java.awt.*; @@ -121,12 +122,12 @@ public class Master extends JPanel { } - /** * This method is the entry method for each frame. It handles everything about the frame */ public void refresh() { long time = System.currentTimeMillis(); + Input.calculate(); objects.clear(); objects.addAll(objectBuffer); collidables.clear(); @@ -238,7 +239,9 @@ public class Master extends JPanel { drawables.get(gameObject.getLayer()).remove(gameObject); if (gameObject instanceof Collidable) { collidablesBuffer.remove(gameObject); - drawables.get(Hitbox.HITBOX_RENDER_LAYER).remove(((CollGameObject) gameObject).getHitbox()); + if (Init.DEBUG_MODE) { + drawables.get(Hitbox.HITBOX_RENDER_LAYER).remove(((CollGameObject) gameObject).getHitbox()); + } } } -} +} \ No newline at end of file diff --git a/src/main/java/core/objects/core/GameObject.java b/src/main/java/core/objects/core/GameObject.java index c409255..a25a285 100644 --- a/src/main/java/core/objects/core/GameObject.java +++ b/src/main/java/core/objects/core/GameObject.java @@ -16,8 +16,8 @@ public abstract class GameObject implements Drawable { protected boolean doesDespawn = true; protected Vector2D position; + protected double rotation; protected Vector2D size; - protected Vector2D velocity; protected Color mainColor; @@ -34,7 +34,7 @@ public abstract class GameObject implements Drawable { this.position = position; this.size = size; this.velocity = new Vector2D(); - mainColor = Color.BLACK; + this.mainColor = Color.BLACK; this.master = Master.getMaster(); this.layer = 0; } @@ -130,11 +130,19 @@ public abstract class GameObject implements Drawable { * @param arcH The arc height of the rectangle */ public void drawRoundRect(Graphics2D g2d, int arcW, int arcH) { + Vector2D abs = Coords.getWorldCoords(position); Vector2D sizeAbs = Coords.getWorldCoords(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); } public void destroy() { @@ -165,4 +173,8 @@ public abstract class GameObject implements Drawable { public int getLayer() { return layer; } + + protected Vector2D getV2DRotation(){ + return Vector2D.getUnitVector(rotation); + } } diff --git a/src/main/java/core/physics/hitboxes/Hitbox.java b/src/main/java/core/physics/hitboxes/Hitbox.java index 4f005fa..a7eebb9 100644 --- a/src/main/java/core/physics/hitboxes/Hitbox.java +++ b/src/main/java/core/physics/hitboxes/Hitbox.java @@ -7,7 +7,8 @@ public abstract class Hitbox implements Drawable { public static final int HITBOX_RENDER_LAYER = 1; - private final boolean isTrigger; + + private final boolean isTrigger; //TODO trigger protected Hitbox(boolean isTrigger) { this.isTrigger = isTrigger; diff --git a/src/main/java/core/physics/hitboxes/RectHitBox.java b/src/main/java/core/physics/hitboxes/RectHitBox.java index 866c9ca..ccbc774 100644 --- a/src/main/java/core/physics/hitboxes/RectHitBox.java +++ b/src/main/java/core/physics/hitboxes/RectHitBox.java @@ -3,6 +3,7 @@ package core.physics.hitboxes; import core.math.Coords; import core.general.Master; import core.math.Vector2D; +import objects.Init; import java.awt.*; @@ -21,8 +22,8 @@ public class RectHitBox extends Hitbox { /** * Create a new RectHitbox with the position of the top left point {@code x1} and the size * - * @param x1 The top left point - * @param size the size + * @param x1 The top left point + * @param size the size * @param isTrigger Whether the hitbox is a trigger (only collision detection, not restricting movement */ public RectHitBox(Vector2D x1, Vector2D size, boolean isTrigger) { @@ -31,7 +32,9 @@ 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, HITBOX_RENDER_LAYER); + if (Init.DEBUG_MODE) { + Master.getMaster().addDrawable(this, HITBOX_RENDER_LAYER); + } } /** @@ -51,7 +54,7 @@ public class RectHitBox extends Hitbox { /** * Move the hitbox to a new position * - * @param x1 The new position + * @param x1 The new position * @param size The new size */ @Override @@ -177,6 +180,6 @@ public class RectHitBox extends Hitbox { Vector2D sizeAbs = Coords.getWorldCoords(Vector2D.subtract(y2, x1)); g2d.setPaint(Color.MAGENTA); - g2d.drawRect((int)abs.x, (int)abs.y, (int)sizeAbs.x, (int)sizeAbs.y); + g2d.drawRect((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y); } } diff --git a/src/main/java/objects/Init.java b/src/main/java/objects/Init.java index e1f5f76..55548e8 100644 --- a/src/main/java/objects/Init.java +++ b/src/main/java/objects/Init.java @@ -16,6 +16,8 @@ import java.awt.*; */ public class Init { + public static final boolean DEBUG_MODE = false; + /** * Create a new GameObject * @param o The GameObject diff --git a/src/main/java/objects/ships/BattleShip.java b/src/main/java/objects/ships/BattleShip.java index 999d06f..22dd72f 100644 --- a/src/main/java/objects/ships/BattleShip.java +++ b/src/main/java/objects/ships/BattleShip.java @@ -1,8 +1,11 @@ package objects.ships; +import core.general.Input; +import core.math.Vector2D; import core.objects.core.GameObject; import java.awt.*; +import java.awt.event.KeyEvent; import java.util.ArrayList; /** @@ -10,34 +13,52 @@ import java.util.ArrayList; */ public class BattleShip extends GameObject { + public static final double SPPED = 1; + private static final double TURN_RATE = 0.05; + private final ArrayList turrets; + private boolean playerControlled = false; + public BattleShip(Color mainColor) { this(20, 20, 10, 40, mainColor); turrets.add(new Turret(this, 2.5, 7, 5, 3)); turrets.add(new Turret(this, 2.5, 15, 5, 3)); turrets.add(new Turret(this, 2.5, 25, 5, 3)); + this.playerControlled = true; } public BattleShip(double x, double y, double xSize, double ySize, Color mainColor) { super(x, y, xSize, ySize); turrets = new ArrayList<>(); this.mainColor = mainColor; + this.doesDespawn = false; } @Override public void draw(Graphics2D g2d) { - drawRoundRect(g2d, master.getW()/10, master.getW()/10); + drawRoundRect(g2d, master.getW() / 10, master.getW() / 10); turrets.forEach((turret -> turret.draw(g2d))); } @Override public void update() { + + if (playerControlled) { + position.add(getV2DRotation().multiply(Input.getVerticalAxis() * SPPED)); + + rotation += Input.getHorizontalAxis() * TURN_RATE; + } + turrets.forEach(Turret::update); } - public void addTurret(Turret turret){ + public void addTurret(Turret turret) { turrets.add(turret); } + + public boolean isPlayerControlled() { + return playerControlled; + } } \ No newline at end of file diff --git a/src/main/java/objects/ships/Shell.java b/src/main/java/objects/ships/Shell.java index 3c82ae7..7203827 100644 --- a/src/main/java/objects/ships/Shell.java +++ b/src/main/java/objects/ships/Shell.java @@ -18,6 +18,7 @@ public class Shell extends CollGameObject { this.mainColor = Color.ORANGE; this.ignores.add(Shell.class); this.isTrigger = true; + this.doesDespawn = false; } @Override diff --git a/src/main/java/objects/ships/Turret.java b/src/main/java/objects/ships/Turret.java index 94458d3..4856d38 100644 --- a/src/main/java/objects/ships/Turret.java +++ b/src/main/java/objects/ships/Turret.java @@ -23,8 +23,6 @@ public class Turret extends GameObject { private int barrelAmount = 3; - private double rotation; - private final Color mainColor; private long lastShot = 0;