coordinate stuff

This commit is contained in:
nora 2020-12-24 14:34:39 +01:00
parent 81855c6a6d
commit 5bb0282443
2 changed files with 26 additions and 9 deletions

View file

@ -26,6 +26,8 @@ public abstract class GameObject implements Drawable {
protected int layer;
protected GameObject parent;
public GameObject(double x, double y, double xSize, double ySize) {
this(new Vector2D(x, y), new Vector2D(xSize, ySize));
}
@ -150,17 +152,35 @@ public abstract class GameObject implements Drawable {
}
/**
* Returns the value as map coords (only called on a parent)
* @param value
* @return
*/
public Vector2D getMapCoords(Vector2D value) {
double x = position.x + value.x;
double y = position.y + value.y;
return new Vector2D(x, y);
}
/**
* Returns the value as world coordinates (only called on a parent)
* @param value
* @return
*/
public Vector2D getWorldCoordsFromLocal(Vector2D value) {
return Coordinates.getWorldCoordinates(getMapCoords(value));
}
/**
* Get world coords of a value (called on itself)
* @param value
* @return
*/
public Vector2D getWorldCoords(Vector2D value){
return parent.getWorldCoordsFromLocal(value);
}
public Vector2D getCenterPosition(Vector2D position){
return new Vector2D(position.x + size.x / 2, position.y + size.y / 2);

View file

@ -18,9 +18,6 @@ public class Turret extends GameObject {
private static final int SHELL_SPEED = 1;
public static final double SHELL_SIZE = 2;
private static final double BARREL_THICKNESS = 1.7;
BattleShip battleShip;
private int barrelAmount = 3;
private final Color mainColor;
@ -29,21 +26,21 @@ public class Turret extends GameObject {
public Turret(BattleShip battleShip, double x, double y, double size, int barrelAmount) {
super(x, y, size, size);
this.battleShip = battleShip;
this.parent = battleShip;
this.barrelAmount = barrelAmount;
mainColor = Color.GRAY;
}
public Turret(BattleShip battleShip) {
super(25, 50, 1.25, 0.5);
this.battleShip = battleShip;
this.parent = battleShip;
mainColor = Color.GRAY;
}
@Override
public void draw(Graphics2D g2d) {
g2d.setPaint(mainColor);
Vector2D abs = battleShip.getWorldCoordsFromLocal(position);
Vector2D abs = parent.getWorldCoordsFromLocal(position);
int sizeAbs = (int) Coordinates.getWorldCoordinates(size).x;
int xCenterAbs = (int) (abs.x + sizeAbs / 2);
int yCenterAbs = (int) (abs.y + sizeAbs / 2);
@ -80,7 +77,7 @@ public class Turret extends GameObject {
Point msLoc = master.getMouseLocation();
Vector2D mouseRel = Coordinates.getMapCoordinatesFromWorld(Vector2D.fromPoint(msLoc)); //100 correct
Vector2D centerMap = battleShip.getMapCoords(getCenterPosition());
Vector2D centerMap = parent.getMapCoords(getCenterPosition());
double targetRotation = -Math.atan2(centerMap.x - mouseRel.x, centerMap.y - mouseRel.y);
rotation = ExMath.angleLerp(rotation, targetRotation, ROTATION_SPEED);
@ -91,7 +88,7 @@ public class Turret extends GameObject {
int barrelX = (int) (position.x + (i + 1) * barrelSpacing);
int frontPosY = (int) (position.y - size.x / 2);
Vector2D spawnPosNR = battleShip.getMapCoords(new Vector2D(barrelX, frontPosY));
Vector2D spawnPosNR = parent.getMapCoords(new Vector2D(barrelX, frontPosY));
if (Input.isMousePressed()) {
lastShot = System.currentTimeMillis();