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