organized things

This commit is contained in:
nora 2020-12-19 20:18:30 +01:00
parent 8cd6b71fd0
commit 9502ea97b8
11 changed files with 119 additions and 107 deletions

Binary file not shown.

View file

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

View file

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

View file

@ -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<Collidable> 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);
}
/**

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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