mirror of
https://github.com/Noratrieb/Java2DGame.git
synced 2026-01-14 14:05:01 +01:00
Better collidable implementation
This commit is contained in:
parent
69f784b913
commit
435de55eb1
12 changed files with 77 additions and 49 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package objects;
|
||||
|
||||
import core.math.Vector2D;
|
||||
import objects.core.GameObject;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
|
|
|
|||
56
src/main/java/objects/core/CollGameObject.java
Normal file
56
src/main/java/objects/core/CollGameObject.java
Normal 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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
package objects.ships;
|
||||
|
||||
import objects.GameObject;
|
||||
import objects.core.GameObject;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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.*;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package objects.world;
|
||||
|
||||
import core.Master;
|
||||
import objects.GameObject;
|
||||
import objects.core.GameObject;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
|
|
|
|||
|
|
@ -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.
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue