This commit is contained in:
Mike Schwörer 2014-08-11 15:36:03 +02:00
parent 30f911bf4f
commit 82be5b223e
7 changed files with 208 additions and 30 deletions

View File

@ -5,6 +5,7 @@ import com.badlogic.gdx.Game;
import de.samdev.colorrunner.screens.gameScreen.GameScreen;
public class CRGame extends Game {
public final static boolean DEBUG = true;
private GameScreen gameScreen;

View File

@ -21,63 +21,63 @@ public class CRAssets {
static {
{
Pixmap pixmap = new Pixmap(64, 64, Format.RGBA8888);
Pixmap pixmap = new Pixmap(32, 32, Format.RGBA8888);
pixmap.setColor(1, 0, 0, 1);
pixmap.fill();
TEX_PLAYER_UP = new Texture(pixmap);
}
{
Pixmap pixmap = new Pixmap(64, 64, Format.RGBA8888);
Pixmap pixmap = new Pixmap(32, 32, Format.RGBA8888);
pixmap.setColor(0, 1, 0, 1);
pixmap.fill();
TEX_PLAYER_RIGHT = new Texture(pixmap);
}
{
Pixmap pixmap = new Pixmap(64, 64, Format.RGBA8888);
Pixmap pixmap = new Pixmap(32, 32, Format.RGBA8888);
pixmap.setColor(0, 0, 1, 1);
pixmap.fill();
TEX_PLAYER_DOWN = new Texture(pixmap);
}
{
Pixmap pixmap = new Pixmap(64, 64, Format.RGBA8888);
Pixmap pixmap = new Pixmap(32, 32, Format.RGBA8888);
pixmap.setColor(1, 1, 0, 1);
pixmap.fill();
TEX_PLAYER_LEFT = new Texture(pixmap);
}
{
Pixmap pixmap = new Pixmap(64, 64, Format.RGBA8888);
Pixmap pixmap = new Pixmap(32, 32, Format.RGBA8888);
pixmap.setColor(1, 0, 0, 1);
pixmap.fill();
TEX_FLOORTILE_UP = new Texture(pixmap);
}
{
Pixmap pixmap = new Pixmap(64, 64, Format.RGBA8888);
Pixmap pixmap = new Pixmap(32, 32, Format.RGBA8888);
pixmap.setColor(0, 1, 0, 1);
pixmap.fill();
TEX_FLOORTILE_RIGHT = new Texture(pixmap);
}
{
Pixmap pixmap = new Pixmap(64, 64, Format.RGBA8888);
Pixmap pixmap = new Pixmap(32, 32, Format.RGBA8888);
pixmap.setColor(0, 0, 1, 1);
pixmap.fill();
TEX_FLOORTILE_DOWN = new Texture(pixmap);
}
{
Pixmap pixmap = new Pixmap(64, 64, Format.RGBA8888);
Pixmap pixmap = new Pixmap(32, 32, Format.RGBA8888);
pixmap.setColor(1, 1, 0, 1);
pixmap.fill();
TEX_FLOORTILE_LEFT = new Texture(pixmap);
}
{
Pixmap pixmap = new Pixmap(64, 64, Format.RGBA8888);
Pixmap pixmap = new Pixmap(32, 32, Format.RGBA8888);
pixmap.setColor(0, 0, 0, 1);
pixmap.fill();
TEX_FLOORTILE_STATELESS = new Texture(pixmap);

View File

@ -1,10 +1,12 @@
package de.samdev.colorrunner.game.renderer;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import de.samdev.colorrunner.CRGame;
import de.samdev.colorrunner.game.world.CRGameWorld;
import de.samdev.colorrunner.game.world.entities.CRGameEntity;
import de.samdev.colorrunner.game.world.entities.MovingEntity;
@ -21,14 +23,14 @@ public class CRGameRenderer extends AbstractGameRenderer {
@Override
public void doRender() {
updateCameraOffset(gameworld.player.bounds.x);
updateCameraOffset(gameworld.player.bounds.x + gameworld.player.bounds.width/2);
gameworld.camViewBoundaries = getCamViewRectangle();
renderDebugInfo();
if (CRGame.DEBUG) renderDebugInfo();
renderMain();
renderDebugBoxes();
if (CRGame.DEBUG) renderDebugBoxes();
}
private void renderDebugBoxes() {
@ -53,10 +55,16 @@ public class CRGameRenderer extends AbstractGameRenderer {
spriteBatch.begin();
for (CRGameEntity e : gameworld.entities) {
spriteBatch.draw(e.getTexture(), e.bounds.x, e.bounds.y, e.bounds.width, e.bounds.height);
TextureRegion tr = new TextureRegion(e.getTexture());
spriteBatch.draw(new TextureRegion(e.getTexture()),
e.bounds.x + (e.bounds.width - tr.getRegionWidth())/2, e.bounds.y + (e.bounds.height - tr.getRegionHeight())/2,
tr.getRegionWidth()/2, tr.getRegionHeight()/2,
tr.getRegionWidth(), tr.getRegionHeight(),
e.visualZoomX, e.visualZoomY,
e.visualRotation, true);
}
spriteBatch.enableBlending();
Vector3 cw_coords = cam.unproject(new Vector3(Gdx.graphics.getWidth(), 0, 0));
spriteBatch.draw(CRAssets.TEX_COLORWHEEL, cw_coords.x - 70, cw_coords.y - 70, 64, 64);

View File

@ -12,6 +12,10 @@ public abstract class CRGameEntity {
protected CRGameWorld world;
public Rectangle bounds = new Rectangle();
public float visualRotation = 0f; // 0..360
public float visualZoomX = 1f; // 0..360
public float visualZoomY = 1f; // 0..360
public boolean active = false; // true ::= marked for removal
protected Vector2 tmp_pos = new Vector2();

View File

@ -22,9 +22,9 @@ public abstract class GravityEntity extends MovingEntity {
velocity.y = -TERMINAL_VELOCITY;
}
super.update(delta);
if (isTouching_BOTTOM() && velocity.y < 0)
velocity.y = 0;
super.update(delta);
}
}

View File

@ -102,6 +102,139 @@ public abstract class MovingEntity extends CRGameEntity {
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 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;
}
}
private void updateTouchCollisions() {
face_TOP_isTouching = false;
face_LEFT_isTouching = false;

View File

@ -17,26 +17,53 @@ public class PlayerEntity extends GravityEntity {
public final static float PLAYER_SPEED_FORCE = 166f;
public final static float PLAYER_TERMINAL_SPEED = 320f;
public final static float PLAYER_ROTATION_SPEED = -200f;
private SwipeDirection phase = SwipeDirection.UP;
private boolean doFly = false;
public PlayerEntity(CRGameWorld _owner, float x, float y) {
super(_owner, x, y, PLAYER_WIDTH, PLAYER_HEIGHT);
}
@Override
public void update(float delta) {
super.update(delta);
public void update(float delta) {
if (velocity.x < PLAYER_TERMINAL_SPEED)
velocity.x += PLAYER_SPEED_FORCE * delta;
if (doFly)
velocity.y += PLAYER_FLY_FORCE * delta;
doFly = false;
if (!isTouching_BOTTOM() || visualRotation % 90 != 0) {
if (! isTouching_BOTTOM()) {
visualRotation = visualRotation + PLAYER_ROTATION_SPEED * delta;
visualRotation = (visualRotation + 720f) % 360f;
float p = (float) ((Math.max(Math.abs(Math.cos(Math.toRadians(visualRotation + 45))), Math.abs(Math.sin(Math.toRadians(visualRotation + 45)))) * Math.sqrt(PLAYER_WIDTH * PLAYER_WIDTH + PLAYER_HEIGHT * PLAYER_HEIGHT)));
updateHitBox(p, p);
} else {
visualRotation = (visualRotation + 720f) % 90f;
if (visualRotation < 45)
visualRotation = visualRotation + PLAYER_ROTATION_SPEED * delta;
else
visualRotation = visualRotation - PLAYER_ROTATION_SPEED * delta;
if (visualRotation >= 90f || visualRotation <= 0f)
visualRotation = 0f;
visualRotation = (visualRotation + 720f) % 90f;
float p = (float) ((Math.max(Math.abs(Math.cos(Math.toRadians(visualRotation + 45))), Math.abs(Math.sin(Math.toRadians(visualRotation + 45)))) * Math.sqrt(PLAYER_WIDTH * PLAYER_WIDTH + PLAYER_HEIGHT * PLAYER_HEIGHT)));
updateHitBox(p, p);
}
}
super.update(delta);
}
public void jump() {
@ -49,19 +76,24 @@ public class PlayerEntity extends GravityEntity {
}
public void fly() {
if (! isTouching_BOTTOM())
if (!isTouching_BOTTOM())
doFly = true;
}
@Override
public Texture getTexture() {
switch (phase) {
case UP: return CRAssets.TEX_PLAYER_UP;
case RIGHT: return CRAssets.TEX_PLAYER_RIGHT;
case DOWN: return CRAssets.TEX_PLAYER_DOWN;
case LEFT: return CRAssets.TEX_PLAYER_LEFT;
default: return null;
case UP:
return CRAssets.TEX_PLAYER_UP;
case RIGHT:
return CRAssets.TEX_PLAYER_RIGHT;
case DOWN:
return CRAssets.TEX_PLAYER_DOWN;
case LEFT:
return CRAssets.TEX_PLAYER_LEFT;
default:
return null;
}
}