moved lots of code around

This commit is contained in:
nora 2020-12-13 12:07:30 +01:00
parent 3566916125
commit adb8e185fb
21 changed files with 230 additions and 108 deletions

View file

@ -0,0 +1,50 @@
package core;
import core.math.Vector2D;
/**
* This class provides everything about the local coordinate system the game uses
* <p>In this system, the screen is always 100 high and 100 * the screen ration wide.
* This class is the used to convert these coordinates into the true Java 2D coordinates for the drawing</p>
*/
//TODO make this return a vector2d instead of the isX
public class Coords {
/**
* This utility class should not be instantiated
*/
private Coords() {
}
/**
* The master object used to get the screen size
*/
public static Master master = Master.getMaster();
public static Vector2D getWorldCoords(Vector2D value) {
double x = (value.x / (Master.SCREEN_Y_COORDINATES * Master.SCREEN_RATIO) * master.getW());
double y = (value.y / Master.SCREEN_Y_COORDINATES * master.getH());
return new Vector2D(x, y);
}
/**
* Get local map coordinates (h=100) from the absolute Java 2D coordinates
*
* @param value The Vector2D position in Java 2D coordinates
* @return The Vector2D position in map coordinates
*/
public static Vector2D getMapCoordsFromWorld(Vector2D value) {
double x = (value.x / master.getW()) * Master.SCREEN_Y_COORDINATES * Master.SCREEN_RATIO;
double y = (value.y / master.getH()) * Master.SCREEN_Y_COORDINATES;
return new Vector2D(x, y);
}
public static Vector2D getWorldCoordsSize(Vector2D value) {
//TODO h not w
double x = (value.x / Master.SCREEN_Y_COORDINATES * master.getW());
double y = (value.y / Master.SCREEN_Y_COORDINATES * master.getH());
return new Vector2D(x, y);
}
}

View file

@ -3,6 +3,7 @@ package core;
import java.awt.*;
public interface Drawable {
/**
* <p>The draw method is called every frame after the update by the master object on each object. Everything
* about drawing should be handled here in this method.</p>

View file

@ -14,8 +14,6 @@ import java.awt.*;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
//TODO master better accessible or something
/**
* The main object that controls everything
*/
@ -31,8 +29,10 @@ public class Master extends JPanel {
*/
public static final double SCREEN_Y_COORDINATES = 100d;
@Deprecated
public static final double SCREEN_X_COORDINATES = 100d * SCREEN_RATIO;
/**
* The master object
*/
private static Master master;
/**
* All GameObjects that exist
@ -54,12 +54,22 @@ public class Master extends JPanel {
*/
private final ArrayList<GameObject> objectBuffer;
/**
* 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
*/
private int w,h;
/**
* Create a new master object
*/
public Master() {
master = this;
objects = new ArrayList<>();
objectBuffer = new ArrayList<>();
collidables = new ArrayList<>();
@ -80,6 +90,10 @@ public class Master extends JPanel {
create(new Wall(20, 80, 50, 2));
}
public static Master getMaster() {
return master;
}
/**
* The mein drawing method, handles everything about drawing
*
@ -87,7 +101,6 @@ public class Master extends JPanel {
*/
private void doDrawing(Graphics g) {
int w, h;
if (getWidth() * 9 > getHeight() * 16) {
h = getHeight();
w = h / 9 * 16;
@ -111,7 +124,7 @@ public class Master extends JPanel {
/**
* Debug a position, creates a green dot at the position
*
* @param pos
* @param pos The position
*/
public void debugPos(Vector2D pos) {
create(new DebugPos(pos, new Vector2D(10, 10)));
@ -130,7 +143,7 @@ public class Master extends JPanel {
public void refresh() {
objects.addAll(objectBuffer);
objectBuffer.clear();
objects.forEach(t -> t.update(this));
objects.forEach(GameObject::update);
mousePressed = false;
repaint();
}
@ -164,10 +177,19 @@ public class Master extends JPanel {
}
public void addDrawable(Drawable d){
public void addDrawable(Drawable d) {
drawables.add(d);
}
/**
* Check whether a collidables collide with another one
*
* @param col The collidable to be checked
* @return True if it collides with something, false if it doesn't - Should return a Collision
*/
@Deprecated
//TODO make it return a Collision
public boolean doesCollide(Collidable col) {
boolean collides = false;
@ -187,4 +209,20 @@ public class Master extends JPanel {
}
return collides;
}
public int getW() {
return w;
}
public int getH() {
return h;
}
public void destroy(GameObject gameObject) {
objects.remove(gameObject);
drawables.remove(gameObject);
if(gameObject instanceof Collidable){
collidables.remove(gameObject);
}
}
}

View file

@ -92,6 +92,7 @@ public class Vector2D {
/**
* Add two Vectors
*
* @param a Vector a
* @param b Vector b
* @return The result
@ -102,6 +103,7 @@ public class Vector2D {
/**
* Subtract two Vectors
*
* @param a Vector a
* @param b Vector b
* @return The result
@ -112,16 +114,16 @@ public class Vector2D {
/**
* Rotate a point around another point
*
* <p>
* This method can now be trusted
*
* @param center The center of the rotation
* @param value The point to be rotated, absolut to the center
* @param rotation The rotation angle in radians
* @param center The center of the rotation
* @param value The point to be rotated, absolut to the center
* @param rotation The rotation angle in radians
* @param rotationDirection The direction, -1 for clockwise, 1 for counterclockwise
* @return The rotated point
*/
public static Vector2D rotateAround(Vector2D center, Vector2D value, double rotation, int rotationDirection){
public static Vector2D rotateAround(Vector2D center, Vector2D value, double rotation, int rotationDirection) {
Vector2D dif = Vector2D.subtract(value, center);
@ -135,29 +137,30 @@ public class Vector2D {
/**
* Rotate a point around another point
*
* <p>
* This method can now be trusted
*
* @param center The center of the rotation
* @param value The point to be rotated, absolut to the center
* @param center The center of the rotation
* @param value The point to be rotated, absolut to the center
* @param rotation The rotation angle in radians
* @return The rotated point
*/
public static Vector2D rotateAround(Vector2D center, Vector2D value, double rotation){
public static Vector2D rotateAround(Vector2D center, Vector2D value, double rotation) {
return rotateAround(center, value, rotation, COUNTERCLOCKWISE);
}
/**
* Get a unit vector with magnitude 1 and the direction
*
* @param direction The direction of the vector
* @return The unit vector
*/
public static Vector2D getUnitVector(double direction){
public static Vector2D getUnitVector(double direction) {
return rotateAround(new Vector2D(), new Vector2D(0, 1), direction);
}
public static double distance(Vector2D a, Vector2D b){
public static double distance(Vector2D a, Vector2D b) {
Vector2D dif = subtract(a, b);
return Math.sqrt(dif.x * dif.x + dif.y + dif.y);
}
@ -165,9 +168,10 @@ public class Vector2D {
/**
* Copy this object
*
* @return A copy of this object
*/
public Vector2D copy(){
public Vector2D copy() {
return new Vector2D(x, y);
}

View file

@ -0,0 +1,27 @@
package core.physics;
public class Collision {
Collidable a;
Collidable b;
boolean haveCollided;
public Collision(Collidable a, Collidable b, boolean haveCollided) {
this.a = a;
this.b = b;
this.haveCollided = haveCollided;
}
public Collidable getA() {
return a;
}
public Collidable getB() {
return b;
}
public boolean isHaveCollided() {
return haveCollided;
}
}

View file

@ -10,6 +10,8 @@ import java.awt.*;
*/
public class RectHitBox extends Hitbox {
int w,h;
/**
* The corners of the rectangle like this:
* <p>x1 x2</p>
@ -148,7 +150,32 @@ public class RectHitBox extends Hitbox {
@Override
public void draw(Graphics2D g2d, int w, Master master) {
g2d.setPaint(Color.BLUE);
g2d.fillRect((int) x1.y, (int) x1.y, (int) (x2.x - x1.x), (int) (y1.y - x1.y));
this.w = w;
int h = (int) (w / Master.SCREEN_RATIO);
int xAbs = (int) getWorldCoords(x1.x, true);
int yAbs = (int) getWorldCoords(x1.y, false);
int sizeXAbs = (int) getWorldCoordsSize(x2.x - x1.x, true);
int sizeYAbs = (int) getWorldCoordsSize(y1.y - x1.y, false);
g2d.fillRect(xAbs, yAbs, sizeXAbs, sizeYAbs);
g2d.setPaint(Color.MAGENTA);
}
public double getWorldCoords(double value, boolean isX) {
if (isX) {
return (value / (Master.SCREEN_Y_COORDINATES * Master.SCREEN_RATIO) * w);
} else {
return (value / Master.SCREEN_Y_COORDINATES * h);
}
}
public double getWorldCoordsSize(double value, boolean isX) {
if (isX) {
return (value / Master.SCREEN_Y_COORDINATES * w);
} else {
return (value / Master.SCREEN_Y_COORDINATES * h);
}
}
}

View file

@ -21,6 +21,6 @@ public class DebugPos extends GameObject {
}
@Override
public void update(Master master) {
public void update() {
}
}

View file

@ -1,5 +1,6 @@
package objects;
import core.Coords;
import core.Drawable;
import core.Master;
import core.math.Vector2D;
@ -9,7 +10,7 @@ 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
* {@link #update()} and {@link #draw(Graphics2D, int, Master)} methods that have to be
*/
public abstract class GameObject implements Drawable {
@ -23,6 +24,8 @@ public abstract class GameObject implements Drawable {
protected Color mainColor;
protected Master master;
public GameObject(double x, double y, double xSize, double ySize) {
this(new Vector2D(x, y), new Vector2D(xSize, ySize));
}
@ -32,6 +35,7 @@ public abstract class GameObject implements Drawable {
this.size = size;
this.velocity = new Vector2D();
mainColor = Color.BLACK;
this.master = Master.getMaster();
}
@ -40,17 +44,15 @@ public abstract class GameObject implements Drawable {
* that is needed for the game to work should be here in this method.</p>
* <p>No drawing should be made in this method. The {@code debug} method can be called on the master.</p>
* <p>This function is <i>NOT</i> intended to be called manually.</p>
*
* @param master The master object itself
*/
public abstract void update(Master master);
public abstract void update();
/**
* A simple method to move the object to a Vector2D. This method should be called instead of doing it manually.
*
* @param target The target position
*/
public void moveTo(Vector2D target, Master master) {
public void moveTo(Vector2D target) {
Vector2D oldPos = position;
this.position = target;
@ -70,13 +72,11 @@ public abstract class GameObject implements Drawable {
public void drawRect(Graphics2D g2d, int w) {
this.w = w;
h = (int) (this.w / Master.SCREEN_RATIO);
int xAbs = (int) getWorldCoords(position.x, true);
int yAbs = (int) getWorldCoords(position.y, false);
int sizeXAbs = (int) getWorldCoordsSize(size.x, true);
int sizeYAbs = (int) getWorldCoordsSize(size.y, false);
Vector2D abs = Coords.getWorldCoords(position);
Vector2D sizeAbs = Coords.getWorldCoordsSize(size);
g2d.setPaint(mainColor);
g2d.fillRect(xAbs, yAbs, sizeXAbs, sizeYAbs);
g2d.fillRect((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y);
}
/**
@ -88,13 +88,11 @@ public abstract class GameObject implements Drawable {
public void drawOval(Graphics2D g2d, int w) {
this.w = w;
h = (int) (this.w / Master.SCREEN_RATIO);
int xAbs = (int) getWorldCoords(position.x, true);
int yAbs = (int) getWorldCoords(position.y, false);
int sizeXAbs = (int) getWorldCoordsSize(size.x, true);
int sizeYAbs = (int) getWorldCoordsSize(size.y, false);
Vector2D abs = Coords.getWorldCoords(position);
Vector2D sizeAbs = Coords.getWorldCoordsSize(size);
g2d.setPaint(mainColor);
g2d.fillOval(xAbs, yAbs, sizeXAbs, sizeYAbs);
g2d.fillOval((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y);
}
/**
@ -108,63 +106,41 @@ public abstract class GameObject implements Drawable {
public void drawRoundRect(Graphics2D g2d, int w, int arcW, int arcH) {
this.w = w;
h = (int) (w / Master.SCREEN_RATIO);
int xAbs = (int) getWorldCoords(position.x, true);
int yAbs = (int) getWorldCoords(position.y, false);
int sizeXAbs = (int) getWorldCoordsSize(size.x, true);
int sizeYAbs = (int) getWorldCoordsSize(size.y, false);
Vector2D abs = Coords.getWorldCoords(position);
Vector2D sizeAbs = Coords.getWorldCoordsSize(size);
g2d.setPaint(mainColor);
g2d.fillRoundRect(xAbs, yAbs, sizeXAbs, sizeYAbs, arcW, arcH);
g2d.fillRoundRect((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y, arcW, arcH);
}
public double getWorldCoords(double value, boolean isX) {
if (isX) {
return (value / (Master.SCREEN_Y_COORDINATES * Master.SCREEN_RATIO) * w);
} else {
return (value / Master.SCREEN_Y_COORDINATES * h);
}
public void destroy() {
master.destroy(this);
}
public Vector2D getMapCoordsFromWorld(Vector2D value){
double x = (value.x / w) * Master.SCREEN_Y_COORDINATES * Master.SCREEN_RATIO;
double y = (value.y / h) * Master.SCREEN_Y_COORDINATES;
public Vector2D getMapCoords(Vector2D value) {
double x = (position.x + value.x / 100d * size.x);
double y = (position.y + value.y / 100d * size.y);
return new Vector2D(x, y);
}
public double getWorldCoordsSize(double value, boolean isX) {
if (isX) {
return (value / Master.SCREEN_Y_COORDINATES * w);
} else {
return (value / Master.SCREEN_Y_COORDINATES * h);
}
public Vector2D getMapCoordsSize(Vector2D value) {
double x = (value.x / 100d * size.x);
double y = (value.y / 100d * size.y);
return new Vector2D(x, y);
}
public double getMapCoords(double value, boolean isX) {
if (isX) {
return (position.x + value / 100d * size.x);
} else {
return (position.y + value / 100d * size.y);
}
public Vector2D getWorldCoordsFromLocal(Vector2D value) {
return Coords.getWorldCoords(getMapCoords(value));
}
public double getMapCoordsSize(double value, boolean useX) {
if (useX) {
return (value / 100d * size.x);
} else {
return (value / 100d * size.y);
}
public Vector2D getWorldCoordsFromLocalSize(Vector2D value) {
return Coords.getWorldCoordsSize(getMapCoordsSize(value));
}
public int getWorldCoordsFromLocal(double value, boolean isX) {
return (int) getWorldCoords(getMapCoords(value, isX), isX);
}
public int getWorldCoordsFromLocalSize(double value, boolean useX) {
return (int) getWorldCoordsSize(getMapCoordsSize(value, useX), useX);
}
public Vector2D getCenterPosition(){
return new Vector2D(position.x - size.x/2, position.y - size.y/2);
public Vector2D getCenterPosition() {
return new Vector2D(position.x - size.x / 2, position.y - size.y / 2);
}
}

View file

@ -35,8 +35,8 @@ public class BattleShip extends GameObject {
}
@Override
public void update(Master master) {
turrets.forEach((turret -> turret.update(master)));
public void update() {
turrets.forEach(Turret::update);
}
public void addTurret(Turret turret){

View file

@ -21,7 +21,7 @@ public class Shell extends GameObject {
}
@Override
public void update(Master master) {
public void update() {
position.add(velocity);
}
}

View file

@ -1,5 +1,6 @@
package objects.ships;
import core.Coords;
import core.Master;
import core.math.Vector2D;
import core.physics.Collidable;
@ -26,11 +27,11 @@ public class Submarine extends GameObject implements Collidable {
}
@Override
public void update(Master master) {
public void update() {
Point mouse = master.getMouseLocation();
Vector2D relPos = getMapCoordsFromWorld(new Vector2D(mouse.x, mouse.y));
Vector2D relPos = Coords.getMapCoordsFromWorld(new Vector2D(mouse.x, mouse.y));
Vector2D centerRelPos = new Vector2D(relPos.x - size.x/2, relPos.y - size.y/2);
moveTo(centerRelPos, master);
moveTo(centerRelPos);
}
@Override

View file

@ -45,26 +45,25 @@ public class Turret extends GameObject {
public void draw(Graphics2D g2d, int w, Master master) {
h = w / 16 * 9;
g2d.setPaint(mainColor);
int xAbs = battleShip.getWorldCoordsFromLocal(position.x, true);
int yAbs = battleShip.getWorldCoordsFromLocal(position.y, false);
int sizeAbs = battleShip.getWorldCoordsFromLocalSize(size.x, true);
int xCenterAbs = xAbs + sizeAbs / 2;
int yCenterAbs = yAbs + sizeAbs / 2;
Vector2D abs = battleShip.getWorldCoordsFromLocal(position);
int sizeAbs = (int) battleShip.getWorldCoordsFromLocalSize(size).x;
int xCenterAbs = (int) (abs.x + sizeAbs / 2);
int yCenterAbs = (int) (abs.y + sizeAbs / 2);
g2d.fillOval(xAbs, yAbs, sizeAbs, sizeAbs);
g2d.fillOval((int) abs.x, (int) abs.y, (int) sizeAbs, (int) sizeAbs);
g2d.setStroke(new BasicStroke(battleShip.getWorldCoordsFromLocalSize(10, true), BasicStroke.CAP_BUTT,
g2d.setStroke(new BasicStroke((int) battleShip.getWorldCoordsFromLocalSize(new Vector2D(10, 0)).x, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL));
//BARRELS---------------------------------------
g2d.setPaint(Color.BLACK);
int barrelSpacing = sizeAbs / (barrelAmount + 1);
int barrelSpacing = (int) (sizeAbs / (barrelAmount + 1));
g2d.rotate(rotation, xCenterAbs, yCenterAbs);
for (int i = 0; i < barrelAmount; i++) {
int barrelX = xAbs + (i + 1) * barrelSpacing;
int frontPosY = yAbs - sizeAbs / 2;
int barrelX = (int) (abs.x + (i + 1) * barrelSpacing);
int frontPosY = (int) (abs.y - sizeAbs / 2);
g2d.drawLine(barrelX, yCenterAbs, barrelX, frontPosY);
if (lastShot + SHOT_EFFECT_TIME > System.currentTimeMillis()) {
@ -79,13 +78,12 @@ public class Turret extends GameObject {
}
@Override
public void update(Master master) {
public void update() {
int xAbs = battleShip.getWorldCoordsFromLocal(position.x, true);
int yAbs = battleShip.getWorldCoordsFromLocal(position.y, false);
int sizeAbs = battleShip.getWorldCoordsFromLocalSize(size.x, true);
int xCenterAbs = xAbs + sizeAbs / 2;
int yCenterAbs = yAbs + sizeAbs / 2;
Vector2D abs = battleShip.getWorldCoordsFromLocal(position);
int sizeAbs = (int) battleShip.getWorldCoordsFromLocalSize(size).x;
int xCenterAbs = (int) (abs.x + sizeAbs / 2);
int yCenterAbs = (int) (abs.y + sizeAbs / 2);
Point msLoc = master.getMouseLocation();
double targetRotation = -Math.atan2(xCenterAbs - msLoc.x, yCenterAbs - msLoc.y);
@ -93,11 +91,11 @@ public class Turret extends GameObject {
rotation = ExMath.angleLerp(rotation, targetRotation, ROTATION_SPEED);
int barrelSpacing = sizeAbs / (barrelAmount + 1);
int barrelSpacing = (int) (sizeAbs / (barrelAmount + 1));
for (int i = 0; i < barrelAmount; i++) {
int barrelX = xAbs + (i + 1) * barrelSpacing;
int frontPosY = yAbs - sizeAbs / 2;
int barrelX = (int) (abs.x + (i + 1) * barrelSpacing);
int frontPosY = (int) (abs.y - sizeAbs / 2);
if (master.isMousePressed()) {
lastShot = System.currentTimeMillis();

View file

@ -41,7 +41,7 @@ public class Grid extends GameObject {
}
@Override
public void update(Master master) {
public void update() {
}
}

View file

@ -24,7 +24,7 @@ public class Wall extends GameObject implements Collidable {
}
@Override
public void update(Master master) {
public void update() {
}
@Override