mainly cleanup

This commit is contained in:
nora 2020-12-27 16:07:03 +01:00
parent db584c83d4
commit fa911f501e
13 changed files with 102 additions and 99 deletions

View file

@ -167,8 +167,9 @@ public class Master extends JPanel {
* *
* @param obj The new object * @param obj The new object
*/ */
public void create(GameObject obj) { public <T extends GameObject> T create(T obj) {
create(obj, obj.getLayer()); create(obj, obj.getLayer());
return obj;
} }
/** /**

View file

@ -3,18 +3,12 @@ package core.objects.base;
import core.math.Vector2D; import core.math.Vector2D;
import core.objects.core.GameObject; import core.objects.core.GameObject;
import java.awt.*;
public class Empty extends GameObject { public class Empty extends GameObject {
public Empty(Vector2D position) { public Empty(Vector2D position) {
super(position, new Vector2D(0, 0)); super(position, new Vector2D(0, 0));
} }
@Override
public void draw(Graphics2D g2d) {
}
@Override @Override
public void update() { public void update() {
} }

View file

@ -23,11 +23,6 @@ public abstract class CollGameObject extends GameObject implements Collidable {
this.hitbox = hitbox; this.hitbox = hitbox;
} }
public CollGameObject(double x, double y, double xSize, double ySize, Hitbox hitbox) {
super(x, y, xSize, ySize);
this.hitbox = hitbox;
}
/** /**
* A simple method to move the object to a Vector2D. This method should be called instead of doing it manually. * A simple method to move the object to a Vector2D. This method should be called instead of doing it manually.
* Does not move if it encounters a collision * Does not move if it encounters a collision

View file

@ -31,11 +31,6 @@ public abstract class GameObject implements Drawable {
private Renderer renderer; private Renderer renderer;
@Deprecated
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) { public GameObject(Vector2D position, Vector2D size) {
this.position = position; this.position = position;
this.size = size; this.size = size;
@ -49,8 +44,8 @@ public abstract class GameObject implements Drawable {
/** /**
* Gets called at the start of the update method * Gets called at the start of the update method
*/ */
public void startUpdate(){ public void startUpdate() {
if(doesDespawn && Coordinates.outOfBounds(position, size)){ if (doesDespawn && Coordinates.outOfBounds(position, size)) {
destroy(); destroy();
} }
update(); update();
@ -62,7 +57,7 @@ public abstract class GameObject implements Drawable {
* <p>No drawing should be made in this method. The {@code debug} method can be called on the master.</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> * <p>This function is <i>NOT</i> intended to be called manually.</p>
*/ */
public abstract void update(); protected abstract void update();
@Override @Override
public void draw(Graphics2D g2d) { public void draw(Graphics2D g2d) {
@ -113,6 +108,7 @@ public abstract class GameObject implements Drawable {
/** /**
* Returns the value as map coords * Returns the value as map coords
*
* @param value The value relative to the parent * @param value The value relative to the parent
* @return The value in global map coordinates * @return The value in global map coordinates
*/ */
@ -126,6 +122,7 @@ public abstract class GameObject implements Drawable {
/** /**
* Returns the value as world coordinates (only called on a parent) * Returns the value as world coordinates (only called on a parent)
*
* @param value The value relative to the parent * @param value The value relative to the parent
* @return The absolute world coordinates * @return The absolute world coordinates
*/ */
@ -135,22 +132,25 @@ public abstract class GameObject implements Drawable {
/** /**
* Get the center position of the object * Get the center position of the object
*
* @return The center position * @return The center position
*/ */
public Vector2D getCenterPosition(){ public Vector2D getCenterPosition() {
return new Vector2D(position.x + size.x / 2, position.y + size.y / 2); return new Vector2D(position.x + size.x / 2, position.y + size.y / 2);
} }
/** /**
* Get the local center position of the object * Get the local center position of the object
*
* @return The center position * @return The center position
*/ */
public Vector2D getCenterPositionLocal(){ public Vector2D getCenterPositionLocal() {
return new Vector2D(size.x / 2, size.y / 2); return new Vector2D(size.x / 2, size.y / 2);
} }
/** /**
* Get the render layer of the object * Get the render layer of the object
*
* @return The render layer * @return The render layer
*/ */
public int getLayer() { public int getLayer() {
@ -159,15 +159,27 @@ public abstract class GameObject implements Drawable {
/** /**
* Get the rotation of the object as a Vector2D * Get the rotation of the object as a Vector2D
*
* @return The rotation * @return The rotation
*/ */
protected Vector2D getV2DRotation(){ protected Vector2D getV2DRotation() {
return Vector2D.getUnitVector(rotation); return Vector2D.getUnitVector(rotation);
} }
/**
* Create a new {@code GameObject}
* @param gameObject The {@code GameObject}
* @return The {@code GameObject}
*/
protected <T extends GameObject> T create(T gameObject) {
master.create(gameObject);
return gameObject;
}
//----------- Getters and setters ----------------- //----------- Getters and setters -----------------
public Vector2D getMapPosition(){ public Vector2D getMapPosition() {
return getMapCoords(position); return getMapCoords(position);
} }
@ -183,7 +195,7 @@ public abstract class GameObject implements Drawable {
return size; return size;
} }
protected void setRenderer(Renderer renderer){ protected void setRenderer(Renderer renderer) {
this.renderer = renderer; this.renderer = renderer;
} }
} }

View file

@ -2,8 +2,6 @@ package core.objects.core;
import core.math.Vector2D; import core.math.Vector2D;
import java.awt.*;
/** /**
* The {@code mapAnchor} objects is the default parent of every new {@code GameObject}. * The {@code mapAnchor} objects is the default parent of every new {@code GameObject}.
*/ */
@ -13,10 +11,6 @@ public class MapAnchor extends GameObject {
super(new Vector2D(0, 0), new Vector2D(0, 0)); super(new Vector2D(0, 0), new Vector2D(0, 0));
} }
@Override
public void draw(Graphics2D g2d) {
}
@Override @Override
public void update() { public void update() {
} }

View file

@ -1,5 +1,7 @@
package core.renderer; package core.renderer;
import core.general.Master;
import core.math.Coordinates;
import core.math.Vector2D; import core.math.Vector2D;
import core.objects.core.GameObject; import core.objects.core.GameObject;
@ -7,15 +9,27 @@ import java.awt.*;
public class RectRenderer extends Renderer{ public class RectRenderer extends Renderer{
private int width; private Vector2D size;
private int height;
public RectRenderer(Color color, GameObject object) { public RectRenderer(Color color, GameObject object, Vector2D size) {
super(color, object); super(color, object);
this.size = size;
} }
@Override @Override
public void draw(Graphics2D g2d) { public void draw(Graphics2D g2d) {
Vector2D abs = Coordinates.getWorldCoordinates(object.getMapPosition());
Vector2D sizeAbs = Coordinates.getWorldCoordinates(size);
int xCenterAbs = (int) (abs.x + sizeAbs.x / 2);
int yCenterAbs = (int) (abs.y + sizeAbs.y / 2);
g2d.setPaint(color);
g2d.rotate(object.getRotation(), xCenterAbs, yCenterAbs);
g2d.fillRect((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y);
g2d.rotate(-object.getRotation(), xCenterAbs, yCenterAbs);
} }
} }

View file

@ -26,11 +26,11 @@ public class RoundRectRenderer extends Renderer {
Vector2D abs = Coordinates.getWorldCoordinates(object.getMapPosition()); Vector2D abs = Coordinates.getWorldCoordinates(object.getMapPosition());
Vector2D sizeAbs = Coordinates.getWorldCoordinates(size); Vector2D sizeAbs = Coordinates.getWorldCoordinates(size);
master.debugPos(object.getMapPosition());
int xCenterAbs = (int) (abs.x + sizeAbs.x / 2); int xCenterAbs = (int) (abs.x + sizeAbs.x / 2);
int yCenterAbs = (int) (abs.y + sizeAbs.y / 2); int yCenterAbs = (int) (abs.y + sizeAbs.y / 2);
master.debugPos(object.getCenterPosition(), 500);
g2d.setPaint(color); g2d.setPaint(color);
g2d.rotate(object.getRotation(), xCenterAbs, yCenterAbs); g2d.rotate(object.getRotation(), xCenterAbs, yCenterAbs);

View file

@ -22,8 +22,8 @@ public class Init {
* Create a new GameObject * Create a new GameObject
* @param o The GameObject * @param o The GameObject
*/ */
public static void create(GameObject o){ public static <T extends GameObject> T create(T o){
Master.getMaster().create(o); return Master.getMaster().create(o);
} }
/** /**
@ -34,17 +34,20 @@ public class Init {
//INIT GOES HERE //INIT GOES HERE
create(new Grid()); create(new Grid());
BattleShip battleShip = new BattleShip(Color.DARK_GRAY); BattleShip battleShip = create(new BattleShip());
BattleShip bs = new BattleShip(140, 10, 10, 80, Color.GREEN);
battleShip.addTurret(create(new Turret(battleShip, new Vector2D(2.5, 7), 5, 3)));
battleShip.addTurret(create(new Turret(battleShip, new Vector2D(2.5, 15), 5, 3)));
battleShip.addTurret(create(new Turret(battleShip, new Vector2D(2.5, 25), 5, 3)));
BattleShip bs = create(new BattleShip(new Vector2D(140, 10), new Vector2D(10, 80), Color.GREEN));
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
bs.addTurret(new Turret(bs, 2.5, 10 * i + 1, 5, (i % 5 )+ 1)); bs.addTurret(create(new Turret(bs, new Vector2D(2.5, 10 * i + 1), 5, (i % 5 )+ 1)));
} }
create(bs);
create(battleShip);
create(new Submarine(new Vector2D(), new Vector2D(5, 5))); create(new Submarine(new Vector2D(), new Vector2D(5, 5)));
create(new Wall(20, 80, 50, 2)); create(new Wall(new Vector2D(20, 80), new Vector2D(50, 2)));
} }
} }

View file

@ -5,6 +5,7 @@ import core.math.Vector2D;
import core.objects.core.CollGameObject; import core.objects.core.CollGameObject;
import core.objects.core.GameObject; import core.objects.core.GameObject;
import core.physics.hitboxes.RectHitBox; import core.physics.hitboxes.RectHitBox;
import core.renderer.RectRenderer;
import core.renderer.RoundRectRenderer; import core.renderer.RoundRectRenderer;
import java.awt.*; import java.awt.*;
@ -23,26 +24,18 @@ public class BattleShip extends GameObject {
private boolean playerControlled = false; private boolean playerControlled = false;
public BattleShip(Color mainColor) { public BattleShip() {
this(20, 20, 10, 40, mainColor); this(new Vector2D(20, 20), new Vector2D(10, 40), Color.DARK_GRAY);
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; this.playerControlled = true;
} }
public BattleShip(double x, double y, double xSize, double ySize, Color mainColor) { public BattleShip(Vector2D position, Vector2D size, Color mainColor) {
super(x, y, xSize, ySize); super(position, size);
turrets = new ArrayList<>(); turrets = new ArrayList<>();
this.mainColor = mainColor; this.mainColor = mainColor;
this.doesDespawn = false; this.doesDespawn = false;
setRenderer(new RoundRectRenderer(mainColor, this, size, 10, 10)); //setRenderer(new RoundRectRenderer(mainColor, this, size, 10, 10));
} setRenderer(new RectRenderer(mainColor, this, size));
@Override
public void draw(Graphics2D g2d) {
super.draw(g2d);
turrets.forEach((turret -> turret.draw(g2d)));
} }
@Override @Override
@ -54,8 +47,6 @@ public class BattleShip extends GameObject {
rotation += Input.getHorizontalAxis() * TURN_RATE; rotation += Input.getHorizontalAxis() * TURN_RATE;
} }
turrets.forEach(Turret::update);
} }
public void addTurret(Turret turret) { public void addTurret(Turret turret) {

View file

@ -5,6 +5,7 @@ import core.math.Coordinates;
import core.math.ExMath; import core.math.ExMath;
import core.math.Vector2D; import core.math.Vector2D;
import core.objects.core.GameObject; import core.objects.core.GameObject;
import core.renderer.CustomRenderer;
import java.awt.*; import java.awt.*;
@ -24,38 +25,33 @@ public class Turret extends GameObject {
private long lastShot = 0; private long lastShot = 0;
public Turret(BattleShip battleShip, double x, double y, double size, int barrelAmount) { public Turret(BattleShip battleShip, Vector2D position, double size, int barrelAmount) {
super(x, y, size, size); super(position, new Vector2D(size, size));
this.parent = battleShip; this.parent = battleShip;
this.barrelAmount = barrelAmount; this.barrelAmount = barrelAmount;
mainColor = Color.GRAY; mainColor = Color.GRAY;
}
public Turret(BattleShip battleShip) {
super(25, 50, 1.25, 0.5);
this.parent = battleShip;
mainColor = Color.GRAY;
}
setRenderer(new CustomRenderer(mainColor, this) {
@Override @Override
public void draw(Graphics2D g2d) { public void draw(Graphics2D g2d) {
g2d.setPaint(mainColor); g2d.setPaint(mainColor);
Vector2D abs = getWorldCoordsFromLocal(position); Vector2D abs = getWorldCoordsFromLocal(position);
int sizeAbs = (int) Coordinates.getWorldCoordinates(size).x; int sizeAbs = (int) Coordinates.getWorldCoordinates(object.getSize()).x;
int xCenterAbs = (int) (abs.x + sizeAbs / 2); int xCenterAbs = (int) (abs.x + sizeAbs / 2);
int yCenterAbs = (int) (abs.y + sizeAbs / 2); int yCenterAbs = (int) (abs.y + sizeAbs / 2);
g2d.rotate(rotation, xCenterAbs, yCenterAbs);
g2d.fillOval((int) abs.x, (int) abs.y, sizeAbs, sizeAbs); g2d.fillOval((int) abs.x, (int) abs.y, sizeAbs, sizeAbs);
master.debugPos(abs, 1000);
//BARRELS--------------------------------------- //BARRELS---------------------------------------
g2d.setStroke(new BasicStroke((int) Coordinates.getWorldCoordinates(new Vector2D(size.x / barrelAmount / BARREL_THICKNESS, 0)).x, BasicStroke.CAP_BUTT, g2d.setStroke(new BasicStroke((int) Coordinates.getWorldCoordinates(new Vector2D(object.getSize().x / barrelAmount / BARREL_THICKNESS, 0)).x, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL)); BasicStroke.JOIN_BEVEL));
g2d.setPaint(Color.BLACK); g2d.setPaint(Color.BLACK);
int barrelSpacing = sizeAbs / (barrelAmount + 1); int barrelSpacing = sizeAbs / (barrelAmount + 1);
g2d.rotate(rotation, xCenterAbs, yCenterAbs);
for (int i = 0; i < barrelAmount; i++) { for (int i = 0; i < barrelAmount; i++) {
int barrelX = (int) (abs.x + (i + 1) * barrelSpacing); int barrelX = (int) (abs.x + (i + 1) * barrelSpacing);
@ -72,6 +68,8 @@ public class Turret extends GameObject {
g2d.setStroke(new BasicStroke()); g2d.setStroke(new BasicStroke());
} }
});
}
@Override @Override
public void update() { public void update() {

View file

@ -1,5 +1,6 @@
package objects.world; package objects.world;
import core.math.Vector2D;
import core.objects.core.GameObject; import core.objects.core.GameObject;
import java.awt.*; import java.awt.*;
@ -12,7 +13,7 @@ public class Grid extends GameObject {
private static final int GRID_SPACING = 50; private static final int GRID_SPACING = 50;
public Grid() { public Grid() {
super(0, 0, 0, 0); super(Vector2D.zero(), Vector2D.zero());
} }
@Override @Override

View file

@ -9,8 +9,8 @@ import java.awt.*;
public class Wall extends CollGameObject { public class Wall extends CollGameObject {
public Wall(double x, double y, double xSize, double ySize) { public Wall(Vector2D position, Vector2D size) {
super(x, y, xSize, ySize, new RectHitBox(new Vector2D(x, y), new Vector2D(xSize, ySize))); super(position, size, new RectHitBox(position, size));
} }
@Override @Override