From 703be6f8aa1bca76be07a6fe976d0938368fe4ed Mon Sep 17 00:00:00 2001
From: timeo <timeo.jacquier@hotmail.com>
Date: Tue, 7 Dec 2021 19:40:07 +0100
Subject: [PATCH] =?UTF-8?q?Cr=C3=A9ation=20Classes=20pour=20g=C3=A9rer=20l?=
 =?UTF-8?q?es=20contacts=20et=20les=20Inputs?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 core/src/com/mygdx/game/PlatVenture.java    |  4 ++
 core/src/etat/Play.java                     |  2 +
 core/src/handlers/ContactListenerPerso.java | 47 ++++++++++++++++++++
 core/src/handlers/InputPerso.java           | 49 +++++++++++++++++++++
 core/src/handlers/InputProcessorPerso.java  | 35 +++++++++++++++
 5 files changed, 137 insertions(+)
 create mode 100644 core/src/handlers/ContactListenerPerso.java
 create mode 100644 core/src/handlers/InputPerso.java
 create mode 100644 core/src/handlers/InputProcessorPerso.java

diff --git a/core/src/com/mygdx/game/PlatVenture.java b/core/src/com/mygdx/game/PlatVenture.java
index 062bc81..768e58e 100644
--- a/core/src/com/mygdx/game/PlatVenture.java
+++ b/core/src/com/mygdx/game/PlatVenture.java
@@ -7,6 +7,8 @@ import com.badlogic.gdx.graphics.Texture;
 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
 import com.badlogic.gdx.utils.ScreenUtils;
 import handlers.GameStateManager;
+import handlers.InputPerso;
+import handlers.InputProcessorPerso;
 
 public class PlatVenture extends ApplicationAdapter {
 
@@ -26,6 +28,7 @@ public class PlatVenture extends ApplicationAdapter {
 
 	@Override
 	public void create () {
+		Gdx.input.setInputProcessor(new InputProcessorPerso());
 		batch = new SpriteBatch();
 		cam = new OrthographicCamera();
 		cam.setToOrtho(false, largeur, hauteur);
@@ -43,6 +46,7 @@ public class PlatVenture extends ApplicationAdapter {
 			accum -= STEP;
 			gsm.update(STEP);
 			gsm.render();
+			InputPerso.update();
 		}
 	}
 	
diff --git a/core/src/etat/Play.java b/core/src/etat/Play.java
index 66692cb..2508bd8 100644
--- a/core/src/etat/Play.java
+++ b/core/src/etat/Play.java
@@ -14,6 +14,7 @@ import com.badlogic.gdx.physics.box2d.PolygonShape;
 import com.badlogic.gdx.physics.box2d.World;
 import com.mygdx.game.PlatVenture;
 
+import handlers.ContactListenerPerso;
 import handlers.GameStateManager;
 
 public class Play extends GameState {
@@ -26,6 +27,7 @@ public class Play extends GameState {
     public Play(GameStateManager gsm){
         super(gsm);
         world = new World(new Vector2(0, -10f), true);
+        world.setContactListener(new ContactListenerPerso());
         b2dr = new Box2DDebugRenderer();
 
         BodyDef bdef = new BodyDef();
diff --git a/core/src/handlers/ContactListenerPerso.java b/core/src/handlers/ContactListenerPerso.java
new file mode 100644
index 0000000..fe6758a
--- /dev/null
+++ b/core/src/handlers/ContactListenerPerso.java
@@ -0,0 +1,47 @@
+package handlers;
+
+import com.badlogic.gdx.physics.box2d.Contact;
+import com.badlogic.gdx.physics.box2d.ContactImpulse;
+import com.badlogic.gdx.physics.box2d.ContactListener;
+import com.badlogic.gdx.physics.box2d.Manifold;
+
+public class ContactListenerPerso implements ContactListener {
+
+    /**
+     * Appeler quand 2 fixtures rentre en collision
+     * @param c
+     */
+    @Override
+    public void beginContact(Contact c) {
+        System.out.println("contact");
+    }
+
+    /**
+     * Appeler quand 2 fixtures sorte de collision
+     * @param c
+     */
+    @Override
+    public void endContact(Contact c) {
+
+    }
+
+    /**
+     * Ce qu'il se passe entre la détection de collision et son handling
+     * @param c
+     * @param m
+     */
+    @Override
+    public void preSolve(Contact c, Manifold m) {
+
+    }
+
+    /**
+     * Ce qu'il se passe après le handling d'une collision
+     * @param c
+     * @param ContactImpulse
+     */
+    @Override
+    public void postSolve(Contact c, ContactImpulse ContactImpulse) {
+
+    }
+}
diff --git a/core/src/handlers/InputPerso.java b/core/src/handlers/InputPerso.java
new file mode 100644
index 0000000..7ffb00f
--- /dev/null
+++ b/core/src/handlers/InputPerso.java
@@ -0,0 +1,49 @@
+package handlers;
+
+public class InputPerso {
+    public static boolean[] keys;
+    public static boolean[] pkeys;
+
+    public static final int NUM_KEYS = 3;
+
+    /**
+     * Saut
+     */
+    public static final int BUTTON1 = 0;
+    /**
+     * Gauche
+     */
+    public static final int BUTTON2 = 1;
+    /**
+     * Droite
+     */
+    public static final int BUTTON3 = 2;
+
+    static{
+        keys = new boolean[NUM_KEYS];
+        pkeys = new boolean[NUM_KEYS];
+    }
+
+    public static void update(){
+        for (int i = 0; i <NUM_KEYS; i++){
+            pkeys[i] = keys[i];
+        }
+    }
+
+    public static void setKey(int i, boolean b){
+        keys[i] = b;
+    }
+
+    public static boolean isDown(int i){
+        return keys[i];
+    }
+
+    /**
+     * Savoir si une touche a été appuyé une fois
+     * @param i touche à regarder
+     * @return vrai si la touche est appuyé et ne l'était pas à l'état d'avant
+     */
+    public static boolean isPressed(int i){
+        return keys[i] && !pkeys[i];
+    }
+}
diff --git a/core/src/handlers/InputProcessorPerso.java b/core/src/handlers/InputProcessorPerso.java
new file mode 100644
index 0000000..37c8846
--- /dev/null
+++ b/core/src/handlers/InputProcessorPerso.java
@@ -0,0 +1,35 @@
+package handlers;
+
+import com.badlogic.gdx.Input;
+import com.badlogic.gdx.InputAdapter;
+
+public class InputProcessorPerso extends InputAdapter {
+
+    @Override
+    public boolean keyDown(int k){
+        if (k == Input.Keys.UP){
+            InputPerso.setKey(InputPerso.BUTTON1, true);
+        }
+        if (k == Input.Keys.LEFT){
+            InputPerso.setKey(InputPerso.BUTTON2, true);
+        }
+        if (k == Input.Keys.RIGHT){
+            InputPerso.setKey(InputPerso.BUTTON3, true);
+        }
+        return true;
+    }
+
+    @Override
+    public boolean keyUp(int k){
+        if (k == Input.Keys.UP){
+            InputPerso.setKey(InputPerso.BUTTON1, false);
+        }
+        if (k == Input.Keys.LEFT){
+            InputPerso.setKey(InputPerso.BUTTON2, false);
+        }
+        if (k == Input.Keys.RIGHT){
+            InputPerso.setKey(InputPerso.BUTTON3, false);
+        }
+        return true;
+    }
+}
-- 
GitLab