This commit is contained in:
Ash Reynolds 2016-02-09 00:05:48 +00:00
parent 6edb84511f
commit e663268480
10 changed files with 358 additions and 11 deletions

View file

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="40" height="8" tilewidth="64" tileheight="64" nextobjectid="21">
<tileset firstgid="1" name="platformSet" tilewidth="64" tileheight="64" tilecount="3" columns="3">
<image source="../spritesheets/platformSet.png" width="192" height="64"/>
</tileset>
<layer name="PLATFORM" width="40" height="8">
<data encoding="base64" compression="zlib">
eJxjYBgF+AATEh4sgBGKRwpgxoPx6UEGjAz4w41Y85mwYHyAEQtGtg+fPmSaWPNpAXD5daikQXzpBwC0fABt
</data>
</layer>
<objectgroup name="BOUNDARY">
<object id="17" x="0" y="0" width="2560" height="704"/>
</objectgroup>
<objectgroup name="PLAYER">
<object id="19" gid="3" x="0" y="384" width="64" height="64"/>
</objectgroup>
</map>

Binary file not shown.

After

Width:  |  Height:  |  Size: 298 B

View file

Before

Width:  |  Height:  |  Size: 67 KiB

After

Width:  |  Height:  |  Size: 67 KiB

View file

@ -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; }
}

View file

@ -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);
}
}

View file

@ -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; }
}

View file

@ -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();
}

View file

@ -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;
}

View file

@ -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<Platform> platforms = new ArrayList<Platform>();
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
}

View file

@ -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<State> states;
private HashMap<States, State> states = new HashMap<States, State>();
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
}