From e6632684800b4218900f02c623b7572499eb47e8 Mon Sep 17 00:00:00 2001 From: Ash Reynolds Date: Tue, 9 Feb 2016 00:05:48 +0000 Subject: [PATCH] ? --- core/assets/levels/level1.tmx | 17 +++ core/assets/spritesheets/platformSet.png | Bin 0 -> 298 bytes core/assets/{ => textures}/badlogic.jpg | Bin core/src/com/game/Actor/Base.java | 53 +++++++++ core/src/com/game/Actor/Platform.java | 52 +++++++++ core/src/com/game/Actor/Player.java | 103 +++++++++++++++++ core/src/com/game/Game.java | 2 +- core/src/com/game/Misc/Vars.java | 13 +++ core/src/com/game/States/Play.java | 126 +++++++++++++++++++-- core/src/com/game/States/StateManager.java | 3 +- 10 files changed, 358 insertions(+), 11 deletions(-) create mode 100644 core/assets/levels/level1.tmx create mode 100644 core/assets/spritesheets/platformSet.png rename core/assets/{ => textures}/badlogic.jpg (100%) create mode 100644 core/src/com/game/Actor/Base.java create mode 100644 core/src/com/game/Actor/Platform.java create mode 100644 core/src/com/game/Actor/Player.java diff --git a/core/assets/levels/level1.tmx b/core/assets/levels/level1.tmx new file mode 100644 index 0000000..51434f2 --- /dev/null +++ b/core/assets/levels/level1.tmx @@ -0,0 +1,17 @@ + + + + + + + + eJxjYBgF+AATEh4sgBGKRwpgxoPx6UEGjAz4w41Y85mwYHyAEQtGtg+fPmSaWPNpAXD5daikQXzpBwC0fABt + + + + + + + + + diff --git a/core/assets/spritesheets/platformSet.png b/core/assets/spritesheets/platformSet.png new file mode 100644 index 0000000000000000000000000000000000000000..7840dca3c096afe12b920cbacbaa47fb09803e1d GIT binary patch literal 298 zcmeAS@N?(olHy`uVBq!ia0vp^2Y}dtgAGXf= Vars.STEP) { accum -= Vars.STEP; + sm.handleInput(); sm.update(Vars.STEP); sm.render(); } diff --git a/core/src/com/game/Misc/Vars.java b/core/src/com/game/Misc/Vars.java index a254109..ad53080 100644 --- a/core/src/com/game/Misc/Vars.java +++ b/core/src/com/game/Misc/Vars.java @@ -1,5 +1,7 @@ package com.game.Misc; +import com.badlogic.gdx.math.Vector2; + /** * Created by Ash on 08/02/2016. */ @@ -13,4 +15,15 @@ public class Vars { // Physics related 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 SCROLLSPEED = new Vector2(150, 0); + + + + // Filter bits + public static final short BIT_RED = 2; + public static final short BIT_GREEN = 4; + public static final short BIT_BLUE = 8; } diff --git a/core/src/com/game/States/Play.java b/core/src/com/game/States/Play.java index 8d4427a..eb9b4fd 100644 --- a/core/src/com/game/States/Play.java +++ b/core/src/com/game/States/Play.java @@ -1,43 +1,153 @@ package com.game.States; -import com.badlogic.gdx.graphics.Texture; +import static com.game.Misc.Vars.PPM; +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.Input; +import com.badlogic.gdx.graphics.GL20; +import com.badlogic.gdx.graphics.OrthographicCamera; +import com.badlogic.gdx.maps.tiled.TiledMap; +import com.badlogic.gdx.maps.tiled.TiledMapTileLayer; +import com.badlogic.gdx.maps.tiled.TmxMapLoader; +import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer; +import com.badlogic.gdx.math.Vector2; +import com.badlogic.gdx.physics.box2d.*; +import com.game.Actor.Base; +import com.game.Actor.Platform; +import com.game.Actor.Player; +import com.game.Misc.Vars; + +import java.util.ArrayList; /** * Created by Ash on 08/02/2016. */ public class Play extends State { - Texture img; + // TODO, remove + public boolean isDebug = true; + + // Physics related + private World world; + private Box2DDebugRenderer b2dr; // TODO, remove + + private OrthographicCamera b2dCam; + + private float tileSize; + + Player player; + + private TiledMap tileMap; + private OrthogonalTiledMapRenderer tmr; + + ArrayList platforms = new ArrayList(); 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) { + 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 + + b2dCam = new OrthographicCamera(); + b2dCam.setToOrtho(false, Vars.SCREEN_WIDTH / PPM, Vars.SCREEN_HEIGHT / PPM); } @Override public void init() { - img = new Texture("badlogic.jpg"); + setupLevel(); } @Override public void update(float dt) { + world.step(dt, 6, 2); + cam.position.x = player.getPos().x * PPM; + cam.update(); + b2dCam.position.x = player.getPos().x; + b2dCam.update(); + + + player.update(dt); } @Override public void render() { - sb.setProjectionMatrix(cam.combined); - sb.begin(); - sb.draw(img, 0, 0); - sb.end(); + Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); + + if(!isDebug) + { + tmr.setView(cam); + tmr.render(); + } + else + { + b2dr.render(world, b2dCam.combined); + } } @Override public void handleInput() { + if(Gdx.input.isKeyPressed(Input.Keys.SPACE)) + { + player.jump(); + } + if(Gdx.input.isKeyJustPressed(Input.Keys.V)) { isDebug = !isDebug; } } @Override public void dispose() { - img.dispose(); + } + + private void setupLevel() + { + player = new Player(world, new Vector2(500, 500), new Vector2(60, 60), Base.Colours.NONE); + + tileMap = new TmxMapLoader().load("levels/level1.tmx"); + tmr = new OrthogonalTiledMapRenderer(tileMap); + + TiledMapTileLayer layer = (TiledMapTileLayer)tileMap.getLayers().get("PLATFORM"); + tileSize = layer.getTileWidth(); + + for(int row = 0; row < layer.getHeight(); row++) + { + for(int col = 0; col < layer.getWidth(); col++) + { + TiledMapTileLayer.Cell cell = layer.getCell(col, row); + + 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)); } + } + } + } + + // Accessors + + // Mutators } diff --git a/core/src/com/game/States/StateManager.java b/core/src/com/game/States/StateManager.java index c1246fe..15012c8 100644 --- a/core/src/com/game/States/StateManager.java +++ b/core/src/com/game/States/StateManager.java @@ -3,7 +3,6 @@ package com.game.States; import com.game.Game; import java.util.HashMap; -import java.util.Stack; /** * Created by Ash on 08/02/2016. @@ -11,7 +10,6 @@ import java.util.Stack; public class StateManager { private Game game; - //private Stack states; private HashMap states = new HashMap(); private States currentState; @@ -27,6 +25,7 @@ public class StateManager { this.game = game; states.put(States.MENU, new Menu(this)); states.put(States.PLAY, new Play(this)); + setState(States.PLAY); // TODO, set to MENU }