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 0000000..7840dca Binary files /dev/null and b/core/assets/spritesheets/platformSet.png differ diff --git a/core/assets/badlogic.jpg b/core/assets/textures/badlogic.jpg similarity index 100% rename from core/assets/badlogic.jpg rename to core/assets/textures/badlogic.jpg diff --git a/core/src/com/game/Actor/Base.java b/core/src/com/game/Actor/Base.java new file mode 100644 index 0000000..bfc45d7 --- /dev/null +++ b/core/src/com/game/Actor/Base.java @@ -0,0 +1,53 @@ +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. + */ +public abstract class Base { + // Physics world reference + protected World world; + + // Physics definitions + protected BodyDef bd; + protected Body body; + protected String bodyType; + + // Position and Size + protected Vector2 pos; + protected Vector2 size; + + protected Colours curColour; + public enum Colours + { + RED, + GREEN, + BLUE, + NONE, + } + + public Base(World world, Vector2 pos, Vector2 size, String bodyType, Colours curColour) + { + this.world = world; + this.pos = pos; + this.size = size; + this.bodyType = bodyType; + this.curColour = curColour; + + makeBody(); + } + + protected abstract void makeBody(); + + // Accessors + public Vector2 getPos() { return body.getPosition(); } + public Vector2 getSize() { return size; } + public Colours getCurColour() { return curColour; } + + // Mutators + public void setCurColour(Colours curColour) { this.curColour = curColour; } +} diff --git a/core/src/com/game/Actor/Platform.java b/core/src/com/game/Actor/Platform.java new file mode 100644 index 0000000..24a1154 --- /dev/null +++ b/core/src/com/game/Actor/Platform.java @@ -0,0 +1,52 @@ +package com.game.Actor; + +import com.badlogic.gdx.math.Vector2; +import com.badlogic.gdx.physics.box2d.*; +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); + } + + @Override + protected void makeBody() { + bd = new BodyDef(); + + if(bodyType.equals("STATIC")) { bd.type = BodyDef.BodyType.StaticBody; } // Doesn't move, isn't affected by forces + else if(bodyType.equals("KINEMATIC")) { bd.type = BodyDef.BodyType.KinematicBody; } // Can move, isn't affected by forces + else { bd.type = BodyDef.BodyType.DynamicBody; } // Can move, is affected by forces + bd.position.set(pos.x / PPM, pos.y / PPM); + + body = world.createBody(bd); + + FixtureDef fd = new FixtureDef(); + + /*ChainShape chain = new ChainShape(); + Vector2[] v = new Vector2[3]; + v[0] = new Vector2( + -TILESIZE / 2 / PPM, -TILESIZE / 2 / PPM); + v[1] = new Vector2( + -TILESIZE / 2 / PPM, TILESIZE / 2 / PPM); + v[2] = new Vector2( + TILESIZE / 2 / PPM, TILESIZE / 2 / PPM); + chain.createChain(v); + fd.shape = chain;*/ + PolygonShape polygon = new PolygonShape(); + polygon.setAsBox((TILESIZE / 2) / PPM, (TILESIZE / 2) / PPM); + fd.shape = polygon; + + fd.density = 1f; + fd.restitution = 0f; + fd.friction = 1f; + + body.createFixture(fd); + } +} diff --git a/core/src/com/game/Actor/Player.java b/core/src/com/game/Actor/Player.java new file mode 100644 index 0000000..f090c34 --- /dev/null +++ b/core/src/com/game/Actor/Player.java @@ -0,0 +1,103 @@ +package com.game.Actor; + +import com.badlogic.gdx.math.Vector2; +import com.badlogic.gdx.physics.box2d.*; +import com.game.Misc.Vars; +import javafx.scene.shape.Circle; + +import static com.game.Misc.Vars.PPM; + +/** + * Created by Ash on 08/02/2016. + */ +public class Player extends Base { + + private Action curAction; + public enum Action + { + IDLE, + JUMPING, + FALLING, + DEAD + } + + public Player(World world, Vector2 pos, Vector2 size, Colours curColour) { + super(world, pos, size, "", curColour); + curAction = Action.IDLE; + } + + @Override + public void makeBody() { + bd = new BodyDef(); + + if(bodyType.equals("STATIC")) { bd.type = BodyDef.BodyType.StaticBody; } // Doesn't move, isn't affected by forces + else if(bodyType.equals("KINEMATIC")) { bd.type = BodyDef.BodyType.KinematicBody; } // Can move, isn't affected by forces + else { bd.type = BodyDef.BodyType.DynamicBody; } // Can move, is affected by forces + bd.position.set(pos.x / PPM, pos.y / PPM); + + body = world.createBody(bd); + //body.setFixedRotation(true); + + FixtureDef fd = new FixtureDef(); + //PolygonShape polygon = new PolygonShape(); + //polygon.setAsBox((size.x / 2) / PPM, (size.y / 2) / PPM); + //fd.shape = polygon; + + CircleShape circle = new CircleShape(); + circle.setRadius((size.x / 2) / PPM); + fd.shape = circle; + + fd.density = 1f; + fd.restitution = 0f; + fd.friction = 0.9f; + + body.createFixture(fd).setUserData("player"); + + /*PolygonShape polygon = new PolygonShape(); + polygon.setAsBox((size.x / 4) / PPM, (size.y / 4) / PPM, new Vector2(0, (size.y / -3) / PPM), 0); + fd.shape = polygon; + fd.isSensor = true; + body.createFixture(fd).setUserData("sensor");*/ + } + + public void update(float dt) + { + if(curAction == Action.JUMPING) + { + body.applyForceToCenter(new Vector2(0, 100), true); + curAction = Action.FALLING; + } + + Vector2 curVel = body.getLinearVelocity(); + curVel.x = Vars.SCROLLSPEED.x * dt; + body.setLinearVelocity(curVel); + } + + public void jump() + { + if(curAction != Action.FALLING) + { + curAction = Action.JUMPING; + } + } + + public void moveLeft() + { + Vector2 vel = body.getLinearVelocity(); + vel.x = -5f; + body.setLinearVelocity(vel); + } + + public void moveRight() + { + Vector2 vel = body.getLinearVelocity(); + vel.x = 5f; + body.setLinearVelocity(vel); + } + + // Accessors + public Action getCurAction() { return curAction; } + + // Mutators + public void setAction(Action curAction) { this.curAction = curAction; } +} diff --git a/core/src/com/game/Game.java b/core/src/com/game/Game.java index 2c0373d..a7bafb4 100644 --- a/core/src/com/game/Game.java +++ b/core/src/com/game/Game.java @@ -25,7 +25,6 @@ public class Game extends ApplicationAdapter { cam = new OrthographicCamera(); cam.setToOrtho(false, Vars.SCREEN_WIDTH, Vars.SCREEN_HEIGHT); - hudCam = new OrthographicCamera(); hudCam.setToOrtho(false, Vars.SCREEN_WIDTH, Vars.SCREEN_HEIGHT); @@ -41,6 +40,7 @@ public class Game extends ApplicationAdapter { accum += Gdx.graphics.getDeltaTime(); while (accum >= 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 }