From f34f13b05398d626d066992190e52af6b5256815 Mon Sep 17 00:00:00 2001
From: timeo <timeo.jacquier@gmail.com>
Date: Thu, 9 Dec 2021 08:52:31 +0100
Subject: [PATCH] mort si on sort du monde en x sans passer par un panneau

---
 core/src/body/Panneau.java                  |  2 +-
 core/src/etat/Play.java                     | 33 ++++++++++++++++++---
 core/src/handlers/ContactListenerPerso.java | 12 ++++++++
 3 files changed, 42 insertions(+), 5 deletions(-)

diff --git a/core/src/body/Panneau.java b/core/src/body/Panneau.java
index 3d0b858..5ac096f 100644
--- a/core/src/body/Panneau.java
+++ b/core/src/body/Panneau.java
@@ -33,7 +33,7 @@ public class Panneau {
         fixtureDefStruct.isSensor = true;
 
 
-        structBody.createFixture(fixtureDefStruct);
+        structBody.createFixture(fixtureDefStruct).setUserData("panneau");
         shapeStruct.dispose();
     }
 }
\ No newline at end of file
diff --git a/core/src/etat/Play.java b/core/src/etat/Play.java
index aff4f01..dd32f80 100644
--- a/core/src/etat/Play.java
+++ b/core/src/etat/Play.java
@@ -5,9 +5,7 @@ import com.badlogic.gdx.Gdx;
 import com.badlogic.gdx.files.FileHandle;
 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.math.Vector3;
 import com.badlogic.gdx.physics.box2d.Body;
 import com.badlogic.gdx.physics.box2d.BodyDef;
 import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
@@ -21,7 +19,6 @@ import com.mygdx.game.PlatVenture;
 import body.Joyau;
 import body.Panneau;
 import body.Platform;
-import body.Water;
 import handlers.ContactListenerPerso;
 import handlers.GameStateManager;
 import handlers.InputPerso;
@@ -35,6 +32,8 @@ public class Play extends GameState {
     private int temps;
     private ContactListenerPerso cl;
     private int score = 0;
+    private boolean dead = false;
+    private float totalTimeSinceDeath = 0;
 
     private Body player;
 
@@ -48,7 +47,7 @@ public class Play extends GameState {
         b2dr = new Box2DDebugRenderer();
 
 
-        createWorld("assets/level_002.txt");
+        createWorld("assets/level_001.txt");
         createPlayer();
 
         //Init b2dCam
@@ -89,6 +88,7 @@ public class Play extends GameState {
         }
         joyauToRemove.clear();
 
+        death(dt);
         // Bouger la caméra en x
         if (player.getPosition().x * PPM > (float) (PlatVenture.largeur / 2) && player.getPosition().y * PPM <= (float) (PlatVenture.hauteur / 2)
                 && largeur > 16) {
@@ -208,4 +208,29 @@ public class Play extends GameState {
 
         player.createFixture(foot).setUserData("foot");
     }
+
+    private boolean isInside(){
+        boolean isinside = true;
+        if (player.getPosition().x < -5/PPM){ // Si la totalité du corps du joueur sort
+            isinside = false;
+        }
+
+        return isinside;
+    }
+
+    private void death(float dt){
+        if (!isInside() && !cl.isTriggerEndLevel()){
+            dead = true;
+        }
+        if (dead){
+            if (totalTimeSinceDeath > 2){
+                player.setLinearVelocity(0, 0);
+                player.setTransform(15/PPM, 20/PPM, 0);
+                dead = false;
+                totalTimeSinceDeath = 0;
+            } else{
+                totalTimeSinceDeath += dt;
+            }
+        }
+    }
 }
diff --git a/core/src/handlers/ContactListenerPerso.java b/core/src/handlers/ContactListenerPerso.java
index 7c45d3c..3b0a199 100644
--- a/core/src/handlers/ContactListenerPerso.java
+++ b/core/src/handlers/ContactListenerPerso.java
@@ -13,6 +13,7 @@ public class ContactListenerPerso implements ContactListener {
     private int numPlayerOnGround = 0;
     Array<Body> bodyToRemove = new Array<Body>();
     private int score = 0;
+    private boolean triggerEndLevel = false;
 
     /**
      * Appeler quand 2 fixtures rentre en collision
@@ -55,6 +56,13 @@ public class ContactListenerPerso implements ContactListener {
         if (fb.getUserData() != null && fb.getUserData().equals("foot") && fa.getUserData() != null && fa.getUserData().equals("platform")){
             numPlayerOnGround--;
         }
+
+        if (fa.getUserData() != null && fa.getUserData().equals("panneau") && fb.getUserData() != null && fb.getUserData().equals("body")){
+            triggerEndLevel = true;
+        }
+        else{
+            triggerEndLevel = false;
+        }
     }
 
     /**
@@ -88,4 +96,8 @@ public class ContactListenerPerso implements ContactListener {
     public int getScore() {
         return score;
     }
+
+    public boolean isTriggerEndLevel() {
+        return triggerEndLevel;
+    }
 }
-- 
GitLab