From 61a10aa7e72e31e3a93b740b8503ac2d5756a1ab Mon Sep 17 00:00:00 2001 From: Ash Reynolds Date: Wed, 10 Feb 2016 18:21:32 +0000 Subject: [PATCH] Camera utilities and menu implementation --- core/assets/levels/level1.tmx | 6 +- core/src/com/game/Actor/Base.java | 2 - core/src/com/game/Actor/Platform.java | 10 +- core/src/com/game/{Game.java => App.java} | 33 +++-- core/src/com/game/Managers/Assets.java | 7 + .../{States => Managers}/StateManager.java | 18 ++- core/src/com/game/Misc/CameraUtils.java | 54 ++++++++ core/src/com/game/Misc/Vars.java | 4 +- core/src/com/game/States/Menu.java | 10 +- core/src/com/game/States/Play.java | 127 +++++++++--------- core/src/com/game/States/State.java | 30 ++++- .../src/com/game/desktop/DesktopLauncher.java | 6 +- 12 files changed, 203 insertions(+), 104 deletions(-) rename core/src/com/game/{Game.java => App.java} (66%) create mode 100644 core/src/com/game/Managers/Assets.java rename core/src/com/game/{States => Managers}/StateManager.java (75%) create mode 100644 core/src/com/game/Misc/CameraUtils.java diff --git a/core/assets/levels/level1.tmx b/core/assets/levels/level1.tmx index ca767de..4790374 100644 --- a/core/assets/levels/level1.tmx +++ b/core/assets/levels/level1.tmx @@ -5,15 +5,15 @@ - eJxjYBgF1ABMSJgYwAjEzGj0QAHGAbZ/qANmPBifHmTAyIA/Hkg1nxBgwoLxAUYsGNld+PQh06S4DZvZ2NxAbYArTCi1j5TygR6AnHSLDgAqVwCE + eJztk8EKACAIQzX//5/rOEKWktGhHgxPy5kl8qmggSLokE31FprsH53xFYyIeRAVvofV+dmdNEcMdYS5mA/rLl6GarJ/OeLP3PVJsH/03bLsHTB3AJM= - - + + diff --git a/core/src/com/game/Actor/Base.java b/core/src/com/game/Actor/Base.java index 033b4b7..3fd3267 100644 --- a/core/src/com/game/Actor/Base.java +++ b/core/src/com/game/Actor/Base.java @@ -1,9 +1,7 @@ package com.game.Actor; -import static com.game.Misc.Vars.PPM; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.physics.box2d.*; -import com.game.Misc.Vars; /** * Created by Ash on 08/02/2016. diff --git a/core/src/com/game/Actor/Platform.java b/core/src/com/game/Actor/Platform.java index dac8229..b4669de 100644 --- a/core/src/com/game/Actor/Platform.java +++ b/core/src/com/game/Actor/Platform.java @@ -3,18 +3,16 @@ package com.game.Actor; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.physics.box2d.*; import com.game.Misc.Box2dUtils; -import com.game.Misc.Vars; import static com.game.Misc.Vars.PPM; -import static com.game.Misc.Vars.TILESIZE; /** * Created by Ash on 08/02/2016. */ public class Platform extends Base { - public Platform(World world, Vector2 pos, Colours curColour) { - super(world, pos, new Vector2(TILESIZE, TILESIZE), "STATIC", curColour); + public Platform(World world, Vector2 pos, Vector2 size, Colours curColour) { + super(world, pos, size, "STATIC", curColour); body = Box2dUtils.makeBody(world, BodyDef.BodyType.StaticBody, @@ -23,8 +21,8 @@ public class Platform extends Base { Box2dUtils.makePolygon(body, size, "", false); Box2dUtils.makeChain(body, new Vector2[]{ - new Vector2((-TILESIZE / 2 + 5) / PPM, (TILESIZE / 2 + 5) / PPM), - new Vector2((TILESIZE / 2 - 5) / PPM, (TILESIZE / 2 + 5) / PPM) + new Vector2((-size.x / 2 + 5) / PPM, (size.y / 2 + 5) / PPM), + new Vector2((size.x / 2 - 5) / PPM, (size.y / 2 + 5) / PPM) }, "PLATFORM", true diff --git a/core/src/com/game/Game.java b/core/src/com/game/App.java similarity index 66% rename from core/src/com/game/Game.java rename to core/src/com/game/App.java index 049cf80..fbe833b 100644 --- a/core/src/com/game/Game.java +++ b/core/src/com/game/App.java @@ -2,38 +2,53 @@ package com.game; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.Input; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.game.Misc.Vars; -import com.game.States.StateManager; +import com.game.Managers.StateManager; -public class Game extends ApplicationAdapter { +public class App extends ApplicationAdapter { private float accum; + // Batches private SpriteBatch sb; + private ShapeRenderer sr; + + // Cameras private OrthographicCamera cam; private OrthographicCamera hudCam; + // Managers private StateManager sm; @Override public void create() { - sb = new SpriteBatch(); + // Create batches + sb = new SpriteBatch(); + sr = new ShapeRenderer(); + + // Create Main + HUD cameras cam = new OrthographicCamera(); cam.setToOrtho(false, Vars.SCREEN_WIDTH, Vars.SCREEN_HEIGHT); hudCam = new OrthographicCamera(); hudCam.setToOrtho(false, Vars.SCREEN_WIDTH, Vars.SCREEN_HEIGHT); + + // Create statemanager (Should always happen last) sm = new StateManager(this); } @Override public void resize (int width, int height) { + } @Override public void render () { + super.render(); accum += Gdx.graphics.getDeltaTime(); while (accum >= Vars.STEP) { accum -= Vars.STEP; @@ -41,22 +56,20 @@ public class Game extends ApplicationAdapter { sm.update(Vars.STEP); sm.render(); } - } - @Override - public void pause () { - } - - @Override - public void resume () { + if(Gdx.input.isKeyPressed(Input.Keys.ESCAPE)) { Gdx.app.exit(); } } @Override public void dispose () { sm.dispose(); + sb.dispose(); + sr.dispose(); } public SpriteBatch getSpriteBatch() { return sb; } + public ShapeRenderer getSr() { return sr; } + public StateManager getSm() { return sm; } public OrthographicCamera getCam() { return cam; } public OrthographicCamera getHudCam() { return hudCam; } } diff --git a/core/src/com/game/Managers/Assets.java b/core/src/com/game/Managers/Assets.java new file mode 100644 index 0000000..f4fe990 --- /dev/null +++ b/core/src/com/game/Managers/Assets.java @@ -0,0 +1,7 @@ +package com.game.Managers; + +/** + * Created by Ash on 10/02/2016. + */ +public class Assets { +} diff --git a/core/src/com/game/States/StateManager.java b/core/src/com/game/Managers/StateManager.java similarity index 75% rename from core/src/com/game/States/StateManager.java rename to core/src/com/game/Managers/StateManager.java index 15012c8..87658ff 100644 --- a/core/src/com/game/States/StateManager.java +++ b/core/src/com/game/Managers/StateManager.java @@ -1,6 +1,9 @@ -package com.game.States; +package com.game.Managers; -import com.game.Game; +import com.game.App; +import com.game.States.Menu; +import com.game.States.Play; +import com.game.States.State; import java.util.HashMap; @@ -8,7 +11,8 @@ import java.util.HashMap; * Created by Ash on 08/02/2016. */ public class StateManager { - private Game game; + + protected final App app; private HashMap states = new HashMap(); @@ -20,13 +24,13 @@ public class StateManager { PLAY, } - public StateManager(Game game) + public StateManager(App app) { - this.game = game; + this.app = app; states.put(States.MENU, new Menu(this)); states.put(States.PLAY, new Play(this)); - setState(States.PLAY); // TODO, set to MENU + setState(States.MENU); // TODO, set to MENU } public void update(float dt) @@ -50,7 +54,7 @@ public class StateManager { } // Accessors - public Game game() { return game; } + public App app() { return app; } // Mutators public void setState(States state) diff --git a/core/src/com/game/Misc/CameraUtils.java b/core/src/com/game/Misc/CameraUtils.java new file mode 100644 index 0000000..6e58ec6 --- /dev/null +++ b/core/src/com/game/Misc/CameraUtils.java @@ -0,0 +1,54 @@ +package com.game.Misc; + +import com.badlogic.gdx.graphics.Camera; +import com.badlogic.gdx.math.Vector2; +import com.badlogic.gdx.math.Vector3; + +/** + * Created by Ash on 10/02/2016. + */ +public class CameraUtils { + + public static void lockOnTarget(Camera cam, float tarX, float tarY) + { + Vector3 pos = cam.position; + pos.x = tarX; + pos.y = tarY; + cam.position.set(pos); + cam.update(); + } + + public static void lerpToTarget(Camera cam, float tarX, float tarY) + { + Vector3 pos = cam.position; + pos.x = cam.position.x + (tarX - cam.position.x) * .2f; + pos.y = cam.position.y + (tarY - cam.position.y) * .2f;; + cam.position.set(pos); + cam.update(); + } + + public static void setBoundary(Camera cam, Vector2 start, Vector2 size) + { + Vector3 pos = cam.position; + if(pos.x < start.x) + { + pos.x = start.x; + } + if(pos.y < start.y) + { + pos.y = start.y; + } + + if(pos.x > start.x + size.x) + { + pos.x = start.x + size.x; + } + + if(pos.y > start.y + size.y) + { + pos.y = start.y + size.y; + } + cam.position.set(pos); + cam.update(); + } +} diff --git a/core/src/com/game/Misc/Vars.java b/core/src/com/game/Misc/Vars.java index eebafee..910b08b 100644 --- a/core/src/com/game/Misc/Vars.java +++ b/core/src/com/game/Misc/Vars.java @@ -2,8 +2,6 @@ package com.game.Misc; import com.badlogic.gdx.math.Vector2; -import java.io.File; - /** * Created by Ash on 08/02/2016. */ @@ -19,7 +17,7 @@ public class Vars { public static final float STEP = 1 / FRAMERATE; public static final Vector2 GRAVITY = new Vector2(0, -9.81f); public static final float PPM = 100f; // Pixels per meter - public static final float TILESIZE = 64f; + //public static final Vector2 TILESIZE = new Vector2(64, 64); public static final Vector2 SCROLLSPEED = new Vector2(150f, 0); // Filter bits diff --git a/core/src/com/game/States/Menu.java b/core/src/com/game/States/Menu.java index 0f0a682..dfb817f 100644 --- a/core/src/com/game/States/Menu.java +++ b/core/src/com/game/States/Menu.java @@ -1,10 +1,15 @@ package com.game.States; +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.Input; +import com.game.Managers.StateManager; + /** * Created by Ash on 08/02/2016. */ public class Menu extends State { + public Menu(StateManager sm) { super(sm); } @@ -26,7 +31,10 @@ public class Menu extends State { @Override public void handleInput() { - + if(Gdx.input.isKeyPressed(Input.Keys.ENTER)) + { + sm.setState(StateManager.States.PLAY); + } } @Override diff --git a/core/src/com/game/States/Play.java b/core/src/com/game/States/Play.java index d9bbef5..ab141ea 100644 --- a/core/src/com/game/States/Play.java +++ b/core/src/com/game/States/Play.java @@ -6,8 +6,8 @@ import com.badlogic.gdx.Input; import com.badlogic.gdx.audio.Sound; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; -import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.maps.MapLayer; +import com.badlogic.gdx.maps.MapProperties; import com.badlogic.gdx.maps.objects.PolylineMapObject; import com.badlogic.gdx.maps.objects.TextureMapObject; import com.badlogic.gdx.maps.tiled.TiledMap; @@ -21,7 +21,11 @@ import com.badlogic.gdx.physics.box2d.*; import com.game.Actor.Base; import com.game.Actor.Platform; import com.game.Actor.Player; +import com.game.App; +import com.game.Managers.StateManager; +import com.game.Misc.CameraUtils; import com.game.Misc.Vars; +import javafx.beans.property.MapProperty; import java.util.ArrayList; @@ -31,61 +35,29 @@ import java.util.ArrayList; public class Play extends State { // TODO, remove - public boolean isDebug = true; + public boolean isDebug = false; // Physics related private World world; private Box2DDebugRenderer b2dr; // TODO, remove + private OrthographicCamera b2dCam; // TODO, remove - private OrthographicCamera b2dCam; - - private float tileSize; - - Player player; - + // TileMap and Map Renderer private TiledMap tileMap; private OrthogonalTiledMapRenderer tmr; + private float mapWidth, mapHeight; + private Vector2 tileSize; + + // All Actors in level + private Player player; + private ArrayList platforms = new ArrayList(); - ArrayList platforms = new ArrayList(); private Sound jumpSound = Gdx.audio.newSound(Gdx.files.internal("sounds/jumping.mp3")); public Play(StateManager sm) { super(sm); world = new World(new Vector2(0, Vars.GRAVITY.y), true); - ContactListener cl = new ContactListener() { - @Override - public void beginContact(Contact contact) { - Fixture fa = contact.getFixtureA(); - Fixture fb = contact.getFixtureB(); - - if(fa == null || fb == null) { return; } - if(fa.getUserData() == null || fb.getUserData() == null) { return; } - - if(fa.getUserData().equals("PLAYER") && fb.getUserData().equals("PLATFORM") || - fb.getUserData().equals("PLAYER") && fa.getUserData().equals("PLATFORM")) - { - if(player.getCurAction() != Player.Action.IDLE) { - player.setAction(Player.Action.IDLE); - } - } - } - - @Override - public void endContact(Contact contact) { - - } - - @Override - public void preSolve(Contact contact, Manifold oldManifold) { - - } - - @Override - public void postSolve(Contact contact, ContactImpulse impulse) { - - } - }; world.setContactListener(cl); b2dr = new Box2DDebugRenderer(); // TODO, remove @@ -103,10 +75,11 @@ public class Play extends State { public void update(float dt) { world.step(dt, 6, 2); - cameraUpdate(dt); + CameraUtils.lerpToTarget(cam, player.getPos().scl(PPM).x, 0); + CameraUtils.lockOnTarget(b2dCam, player.getPos().x, player.getPos().y); - b2dCam.position.x = player.getPos().x; - b2dCam.update(); + Vector2 start = new Vector2(cam.viewportWidth / 2, cam.viewportHeight / 2); + CameraUtils.setBoundary(cam, start, new Vector2(mapWidth * tileSize.x - start.x * 2, mapHeight * tileSize.y - start.y * 2)); player.update(dt); } @@ -136,7 +109,7 @@ public class Play extends State { public void handleInput() { if(Gdx.input.isKeyPressed(Input.Keys.SPACE)) { - //jumpSound.play(); TODO, fix sound? + //jumpSound.play(); //TODO, fix sound? player.jump(); } @@ -145,17 +118,7 @@ public class Play extends State { @Override public void dispose() { - - } - - private void cameraUpdate(float dt) - { - Vector3 camPos = cam.position; - - camPos.x = cam.position.x + ((player.getPos().x * PPM) - cam.position.x) * .2f; - //camPos.y = cam.position.y + ((player.getPos().y * PPM) - cam.position.y) * .2f; - cam.position.set(camPos); - cam.update(); + world.dispose(); } private void setupLevel() @@ -163,8 +126,13 @@ public class Play extends State { tileMap = new TmxMapLoader().load("levels/level1.tmx"); tmr = new OrthogonalTiledMapRenderer(tileMap); + MapProperties mapProp = tileMap.getProperties(); + mapWidth = mapProp.get("width", Integer.class); + mapHeight = mapProp.get("height", Integer.class); + tileSize = new Vector2(mapProp.get("tilewidth", Integer.class), mapProp.get("tileheight", Integer.class)); + + TiledMapTileLayer platformLayer = (TiledMapTileLayer)tileMap.getLayers().get("PLATFORM"); - tileSize = platformLayer.getTileWidth(); MapLayer boundaryLayer = tileMap.getLayers().get("BOUNDARY"); PolylineMapObject polylineObj = (PolylineMapObject)boundaryLayer.getObjects().get(0); @@ -183,14 +151,14 @@ public class Play extends State { if(cell == null) { continue; } if(cell.getTile() == null) { continue; } - if(cell.getTile().getId() == 1) { platforms.add(new Platform(world, new Vector2((col + 0.5f) * tileSize, (row + 0.5f) * tileSize), Base.Colours.RED)); } - else if(cell.getTile().getId() == 2) { platforms.add(new Platform(world, new Vector2((col + 0.5f) * tileSize, (row + 0.5f) * tileSize), Base.Colours.GREEN)); } - else if(cell.getTile().getId() == 3) { platforms.add(new Platform(world, new Vector2((col + 0.5f) * tileSize, (row + 0.5f) * tileSize), Base.Colours.BLUE)); } + if(cell.getTile().getId() == 1) { platforms.add(new Platform(world, new Vector2((col + 0.5f) * tileSize.x, (row + 0.5f) * tileSize.y), new Vector2(tileSize.x, tileSize.y), Base.Colours.RED)); } + else if(cell.getTile().getId() == 2) { platforms.add(new Platform(world, new Vector2((col + 0.5f) * tileSize.x, (row + 0.5f) * tileSize.y), new Vector2(tileSize.x, tileSize.y), Base.Colours.GREEN)); } + else if(cell.getTile().getId() == 3) { platforms.add(new Platform(world, new Vector2((col + 0.5f) * tileSize.x, (row + 0.5f) * tileSize.y), new Vector2(tileSize.x, tileSize.y), Base.Colours.BLUE)); } } } } - public void buildBoundary(PolylineMapObject polylineObj) + private void buildBoundary(PolylineMapObject polylineObj) { Polyline r = polylineObj.getPolyline(); BodyDef bd = new BodyDef(); @@ -221,4 +189,39 @@ public class Play extends State { // Accessors // Mutators + + // Contact Listener + ContactListener cl = new ContactListener() { + @Override + public void beginContact(Contact contact) { + Fixture fa = contact.getFixtureA(); + Fixture fb = contact.getFixtureB(); + + if(fa == null || fb == null) { return; } + if(fa.getUserData() == null || fb.getUserData() == null) { return; } + + if(fa.getUserData().equals("PLAYER") && fb.getUserData().equals("PLATFORM") || + fb.getUserData().equals("PLAYER") && fa.getUserData().equals("PLATFORM")) + { + if(player.getCurAction() != Player.Action.IDLE) { + player.setAction(Player.Action.IDLE); + } + } + } + + @Override + public void endContact(Contact contact) { + + } + + @Override + public void preSolve(Contact contact, Manifold oldManifold) { + + } + + @Override + public void postSolve(Contact contact, ContactImpulse impulse) { + + } + }; } diff --git a/core/src/com/game/States/State.java b/core/src/com/game/States/State.java index 887a384..5abe6b2 100644 --- a/core/src/com/game/States/State.java +++ b/core/src/com/game/States/State.java @@ -2,26 +2,42 @@ package com.game.States; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.g2d.SpriteBatch; -import com.game.Game; +import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import com.badlogic.gdx.scenes.scene2d.Stage; +import com.game.App; +import com.game.Managers.StateManager; /** * Created by Ash on 08/02/2016. */ public abstract class State { - protected StateManager sm; - protected Game game; + // App reference + protected final App app; + + // Batches protected SpriteBatch sb; + protected ShapeRenderer sr; + + // Cameras protected OrthographicCamera cam; protected OrthographicCamera hudCam; + // Stage + protected Stage stage; + + // Managers + protected final StateManager sm; + public State (StateManager sm) { this.sm = sm; - game = sm.game(); - sb = game.getSpriteBatch(); - cam = game.getCam(); - hudCam = game.getHudCam(); + this.app = sm.app(); + sb = app.getSpriteBatch(); + sr = app.getSr(); + cam = app.getCam(); + hudCam = app.getHudCam(); + stage = new Stage(); } public abstract void init(); diff --git a/desktop/src/com/game/desktop/DesktopLauncher.java b/desktop/src/com/game/desktop/DesktopLauncher.java index 54611b5..581f411 100644 --- a/desktop/src/com/game/desktop/DesktopLauncher.java +++ b/desktop/src/com/game/desktop/DesktopLauncher.java @@ -3,19 +3,19 @@ package com.game.desktop; import com.badlogic.gdx.Files; import com.badlogic.gdx.backends.lwjgl.LwjglApplication; import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration; -import com.badlogic.gdx.graphics.PixmapIO; -import com.game.Game; +import com.game.App; import com.game.Misc.Vars; public class DesktopLauncher { public static void main (String[] arg) { LwjglApplicationConfiguration config = new LwjglApplicationConfiguration(); - new LwjglApplication(new Game(), config); + new LwjglApplication(new App(), config); config.title = Vars.TITLE; config.width = Vars.SCREEN_WIDTH; config.height = Vars.SCREEN_HEIGHT; config.resizable = Vars.RESIZABLE; config.addIcon("spritesheets/icon.jpg", Files.FileType.Internal); config.foregroundFPS = (int)Vars.FRAMERATE; + config.backgroundFPS = (int)Vars.FRAMERATE; } }