Skip to content
Snippets Groups Projects
Commit 23281d86 authored by timeo's avatar timeo
Browse files

1 boite tombe sur une plateforme

parent e7f876cb
No related branches found
No related tags found
No related merge requests found
package com.mygdx.game;
import android.os.Bundle;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.mygdx.game.PlatVenture;
public class AndroidLauncher extends AndroidApplication {
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
initialize(new PlatVenture(), config);
}
}
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.ScreenUtils;
import handlers.GameStateManager;
public class PlatVenture extends ApplicationAdapter {
public static final String Titre = "PlatVenture";
public static final int largeur = 160;
public static final int hauteur = 120;
public static final int scale = 8;
public static final float STEP = 1 /60f; // Pas entre deux images
private float accum;
private SpriteBatch batch;
private OrthographicCamera cam;
private OrthographicCamera hudCam;
private GameStateManager gsm;
@Override
public void create () {
batch = new SpriteBatch();
cam = new OrthographicCamera();
cam.setToOrtho(false, largeur, hauteur);
hudCam = new OrthographicCamera();
hudCam.setToOrtho(false, largeur, hauteur);
gsm = new GameStateManager(this);
}
@Override
public void render () {
accum += Gdx.graphics.getDeltaTime(); // Récupération du temps depuis le dernier render
// update seulement si suffisamment de temps est passé depuis la dernière étape
while (accum >= STEP){
accum -= STEP;
gsm.update(STEP);
gsm.render();
}
}
@Override
public void dispose () {
}
public SpriteBatch getBatch() {
return batch;
}
public OrthographicCamera getCam() {
return cam;
}
public OrthographicCamera getHudCam() {
return hudCam;
}
}
package etat;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.mygdx.game.PlatVenture;
import handlers.GameStateManager;
public abstract class GameState {
protected GameStateManager gsm;
protected PlatVenture platVenture;
protected SpriteBatch batch;
protected OrthographicCamera cam;
protected OrthographicCamera hudCam;
protected GameState(GameStateManager gsm){
this.gsm = gsm;
platVenture = gsm.getPlatVenture();
batch = platVenture.getBatch();
cam = platVenture.getCam();
hudCam = platVenture.getHudCam();
}
public abstract void handleInput();
public abstract void update(float dt);
public abstract void render();
public abstract void dispose();
}
package etat;
import static handlers.B2DVars.PPM;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.mygdx.game.PlatVenture;
import handlers.GameStateManager;
public class Play extends GameState {
private World world;
private Box2DDebugRenderer b2dr;
private OrthographicCamera b2dCam;
public Play(GameStateManager gsm){
super(gsm);
world = new World(new Vector2(0, -10f), true);
b2dr = new Box2DDebugRenderer();
BodyDef bdef = new BodyDef();
bdef.position.set(80/PPM, 60/PPM);
bdef.type = BodyDef.BodyType.StaticBody;
Body body = world.createBody(bdef);
PolygonShape shape = new PolygonShape();
shape.setAsBox(50/PPM, 5/PPM);
FixtureDef fdef = new FixtureDef();
fdef.shape = shape;
body.createFixture(fdef);
bdef.position.set(80/PPM, 100/PPM);
bdef.type = BodyDef.BodyType.DynamicBody;
body = world.createBody(bdef);
shape.setAsBox(5/PPM, 5/PPM);
fdef = new FixtureDef();
fdef.shape = shape;
body.createFixture(fdef);
//Init b2dCam
b2dCam = new OrthographicCamera();
b2dCam.setToOrtho(false, PlatVenture.largeur /PPM, PlatVenture.hauteur/PPM);
}
@Override
public void handleInput() {
}
@Override
public void update(float dt) {
//dt => delta time
/*TODO remettre à 1 quand j'aurai bien compris, on a pas besoin d'autant de précisions
*/
world.step(dt, 6, 2);
}
@Override
public void render() {
//Clear l'écran
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
//dessine le monde box2d
b2dr.render(world, b2dCam.combined);
}
@Override
public void dispose() {
}
}
package handlers;
public class B2DVars {
public static final float PPM = 100;
}
package handlers;
import com.mygdx.game.PlatVenture;
import java.util.Stack;
import etat.GameState;
import etat.Play;
public class GameStateManager {
private PlatVenture platVenture;
private Stack<GameState> gameStates;
public static final int PLAY = 4646;
public GameStateManager(PlatVenture game){
this.platVenture = game;
gameStates = new Stack<GameState>();
pushState(PLAY);
}
public PlatVenture getPlatVenture(){
return platVenture;
}
public void update(float dt){
gameStates.peek().update(dt);
}
public void render(){
gameStates.peek().render();
}
private GameState getState(int state){
if (state == PLAY){
return new Play(this);
}
return null;
}
/**
* Remplace ce qu'il y a en haut de la pile
* @param state
*/
public void setState(int state){
popState();
pushState(state);
}
/**
* Met en haut de la pile un nouvel état correspondant à celui passé en paramètres
* @param state
*/
public void pushState(int state){
gameStates.push(getState(state));
}
/**
* Retire ce qu'il y a en haut de la pile et le supprime
*/
public void popState(){
GameState g = gameStates.pop();
g.dispose();
}
}
package com.mygdx.game.desktop;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.mygdx.game.PlatVenture;
public class DesktopLauncher {
public static void main (String[] arg) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.title = PlatVenture.Titre;
config.width = PlatVenture.largeur * PlatVenture.scale;
config.height = PlatVenture.hauteur * PlatVenture.scale;
new LwjglApplication(new PlatVenture(), config);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment