From 82be5b223ec7ae8a3ea3efddbb52243c02733032 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Mon, 11 Aug 2014 15:36:03 +0200 Subject: [PATCH] things --- core/src/de/samdev/colorrunner/CRGame.java | 1 + .../colorrunner/game/renderer/CRAssets.java | 18 +-- .../game/renderer/CRGameRenderer.java | 18 ++- .../game/world/entities/CRGameEntity.java | 4 + .../game/world/entities/GravityEntity.java | 4 +- .../game/world/entities/MovingEntity.java | 133 ++++++++++++++++++ .../entities/gameentities/PlayerEntity.java | 60 ++++++-- 7 files changed, 208 insertions(+), 30 deletions(-) diff --git a/core/src/de/samdev/colorrunner/CRGame.java b/core/src/de/samdev/colorrunner/CRGame.java index c650dbb..0a871ca 100644 --- a/core/src/de/samdev/colorrunner/CRGame.java +++ b/core/src/de/samdev/colorrunner/CRGame.java @@ -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; diff --git a/core/src/de/samdev/colorrunner/game/renderer/CRAssets.java b/core/src/de/samdev/colorrunner/game/renderer/CRAssets.java index cf7cd53..e5d4882 100644 --- a/core/src/de/samdev/colorrunner/game/renderer/CRAssets.java +++ b/core/src/de/samdev/colorrunner/game/renderer/CRAssets.java @@ -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); diff --git a/core/src/de/samdev/colorrunner/game/renderer/CRGameRenderer.java b/core/src/de/samdev/colorrunner/game/renderer/CRGameRenderer.java index a20b212..823bcc6 100644 --- a/core/src/de/samdev/colorrunner/game/renderer/CRGameRenderer.java +++ b/core/src/de/samdev/colorrunner/game/renderer/CRGameRenderer.java @@ -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); diff --git a/core/src/de/samdev/colorrunner/game/world/entities/CRGameEntity.java b/core/src/de/samdev/colorrunner/game/world/entities/CRGameEntity.java index 243399f..2b2368a 100644 --- a/core/src/de/samdev/colorrunner/game/world/entities/CRGameEntity.java +++ b/core/src/de/samdev/colorrunner/game/world/entities/CRGameEntity.java @@ -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(); diff --git a/core/src/de/samdev/colorrunner/game/world/entities/GravityEntity.java b/core/src/de/samdev/colorrunner/game/world/entities/GravityEntity.java index dc2d5b1..e4c9c27 100644 --- a/core/src/de/samdev/colorrunner/game/world/entities/GravityEntity.java +++ b/core/src/de/samdev/colorrunner/game/world/entities/GravityEntity.java @@ -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); } } diff --git a/core/src/de/samdev/colorrunner/game/world/entities/MovingEntity.java b/core/src/de/samdev/colorrunner/game/world/entities/MovingEntity.java index 673a6e0..a16b12c 100644 --- a/core/src/de/samdev/colorrunner/game/world/entities/MovingEntity.java +++ b/core/src/de/samdev/colorrunner/game/world/entities/MovingEntity.java @@ -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; diff --git a/core/src/de/samdev/colorrunner/game/world/entities/gameentities/PlayerEntity.java b/core/src/de/samdev/colorrunner/game/world/entities/gameentities/PlayerEntity.java index b4b1502..9b5525e 100644 --- a/core/src/de/samdev/colorrunner/game/world/entities/gameentities/PlayerEntity.java +++ b/core/src/de/samdev/colorrunner/game/world/entities/gameentities/PlayerEntity.java @@ -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; } }