"fixed" sub

This commit is contained in:
nora 2020-12-12 14:57:30 +01:00
parent 87ca37c9ca
commit 3566916125
6 changed files with 43 additions and 5 deletions

View file

@ -76,7 +76,7 @@ public class Master extends JPanel {
create(bs); create(bs);
create(battleShip); create(battleShip);
create(new Submarine(new Vector2D(), new Vector2D(20, 20))); create(new Submarine(new Vector2D(), new Vector2D(5, 5)));
create(new Wall(20, 80, 50, 2)); create(new Wall(20, 80, 50, 2));
} }
@ -173,8 +173,14 @@ public class Master extends JPanel {
for (Collidable c : collidables) { for (Collidable c : collidables) {
double distance = Vector2D.distance(c.getCenterPos(), col.getCenterPos()); double distance = Vector2D.distance(c.getCenterPos(), col.getCenterPos());
if (!(distance > c.getSize().magnitude() && distance > col.getSize().magnitude())) {
if (c != col /*&& !(distance > c.getSize().magnitude() && distance > col.getSize().magnitude())*/) {
System.out.println("\n\nCOLL POSSIBLE");
System.out.println("This: " + c.getHitbox() + "\n\nother: " + col.getHitbox());
if (c.collidesWith(col)) { if (c.collidesWith(col)) {
System.err.println("COLL");
collides = true; collides = true;
} }
} }

View file

@ -1,8 +1,13 @@
package core.physics; package core.physics;
import core.Drawable; import core.Drawable;
import core.math.Vector2D;
import objects.GameObject; import objects.GameObject;
public abstract class Hitbox implements Drawable { public abstract class Hitbox implements Drawable {
@Override
public String toString() {
return super.toString();
}
} }

View file

@ -79,6 +79,24 @@ public abstract class GameObject implements Drawable {
g2d.fillRect(xAbs, yAbs, sizeXAbs, sizeYAbs); g2d.fillRect(xAbs, yAbs, sizeXAbs, sizeYAbs);
} }
/**
* This method draws a rectangle at the current position and size
*
* @param g2d The Graphics2D object provided by the master
* @param w The width of the screen
*/
public void drawOval(Graphics2D g2d, int w) {
this.w = w;
h = (int) (this.w / Master.SCREEN_RATIO);
int xAbs = (int) getWorldCoords(position.x, true);
int yAbs = (int) getWorldCoords(position.y, false);
int sizeXAbs = (int) getWorldCoordsSize(size.x, true);
int sizeYAbs = (int) getWorldCoordsSize(size.y, false);
g2d.setPaint(mainColor);
g2d.fillOval(xAbs, yAbs, sizeXAbs, sizeYAbs);
}
/** /**
* This method draws a rounded rectangle at the current position and size * This method draws a rounded rectangle at the current position and size
* *
@ -107,6 +125,12 @@ public abstract class GameObject implements Drawable {
} }
} }
public Vector2D getMapCoordsFromWorld(Vector2D value){
double x = (value.x / w) * Master.SCREEN_Y_COORDINATES * Master.SCREEN_RATIO;
double y = (value.y / h) * Master.SCREEN_Y_COORDINATES;
return new Vector2D(x, y);
}
public double getWorldCoordsSize(double value, boolean isX) { public double getWorldCoordsSize(double value, boolean isX) {
if (isX) { if (isX) {
return (value / Master.SCREEN_Y_COORDINATES * w); return (value / Master.SCREEN_Y_COORDINATES * w);
@ -141,6 +165,6 @@ public abstract class GameObject implements Drawable {
} }
public Vector2D getCenterPosition(){ public Vector2D getCenterPosition(){
return new Vector2D(position.x + size.x, position.y + size.y); return new Vector2D(position.x - size.x/2, position.y - size.y/2);
} }
} }

View file

@ -16,18 +16,21 @@ public class Submarine extends GameObject implements Collidable {
public Submarine(Vector2D position, Vector2D size) { public Submarine(Vector2D position, Vector2D size) {
super(position, size); super(position, size);
this.hitbox = new RectHitBox(position, size); this.hitbox = new RectHitBox(position, size);
this.mainColor = Color.BLUE;
} }
@Override @Override
public void draw(Graphics2D g2d, int w, Master master) { public void draw(Graphics2D g2d, int w, Master master) {
g2d.setPaint(Color.BLUE); g2d.setPaint(Color.BLUE);
g2d.fillOval((int) (position.x - size.x / 2), (int) (position.y - size.y / 2), (int) size.x, (int) size.y); drawOval(g2d, w);
} }
@Override @Override
public void update(Master master) { public void update(Master master) {
Point mouse = master.getMouseLocation(); Point mouse = master.getMouseLocation();
moveTo(new Vector2D(mouse.x, mouse.y), master); Vector2D relPos = getMapCoordsFromWorld(new Vector2D(mouse.x, mouse.y));
Vector2D centerRelPos = new Vector2D(relPos.x - size.x/2, relPos.y - size.y/2);
moveTo(centerRelPos, master);
} }
@Override @Override

Binary file not shown.