mirror of
https://github.com/Noratrieb/Java2DGame.git
synced 2026-01-14 14:05:01 +01:00
destroyed things
This commit is contained in:
parent
435de55eb1
commit
4fb5bcc543
14 changed files with 61 additions and 18 deletions
|
|
@ -129,7 +129,7 @@ public class Master extends JPanel {
|
|||
* @param pos The position
|
||||
*/
|
||||
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) {
|
||||
objects.remove(gameObject);
|
||||
objectBuffer.remove(gameObject);
|
||||
drawables.get(gameObject.getLayer()).remove(gameObject);
|
||||
if (gameObject instanceof Collidable) {
|
||||
collidables.remove(gameObject);
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
package core.math;
|
||||
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* A 2-dimensional Vector that can be used to store position or velocity
|
||||
*/
|
||||
|
|
@ -33,6 +35,15 @@ public class Vector2D {
|
|||
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
|
||||
*
|
||||
|
|
|
|||
|
|
@ -12,12 +12,12 @@ public class DebugPos extends GameObject {
|
|||
public DebugPos(Vector2D position, Vector2D size) {
|
||||
super(position.copy(), size);
|
||||
this.velocity = new Vector2D();
|
||||
this.mainColor = Color.GREEN;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Graphics2D g2d) {
|
||||
g2d.setPaint(Color.green);
|
||||
g2d.drawOval((int) (position.x - size.x / 2), (int) (position.y - size.y / 2), (int) size.x, (int) size.y);
|
||||
drawOval(g2d);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ import core.math.Coords;
|
|||
import core.Drawable;
|
||||
import core.Master;
|
||||
import core.math.Vector2D;
|
||||
import core.physics.Collidable;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
|
|
@ -74,7 +73,7 @@ public abstract class GameObject implements Drawable {
|
|||
*
|
||||
* @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 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void destroy() {
|
||||
master.destroy(this);
|
||||
}
|
||||
|
|
@ -124,8 +137,12 @@ public abstract class GameObject implements Drawable {
|
|||
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() {
|
||||
return new Vector2D(position.x - size.x / 2, position.y - size.y / 2);
|
||||
return getCenterPosition(position);
|
||||
}
|
||||
|
||||
public int getLayer() {
|
||||
|
|
|
|||
|
|
@ -14,6 +14,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, 25, 25, 50, 3));
|
||||
//turrets.add(new Turret(this, 25, 10, 50, 2));
|
||||
//turrets.add(new Turret(this, 25, 70, 50, 2));
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package objects.ships;
|
|||
import core.math.Vector2D;
|
||||
import core.physics.hitboxes.RectHitBox;
|
||||
import objects.core.CollGameObject;
|
||||
import objects.core.GameObject;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
|
|
@ -10,11 +11,11 @@ import java.awt.*;
|
|||
* A shell fired by a cannon
|
||||
*/
|
||||
//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) {
|
||||
super(position, size, new RectHitBox(position, size));
|
||||
super(position, size/*, new RectHitBox(position, size)*/);
|
||||
this.velocity = velocity;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ public class Submarine extends CollGameObject {
|
|||
@Override
|
||||
public void draw(Graphics2D g2d) {
|
||||
g2d.setPaint(Color.BLUE);
|
||||
drawOval(g2d);
|
||||
fillOval(g2d);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -1,9 +1,13 @@
|
|||
package objects.ships;
|
||||
|
||||
import core.Master;
|
||||
import core.math.Coords;
|
||||
import core.math.ExMath;
|
||||
import core.math.Vector2D;
|
||||
import jdk.swing.interop.SwingInterOpUtils;
|
||||
import objects.core.GameObject;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
|
|
@ -25,8 +29,6 @@ public class Turret extends GameObject {
|
|||
|
||||
private long lastShot = 0;
|
||||
|
||||
//private double rotation;
|
||||
|
||||
public Turret(BattleShip battleShip, double x, double y, double size, int barrelAmount) {
|
||||
super(x, y, size, size);
|
||||
this.battleShip = battleShip;
|
||||
|
|
@ -78,28 +80,38 @@ public class Turret extends GameObject {
|
|||
@Override
|
||||
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);
|
||||
int sizeAbs = (int) battleShip.getWorldCoordsFromLocalSize(size).x;
|
||||
int xCenterAbs = (int) (abs.x + sizeAbs / 2);
|
||||
int yCenterAbs = (int) (abs.y + sizeAbs / 2);
|
||||
|
||||
Point msLoc = master.getMouseLocation();
|
||||
double targetRotation = -Math.atan2(xCenterAbs - msLoc.x, yCenterAbs - msLoc.y);
|
||||
double targetRotationOld = -Math.atan2(xCenterAbs - msLoc.x, yCenterAbs - msLoc.y);
|
||||
//----------------
|
||||
|
||||
|
||||
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++) {
|
||||
int barrelX = (int) (abs.x + (i + 1) * barrelSpacing);
|
||||
int frontPosY = (int) (abs.y - sizeAbs / 2);
|
||||
int barrelX = (int) (position.x + (i + 1) * barrelSpacing);
|
||||
int frontPosY = (int) (position.y - size.x / 2);
|
||||
|
||||
if (master.isMousePressed()) {
|
||||
lastShot = System.currentTimeMillis();
|
||||
|
||||
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));
|
||||
}
|
||||
|
|
|
|||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue