tf happening

This commit is contained in:
nora 2020-12-14 14:35:10 +01:00
parent 3f719e573a
commit 9f460d499a
4 changed files with 15 additions and 30 deletions

View file

@ -24,7 +24,7 @@ class Main extends JFrame {
add(master);
setTitle("Points");
setSize(1600, 900);
setSize(1000, (int) (1000 / Master.SCREEN_RATIO));
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

View file

@ -134,20 +134,10 @@ public abstract class GameObject implements Drawable {
}
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);

View file

@ -15,7 +15,7 @@ public class BattleShip extends GameObject {
public BattleShip(Color mainColor) {
this(20, 20, 10, 40, mainColor);
//TODO turret size should use w and h but correct just like with world coords
turrets.add(new Turret(this, 2.5, 10, 50, 3));
turrets.add(new Turret(this, 2.5, 10, 5, 3));
//turrets.add(new Turret(this, 25, 10, 50, 2));
//turrets.add(new Turret(this, 25, 70, 50, 2));
}

View file

@ -16,6 +16,7 @@ public class Turret extends GameObject {
private static final int SHOT_EFFECT_TIME = 300;
private static final int SHELL_SPEED = 1;
public static final double SHELL_SIZE = 2;
private static final double BARREL_THICKNESS = 1.7;
BattleShip battleShip;
@ -35,7 +36,7 @@ public class Turret extends GameObject {
}
public Turret(BattleShip battleShip) {
super(25, 50, 50, 50);
super(25, 50, 1.25, 0.5);
this.battleShip = battleShip;
mainColor = Color.GRAY;
}
@ -44,13 +45,13 @@ public class Turret extends GameObject {
public void draw(Graphics2D g2d) {
g2d.setPaint(mainColor);
Vector2D abs = battleShip.getWorldCoordsFromLocal(position);
int sizeAbs = (int) battleShip.getWorldCoordsFromLocalSize(size).x;
int sizeAbs = (int) Coords.getWorldCoords(size).x;
int xCenterAbs = (int) (abs.x + sizeAbs / 2);
int yCenterAbs = (int) (abs.y + sizeAbs / 2);
g2d.fillOval((int) abs.x, (int) abs.y, sizeAbs, sizeAbs);
g2d.setStroke(new BasicStroke((int) battleShip.getWorldCoordsFromLocalSize(new Vector2D(10, 0)).x, BasicStroke.CAP_BUTT,
g2d.setStroke(new BasicStroke((int) Coords.getWorldCoords(new Vector2D(size.x / barrelAmount / BARREL_THICKNESS, 0)).x, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL));
//BARRELS---------------------------------------
@ -80,22 +81,9 @@ public class Turret extends GameObject {
Point msLoc = master.getMouseLocation();
Vector2D mouseRel = Coords.getMapCoordsFromWorld(Vector2D.fromPoint(msLoc)); //100 correct
//TODO getCenter uses the wrong size
Vector2D center = battleShip.getMapCoords(getCenterPosition(position));
master.debugPos(battleShip.getMapCoords(position));
master.debugPos(center);
Vector2D center = battleShip.getMapCoords(getCenterPosition());
double targetRotation = -Math.atan2(center.x - mouseRel.x, center.y - mouseRel.y);
//---------------OLD IMPLEMENTATION
Vector2D abs = battleShip.getWorldCoordsFromLocal(position);
int sizeAbs = (int) battleShip.getWorldCoordsFromLocalSize(size).x;
int xCenterAbs = (int) (abs.x + sizeAbs / 2);
int yCenterAbs = (int) (abs.y + sizeAbs / 2);
double targetRotationOld = -Math.atan2(xCenterAbs - msLoc.x, yCenterAbs - msLoc.y);
//----------------
rotation = ExMath.angleLerp(rotation, targetRotation, ROTATION_SPEED);
int barrelSpacing = (int) (size.x / (barrelAmount + 1));
@ -104,11 +92,18 @@ 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));
if (master.isMousePressed()) {
lastShot = System.currentTimeMillis();
Vector2D shellVel = Vector2D.getUnitVector(rotation).negative().multiply(SHELL_SPEED);
Vector2D pos = Vector2D.rotateAround(new Vector2D(center.x, center.y), new Vector2D(barrelX, frontPosY), rotation, Vector2D.COUNTERCLOCKWISE);
master.debugPos(battleShip.getMapCoords(center));
master.debugPos(center);
Vector2D pos = Vector2D.rotateAround(
battleShip.getMapCoords(center),
spawnPosNR,
rotation);
master.debugPos(pos);
master.create(new Shell(pos, new Vector2D(SHELL_SIZE, SHELL_SIZE), shellVel));