destroyed things

This commit is contained in:
nora 2020-12-13 16:55:19 +01:00
parent 435de55eb1
commit 4fb5bcc543
14 changed files with 61 additions and 18 deletions

View file

@ -129,7 +129,7 @@ public class Master extends JPanel {
* @param pos The position * @param pos The position
*/ */
public void debugPos(Vector2D pos) { public void debugPos(Vector2D pos) {
create(new DebugPos(pos, new Vector2D(10, 10))); create(new DebugPos(pos, new Vector2D(2, 2)), 3);
} }
/** /**
@ -246,6 +246,7 @@ public class Master extends JPanel {
public void destroy(GameObject gameObject) { public void destroy(GameObject gameObject) {
objects.remove(gameObject); objects.remove(gameObject);
objectBuffer.remove(gameObject);
drawables.get(gameObject.getLayer()).remove(gameObject); drawables.get(gameObject.getLayer()).remove(gameObject);
if (gameObject instanceof Collidable) { if (gameObject instanceof Collidable) {
collidables.remove(gameObject); collidables.remove(gameObject);

View file

@ -1,6 +1,8 @@
package core.math; package core.math;
import java.awt.*;
/** /**
* A 2-dimensional Vector that can be used to store position or velocity * A 2-dimensional Vector that can be used to store position or velocity
*/ */
@ -33,6 +35,15 @@ public class Vector2D {
y = 0; y = 0;
} }
/**
* Get a new Vector2D from a Point
* @param point The point
* @return The Vector2D
*/
public static Vector2D fromPoint(Point point) {
return new Vector2D(point.x, point.y);
}
/** /**
* Add another Vector to this vector, modifies this object * Add another Vector to this vector, modifies this object
* *

View file

@ -12,12 +12,12 @@ public class DebugPos extends GameObject {
public DebugPos(Vector2D position, Vector2D size) { public DebugPos(Vector2D position, Vector2D size) {
super(position.copy(), size); super(position.copy(), size);
this.velocity = new Vector2D(); this.velocity = new Vector2D();
this.mainColor = Color.GREEN;
} }
@Override @Override
public void draw(Graphics2D g2d) { public void draw(Graphics2D g2d) {
g2d.setPaint(Color.green); drawOval(g2d);
g2d.drawOval((int) (position.x - size.x / 2), (int) (position.y - size.y / 2), (int) size.x, (int) size.y);
} }
@Override @Override

View file

@ -4,7 +4,6 @@ import core.math.Coords;
import core.Drawable; import core.Drawable;
import core.Master; import core.Master;
import core.math.Vector2D; import core.math.Vector2D;
import core.physics.Collidable;
import java.awt.*; import java.awt.*;
@ -74,7 +73,7 @@ public abstract class GameObject implements Drawable {
* *
* @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 fillOval(Graphics2D g2d) {
Vector2D abs = Coords.getWorldCoords(position); Vector2D abs = Coords.getWorldCoords(position);
Vector2D sizeAbs = Coords.getWorldCoordsSize(size); Vector2D sizeAbs = Coords.getWorldCoordsSize(size);
@ -82,6 +81,19 @@ public abstract class GameObject implements Drawable {
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
*
* @param g2d The Graphics2D object provided by the master
*/
public void drawOval(Graphics2D g2d) {
Vector2D abs = Coords.getWorldCoords(position);
Vector2D sizeAbs = Coords.getWorldCoordsSize(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 * This method draws a rounded rectangle at the current position and size
* *
@ -97,6 +109,7 @@ public abstract class GameObject implements Drawable {
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);
} }
@Deprecated
public void destroy() { public void destroy() {
master.destroy(this); master.destroy(this);
} }
@ -124,8 +137,12 @@ public abstract class GameObject implements Drawable {
return Coords.getWorldCoordsSize(getMapCoordsSize(value)); return Coords.getWorldCoordsSize(getMapCoordsSize(value));
} }
public Vector2D getCenterPosition(Vector2D position){
return new Vector2D(position.x + size.x / 2, position.y + size.y / 2);
}
public Vector2D getCenterPosition() { public Vector2D getCenterPosition() {
return new Vector2D(position.x - size.x / 2, position.y - size.y / 2); return getCenterPosition(position);
} }
public int getLayer() { public int getLayer() {

View file

@ -14,6 +14,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
turrets.add(new Turret(this, 25, 25, 50, 3)); turrets.add(new Turret(this, 25, 25, 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

@ -3,6 +3,7 @@ package objects.ships;
import core.math.Vector2D; import core.math.Vector2D;
import core.physics.hitboxes.RectHitBox; import core.physics.hitboxes.RectHitBox;
import objects.core.CollGameObject; import objects.core.CollGameObject;
import objects.core.GameObject;
import java.awt.*; import java.awt.*;
@ -10,11 +11,11 @@ import java.awt.*;
* A shell fired by a cannon * A shell fired by a cannon
*/ */
//TODO why tf do shells not use map coords... //TODO why tf do shells not use map coords...
public class Shell extends CollGameObject { 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;
} }

View file

@ -17,7 +17,7 @@ public class Submarine extends CollGameObject {
@Override @Override
public void draw(Graphics2D g2d) { public void draw(Graphics2D g2d) {
g2d.setPaint(Color.BLUE); g2d.setPaint(Color.BLUE);
drawOval(g2d); fillOval(g2d);
} }
@Override @Override

View file

@ -1,9 +1,13 @@
package objects.ships; package objects.ships;
import core.Master;
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.*;
/** /**
@ -25,8 +29,6 @@ public class Turret extends GameObject {
private long lastShot = 0; private long lastShot = 0;
//private double rotation;
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.battleShip = battleShip;
@ -78,28 +80,38 @@ public class Turret extends GameObject {
@Override @Override
public void update() { public void update() {
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));
System.out.println(getCenterPosition(position));
master.debugPos(battleShip.getMapCoords(position));
master.debugPos(center);
double targetRotation = -Math.atan2(center.x - mouseRel.x, center.y - mouseRel.y);
//---------------OLD IMPLEMENTATION
Vector2D abs = battleShip.getWorldCoordsFromLocal(position); Vector2D abs = battleShip.getWorldCoordsFromLocal(position);
int sizeAbs = (int) battleShip.getWorldCoordsFromLocalSize(size).x; int sizeAbs = (int) battleShip.getWorldCoordsFromLocalSize(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);
double targetRotationOld = -Math.atan2(xCenterAbs - msLoc.x, yCenterAbs - msLoc.y);
Point msLoc = master.getMouseLocation(); //----------------
double targetRotation = -Math.atan2(xCenterAbs - msLoc.x, yCenterAbs - msLoc.y);
rotation = ExMath.angleLerp(rotation, targetRotation, ROTATION_SPEED); rotation = ExMath.angleLerp(rotation, targetRotation, ROTATION_SPEED);
int barrelSpacing = sizeAbs / (barrelAmount + 1); int barrelSpacing = (int) (size.x / (barrelAmount + 1));
for (int i = 0; i < barrelAmount; i++) { for (int i = 0; i < barrelAmount; i++) {
int barrelX = (int) (abs.x + (i + 1) * barrelSpacing); int barrelX = (int) (position.x + (i + 1) * barrelSpacing);
int frontPosY = (int) (abs.y - sizeAbs / 2); int frontPosY = (int) (position.y - size.x / 2);
if (master.isMousePressed()) { if (master.isMousePressed()) {
lastShot = System.currentTimeMillis(); lastShot = System.currentTimeMillis();
Vector2D shellVel = Vector2D.getUnitVector(rotation).negative().multiply(SHELL_SPEED); Vector2D shellVel = Vector2D.getUnitVector(rotation).negative().multiply(SHELL_SPEED);
Vector2D pos = Vector2D.rotateAround(new Vector2D(xCenterAbs, yCenterAbs), 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.create(new Shell(pos, new Vector2D(10, 10), shellVel));
} }

Binary file not shown.