top rotation and better input

This commit is contained in:
nora 2020-12-21 14:27:50 +01:00
parent 9502ea97b8
commit 02aa5538f5
10 changed files with 132 additions and 24 deletions

View file

@ -1,6 +1,5 @@
package core.general; package core.general;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.util.ArrayList; import java.util.ArrayList;
@ -9,23 +8,76 @@ import java.util.ArrayList;
*/ */
public class Input { public class Input {
private static ArrayList<Integer> 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; private static boolean mousePressed;
public static boolean keyPressed(int keyKey){ private static double horizontalAxis = 0;
return pressedKeys.contains(keyKey); private static double verticalAxis = 0;
public static boolean keyPressed(int keyKey) {
return presses[keyKey];
} }
public static void addPressedKey(int keyKey){ public static void addPressedKey(int keyCode) {
pressedKeys.add(keyKey); if (keyCode < presses.length) {
presses[keyCode] = true;
}
} }
public static void frameReset(){ public static void removePressKey(int keyCode) {
pressedKeys.clear(); 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; mousePressed = false;
} }
public static double getHorizontalAxis() {
return horizontalAxis;
}
public static double getVerticalAxis() {
return verticalAxis;
}
public static boolean isMousePressed() { public static boolean isMousePressed() {
return mousePressed; return mousePressed;
} }
@ -36,5 +88,4 @@ public class Input {
public static void mousePressed() { public static void mousePressed() {
mousePressed = true; mousePressed = true;
} }
} }

View file

@ -4,6 +4,8 @@ import objects.Init;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
@ -39,10 +41,24 @@ class Main extends JFrame {
Input.mousePressed(); 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) { public static void main(String[] args) {
EventQueue.invokeLater(() -> { EventQueue.invokeLater(() -> {
Main ex = new Main(); Main ex = new Main();

View file

@ -7,6 +7,7 @@ import core.physics.Collision;
import core.objects.base.DebugPos; import core.objects.base.DebugPos;
import core.physics.hitboxes.Hitbox; import core.physics.hitboxes.Hitbox;
import core.objects.core.GameObject; import core.objects.core.GameObject;
import objects.Init;
import javax.swing.*; import javax.swing.*;
import java.awt.*; 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 * This method is the entry method for each frame. It handles everything about the frame
*/ */
public void refresh() { public void refresh() {
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
Input.calculate();
objects.clear(); objects.clear();
objects.addAll(objectBuffer); objects.addAll(objectBuffer);
collidables.clear(); collidables.clear();
@ -238,7 +239,9 @@ public class Master extends JPanel {
drawables.get(gameObject.getLayer()).remove(gameObject); drawables.get(gameObject.getLayer()).remove(gameObject);
if (gameObject instanceof Collidable) { if (gameObject instanceof Collidable) {
collidablesBuffer.remove(gameObject); collidablesBuffer.remove(gameObject);
if (Init.DEBUG_MODE) {
drawables.get(Hitbox.HITBOX_RENDER_LAYER).remove(((CollGameObject) gameObject).getHitbox()); drawables.get(Hitbox.HITBOX_RENDER_LAYER).remove(((CollGameObject) gameObject).getHitbox());
} }
} }
}
} }

View file

@ -16,8 +16,8 @@ public abstract class GameObject implements Drawable {
protected boolean doesDespawn = true; protected boolean doesDespawn = true;
protected Vector2D position; protected Vector2D position;
protected double rotation;
protected Vector2D size; protected Vector2D size;
protected Vector2D velocity; protected Vector2D velocity;
protected Color mainColor; protected Color mainColor;
@ -34,7 +34,7 @@ public abstract class GameObject implements Drawable {
this.position = position; this.position = position;
this.size = size; this.size = size;
this.velocity = new Vector2D(); this.velocity = new Vector2D();
mainColor = Color.BLACK; this.mainColor = Color.BLACK;
this.master = Master.getMaster(); this.master = Master.getMaster();
this.layer = 0; this.layer = 0;
} }
@ -130,11 +130,19 @@ public abstract class GameObject implements Drawable {
* @param arcH The arc height of the rectangle * @param arcH The arc height of the rectangle
*/ */
public void drawRoundRect(Graphics2D g2d, int arcW, int arcH) { public void drawRoundRect(Graphics2D g2d, int arcW, int arcH) {
Vector2D abs = Coords.getWorldCoords(position); Vector2D abs = Coords.getWorldCoords(position);
Vector2D sizeAbs = Coords.getWorldCoords(size); 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.setPaint(mainColor);
g2d.rotate(rotation, xCenterAbs, yCenterAbs);
g2d.fillRoundRect((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y, arcW, arcH); g2d.fillRoundRect((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y, arcW, arcH);
g2d.rotate(-rotation, xCenterAbs, yCenterAbs);
} }
public void destroy() { public void destroy() {
@ -165,4 +173,8 @@ public abstract class GameObject implements Drawable {
public int getLayer() { public int getLayer() {
return layer; return layer;
} }
protected Vector2D getV2DRotation(){
return Vector2D.getUnitVector(rotation);
}
} }

View file

@ -7,7 +7,8 @@ public abstract class Hitbox implements Drawable {
public static final int HITBOX_RENDER_LAYER = 1; public static final int HITBOX_RENDER_LAYER = 1;
private final boolean isTrigger;
private final boolean isTrigger; //TODO trigger
protected Hitbox(boolean isTrigger) { protected Hitbox(boolean isTrigger) {
this.isTrigger = isTrigger; this.isTrigger = isTrigger;

View file

@ -3,6 +3,7 @@ package core.physics.hitboxes;
import core.math.Coords; import core.math.Coords;
import core.general.Master; import core.general.Master;
import core.math.Vector2D; import core.math.Vector2D;
import objects.Init;
import java.awt.*; import java.awt.*;
@ -31,8 +32,10 @@ public class RectHitBox extends Hitbox {
this.x2 = Vector2D.add(x1, new Vector2D(size.x, 0)); this.x2 = Vector2D.add(x1, new Vector2D(size.x, 0));
this.y1 = Vector2D.add(x1, new Vector2D(0, size.y)); this.y1 = Vector2D.add(x1, new Vector2D(0, size.y));
this.y2 = Vector2D.add(x1, new Vector2D(size.x, size.y)); this.y2 = Vector2D.add(x1, new Vector2D(size.x, size.y));
if (Init.DEBUG_MODE) {
Master.getMaster().addDrawable(this, HITBOX_RENDER_LAYER); Master.getMaster().addDrawable(this, HITBOX_RENDER_LAYER);
} }
}
/** /**
* Create a new RectHitbox with the position of the top left point {@code x1} and the size * Create a new RectHitbox with the position of the top left point {@code x1} and the size
@ -177,6 +180,6 @@ public class RectHitBox extends Hitbox {
Vector2D sizeAbs = Coords.getWorldCoords(Vector2D.subtract(y2, x1)); Vector2D sizeAbs = Coords.getWorldCoords(Vector2D.subtract(y2, x1));
g2d.setPaint(Color.MAGENTA); 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);
} }
} }

View file

@ -16,6 +16,8 @@ import java.awt.*;
*/ */
public class Init { public class Init {
public static final boolean DEBUG_MODE = false;
/** /**
* Create a new GameObject * Create a new GameObject
* @param o The GameObject * @param o The GameObject

View file

@ -1,8 +1,11 @@
package objects.ships; package objects.ships;
import core.general.Input;
import core.math.Vector2D;
import core.objects.core.GameObject; import core.objects.core.GameObject;
import java.awt.*; import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.ArrayList; import java.util.ArrayList;
/** /**
@ -10,34 +13,52 @@ import java.util.ArrayList;
*/ */
public class BattleShip extends GameObject { public class BattleShip extends GameObject {
public static final double SPPED = 1;
private static final double TURN_RATE = 0.05;
private final ArrayList<Turret> turrets; private final ArrayList<Turret> turrets;
private boolean playerControlled = false;
public BattleShip(Color mainColor) { public BattleShip(Color mainColor) {
this(20, 20, 10, 40, mainColor); this(20, 20, 10, 40, mainColor);
turrets.add(new Turret(this, 2.5, 7, 5, 3)); 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, 15, 5, 3));
turrets.add(new Turret(this, 2.5, 25, 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) { public BattleShip(double x, double y, double xSize, double ySize, Color mainColor) {
super(x, y, xSize, ySize); super(x, y, xSize, ySize);
turrets = new ArrayList<>(); turrets = new ArrayList<>();
this.mainColor = mainColor; this.mainColor = mainColor;
this.doesDespawn = false;
} }
@Override @Override
public void draw(Graphics2D g2d) { 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))); turrets.forEach((turret -> turret.draw(g2d)));
} }
@Override @Override
public void update() { public void update() {
if (playerControlled) {
position.add(getV2DRotation().multiply(Input.getVerticalAxis() * SPPED));
rotation += Input.getHorizontalAxis() * TURN_RATE;
}
turrets.forEach(Turret::update); turrets.forEach(Turret::update);
} }
public void addTurret(Turret turret){ public void addTurret(Turret turret) {
turrets.add(turret); turrets.add(turret);
} }
public boolean isPlayerControlled() {
return playerControlled;
}
} }

View file

@ -18,6 +18,7 @@ public class Shell extends CollGameObject {
this.mainColor = Color.ORANGE; this.mainColor = Color.ORANGE;
this.ignores.add(Shell.class); this.ignores.add(Shell.class);
this.isTrigger = true; this.isTrigger = true;
this.doesDespawn = false;
} }
@Override @Override

View file

@ -23,8 +23,6 @@ public class Turret extends GameObject {
private int barrelAmount = 3; private int barrelAmount = 3;
private double rotation;
private final Color mainColor; private final Color mainColor;
private long lastShot = 0; private long lastShot = 0;