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;
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<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 double horizontalAxis = 0;
private static double verticalAxis = 0;
public static boolean keyPressed(int keyKey) {
return pressedKeys.contains(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 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() {
pressedKeys.clear();
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;
}
}

View file

@ -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();

View file

@ -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);
if (Init.DEBUG_MODE) {
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 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);
}
}

View file

@ -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;

View file

@ -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.*;
@ -31,8 +32,10 @@ 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));
if (Init.DEBUG_MODE) {
Master.getMaster().addDrawable(this, HITBOX_RENDER_LAYER);
}
}
/**
* Create a new RectHitbox with the position of the top left point {@code x1} and the size

View file

@ -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

View file

@ -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,19 +13,26 @@ 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<Turret> 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
@ -34,10 +44,21 @@ public class BattleShip extends GameObject {
@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) {
turrets.add(turret);
}
public boolean isPlayerControlled() {
return playerControlled;
}
}

View file

@ -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

View file

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