mirror of
https://github.com/Noratrieb/Java2DGame.git
synced 2026-01-14 14:05:01 +01:00
organized things
This commit is contained in:
parent
8cd6b71fd0
commit
9502ea97b8
11 changed files with 119 additions and 107 deletions
BIN
2DGame.zip
BIN
2DGame.zip
Binary file not shown.
40
src/main/java/core/general/Input.java
Normal file
40
src/main/java/core/general/Input.java
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
import core.general.Master;
|
package core.general;
|
||||||
|
|
||||||
|
import objects.Init;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
@ -23,6 +25,8 @@ class Main extends JFrame {
|
||||||
master = new Master();
|
master = new Master();
|
||||||
add(master);
|
add(master);
|
||||||
|
|
||||||
|
Init.init();
|
||||||
|
|
||||||
setTitle("Points");
|
setTitle("Points");
|
||||||
int w = 1500;
|
int w = 1500;
|
||||||
setSize(w, (int) (w / Master.SCREEN_RATIO));
|
setSize(w, (int) (w / Master.SCREEN_RATIO));
|
||||||
|
|
@ -32,7 +36,7 @@ class Main extends JFrame {
|
||||||
addMouseListener(new MouseAdapter() {
|
addMouseListener(new MouseAdapter() {
|
||||||
@Override
|
@Override
|
||||||
public void mousePressed(MouseEvent e) {
|
public void mousePressed(MouseEvent e) {
|
||||||
master.mousePressed();
|
Input.mousePressed();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -6,12 +6,7 @@ import core.physics.Collidable;
|
||||||
import core.physics.Collision;
|
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 objects.ships.BattleShip;
|
|
||||||
import core.objects.core.GameObject;
|
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 javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
@ -64,10 +59,6 @@ public class Master extends JPanel {
|
||||||
*/
|
*/
|
||||||
private final ArrayList<Collidable> collidablesBuffer;
|
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
|
* The current width and height of the game area
|
||||||
|
|
@ -86,22 +77,6 @@ public class Master extends JPanel {
|
||||||
collidablesBuffer = new ArrayList<>();
|
collidablesBuffer = new ArrayList<>();
|
||||||
drawables = new ArrayList<>();
|
drawables = new ArrayList<>();
|
||||||
drawables.add(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() {
|
public static Master getMaster() {
|
||||||
|
|
@ -113,6 +88,7 @@ public class Master extends JPanel {
|
||||||
*
|
*
|
||||||
* @param g
|
* @param g
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
private void doDrawing(Graphics g) {
|
private void doDrawing(Graphics g) {
|
||||||
|
|
||||||
if (getWidth() * 9 > getHeight() * 16) {
|
if (getWidth() * 9 > getHeight() * 16) {
|
||||||
|
|
@ -144,12 +120,7 @@ public class Master extends JPanel {
|
||||||
create(new DebugPos(pos, new Vector2D(2, 2)), 3);
|
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
|
* 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);
|
collidables.addAll(collidablesBuffer);
|
||||||
objects.forEach(GameObject::startUpdate);
|
objects.forEach(GameObject::startUpdate);
|
||||||
long time2 = System.currentTimeMillis();
|
long time2 = System.currentTimeMillis();
|
||||||
mousePressed = false;
|
Input.frameReset();
|
||||||
repaint();
|
repaint();
|
||||||
System.out.println("Frame took " + (System.currentTimeMillis() - time) + "ms, " + (time2 - time) + "ms for update, " + (System.currentTimeMillis() - time2) + "ms for draw");
|
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;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isMousePressed() {
|
|
||||||
return mousePressed;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method has to be called for every newly created GameObject
|
* This method has to be called for every newly created GameObject
|
||||||
|
|
@ -203,7 +171,6 @@ public class Master extends JPanel {
|
||||||
collidablesBuffer.add((Collidable) obj);
|
collidablesBuffer.add((Collidable) obj);
|
||||||
}
|
}
|
||||||
addDrawable(obj, renderLayer);
|
addDrawable(obj, renderLayer);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -39,8 +39,11 @@ public abstract class GameObject implements Drawable {
|
||||||
this.layer = 0;
|
this.layer = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets called at the start of the update method
|
||||||
|
*/
|
||||||
public void startUpdate(){
|
public void startUpdate(){
|
||||||
if(Coords.outOfBounds(position, size)){
|
if(doesDespawn && Coords.outOfBounds(position, size)){
|
||||||
destroy();
|
destroy();
|
||||||
}
|
}
|
||||||
update();
|
update();
|
||||||
|
|
|
||||||
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
48
src/main/java/objects/Init.java
Normal file
48
src/main/java/objects/Init.java
Normal 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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -12,6 +12,7 @@ public class Submarine extends CollGameObject {
|
||||||
public Submarine(Vector2D position, Vector2D size) {
|
public Submarine(Vector2D position, Vector2D size) {
|
||||||
super(position, size, new RectHitBox(position, size));
|
super(position, size, new RectHitBox(position, size));
|
||||||
this.mainColor = Color.BLUE;
|
this.mainColor = Color.BLUE;
|
||||||
|
doesDespawn = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package objects.ships;
|
package objects.ships;
|
||||||
|
|
||||||
|
import core.general.Input;
|
||||||
import core.math.Coords;
|
import core.math.Coords;
|
||||||
import core.math.ExMath;
|
import core.math.ExMath;
|
||||||
import core.math.Vector2D;
|
import core.math.Vector2D;
|
||||||
|
|
@ -94,7 +95,7 @@ public class Turret extends GameObject {
|
||||||
|
|
||||||
Vector2D spawnPosNR = battleShip.getMapCoords(new Vector2D(barrelX, frontPosY));
|
Vector2D spawnPosNR = battleShip.getMapCoords(new Vector2D(barrelX, frontPosY));
|
||||||
|
|
||||||
if (master.isMousePressed()) {
|
if (Input.isMousePressed()) {
|
||||||
lastShot = System.currentTimeMillis();
|
lastShot = System.currentTimeMillis();
|
||||||
|
|
||||||
Vector2D shellVel = Vector2D.getUnitVector(rotation).negative().multiply(SHELL_SPEED);
|
Vector2D shellVel = Vector2D.getUnitVector(rotation).negative().multiply(SHELL_SPEED);
|
||||||
|
|
|
||||||
15
src/test/java/core/general/InputTest.java
Normal file
15
src/test/java/core/general/InputTest.java
Normal 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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue