mirror of
https://github.com/Noratrieb/Java2DGame.git
synced 2026-01-14 14:05:01 +01:00
coordinate stuff
This commit is contained in:
parent
81855c6a6d
commit
5bb0282443
2 changed files with 26 additions and 9 deletions
|
|
@ -26,6 +26,8 @@ public abstract class GameObject implements Drawable {
|
||||||
|
|
||||||
protected int layer;
|
protected int layer;
|
||||||
|
|
||||||
|
protected GameObject parent;
|
||||||
|
|
||||||
public GameObject(double x, double y, double xSize, double ySize) {
|
public GameObject(double x, double y, double xSize, double ySize) {
|
||||||
this(new Vector2D(x, y), new Vector2D(xSize, 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) {
|
public Vector2D getMapCoords(Vector2D value) {
|
||||||
double x = position.x + value.x;
|
double x = position.x + value.x;
|
||||||
double y = position.y + value.y;
|
double y = position.y + value.y;
|
||||||
return new Vector2D(x, 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) {
|
public Vector2D getWorldCoordsFromLocal(Vector2D value) {
|
||||||
return Coordinates.getWorldCoordinates(getMapCoords(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){
|
public Vector2D getCenterPosition(Vector2D position){
|
||||||
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);
|
||||||
|
|
|
||||||
|
|
@ -18,9 +18,6 @@ public class Turret extends GameObject {
|
||||||
private static final int SHELL_SPEED = 1;
|
private static final int SHELL_SPEED = 1;
|
||||||
public static final double SHELL_SIZE = 2;
|
public static final double SHELL_SIZE = 2;
|
||||||
private static final double BARREL_THICKNESS = 1.7;
|
private static final double BARREL_THICKNESS = 1.7;
|
||||||
|
|
||||||
BattleShip battleShip;
|
|
||||||
|
|
||||||
private int barrelAmount = 3;
|
private int barrelAmount = 3;
|
||||||
|
|
||||||
private final Color mainColor;
|
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) {
|
public Turret(BattleShip battleShip, double x, double y, double size, int barrelAmount) {
|
||||||
super(x, y, size, size);
|
super(x, y, size, size);
|
||||||
this.battleShip = battleShip;
|
this.parent = battleShip;
|
||||||
this.barrelAmount = barrelAmount;
|
this.barrelAmount = barrelAmount;
|
||||||
mainColor = Color.GRAY;
|
mainColor = Color.GRAY;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Turret(BattleShip battleShip) {
|
public Turret(BattleShip battleShip) {
|
||||||
super(25, 50, 1.25, 0.5);
|
super(25, 50, 1.25, 0.5);
|
||||||
this.battleShip = battleShip;
|
this.parent = battleShip;
|
||||||
mainColor = Color.GRAY;
|
mainColor = Color.GRAY;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void draw(Graphics2D g2d) {
|
public void draw(Graphics2D g2d) {
|
||||||
g2d.setPaint(mainColor);
|
g2d.setPaint(mainColor);
|
||||||
Vector2D abs = battleShip.getWorldCoordsFromLocal(position);
|
Vector2D abs = parent.getWorldCoordsFromLocal(position);
|
||||||
int sizeAbs = (int) Coordinates.getWorldCoordinates(size).x;
|
int sizeAbs = (int) Coordinates.getWorldCoordinates(size).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);
|
||||||
|
|
@ -80,7 +77,7 @@ public class Turret extends GameObject {
|
||||||
|
|
||||||
Point msLoc = master.getMouseLocation();
|
Point msLoc = master.getMouseLocation();
|
||||||
Vector2D mouseRel = Coordinates.getMapCoordinatesFromWorld(Vector2D.fromPoint(msLoc)); //100 correct
|
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);
|
double targetRotation = -Math.atan2(centerMap.x - mouseRel.x, centerMap.y - mouseRel.y);
|
||||||
|
|
||||||
rotation = ExMath.angleLerp(rotation, targetRotation, ROTATION_SPEED);
|
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 barrelX = (int) (position.x + (i + 1) * barrelSpacing);
|
||||||
int frontPosY = (int) (position.y - size.x / 2);
|
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()) {
|
if (Input.isMousePressed()) {
|
||||||
lastShot = System.currentTimeMillis();
|
lastShot = System.currentTimeMillis();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue