coordinates

This commit is contained in:
nora 2020-12-14 08:24:26 +01:00
parent 4fb5bcc543
commit b530a5d3a5
10 changed files with 36 additions and 30 deletions

View file

@ -1,2 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4" />

View file

@ -22,8 +22,13 @@ public class Coords {
public static Master master = Master.getMaster(); public static Master master = Master.getMaster();
/**
* Get the world coordinates of a point in map coordinates
* @param value A point in map coordinates
* @return The point in world coordinates
*/
public static Vector2D getWorldCoords(Vector2D value) { public static Vector2D getWorldCoords(Vector2D value) {
double x = (value.x / (Master.SCREEN_Y_COORDINATES * Master.SCREEN_RATIO) * master.getW()); double x = (value.x / Master.SCREEN_Y_COORDINATES / Master.SCREEN_RATIO * master.getW());
double y = (value.y / Master.SCREEN_Y_COORDINATES * master.getH()); double y = (value.y / Master.SCREEN_Y_COORDINATES * master.getH());
return new Vector2D(x, y); return new Vector2D(x, y);
} }
@ -39,11 +44,4 @@ public class Coords {
double y = (value.y / master.getH()) * Master.SCREEN_Y_COORDINATES; double y = (value.y / master.getH()) * Master.SCREEN_Y_COORDINATES;
return new Vector2D(x, y); return new Vector2D(x, y);
} }
public static Vector2D getWorldCoordsSize(Vector2D value) {
double x = (value.x / Master.SCREEN_Y_COORDINATES * master.getH());
double y = (value.y / Master.SCREEN_Y_COORDINATES * master.getH());
return new Vector2D(x, y);
}
} }

View file

@ -175,7 +175,7 @@ public class RectHitBox extends Hitbox {
public void draw(Graphics2D g2d) { public void draw(Graphics2D g2d) {
Vector2D abs = Coords.getWorldCoords(x1); Vector2D abs = Coords.getWorldCoords(x1);
Vector2D sizeAbs = Coords.getWorldCoordsSize(Vector2D.subtract(y2, x1)); Vector2D sizeAbs = Coords.getWorldCoords(Vector2D.subtract(y2, x1));
g2d.drawRect((int)abs.x, (int)abs.y, (int)sizeAbs.x, (int)sizeAbs.y); g2d.drawRect((int)abs.x, (int)abs.y, (int)sizeAbs.x, (int)sizeAbs.y);
g2d.setPaint(Color.MAGENTA); g2d.setPaint(Color.MAGENTA);

View file

@ -17,7 +17,7 @@ public class DebugPos extends GameObject {
@Override @Override
public void draw(Graphics2D g2d) { public void draw(Graphics2D g2d) {
drawOval(g2d); drawOval(g2d, "center");
} }
@Override @Override

View file

@ -62,7 +62,7 @@ public abstract class GameObject implements Drawable {
*/ */
public void drawRect(Graphics2D g2d) { public void drawRect(Graphics2D g2d) {
Vector2D abs = Coords.getWorldCoords(position); Vector2D abs = Coords.getWorldCoords(position);
Vector2D sizeAbs = Coords.getWorldCoordsSize(size); Vector2D sizeAbs = Coords.getWorldCoords(size);
g2d.setPaint(mainColor); g2d.setPaint(mainColor);
g2d.fillRect((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y); g2d.fillRect((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y);
@ -75,20 +75,32 @@ public abstract class GameObject implements Drawable {
*/ */
public void fillOval(Graphics2D g2d) { public void fillOval(Graphics2D g2d) {
Vector2D abs = Coords.getWorldCoords(position); Vector2D abs = Coords.getWorldCoords(position);
Vector2D sizeAbs = Coords.getWorldCoordsSize(size); Vector2D sizeAbs = Coords.getWorldCoords(size);
g2d.setPaint(mainColor); g2d.setPaint(mainColor);
g2d.fillOval((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y); g2d.fillOval((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y);
} }
/** /**
* This method draws a rectangle at the current position and size * This method draws an oval at the current position and size
* *
* @param g2d The Graphics2D object provided by the master * @param g2d The Graphics2D object provided by the master
*/ */
public void drawOval(Graphics2D g2d) { public void drawOval(Graphics2D g2d) {
Vector2D abs = Coords.getWorldCoords(position); drawOval(g2d, "");
Vector2D sizeAbs = Coords.getWorldCoordsSize(size); }
/**
* 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.setPaint(mainColor);
g2d.drawOval((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y); g2d.drawOval((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y);
@ -103,7 +115,7 @@ public abstract class GameObject implements Drawable {
*/ */
public void drawRoundRect(Graphics2D g2d, int arcW, int arcH) { public void drawRoundRect(Graphics2D g2d, int arcW, int arcH) {
Vector2D abs = Coords.getWorldCoords(position); Vector2D abs = Coords.getWorldCoords(position);
Vector2D sizeAbs = Coords.getWorldCoordsSize(size); Vector2D sizeAbs = Coords.getWorldCoords(size);
g2d.setPaint(mainColor); g2d.setPaint(mainColor);
g2d.fillRoundRect((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y, arcW, arcH); g2d.fillRoundRect((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y, arcW, arcH);
@ -116,8 +128,8 @@ public abstract class GameObject implements Drawable {
public Vector2D getMapCoords(Vector2D value) { public Vector2D getMapCoords(Vector2D value) {
double x = (position.x + value.x / 100d * size.x); double x = position.x + value.x;
double y = (position.y + value.y / 100d * size.y); double y = position.y + value.y;
return new Vector2D(x, y); return new Vector2D(x, y);
} }
@ -134,7 +146,7 @@ public abstract class GameObject implements Drawable {
} }
public Vector2D getWorldCoordsFromLocalSize(Vector2D value) { public Vector2D getWorldCoordsFromLocalSize(Vector2D value) {
return Coords.getWorldCoordsSize(getMapCoordsSize(value)); return Coords.getWorldCoords(getMapCoordsSize(value));
} }
public Vector2D getCenterPosition(Vector2D position){ public Vector2D getCenterPosition(Vector2D position){

View file

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

View file

@ -10,19 +10,18 @@ import java.awt.*;
/** /**
* A shell fired by a cannon * A shell fired by a cannon
*/ */
//TODO why tf do shells not use map coords...
public class Shell extends GameObject { public class Shell extends GameObject {
public Shell(Vector2D position, Vector2D size, Vector2D velocity) { public Shell(Vector2D position, Vector2D size, Vector2D velocity) {
super(position, size/*, new RectHitBox(position, size)*/); super(position, size/*, new RectHitBox(position, size)*/);
this.velocity = velocity; this.velocity = velocity;
this.mainColor = Color.ORANGE;
} }
@Override @Override
public void draw(Graphics2D g2d) { public void draw(Graphics2D g2d) {
g2d.setPaint(Color.orange); fillOval(g2d);
g2d.fillOval((int) position.x, (int) position.y, (int) size.x, (int) size.y);
} }
@Override @Override

View file

@ -1,13 +1,10 @@
package objects.ships; package objects.ships;
import core.Master;
import core.math.Coords; import core.math.Coords;
import core.math.ExMath; import core.math.ExMath;
import core.math.Vector2D; import core.math.Vector2D;
import jdk.swing.interop.SwingInterOpUtils;
import objects.core.GameObject; import objects.core.GameObject;
import javax.swing.*;
import java.awt.*; import java.awt.*;
/** /**
@ -17,7 +14,8 @@ public class Turret extends GameObject {
private static final double ROTATION_SPEED = 0.05; private static final double ROTATION_SPEED = 0.05;
private static final int SHOT_EFFECT_TIME = 300; private static final int SHOT_EFFECT_TIME = 300;
private static final int SHELL_SPEED = 10; private static final int SHELL_SPEED = 1;
public static final double SHELL_SIZE = 2;
BattleShip battleShip; BattleShip battleShip;
@ -113,7 +111,8 @@ public class Turret extends GameObject {
Vector2D shellVel = Vector2D.getUnitVector(rotation).negative().multiply(SHELL_SPEED); 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); Vector2D pos = Vector2D.rotateAround(new Vector2D(center.x, center.y), new Vector2D(barrelX, frontPosY), rotation, Vector2D.COUNTERCLOCKWISE);
master.create(new Shell(pos, new Vector2D(10, 10), shellVel)); master.debugPos(pos);
master.create(new Shell(pos, new Vector2D(SHELL_SIZE, SHELL_SIZE), shellVel));
} }
} }
} }