package structure

This commit is contained in:
nora 2020-12-14 12:25:02 +01:00
parent b530a5d3a5
commit 3f719e573a
16 changed files with 19 additions and 21 deletions

View file

@ -3,9 +3,9 @@ package core;
import core.math.Vector2D;
import core.physics.Collidable;
import core.physics.Collision;
import objects.DebugPos;
import core.objects.base.DebugPos;
import objects.ships.BattleShip;
import objects.core.GameObject;
import core.objects.core.GameObject;
import objects.ships.Submarine;
import objects.world.Grid;
import objects.world.Wall;

View file

@ -0,0 +1,26 @@
package core.objects.base;
import core.math.Vector2D;
import core.objects.core.GameObject;
import java.awt.*;
/**
* A GameObject used for debugging
*/
public class DebugPos extends GameObject {
public DebugPos(Vector2D position, Vector2D size) {
super(position.copy(), size);
this.velocity = new Vector2D();
this.mainColor = Color.GREEN;
}
@Override
public void draw(Graphics2D g2d) {
drawOval(g2d, "center");
}
@Override
public void update() {
}
}

View file

@ -0,0 +1,57 @@
package core.objects.core;
import core.math.Vector2D;
import core.objects.core.GameObject;
import core.physics.Collidable;
import core.physics.hitboxes.Hitbox;
/**
* A specialization of GameObject with Collidable properties
*/
public abstract class CollGameObject extends GameObject implements Collidable {
protected Hitbox hitbox;
public CollGameObject(Vector2D position, Vector2D size, Hitbox hitbox) {
super(position, size);
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.
* Does not move if it encounters a collision
*
* @param target The target position
*/
@Override
public void moveTo(Vector2D target) {
Vector2D oldPos = position.copy();
this.position = target;
((Collidable) this).getHitbox().moveTo(position, size);
if (master.doesCollide(this) != null) {
this.position = oldPos;
((Collidable) this).getHitbox().moveTo(oldPos, size);
}
}
@Override
public Hitbox getHitbox() {
return hitbox;
}
@Override
public Vector2D getCenterPos() {
return getCenterPosition();
}
@Override
public Vector2D getSize() {
return size;
}
}

View file

@ -0,0 +1,163 @@
package core.objects.core;
import core.math.Coords;
import core.Drawable;
import core.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()} and {@link #draw(Graphics2D)} methods that have to be
*/
public abstract class GameObject implements Drawable {
protected Vector2D position;
protected Vector2D size;
protected Vector2D velocity;
protected Color mainColor;
protected Master master;
protected int layer;
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();
mainColor = Color.BLACK;
this.master = Master.getMaster();
this.layer = 0;
}
/**
* <p>The update method is called every frame before the {@link #draw(Graphics2D)} method by the master object on each object. Everything
* 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>
*/
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) {
this.position = target;
}
/**
* This method draws a rectangle at the current position and size
*
* @param g2d The Graphics2D object provided by the master
*/
public void drawRect(Graphics2D g2d) {
Vector2D abs = Coords.getWorldCoords(position);
Vector2D sizeAbs = Coords.getWorldCoords(size);
g2d.setPaint(mainColor);
g2d.fillRect((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y);
}
/**
* This method draws a rectangle at the current position and size
*
* @param g2d The Graphics2D object provided by the master
*/
public void fillOval(Graphics2D g2d) {
Vector2D abs = Coords.getWorldCoords(position);
Vector2D sizeAbs = Coords.getWorldCoords(size);
g2d.setPaint(mainColor);
g2d.fillOval((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y);
}
/**
* This method draws an oval at the current position and size
*
* @param g2d The Graphics2D object provided by the master
*/
public void drawOval(Graphics2D g2d) {
drawOval(g2d, "");
}
/**
* This method draws an oval at the current position and size with arguments
*
* @param g2d The Graphics2D object provided by the master
*/
public void drawOval(Graphics2D g2d, String arg) {
Vector2D abs;
abs = (arg.contains("center")) ? Coords.getWorldCoords(getCenterPosition()) : Coords.getWorldCoords(position);
Vector2D sizeAbs = Coords.getWorldCoords(size);
g2d.setPaint(mainColor);
g2d.drawOval((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y);
}
/**
* This method draws a rounded rectangle at the current position and size
*
* @param g2d The Graphics2D object provided by the master
* @param arcW The arc width of the rectangle
* @param arcH The arc height of the rectangle
*/
public void drawRoundRect(Graphics2D g2d, int arcW, int arcH) {
Vector2D abs = Coords.getWorldCoords(position);
Vector2D sizeAbs = Coords.getWorldCoords(size);
g2d.setPaint(mainColor);
g2d.fillRoundRect((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y, arcW, arcH);
}
@Deprecated
public void destroy() {
master.destroy(this);
}
public Vector2D getMapCoords(Vector2D value) {
double x = position.x + value.x;
double y = position.y + value.y;
return new Vector2D(x, y);
}
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 Vector2D getWorldCoordsFromLocal(Vector2D value) {
return Coords.getWorldCoords(getMapCoords(value));
}
public Vector2D getWorldCoordsFromLocalSize(Vector2D value) {
return Coords.getWorldCoords(getMapCoordsSize(value));
}
public Vector2D getCenterPosition(Vector2D position){
return new Vector2D(position.x + size.x / 2, position.y + size.y / 2);
}
public Vector2D getCenterPosition() {
return getCenterPosition(position);
}
public int getLayer() {
return layer;
}
}