Better collidable implementation

This commit is contained in:
nora 2020-12-13 16:14:08 +01:00
parent 69f784b913
commit 435de55eb1
12 changed files with 77 additions and 49 deletions

View file

@ -5,7 +5,7 @@ import core.physics.Collidable;
import core.physics.Collision;
import objects.DebugPos;
import objects.ships.BattleShip;
import objects.GameObject;
import objects.core.GameObject;
import objects.ships.Submarine;
import objects.world.Grid;
import objects.world.Wall;
@ -13,7 +13,6 @@ import objects.world.Wall;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* The main object that controls everything

View file

@ -1,6 +1,7 @@
package objects;
import core.math.Vector2D;
import objects.core.GameObject;
import java.awt.*;

View file

@ -0,0 +1,56 @@
package objects.core;
import core.math.Vector2D;
import core.physics.Collidable;
import core.physics.hitboxes.Hitbox;
/**
* A specialization of GameObject with Collidable properties
*/
public abstract class CollGameObject extends GameObject implements Collidable {
protected Hitbox hitbox;
public CollGameObject(Vector2D position, Vector2D size, Hitbox hitbox) {
super(position, size);
this.hitbox = hitbox;
}
public CollGameObject(double x, double y, double xSize, double ySize, Hitbox hitbox) {
super(x, y, xSize, ySize);
this.hitbox = hitbox;
}
/**
* A simple method to move the object to a Vector2D. This method should be called instead of doing it manually.
* Does not move if it encounters a collision
*
* @param target The target position
*/
@Override
public void moveTo(Vector2D target) {
Vector2D oldPos = position.copy();
this.position = target;
((Collidable) this).getHitbox().moveTo(position, size);
if (master.doesCollide(this) != null) {
this.position = oldPos;
((Collidable) this).getHitbox().moveTo(oldPos, size);
}
}
@Override
public Hitbox getHitbox() {
return hitbox;
}
@Override
public Vector2D getCenterPos() {
return getCenterPosition();
}
@Override
public Vector2D getSize() {
return size;
}
}

View file

@ -1,4 +1,4 @@
package objects;
package objects.core;
import core.math.Coords;
import core.Drawable;
@ -53,16 +53,7 @@ public abstract class GameObject implements Drawable {
* @param target The target position
*/
public void moveTo(Vector2D target) {
Vector2D oldPos = position.copy();
this.position = target;
if (this instanceof Collidable) {
((Collidable) this).getHitbox().moveTo(position, size);
if (master.doesCollide((Collidable) this) != null) {
this.position = oldPos;
((Collidable) this).getHitbox().moveTo(oldPos, size);
}
}
}
/**

View file

@ -1,6 +1,6 @@
package objects.ships;
import objects.GameObject;
import objects.core.GameObject;
import java.awt.*;
import java.util.ArrayList;

View file

@ -1,15 +1,20 @@
package objects.ships;
import core.math.Vector2D;
import objects.GameObject;
import core.physics.hitboxes.RectHitBox;
import objects.core.CollGameObject;
import java.awt.*;
public class Shell extends GameObject {
/**
* A shell fired by a cannon
*/
//TODO why tf do shells not use map coords...
public class Shell extends CollGameObject {
public Shell(Vector2D position, Vector2D size, Vector2D velocity) {
super(position, size);
super(position, size, new RectHitBox(position, size));
this.velocity = velocity;
}
@ -21,6 +26,6 @@ public class Shell extends GameObject {
@Override
public void update() {
position.add(velocity);
moveTo(Vector2D.add(position, velocity));
}
}

View file

@ -2,20 +2,15 @@ package objects.ships;
import core.math.Coords;
import core.math.Vector2D;
import core.physics.Collidable;
import core.physics.hitboxes.Hitbox;
import core.physics.hitboxes.RectHitBox;
import objects.GameObject;
import objects.core.CollGameObject;
import java.awt.*;
public class Submarine extends GameObject implements Collidable {
private final RectHitBox hitbox;
public class Submarine extends CollGameObject {
public Submarine(Vector2D position, Vector2D size) {
super(position, size);
this.hitbox = new RectHitBox(position, size);
super(position, size, new RectHitBox(position, size));
this.mainColor = Color.BLUE;
}
@ -33,18 +28,4 @@ public class Submarine extends GameObject implements Collidable {
moveTo(centerRelPos);
}
@Override
public Hitbox getHitbox() {
return hitbox;
}
@Override
public Vector2D getCenterPos() {
return getCenterPosition();
}
@Override
public Vector2D getSize() {
return size;
}
}

View file

@ -2,7 +2,7 @@ package objects.ships;
import core.math.ExMath;
import core.math.Vector2D;
import objects.GameObject;
import objects.core.GameObject;
import java.awt.*;

View file

@ -1,7 +1,6 @@
package objects.world;
import core.Master;
import objects.GameObject;
import objects.core.GameObject;
import java.awt.*;

View file

@ -1,20 +1,16 @@
package objects.world;
import core.math.Vector2D;
import core.physics.Collidable;
import core.physics.hitboxes.Hitbox;
import core.physics.hitboxes.RectHitBox;
import objects.GameObject;
import objects.core.CollGameObject;
import java.awt.*;
public class Wall extends GameObject implements Collidable {
private final RectHitBox hitbox;
public class Wall extends CollGameObject {
public Wall(double x, double y, double xSize, double ySize) {
super(x, y, xSize, ySize);
this.hitbox = new RectHitBox(new Vector2D(x, y), new Vector2D(xSize, ySize));
super(x, y, xSize, ySize, new RectHitBox(new Vector2D(x, y), new Vector2D(xSize, ySize)));
}
@Override

Binary file not shown.