Main menu

This commit is contained in:
Ash Reynolds 2016-02-12 19:13:05 +00:00
parent 4432e1d3cb
commit 3553152649
15 changed files with 234 additions and 561 deletions

Binary file not shown.

View file

@ -6,13 +6,9 @@ com.badlogic.gdx.graphics.Color: {
black: { a: 1, b: 0, g: 0, r: 0 },
},
com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle: {
default: { down: default-round-down, up: default-round, font: default-font, fontColor: white },
default: { down: default-round-down, up: default-round, font: default-font, fontColor: white, overFontColor: red },
toggle: { down: default-round-down, up: default-round, checked: default-round-down, font: default-font, fontColor: white, downFontColor: red }
},
com.badlogic.gdx.scenes.scene2d.ui.ProgressBar$ProgressBarStyle: {
default-horizontal: { background: default-slider, knob: default-slider-knob },
default-vertical: { background: default-slider, knob: default-round-large }
},
com.badlogic.gdx.scenes.scene2d.ui.Label$LabelStyle: {
default: { font: default-font, fontColor: white }
},

Binary file not shown.

After

Width:  |  Height:  |  Size: 280 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 193 KiB

View file

@ -4,12 +4,18 @@ import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.FileHandleResolver;
import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGeneratorLoader;
import com.badlogic.gdx.graphics.g2d.freetype.FreetypeFontLoader;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.game.Misc.Vars;
import com.game.Screens.Loading;
import com.game.Screens.Splash;
import com.game.Screens.Menu;
public class App extends Game {
@ -20,8 +26,8 @@ public class App extends Game {
private ShapeRenderer sr;
// Cameras
private OrthographicCamera cam;
private OrthographicCamera hudCam;
//private OrthographicCamera cam;
//private OrthographicCamera hudCam;
// Managers
private AssetManager assetManager;
@ -34,21 +40,24 @@ public class App extends Game {
// Create batches
sb = new SpriteBatch();
sr = new ShapeRenderer();
sr.setAutoShapeType(true);
// 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);
//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);
this.setScreen(new Loading(this));
}
@Override
public void render () {
super.render();
if(Gdx.input.isKeyPressed(Input.Keys.M))
{
this.setScreen(new Menu(this));
}
/*accum += Gdx.graphics.getDeltaTime();
@ -65,16 +74,14 @@ public class App extends Game {
@Override
public void dispose () {
super.dispose();
//sm.dispose();
//sb.dispose();
//sr.dispose();
assetManager.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; }
//public OrthographicCamera getCam() { return cam; }
//public OrthographicCamera getHudCam() { return hudCam; }
public AssetManager getAssetManager() { return assetManager; }
}

View file

@ -1,66 +0,0 @@
package com.game.Managers;
import com.game.App;
import com.game.States.Menu;
import com.game.States.Play;
import com.game.States.State;
import java.util.HashMap;
/**
* Created by Ash on 08/02/2016.
*/
public class StateManager {
protected final App app;
private HashMap<States, State> states = new HashMap<States, State>();
private States currentState;
public enum States
{
MENU,
PAUSE,
PLAY,
}
public StateManager(App app)
{
this.app = app;
states.put(States.MENU, new Menu(this));
states.put(States.PLAY, new Play(this));
setState(States.MENU); // TODO, set to MENU
}
public void update(float dt)
{
states.get(currentState).update(dt);
}
public void render()
{
states.get(currentState).render();
}
public void handleInput()
{
states.get(currentState).handleInput();
}
public void dispose()
{
states.get(currentState).dispose();
}
// Accessors
public App app() { return app; }
// Mutators
public void setState(States state)
{
currentState = state;
System.out.println("Setting state: " + currentState.name());
states.get(currentState).init();
}
}

View file

@ -9,6 +9,7 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.game.App;
import com.game.Misc.Vars;
/**
* Created by Ash on 11/02/2016.
@ -37,8 +38,12 @@ public abstract class AbstractScreen implements Screen {
this.app = app;
sb = app.getSpriteBatch();
sr = app.getSr();
cam = app.getCam();
hudCam = app.getHudCam();
cam = new OrthographicCamera();
cam.setToOrtho(false, Vars.SCREEN_WIDTH, Vars.SCREEN_HEIGHT);
hudCam = new OrthographicCamera();
hudCam.setToOrtho(false, Vars.SCREEN_WIDTH, Vars.SCREEN_HEIGHT);
assets = app.getAssetManager();
stage = new Stage();
}
@ -46,7 +51,7 @@ public abstract class AbstractScreen implements Screen {
@Override
public void show() {
Gdx.input.setInputProcessor(stage);
}
public abstract void update(float dt);

View file

@ -1,7 +1,51 @@
package com.game.Screens;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.game.App;
/**
* Created by Ash on 11/02/2016.
*/
public class Leaderboard {
public class Leaderboard extends AbstractScreen {
private TextButton butBack;
private Vector2 buttonSize;
public Leaderboard(App app) {
super(app);
}
@Override
public void show() {
super.show();
}
@Override
public void update(float dt) {
}
@Override
public void render(float dt) {
super.render(dt);
sb.begin();
sb.draw(assets.get("textures/leaderboardBackground.jpg", Texture.class), 0, 0);
assets.get("badaboom25.ttf", BitmapFont.class).draw(sb,"Press M to go back to menu", 100, 100);
sb.end();
}
@Override
public void handleInput() {
}
@Override
public void dispose() {
super.dispose();
}
}

View file

@ -1,7 +1,16 @@
package com.game.Screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.loaders.FileHandleResolver;
import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGeneratorLoader;
import com.badlogic.gdx.graphics.g2d.freetype.FreetypeFontLoader;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.math.Rectangle;
@ -14,6 +23,7 @@ import com.game.App;
public class Loading extends AbstractScreen {
private Image logo;
private float percent;
private Rectangle loadingRect;
@ -24,43 +34,47 @@ public class Loading extends AbstractScreen {
@Override
public void show() {
super.show();
FileHandleResolver resolver = new InternalFileHandleResolver();
assets.setLoader(FreeTypeFontGenerator.class, new FreeTypeFontGeneratorLoader(resolver));
assets.setLoader(BitmapFont.class, ".ttf", new FreetypeFontLoader(resolver));
loadFont("fonts/badaboom.TTF", 60, Color.BLACK);
assets.load("textures/player.png", Texture.class);
assets.finishLoading();
assets.finishLoading(); // make sure player texture and font is loaded
logo = new Image(assets.get("textures/player.png", Texture.class));
logo.setPosition((stage.getWidth() / 2) - logo.getWidth() / 2, (stage.getHeight() / 1.5f) - logo.getHeight() / 2);
// 853.3333
loadingRect = new Rectangle(stage.getWidth() / 6f, (stage.getHeight() / 2f), 0, 25);
loadingRect = new Rectangle(stage.getWidth() / 6f, (stage.getHeight() / 2f - 25), 0, 25);
assets.load("textures/badlogic.jpg", Texture.class);
assets.load("textures/position0.png", Texture.class);
assets.load("textures/position1.png", Texture.class);
assets.load("textures/position2.png", Texture.class);
assetsToLoad();
}
@Override
public void update(float dt) {
percent = Interpolation.linear.apply(percent, assets.getProgress(), 0.1f);
percent = Interpolation.linear.apply(percent, assets.getProgress(), 0.3f);
loadingRect.width = 0 + 853.3333f * percent;
if (assets.update()) {
if (Gdx.input.isTouched()) {
if (assets.update() && loadingRect.width >= 852f) { // continue to menu screen when all assets have loaded
app.setScreen(new Menu(app));
}
}
}
@Override
public void render(float dt) {
super.render(dt);
sr.begin(ShapeRenderer.ShapeType.Filled);
sr.setColor(1, 0, 0, 1);
sr.rect(loadingRect.x, loadingRect.y, loadingRect.width, loadingRect.height); // Red loading bar
sr.set(ShapeRenderer.ShapeType.Line);
sr.rect(loadingRect.x, loadingRect.y, 853.3333f, loadingRect.height); // Outline
sr.end();
sb.begin();
assets.get("badaboom60.ttf", BitmapFont.class).draw(sb, "Loading", stage.getWidth() / 2 - 80, loadingRect.y + 80);
logo.draw(sb, 1f);
sb.end();
}
@ -74,4 +88,34 @@ public class Loading extends AbstractScreen {
public void dispose() {
super.dispose();
}
private void assetsToLoad()
{
loadFont("fonts/badaboom.TTF", 25, Color.BLACK);
loadFont("fonts/badaboom.TTF", 30, Color.BLACK);
loadFont("fonts/badaboom.TTF", 45, Color.BLACK);
assets.load("textures/badlogic.jpg", Texture.class);
assets.load("textures/menuBackground.jpg", Texture.class);
assets.load("textures/leaderboardBackground.jpg", Texture.class);
assets.load("textures/position0.png", Texture.class);
assets.load("textures/position1.png", Texture.class);
assets.load("textures/position2.png", Texture.class);
assets.load("sounds/jumping.mp3", Sound.class);
assets.load("music/TheComplex.mp3", Music.class);
}
private void loadFont(String fontFileName, int size, Color borderColour)
{
FreetypeFontLoader.FreeTypeFontLoaderParameter params = new FreetypeFontLoader.FreeTypeFontLoaderParameter();
params.fontFileName = fontFileName;
params.fontParameters.size = size;
params.fontParameters.borderWidth = 2;
params.fontParameters.borderColor = borderColour;
assets.load("badaboom" + size + ".ttf", BitmapFont.class, params);
}
}

View file

@ -1,11 +1,22 @@
package com.game.Screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.game.App;
import static com.badlogic.gdx.scenes.scene2d.actions.Actions.*;
import static com.badlogic.gdx.scenes.scene2d.actions.Actions.alpha;
import static com.badlogic.gdx.scenes.scene2d.actions.Actions.fadeIn;
/**
* Created by Ash on 11/02/2016.
*/
@ -16,26 +27,47 @@ public class Menu extends AbstractScreen {
// Buttons
private TextButton butPlay, butExit, butLeaderboard;
private Vector2 buttonSize;
// Font
private Music music;
public Menu(App app) {
super(app);
atlas = new TextureAtlas("spriteSheets/uiskin.atlas");
skin = new Skin(atlas);
skin.add("default-font", assets.get("badaboom60.ttf", BitmapFont.class));
skin.load(Gdx.files.internal("spritesheets/uiskin.json"));
music = assets.get("music/TheComplex.mp3", Music.class);
music.setLooping(true);
buttonSize = new Vector2(128, 40);
}
@Override
public void show() {
super.show();
initButtons();
music.play();
}
@Override
public void update(float dt) {
if (Gdx.input.isTouched()) {
app.setScreen(new Play(app));
}
stage.act(dt);
}
@Override
public void render(float dt)
{
super.render(dt);
sb.begin();
sb.draw(assets.get("textures/menuBackground.jpg", Texture.class), 0, 0);
sb.end();
stage.draw();
}
@Override
@ -43,4 +75,59 @@ public class Menu extends AbstractScreen {
}
@Override
public void hide() {
super.hide();
}
@Override
public void dispose() {
super.dispose();
}
private void initButtons()
{
butPlay = new TextButton("Play", skin, "default");
butPlay.setPosition((stage.getWidth() / 2) - buttonSize.x / 2, buttonSize.y + 180);
butPlay.setSize(buttonSize.x, buttonSize.y);
butPlay.addListener(new ClickListener() {
@Override
public void clicked (com.badlogic.gdx.scenes.scene2d.InputEvent event, float x, float y) {
Gdx.input.setInputProcessor(null);
music.stop();
app.setScreen(new Play(app));
}
});
butLeaderboard = new TextButton("Leaderboard", skin, "default");
butLeaderboard.setPosition((stage.getWidth() / 2) - buttonSize.x / 2, buttonSize.y + 100);
butLeaderboard.setSize(buttonSize.x, buttonSize.y);
butLeaderboard.addListener(new ClickListener() {
@Override
public void clicked (com.badlogic.gdx.scenes.scene2d.InputEvent event, float x, float y) {
Gdx.input.setInputProcessor(null);
app.setScreen(new Leaderboard(app));
}
});
butExit = new TextButton("Exit", skin, "default");
butExit.setPosition((stage.getWidth() / 2) - buttonSize.x / 2, buttonSize.y + 20);
butExit.setSize(buttonSize.x, buttonSize.y);
butExit.addListener(new ClickListener() {
@Override
public void clicked(com.badlogic.gdx.scenes.scene2d.InputEvent event, float x, float y) {
Gdx.app.exit();
}
});
//butPlay.addAction(parallel(alpha(0), moveTo(stage.getWidth() / 2, stage.getHeight() / 2, 5f, Interpolation.pow5), sequence(alpha(0f), fadeIn(1.5f, Interpolation.pow2))));
butPlay.addAction(sequence(alpha(0f), fadeIn(1f, Interpolation.pow2)));
butLeaderboard.addAction(sequence(alpha(0f), fadeIn(1.5f, Interpolation.pow2)));
butExit.addAction(sequence(alpha(0f), fadeIn(2f, Interpolation.pow2)));
stage.addActor(butPlay);
stage.addActor(butLeaderboard);
stage.addActor(butExit);
}
}

View file

@ -5,6 +5,7 @@ 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.g2d.BitmapFont;
import com.badlogic.gdx.maps.MapLayer;
import com.badlogic.gdx.maps.MapProperties;
import com.badlogic.gdx.maps.objects.PolylineMapObject;
@ -67,6 +68,9 @@ public class Play extends AbstractScreen {
@Override
public void show()
{
super.show();
setupLevel();
}
@ -93,7 +97,7 @@ public class Play extends AbstractScreen {
if(!isDebug)
{
sb.begin();
assets.get("badaboom25.ttf", BitmapFont.class).draw(sb,"Press M to go back to menu", 0, 0);
player.render(sb);
sb.end();
@ -114,6 +118,8 @@ public class Play extends AbstractScreen {
player.jump();
}
if(Gdx.input.isKeyJustPressed(Input.Keys.V)) { isDebug = !isDebug; }
}

View file

@ -1,36 +0,0 @@
package com.game.Screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.game.App;
/**
* Created by Ash on 11/02/2016.
*/
public class Splash extends AbstractScreen {
public Splash(App app) {
super(app);
}
@Override
public void update(float dt) {
stage.act(dt);
}
@Override
public void render(float dt)
{
super.render(dt);
stage.draw();
}
@Override
public void handleInput() {
}
}

View file

@ -1,138 +0,0 @@
package com.game.States;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.game.Managers.StateManager;
import java.awt.event.InputEvent;
import java.util.Random;
import static com.badlogic.gdx.scenes.scene2d.actions.Actions.*;
/**
* Created by Ash on 08/02/2016.
*/
public class Menu extends State {
Random rand = new Random();
int threshhold = 200;
long lastChanged = 0;
private Image tempImage;
private TextureAtlas atlas;
private Skin skin;
private Table table;
private TextButton butPlay, butExit, butLeaderboard;
public BitmapFont font64;
public FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/badaboom.TTF"));
public FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
private Label heading;
public Menu(StateManager sm) {
super(sm);
Gdx.input.setInputProcessor(stage);
}
@Override
public void init() {
atlas = new TextureAtlas("spritesheets/uiskin.atlas");
skin = new Skin(atlas);
parameter.size = 64;
parameter.borderWidth = 2f;
parameter.borderColor = Color.WHITE;
parameter.color = Color.BLACK;
font64 = generator.generateFont(parameter);
skin.add("default-font", font64);
skin.load(Gdx.files.internal("spritesheets/uiskin.json"));
initButtons();
}
@Override
public void update(float dt) {
stage.act(dt);
//genRandColour();
}
private void genRandColour()
{
if(System.currentTimeMillis() - lastChanged < threshhold) { return; }
font64.setColor(new Color(
rand.nextFloat() / 2f + 0.5f,
rand.nextFloat() / 2f + 0.5f,
rand.nextFloat() / 2f + 0.5f,
1f
));
lastChanged = System.currentTimeMillis();
}
@Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Gdx.gl.glClearColor(.25f, .25f, .25f, 1f);
stage.draw();
sb.begin();
//font12.draw(sb, "Main menu", 200, 200);
sb.end();
}
@Override
public void handleInput() {
if(Gdx.input.isKeyPressed(Input.Keys.ENTER))
{
sm.setState(StateManager.States.PLAY);
}
}
@Override
public void dispose() {
stage.dispose();
}
private void initButtons()
{
butPlay = new TextButton("Play", skin, "default");
butPlay.setPosition(500, 260);
butPlay.setSize(128, 40);
butPlay.addListener(new ClickListener() {
@Override
public void clicked (com.badlogic.gdx.scenes.scene2d.InputEvent event, float x, float y) {
sm.setState(StateManager.States.PLAY);
}
});
butExit = new TextButton("Exit", skin, "default");
butExit.setPosition(500, 110);
butExit.setSize(128, 40);
butExit.addListener(new ClickListener() {
@Override
public void clicked(com.badlogic.gdx.scenes.scene2d.InputEvent event, float x, float y) {
Gdx.app.exit();
}
});
butPlay.addAction(parallel(alpha(0), moveTo(stage.getWidth() / 2, stage.getHeight() / 2, 5f, Interpolation.pow5), sequence(alpha(0f), fadeIn(1.5f, Interpolation.pow2))));
butExit.addAction(sequence(alpha(0f), fadeIn(2f, Interpolation.pow2)));
stage.addActor(butPlay);
stage.addActor(butExit);
}
}

View file

@ -1,227 +0,0 @@
package com.game.States;
import static com.game.Misc.Vars.PPM;
import com.badlogic.gdx.Gdx;
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.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;
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.Polyline;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
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;
/**
* Created by Ash on 08/02/2016.
*/
public class Play extends State {
// TODO, remove
public boolean isDebug = false;
// Physics related
private World world;
private Box2DDebugRenderer b2dr; // TODO, remove
private OrthographicCamera b2dCam; // TODO, remove
// 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<Platform> platforms = new ArrayList<Platform>();
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);
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() {
setupLevel();
}
@Override
public void update(float dt) {
world.step(dt, 6, 2);
CameraUtils.lerpToTarget(cam, player.getPos().scl(PPM).x, 0);
CameraUtils.lockOnTarget(b2dCam, player.getPos().x, player.getPos().y);
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);
}
@Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
sb.setProjectionMatrix(cam.combined);
if(!isDebug)
{
sb.begin();
player.render(sb);
sb.end();
tmr.setView(cam);
tmr.render();
}
else
{
b2dr.render(world, b2dCam.combined);
}
}
@Override
public void handleInput() {
if(Gdx.input.isKeyPressed(Input.Keys.SPACE))
{
//jumpSound.play(); //TODO, fix sound?
player.jump();
}
if(Gdx.input.isKeyJustPressed(Input.Keys.V)) { isDebug = !isDebug; }
}
@Override
public void dispose() {
world.dispose();
}
private void setupLevel()
{
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");
MapLayer boundaryLayer = tileMap.getLayers().get("BOUNDARY");
PolylineMapObject polylineObj = (PolylineMapObject)boundaryLayer.getObjects().get(0);
buildBoundary(polylineObj);
MapLayer playerLayer = tileMap.getLayers().get("PLAYER");
TextureMapObject playerObj = (TextureMapObject)playerLayer.getObjects().get(0);
player = new Player(world, new Vector2(playerObj.getX(), playerObj.getY()), new Vector2(60, 60), Base.Colours.NONE);
for(int row = 0; row < platformLayer.getHeight(); row++)
{
for(int col = 0; col < platformLayer.getWidth(); col++)
{
TiledMapTileLayer.Cell cell = platformLayer.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.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)); }
}
}
}
private void buildBoundary(PolylineMapObject polylineObj)
{
Polyline r = polylineObj.getPolyline();
BodyDef bd = new BodyDef();
bd.type = BodyDef.BodyType.StaticBody;
Body body = world.createBody(bd);
FixtureDef fd = new FixtureDef();
ChainShape chain = new ChainShape();
float[] v = r.getTransformedVertices();
Vector2[] finalV = new Vector2[v.length / 2];
for(int i = 0; i < v.length / 2; ++i)
{
finalV[i] = new Vector2();
finalV[i].x = v[i * 2] / PPM;
finalV[i].y = v[i * 2 + 1] / PPM;
}
chain.createChain(finalV);
fd.shape = chain;
body.createFixture(fd).setUserData("boundary");
}
// 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) {
}
};
}

View file

@ -1,49 +0,0 @@
package com.game.States;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
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 {
// 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;
this.app = sm.app();
sb = app.getSpriteBatch();
sr = app.getSr();
cam = app.getCam();
hudCam = app.getHudCam();
stage = new Stage();
}
public abstract void init();
public abstract void update(float dt);
public abstract void render();
public abstract void handleInput();
public abstract void dispose();
}