ColorRunner/core/src/de/samdev/colorrunner/game/world/entities/MovingEntity.java

303 lines
7.5 KiB
Java
Raw Normal View History

2014-08-10 17:19:40 +02:00
package de.samdev.colorrunner.game.world.entities;
2014-08-10 20:59:50 +02:00
import com.badlogic.gdx.Gdx;
2014-08-11 00:24:55 +02:00
import com.badlogic.gdx.math.Rectangle;
2014-08-10 19:41:06 +02:00
import com.badlogic.gdx.math.Vector2;
import de.samdev.colorrunner.game.world.CRGameWorld;
2014-08-10 17:19:40 +02:00
2014-08-10 19:41:06 +02:00
public abstract class MovingEntity extends CRGameEntity {
protected Vector2 velocity = new Vector2();
2014-08-10 20:59:50 +02:00
private boolean face_TOP_isTouching = false;
private boolean face_LEFT_isTouching = false;
private boolean face_BOTTOM_isTouching = false;
private boolean face_RIGHT_isTouching = false;
2014-08-10 19:41:06 +02:00
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);
2014-08-10 17:19:40 +02:00
}
2014-08-10 19:41:06 +02:00
protected boolean moveByX(float bx) {
if (bx == 0) return false;
boolean collided = false;
2014-08-11 00:24:55 +02:00
Rectangle original = new Rectangle(bounds);
2014-08-10 19:41:06 +02:00
bounds.x += bx;
for (CRGameEntity ent : world.entities) {
if (ent == this) continue;
2014-08-11 00:24:55 +02:00
if (ent.bounds.overlaps(bounds) && ent.canCollide(true, this)) {
if (ent.bounds.overlaps(original)) {
Gdx.app.log("Collision", "Ignore in bounds collision");
continue;
}
2014-08-10 19:41:06 +02:00
if (bx < 0) { // LEFT
float correction = (ent.bounds.x + ent.bounds.width) - bounds.x;
bounds.x += correction;
bx += correction;
collided = true;
} else { // RIGHT
float correction = ent.bounds.x - (bounds.x + bounds.width);
bounds.x += correction;
bx += correction;
2014-08-10 20:59:50 +02:00
collided = true;
2014-08-10 19:41:06 +02:00
}
}
}
return collided;
}
protected boolean moveByY(float by) {
if (by == 0) return false;
boolean collided = false;
2014-08-11 00:24:55 +02:00
Rectangle original = new Rectangle(bounds);
2014-08-10 19:41:06 +02:00
bounds.y += by;
for (CRGameEntity ent : world.entities) {
if (ent == this) continue;
2014-08-11 00:24:55 +02:00
if (ent.bounds.overlaps(bounds) && ent.canCollide(true, this)) {
if (ent.bounds.overlaps(original)) {
Gdx.app.log("Collision", "Ignore in bounds collision");
continue;
}
2014-08-10 19:41:06 +02:00
if (by < 0) { // DOWN
float correction = (ent.bounds.y + ent.bounds.height) - bounds.y;
bounds.y += correction;
by += correction;
collided = true;
} else { // UP
float correction = ent.bounds.y - (bounds.y + bounds.height);
bounds.y += correction;
by += correction;
2014-08-10 20:59:50 +02:00
collided = true;
2014-08-10 19:41:06 +02:00
}
}
}
return collided;
}
2014-08-11 15:36:03 +02:00
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 original = new Rectangle(bounds);
float add = width - bounds.width;
if (isTouching_LEFT() && ! isTouching_RIGHT()) {
bounds.width += add;
bounds.x -= add;
} else if (! isTouching_LEFT() && isTouching_RIGHT()) {
bounds.width += add;
} else {
bounds.width += add;
bounds.x -= add/2;
}
if (add < 0) {
return true;
} else {
for (CRGameEntity ent : world.entities) {
if (ent == this) continue;
if (ent.bounds.overlaps(bounds) && ent.canCollide(false, this)) {
if (ent.bounds.overlaps(original)) {
Gdx.app.log("Collision", "Ignore in bounds collision on HB Expand");
continue;
}
if (ent.bounds.x > bounds.x) {
bounds.x -= (bounds.x + bounds.width) - ent.bounds.x;
for (CRGameEntity ent2 : world.entities) {
if (ent2 == this) continue;
if (ent2.bounds.overlaps(bounds) && ent2.canCollide(false, this)) {
bounds = original;
Gdx.app.log("Entity Hitbox", "Expand failed X+");
return false;
}
}
} else if (ent.bounds.x < bounds.x) {
bounds.x += (ent.bounds.x + ent.bounds.width) - bounds.x;
for (CRGameEntity ent2 : world.entities) {
if (ent2 == this) continue;
if (ent2.bounds.overlaps(bounds) && ent2.canCollide(false, this)) {
bounds = original;
Gdx.app.log("Entity Hitbox", "Expand failed X-");
return false;
}
}
}
}
}
return true;
}
}
private boolean updateHitBoxHeight(float height) {
Rectangle original = new Rectangle(bounds);
float add = height - bounds.height;
if (isTouching_BOTTOM() && ! isTouching_TOP()) {
bounds.height += add;
} else if (! isTouching_BOTTOM() && isTouching_TOP()) {
bounds.height += add;
bounds.y -= add;
} else {
bounds.height += add;
bounds.y -= add/2;
}
if (add < 0) {
return true;
} else {
for (CRGameEntity ent : world.entities) {
if (ent == this) continue;
if (ent.bounds.overlaps(bounds) && ent.canCollide(false, this)) {
if (ent.bounds.overlaps(original)) {
Gdx.app.log("Collision", "Ignore in bounds collision on HB Expand");
continue;
}
if (ent.bounds.y > bounds.y) {
bounds.y -= (bounds.y + bounds.height) - ent.bounds.y;
for (CRGameEntity ent2 : world.entities) {
if (ent2 == this) continue;
if (ent2.bounds.overlaps(bounds) && ent2.canCollide(false, this)) {
bounds = original;
Gdx.app.log("Entity Hitbox", "Expand failed Y+");
return false;
}
}
} else if (ent.bounds.y < bounds.y) {
bounds.y += (ent.bounds.y + ent.bounds.height) - bounds.y;
for (CRGameEntity ent2 : world.entities) {
if (ent2 == this) continue;
if (ent2.bounds.overlaps(bounds) && ent2.canCollide(false, this)) {
bounds = original;
Gdx.app.log("Entity Hitbox", "Expand failed Y-");
return false;
}
}
}
}
}
return true;
}
}
2014-08-10 20:59:50 +02:00
private void updateTouchCollisions() {
face_TOP_isTouching = false;
face_LEFT_isTouching = false;
face_BOTTOM_isTouching = false;
face_RIGHT_isTouching = false;
2014-08-10 19:41:06 +02:00
for (CRGameEntity ent : world.entities) {
if (ent == this) continue;
2014-08-11 00:24:55 +02:00
if (Math.abs((ent.bounds.y + ent.bounds.height) - bounds.y) < F_EPSILON && ent.bounds.x < (bounds.x + bounds.width) && (ent.bounds.x + ent.bounds.width) > bounds.x && ent.canCollide(false, this)) {
2014-08-10 20:59:50 +02:00
face_BOTTOM_isTouching = true;
}
2014-08-11 00:24:55 +02:00
if (Math.abs((bounds.y + bounds.height) - ent.bounds.y) < F_EPSILON && ent.bounds.x < (bounds.x + bounds.width) && (ent.bounds.x + ent.bounds.width) > bounds.x && ent.canCollide(false, this)) {
2014-08-10 20:59:50 +02:00
face_TOP_isTouching = true;
2014-08-10 19:41:06 +02:00
}
2014-08-11 00:24:55 +02:00
if (Math.abs((bounds.x + bounds.width) - ent.bounds.x) < F_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) < F_EPSILON && ent.bounds.y < (bounds.y + bounds.height) && (ent.bounds.y + ent.bounds.height) > bounds.y && ent.canCollide(false, this)) {
face_LEFT_isTouching = true;
}
2014-08-10 19:41:06 +02:00
}
2014-08-10 20:59:50 +02:00
}
public Vector2 getVelocity() {
return velocity;
2014-08-10 19:41:06 +02:00
}
2014-08-10 17:19:40 +02:00
@Override
public void update(float delta) {
2014-08-10 19:41:06 +02:00
if (velocity.x != 0) {
2014-08-10 20:59:50 +02:00
if (moveByX(velocity.x * delta)) {
velocity.x = 0;
}
2014-08-10 19:41:06 +02:00
}
if (velocity.y != 0) {
2014-08-10 20:59:50 +02:00
if (moveByY(velocity.y * delta)) {
velocity.y = 0;
}
2014-08-10 19:41:06 +02:00
}
2014-08-10 20:59:50 +02:00
updateTouchCollisions();
}
public boolean isTouching_TOP() {
return face_TOP_isTouching;
2014-08-10 17:19:40 +02:00
}
2014-08-10 20:59:50 +02:00
public boolean isTouching_LEFT() {
return face_LEFT_isTouching;
}
public boolean isTouching_BOTTOM() {
return face_BOTTOM_isTouching;
}
public boolean isTouching_RIGHT() {
return face_RIGHT_isTouching;
}
2014-08-10 17:19:40 +02:00
}