From 23281d86981be53889e51460cfaae610eae27da4 Mon Sep 17 00:00:00 2001
From: timeo <timeo.jacquier@hotmail.com>
Date: Tue, 7 Dec 2021 16:07:28 +0100
Subject: [PATCH] 1 boite tombe sur une plateforme

---
 .../src/com/mygdx/game/AndroidLauncher.java   | 16 ++++
 core/src/com/mygdx/game/PlatVenture.java      | 65 ++++++++++++++
 core/src/etat/GameState.java                  | 30 +++++++
 core/src/etat/Play.java                       | 85 +++++++++++++++++++
 core/src/handlers/B2DVars.java                |  6 ++
 core/src/handlers/GameStateManager.java       | 66 ++++++++++++++
 .../mygdx/game/desktop/DesktopLauncher.java   | 15 ++++
 7 files changed, 283 insertions(+)
 create mode 100644 android/src/com/mygdx/game/AndroidLauncher.java
 create mode 100644 core/src/com/mygdx/game/PlatVenture.java
 create mode 100644 core/src/etat/GameState.java
 create mode 100644 core/src/etat/Play.java
 create mode 100644 core/src/handlers/B2DVars.java
 create mode 100644 core/src/handlers/GameStateManager.java
 create mode 100644 desktop/src/com/mygdx/game/desktop/DesktopLauncher.java

diff --git a/android/src/com/mygdx/game/AndroidLauncher.java b/android/src/com/mygdx/game/AndroidLauncher.java
new file mode 100644
index 0000000..8add522
--- /dev/null
+++ b/android/src/com/mygdx/game/AndroidLauncher.java
@@ -0,0 +1,16 @@
+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);
+	}
+}
diff --git a/core/src/com/mygdx/game/PlatVenture.java b/core/src/com/mygdx/game/PlatVenture.java
new file mode 100644
index 0000000..062bc81
--- /dev/null
+++ b/core/src/com/mygdx/game/PlatVenture.java
@@ -0,0 +1,65 @@
+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;
+	}
+}
diff --git a/core/src/etat/GameState.java b/core/src/etat/GameState.java
new file mode 100644
index 0000000..ea1af5b
--- /dev/null
+++ b/core/src/etat/GameState.java
@@ -0,0 +1,30 @@
+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();
+
+}
diff --git a/core/src/etat/Play.java b/core/src/etat/Play.java
new file mode 100644
index 0000000..66692cb
--- /dev/null
+++ b/core/src/etat/Play.java
@@ -0,0 +1,85 @@
+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() {
+
+    }
+}
diff --git a/core/src/handlers/B2DVars.java b/core/src/handlers/B2DVars.java
new file mode 100644
index 0000000..6a205a7
--- /dev/null
+++ b/core/src/handlers/B2DVars.java
@@ -0,0 +1,6 @@
+package handlers;
+
+public class B2DVars {
+
+    public static final float PPM = 100;
+}
diff --git a/core/src/handlers/GameStateManager.java b/core/src/handlers/GameStateManager.java
new file mode 100644
index 0000000..9f72771
--- /dev/null
+++ b/core/src/handlers/GameStateManager.java
@@ -0,0 +1,66 @@
+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();
+    }
+}
diff --git a/desktop/src/com/mygdx/game/desktop/DesktopLauncher.java b/desktop/src/com/mygdx/game/desktop/DesktopLauncher.java
new file mode 100644
index 0000000..e8b64fd
--- /dev/null
+++ b/desktop/src/com/mygdx/game/desktop/DesktopLauncher.java
@@ -0,0 +1,15 @@
+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);
+	}
+}
-- 
GitLab