339 lines
9.2 KiB
Java
339 lines
9.2 KiB
Java
package de.samdev.colorrunner.game.world.entities;
|
|
|
|
import com.badlogic.gdx.Gdx;
|
|
import com.badlogic.gdx.math.Rectangle;
|
|
import com.badlogic.gdx.math.Vector2;
|
|
|
|
import de.samdev.colorrunner.game.world.CRGameWorld;
|
|
|
|
public abstract class MovingEntity extends CRGameEntity {
|
|
protected Vector2 velocity = new Vector2();
|
|
|
|
public final static float COLL_PUSHBACK_EPSILON = 1e-5F;
|
|
public final static float COLL_DETECT_EPSILON = 2e-5f;
|
|
public final static float EXPAND_EPSILON = 1e-6f;
|
|
|
|
private boolean face_TOP_isTouching = false;
|
|
private boolean face_LEFT_isTouching = false;
|
|
private boolean face_BOTTOM_isTouching = false;
|
|
private boolean face_RIGHT_isTouching = false;
|
|
|
|
public MovingEntity(CRGameWorld _owner, float width, float height) {
|
|
this(_owner, 0, 0, width, height);
|
|
}
|
|
|
|
public MovingEntity(CRGameWorld _owner, float x, float y, float width, float height) {
|
|
super(_owner, x, y, width, height);
|
|
}
|
|
|
|
protected boolean moveByX(float bx) {
|
|
if (bx == 0) return false;
|
|
|
|
boolean collided = false;
|
|
|
|
Rectangle next = new Rectangle(bounds);
|
|
|
|
next.x += bx;
|
|
next.set(next.x - COLL_PUSHBACK_EPSILON, next.y - COLL_PUSHBACK_EPSILON, next.width + 2*COLL_PUSHBACK_EPSILON, next.height + 2*COLL_PUSHBACK_EPSILON);
|
|
|
|
for (CRGameEntity ent : world.entities) {
|
|
if (ent == this) continue;
|
|
|
|
if (ent.bounds.overlaps(next) && ent.canCollide(true, this)) {
|
|
|
|
if (ent.bounds.overlaps(bounds)) {
|
|
Gdx.app.log("Collision", "Ignore in bounds collision");
|
|
continue;
|
|
}
|
|
|
|
if (bx < 0) { // DOWN
|
|
float correction = (ent.bounds.x + ent.bounds.height) - next.x;
|
|
|
|
next.x += correction;
|
|
bx += correction;
|
|
|
|
if (bx >= -COLL_PUSHBACK_EPSILON)
|
|
return true; // collision but no movement
|
|
|
|
collided = true;
|
|
} else { // UP
|
|
float correction = ent.bounds.x - (next.x + next.height);
|
|
|
|
next.x += correction;
|
|
bx += correction;
|
|
|
|
if (bx <= COLL_PUSHBACK_EPSILON)
|
|
return true; // collision but no movement
|
|
|
|
collided = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
bounds.x += bx;
|
|
return collided;
|
|
}
|
|
|
|
protected boolean moveByY(float by) {
|
|
if (by == 0) return false;
|
|
|
|
boolean collided = false;
|
|
|
|
Rectangle next = new Rectangle(bounds);
|
|
|
|
next.y += by;
|
|
next.set(next.x - COLL_PUSHBACK_EPSILON, next.y - COLL_PUSHBACK_EPSILON, next.width + 2*COLL_PUSHBACK_EPSILON, next.height + 2*COLL_PUSHBACK_EPSILON);
|
|
|
|
for (CRGameEntity ent : world.entities) {
|
|
if (ent == this) continue;
|
|
|
|
if (ent.bounds.overlaps(next) && ent.canCollide(true, this)) {
|
|
|
|
if (ent.bounds.overlaps(bounds)) {
|
|
Gdx.app.log("Collision", "Ignore in bounds collision");
|
|
continue;
|
|
}
|
|
|
|
if (by < 0) { // DOWN
|
|
float correction = (ent.bounds.y + ent.bounds.height) - next.y;
|
|
|
|
next.y += correction;
|
|
by += correction;
|
|
|
|
if (by >= -COLL_PUSHBACK_EPSILON)
|
|
return true; // collision but no movement
|
|
|
|
collided = true;
|
|
} else { // UP
|
|
float correction = ent.bounds.y - (next.y + next.height);
|
|
|
|
next.y += correction;
|
|
by += correction;
|
|
|
|
if (by <= COLL_PUSHBACK_EPSILON)
|
|
return true; // collision but no movement
|
|
|
|
collided = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
bounds.y += by;
|
|
return collided;
|
|
}
|
|
|
|
protected boolean updateHitBox(float width, float height) {
|
|
boolean succ_x = updateHitBoxWidth(width);
|
|
boolean succ_y = updateHitBoxHeight(height);
|
|
|
|
return succ_x && succ_y;
|
|
}
|
|
|
|
private boolean updateHitBoxWidth(float width) {
|
|
Rectangle next = new Rectangle(bounds);
|
|
|
|
float add = width - bounds.width;
|
|
|
|
if (isTouching_LEFT() && ! isTouching_RIGHT()) {
|
|
next.width += add;
|
|
next.x -= add;
|
|
} else if (! isTouching_LEFT() && isTouching_RIGHT()) {
|
|
next.width += add;
|
|
} else {
|
|
next.width += add;
|
|
next.x -= add/2;
|
|
}
|
|
|
|
|
|
if (add < 0) {
|
|
bounds.set(next);
|
|
return true;
|
|
} else {
|
|
next.set(next.x - COLL_PUSHBACK_EPSILON, next.y - COLL_PUSHBACK_EPSILON, next.width + 2*COLL_PUSHBACK_EPSILON, next.height + 2*COLL_PUSHBACK_EPSILON);
|
|
|
|
for (CRGameEntity ent : world.entities) {
|
|
if (ent == this) continue;
|
|
|
|
if (ent.bounds.overlaps(next) && ent.canCollide(false, this)) {
|
|
|
|
if (ent.bounds.overlaps(bounds)) {
|
|
Gdx.app.log("Collision", "Ignore in bounds collision on HB Expand [X]");
|
|
continue;
|
|
}
|
|
|
|
if (ent.bounds.x > next.x) {
|
|
next.x -= (next.x + next.width) - ent.bounds.x;
|
|
|
|
for (CRGameEntity ent2 : world.entities) {
|
|
if (ent2 == this) continue;
|
|
|
|
if (ent2.bounds.overlaps(next) && ent2.canCollide(false, this)) {
|
|
Gdx.app.log("Entity Hitbox", "Expand failed X+");
|
|
return false;
|
|
}
|
|
}
|
|
} else if (ent.bounds.x < next.x) {
|
|
next.x += (ent.bounds.x + ent.bounds.width) - next.x;
|
|
|
|
for (CRGameEntity ent2 : world.entities) {
|
|
if (ent2 == this) continue;
|
|
|
|
if (ent2.bounds.overlaps(next) && ent2.canCollide(false, this)) {
|
|
Gdx.app.log("Entity Hitbox", "Expand failed X-");
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
bounds.set(next.x + COLL_PUSHBACK_EPSILON, next.y + COLL_PUSHBACK_EPSILON, next.width - 2*COLL_PUSHBACK_EPSILON, next.height - 2*COLL_PUSHBACK_EPSILON);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
private boolean updateHitBoxHeight(float height) {
|
|
Rectangle next = new Rectangle(bounds);
|
|
|
|
float add = height - bounds.height;
|
|
|
|
if (isTouching_BOTTOM() && ! isTouching_TOP()) {
|
|
next.height += add;
|
|
} else if (! isTouching_BOTTOM() && isTouching_TOP()) {
|
|
next.height += add;
|
|
next.y -= add;
|
|
} else {
|
|
next.height += add;
|
|
next.y -= add/2;
|
|
}
|
|
|
|
|
|
if (add < 0) {
|
|
bounds.set(next);
|
|
return true;
|
|
} else {
|
|
next.set(next.x - COLL_PUSHBACK_EPSILON, next.y - COLL_PUSHBACK_EPSILON, next.width + 2*COLL_PUSHBACK_EPSILON, next.height + 2*COLL_PUSHBACK_EPSILON);
|
|
|
|
for (CRGameEntity ent : world.entities) {
|
|
if (ent == this) continue;
|
|
|
|
if (ent.bounds.overlaps(next) && ent.canCollide(false, this)) {
|
|
|
|
if (ent.bounds.overlaps(bounds)) {
|
|
Gdx.app.log("Collision", "Ignore in bounds collision on HB Expand [Y]");
|
|
continue;
|
|
}
|
|
|
|
if (ent.bounds.y > next.y) {
|
|
next.y -= (next.y + next.height) - ent.bounds.y;
|
|
|
|
for (CRGameEntity ent2 : world.entities) {
|
|
if (ent2 == this) continue;
|
|
|
|
if (ent2.bounds.overlaps(next) && ent2.canCollide(false, this)) {
|
|
Gdx.app.log("Entity Hitbox", "Expand failed Y+");
|
|
return false;
|
|
}
|
|
}
|
|
} else if (ent.bounds.y < next.y) {
|
|
next.y += (ent.bounds.y + ent.bounds.height) - next.y;
|
|
|
|
for (CRGameEntity ent2 : world.entities) {
|
|
if (ent2 == this) continue;
|
|
|
|
if (ent2.bounds.overlaps(next) && ent2.canCollide(false, this)) {
|
|
Gdx.app.log("Entity Hitbox", "Expand failed Y-");
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
bounds.set(next.x + COLL_PUSHBACK_EPSILON, next.y + COLL_PUSHBACK_EPSILON, next.width - 2*COLL_PUSHBACK_EPSILON, next.height - 2*COLL_PUSHBACK_EPSILON);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
private void updateTouchCollisions() {
|
|
face_TOP_isTouching = false;
|
|
face_LEFT_isTouching = false;
|
|
face_BOTTOM_isTouching = false;
|
|
face_RIGHT_isTouching = false;
|
|
|
|
for (CRGameEntity ent : world.entities) {
|
|
if (ent == this) continue;
|
|
|
|
if (Math.abs((ent.bounds.y + ent.bounds.height) - bounds.y) < COLL_DETECT_EPSILON && ent.bounds.x < (bounds.x + bounds.width) && (ent.bounds.x + ent.bounds.width) > bounds.x && ent.canCollide(false, this)) {
|
|
face_BOTTOM_isTouching = true;
|
|
}
|
|
|
|
if (Math.abs((bounds.y + bounds.height) - ent.bounds.y) < COLL_DETECT_EPSILON && ent.bounds.x < (bounds.x + bounds.width) && (ent.bounds.x + ent.bounds.width) > bounds.x && ent.canCollide(false, this)) {
|
|
face_TOP_isTouching = true;
|
|
}
|
|
|
|
if (Math.abs((bounds.x + bounds.width) - ent.bounds.x) < COLL_DETECT_EPSILON && ent.bounds.y < (bounds.y + bounds.height) && (ent.bounds.y + ent.bounds.height) > bounds.y && ent.canCollide(false, this)) {
|
|
face_RIGHT_isTouching = true;
|
|
}
|
|
|
|
if (Math.abs((ent.bounds.x + ent.bounds.width) - bounds.x) < COLL_DETECT_EPSILON && ent.bounds.y < (bounds.y + bounds.height) && (ent.bounds.y + ent.bounds.height) > bounds.y && ent.canCollide(false, this)) {
|
|
face_LEFT_isTouching = true;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public Vector2 getVelocity() {
|
|
return velocity;
|
|
}
|
|
|
|
@Override
|
|
public void update(float delta) {
|
|
if (velocity.x != 0) {
|
|
if (moveByX(velocity.x * delta)) {
|
|
velocity.x = 0;
|
|
}
|
|
for (CRGameEntity ent : world.entities) {
|
|
if (ent == this) continue;
|
|
if (ent.bounds.overlaps(bounds) && ent.canCollide(true, this))
|
|
Gdx.app.error("Collision", "Collision after move X");
|
|
}
|
|
}
|
|
|
|
if (velocity.y != 0) {
|
|
if (moveByY(velocity.y * delta)) {
|
|
velocity.y = 0;
|
|
}
|
|
for (CRGameEntity ent : world.entities) {
|
|
if (ent == this) continue;
|
|
if (ent.bounds.overlaps(bounds) && ent.canCollide(true, this))
|
|
Gdx.app.error("Collision", "Collision after Y");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
updateTouchCollisions();
|
|
}
|
|
|
|
public boolean isTouching_ANY() {
|
|
return face_TOP_isTouching || face_LEFT_isTouching || face_BOTTOM_isTouching || face_RIGHT_isTouching;
|
|
}
|
|
|
|
public boolean isTouching_TOP() {
|
|
return face_TOP_isTouching;
|
|
}
|
|
|
|
public boolean isTouching_LEFT() {
|
|
return face_LEFT_isTouching;
|
|
}
|
|
|
|
public boolean isTouching_BOTTOM() {
|
|
return face_BOTTOM_isTouching;
|
|
}
|
|
|
|
public boolean isTouching_RIGHT() {
|
|
return face_RIGHT_isTouching;
|
|
}
|
|
}
|