Added Collision Detection

This commit is contained in:
Mike Schwörer 2014-08-10 19:41:06 +02:00
parent d6f8f324ad
commit 59b679756a
10 changed files with 229 additions and 57 deletions

View File

@ -53,7 +53,7 @@ public abstract class AbstractGameRenderer {
if (cam_width > MAX_GAME_WIDTH)
Gdx.app.error("SIZE", "Maximal aspect ratio is 2:1");
cam.setToOrtho(true, cam_width, GAME_HEIGHT);
cam.setToOrtho(false, cam_width, GAME_HEIGHT);
shapeRenderer.setProjectionMatrix(cam.combined);
}

View File

@ -3,17 +3,48 @@ package de.samdev.colorrunner.game.world;
import java.util.ArrayList;
import java.util.List;
import com.badlogic.gdx.Gdx;
import de.samdev.colorrunner.game.world.entities.CRGameEntity;
import de.samdev.colorrunner.game.world.entities.gameentities.PlayerEntity;
import de.samdev.colorrunner.game.world.entities.gameentities.FloorTileEntity;
import de.samdev.colorrunner.input.GameInputListener;
import de.samdev.colorrunner.input.SwipeDirection;
public class CRGameWorld {
public class CRGameWorld implements GameInputListener {
public PlayerEntity player;
public List<CRGameEntity> entities = new ArrayList<CRGameEntity>();
public FPSCounter fps = new FPSCounter();
public CRGameWorld() {
addEntity(new PlayerEntity());
addEntity(player = new PlayerEntity(this, 40, 290));
addEntity(new FloorTileEntity(this, 1*32, 32));
addEntity(new FloorTileEntity(this, 2*32, 32));
addEntity(new FloorTileEntity(this, 3*32, 32));
addEntity(new FloorTileEntity(this, 4*32, 32));
addEntity(new FloorTileEntity(this, 5*32, 32));
addEntity(new FloorTileEntity(this, 6*32, 32));
addEntity(new FloorTileEntity(this, 7*32, 32));
addEntity(new FloorTileEntity(this, 8*32, 32));
addEntity(new FloorTileEntity(this, 9*32, 32));
addEntity(new FloorTileEntity(this, 11*32, 456));
addEntity(new FloorTileEntity(this, 12*32, 456));
addEntity(new FloorTileEntity(this, 13*32, 456));
addEntity(new FloorTileEntity(this, 14*32, 456));
addEntity(new FloorTileEntity(this, 15*32, 456));
addEntity(new FloorTileEntity(this, 16*32, 456));
addEntity(new FloorTileEntity(this, 17*32, 456));
addEntity(new FloorTileEntity(this, 18*32, 456));
addEntity(new FloorTileEntity(this, 19*32, 456));
}
public void update(float delta) {
@ -25,7 +56,30 @@ public class CRGameWorld {
// Gdx.app.log("GameWorld", "update FPS[" + (int)fps.getFPS() + "] DELTA:[" + delta + "]");
}
public void addEntity(CRGameEntity ent) {
public CRGameEntity addEntity(CRGameEntity ent) {
entities.add(ent);
return ent;
}
@Override
public void doJump() {
player.jump();
Gdx.app.log("GameScreen", "[DO] Jump");
}
@Override
public void switchColor(SwipeDirection sd) {
player.switchPhase(sd);
Gdx.app.log("GameScreen", "[DO] Switch + " + sd.toString());
}
@Override
public void doFly() {
player.fly();
Gdx.app.log("GameScreen", "[DO] Fly");
}
}

View File

@ -3,23 +3,27 @@ package de.samdev.colorrunner.game.world.entities;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import de.samdev.colorrunner.game.world.CRGameWorld;
public abstract class CRGameEntity {
protected CRGameWorld world;
public Rectangle bounds = new Rectangle();
protected Vector2 tmp_pos = new Vector2();
public CRGameEntity(float width, float height) {
bounds = new Rectangle(0, 0, width, height);
public CRGameEntity(CRGameWorld _owner, float width, float height) {
this(_owner, 0, 0, width, height);
}
public CRGameEntity(CRGameWorld _owner, float x, float y, float width, float height) {
bounds = new Rectangle(x, y, width, height);
world = _owner;
}
public Vector2 getPosition() {
return bounds.getPosition(tmp_pos);
}
protected void moveBy(float bx, float by) {
bounds.x += bx;
bounds.y += by;
}
public abstract void update(float delta);
}

View File

@ -1,17 +1,30 @@
package de.samdev.colorrunner.game.world.entities;
public abstract class GravityEntity extends MovingEntity {
import de.samdev.colorrunner.game.world.CRGameWorld;
public GravityEntity(float width, float height) {
super(width, height);
// TODO Auto-generated constructor stub
public abstract class GravityEntity extends MovingEntity {
public final static float GRAVITY_FORCE = 10f;
public final static float TERMINAL_VELOCITY = 900f;
public GravityEntity(CRGameWorld _owner, float width, float height) {
this(_owner, 0, 0, width, height);
}
public GravityEntity(CRGameWorld _owner, float x, float y, float width, float height) {
super(_owner, x, y, width, height);
}
@Override
public void update(float delta) {
super.update(delta);
velocity.y -= GRAVITY_FORCE;
if (velocity.y < -TERMINAL_VELOCITY) {
velocity.y = -TERMINAL_VELOCITY;
}
// TODO Auto-generated method stub
if (isOnFloor && velocity.y < 0)
velocity.y = 0;
super.update(delta);
}
}

View File

@ -1,16 +1,106 @@
package de.samdev.colorrunner.game.world.entities;
public abstract class MovingEntity extends CRGameEntity {
import com.badlogic.gdx.math.Vector2;
public MovingEntity(float width, float height) {
super(width, height);
// TODO Auto-generated constructor stub
import de.samdev.colorrunner.game.world.CRGameWorld;
public abstract class MovingEntity extends CRGameEntity {
protected Vector2 velocity = new Vector2();
protected boolean isOnFloor = 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;
bounds.x += bx;
for (CRGameEntity ent : world.entities) {
if (ent == this) continue;
if (ent.bounds.overlaps(bounds)) {
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;
}
}
}
return collided;
}
protected boolean moveByY(float by) {
if (by == 0) return false;
boolean collided = false;
bounds.y += by;
for (CRGameEntity ent : world.entities) {
if (ent == this) continue;
if (ent.bounds.overlaps(bounds)) {
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;
}
}
}
return collided;
}
private void updateOnFloor() {
for (CRGameEntity ent : world.entities) {
if (ent == this) continue;
if (Math.abs((ent.bounds.y + ent.bounds.height) - bounds.y) < 0.00004f && ent.bounds.x < (bounds.x + bounds.width) && (ent.bounds.x + ent.bounds.width) > bounds.x) {
isOnFloor = true;
return;
}
}
isOnFloor = false;
}
@Override
public void update(float delta) {
// TODO Auto-generated method stub
if (velocity.x != 0) {
moveByX(velocity.x * delta);
}
if (velocity.y != 0) {
moveByY(velocity.y * delta);
}
updateOnFloor();
}
}

View File

@ -1,13 +1,14 @@
package de.samdev.colorrunner.game.world.entities.gameentities;
import de.samdev.colorrunner.game.world.CRGameWorld;
import de.samdev.colorrunner.game.world.entities.CRGameEntity;
public class FloorTileEntity extends CRGameEntity {
public final static float FLOORTILE_WIDTH = 32;
public final static float FLOORTILE_HEIGHT = 32;
public FloorTileEntity() {
super(FLOORTILE_WIDTH, FLOORTILE_HEIGHT);
public FloorTileEntity(CRGameWorld _owner, float x, float y) {
super(_owner, x, y, FLOORTILE_WIDTH, FLOORTILE_HEIGHT);
// TODO Auto-generated constructor stub
}

View File

@ -1,19 +1,41 @@
package de.samdev.colorrunner.game.world.entities.gameentities;
import com.badlogic.gdx.Gdx;
import de.samdev.colorrunner.game.world.CRGameWorld;
import de.samdev.colorrunner.game.world.entities.GravityEntity;
import de.samdev.colorrunner.input.SwipeDirection;
public class PlayerEntity extends GravityEntity {
public final static float PLAYER_WIDTH = 32;
public final static float PLAYER_HEIGHT = 32;
public final static float PLAYER_JUMP_FORCE = 456;
public final static float PLAYER_FLY_FORCE = 1.5f;
public PlayerEntity() {
super(PLAYER_WIDTH, PLAYER_HEIGHT);
public PlayerEntity(CRGameWorld _owner, float x, float y) {
super(_owner, x, y, PLAYER_WIDTH, PLAYER_HEIGHT);
velocity.x = 320;
}
@Override
public void update(float delta) {
public void update(float delta) {
super.update(delta);
}
public void jump() {
if (isOnFloor)
velocity.y = PLAYER_JUMP_FORCE;
}
public void switchPhase(SwipeDirection sd) {
// TODO Auto-generated method stub
moveBy(50 * delta, 50 * delta);
}
public void fly() {
if (! isOnFloor)
velocity.y += PLAYER_FLY_FORCE;
}
}

View File

@ -1,5 +1,6 @@
package de.samdev.colorrunner.input;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.input.GestureDetector.GestureListener;
@ -12,6 +13,12 @@ public class CRGameInputProcessor implements InputProcessor, GestureListener {
this.owner = gsc;
}
public void update() {
if (Gdx.input.isKeyPressed(Input.Keys.SPACE) || Gdx.input.isTouched()) {
owner.doFly();
}
}
@Override
public boolean keyDown(int keycode) {
if (keycode == Input.Keys.UP) {

View File

@ -2,5 +2,6 @@ package de.samdev.colorrunner.input;
public interface GameInputListener {
public void doJump();
public void doFly();
public void switchColor(SwipeDirection sd);
}

View File

@ -3,49 +3,42 @@ package de.samdev.colorrunner.screens.gameScreen;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.input.GestureDetector;
import de.samdev.colorrunner.game.renderer.CRGameRenderer;
import de.samdev.colorrunner.game.world.CRGameWorld;
import de.samdev.colorrunner.input.CRGameInputProcessor;
import de.samdev.colorrunner.input.GameInputListener;
import de.samdev.colorrunner.input.SwipeDirection;
public class GameScreen implements Screen, GameInputListener {
private final static float GAME_WIDTH = 500;
public class GameScreen implements Screen {
private CRGameWorld world;
private CRGameRenderer renderer;
private CRGameInputProcessor input = new CRGameInputProcessor(world);
public GameScreen() {
world = new CRGameWorld(); // initialize world
renderer = new CRGameRenderer(world, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); // initialize renderer
input = new CRGameInputProcessor(world);
}
@Override
public void render(float delta) {
world.update(delta);
renderer.render();
input.update();
}
@Override
public void resize(int width, int height) {
Gdx.app.log("GameScreen", "resize called");
float gameHeight = height / (width / GAME_WIDTH);
float gameX = 0;
float gameY = (height - gameHeight) / 2;
renderer.resize(width, height);
}
@Override
public void show() {
CRGameInputProcessor ip = new CRGameInputProcessor(this);
Gdx.input.setInputProcessor(new InputMultiplexer(ip, new GestureDetector(ip)));
Gdx.input.setInputProcessor(new InputMultiplexer(input, new GestureDetector(input)));
Gdx.app.log("GameScreen", "show called");
}
@ -71,17 +64,4 @@ public class GameScreen implements Screen, GameInputListener {
public void dispose() {
Gdx.app.log("GameScreen", "dispose called");
}
@Override
public void doJump() {
Gdx.app.log("GameScreen", "[DO] Jump");
}
@Override
public void switchColor(SwipeDirection sd) {
Gdx.app.log("GameScreen", "[DO] Switch + " + sd.toString());
}
}