From 297e989f5b1fa7f141f9ab68fa45a855b9593751 Mon Sep 17 00:00:00 2001 From: timeo <timeo.jacquier@gmail.com> Date: Wed, 8 Dec 2021 20:42:54 +0100 Subject: [PATCH] =?UTF-8?q?d=C3=A9placement=20perso=20+=20joyau=20compt?= =?UTF-8?q?=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/src/body/Joyau.java | 8 +- core/src/body/Panneau.java | 6 +- core/src/body/Platform.java | 2 +- core/src/etat/Play.java | 132 ++++++++++---------- core/src/handlers/ContactListenerPerso.java | 34 ++++- 5 files changed, 109 insertions(+), 73 deletions(-) diff --git a/core/src/body/Joyau.java b/core/src/body/Joyau.java index 2b9b158..aec93b6 100644 --- a/core/src/body/Joyau.java +++ b/core/src/body/Joyau.java @@ -1,4 +1,6 @@ package body; +import static handlers.B2DVars.PPM; + import com.badlogic.gdx.math.Circle; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.physics.box2d.Body; @@ -11,7 +13,7 @@ import com.badlogic.gdx.physics.box2d.World; public class Joyau { CircleShape shapeStruct; - public Joyau(int x, int y, World world){ + public Joyau(float x, float y, World world){ BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyDef.BodyType.StaticBody; bodyDef.position.set(x, y); @@ -20,13 +22,15 @@ public class Joyau { FixtureDef fixtureDefStruct = new FixtureDef(); shapeStruct = new CircleShape(); + shapeStruct.setRadius(1/PPM); + shapeStruct.setPosition(new Vector2(5/PPM, 5/PPM)); fixtureDefStruct.shape = shapeStruct; fixtureDefStruct.isSensor = true; //traversable - structBody.createFixture(fixtureDefStruct); + structBody.createFixture(fixtureDefStruct).setUserData("joyau"); shapeStruct.dispose(); } } \ No newline at end of file diff --git a/core/src/body/Panneau.java b/core/src/body/Panneau.java index 3aaa48f..3d0b858 100644 --- a/core/src/body/Panneau.java +++ b/core/src/body/Panneau.java @@ -1,4 +1,6 @@ package body; +import static handlers.B2DVars.PPM; + import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.physics.box2d.Body; import com.badlogic.gdx.physics.box2d.BodyDef; @@ -10,7 +12,7 @@ public class Panneau { PolygonShape shapeStruct; Vector2[] ptsStruct; - public Panneau(int x, int y, World world) { + public Panneau(float x, float y, World world) { BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyDef.BodyType.StaticBody; bodyDef.position.set(x, y); @@ -21,7 +23,7 @@ public class Panneau { shapeStruct = new PolygonShape(); - ptsStruct = new Vector2[]{}; + ptsStruct = new Vector2[]{new Vector2(0, 0), new Vector2(10/PPM, 0), new Vector2(10/PPM, 10/PPM), new Vector2(0, 10/PPM)}; shapeStruct.set(ptsStruct); fixtureDefStruct.shape = shapeStruct; diff --git a/core/src/body/Platform.java b/core/src/body/Platform.java index 9bf8ad6..cc52f0b 100644 --- a/core/src/body/Platform.java +++ b/core/src/body/Platform.java @@ -44,7 +44,7 @@ public class Platform { } fixtureDefStruct.shape = shapeStruct; - fixtureDefStruct.restitution = 0.6f; + fixtureDefStruct.restitution = 0.1f; fixtureDefStruct.density = 1f; fixtureDefStruct.friction = 0.25f; diff --git a/core/src/etat/Play.java b/core/src/etat/Play.java index 4a96181..2c6ae6e 100644 --- a/core/src/etat/Play.java +++ b/core/src/etat/Play.java @@ -11,15 +11,20 @@ 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; +import com.badlogic.gdx.physics.box2d.CircleShape; import com.badlogic.gdx.physics.box2d.FixtureDef; import com.badlogic.gdx.physics.box2d.PolygonShape; import com.badlogic.gdx.physics.box2d.World; +import com.badlogic.gdx.utils.Array; 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; public class Play extends GameState { @@ -28,43 +33,22 @@ public class Play extends GameState { private int largeur; private int hauteur; private int temps; + private ContactListenerPerso cl; + + private Body player; private OrthographicCamera b2dCam; public Play(GameStateManager gsm){ super(gsm); world = new World(new Vector2(0, -10f), true); - world.setContactListener(new ContactListenerPerso()); + cl = new ContactListenerPerso(); + world.setContactListener(cl); b2dr = new Box2DDebugRenderer(); - BodyDef bdef = new BodyDef(); - bdef.position.set(0/PPM, 0/PPM); - bdef.type = BodyDef.BodyType.StaticBody; - Body body = world.createBody(bdef); - - Vector2[] pts = new Vector2[]{new Vector2(0/PPM, 0/PPM), new Vector2(10/PPM, 0/PPM), - new Vector2(10/PPM, 10/PPM), new Vector2(0/PPM, 10/PPM)}; - PolygonShape shape = new PolygonShape(); - shape.set(pts); - - FixtureDef fdef = new FixtureDef(); - fdef.shape = shape; - body.createFixture(fdef).setUserData("platform"); - - //new Platform(10/PPM, 10/PPM, "carre", world); - - - 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).setUserData("perso"); createWorld("assets/level_001.txt"); + createPlayer(); //Init b2dCam b2dCam = new OrthographicCamera(); @@ -72,7 +56,24 @@ public class Play extends GameState { } @Override public void handleInput() { + if (InputPerso.isPressed(0)){ + if (cl.isPlayerOnGround()) { + System.out.println("aled 2"); + player.applyForceToCenter(0, 2 / PPM, true); + } + } + + if (InputPerso.isDown(2)){ + if (cl.isPlayerOnGround()) { + player.setLinearVelocity(0.75f, player.getLinearVelocity().y); + } + } + if (InputPerso.isDown(1)){ + if (cl.isPlayerOnGround()) { + player.setLinearVelocity(-1*0.75f, player.getLinearVelocity().y); + } + } } @Override @@ -82,6 +83,14 @@ public class Play extends GameState { handleInput(); /*TODO remettre à 1 quand j'aurai bien compris, on a pas besoin d'autant de précisions */ + Array<Body> joyauToRemove = cl.getBodyToRemove(); + for (Body b : joyauToRemove){ + world.destroyBody(b); + } + joyauToRemove.clear(); + b2dCam.position.set(player.getPosition(), 0); + b2dCam.update(); + world.step(dt, 6, 2); } @@ -89,7 +98,6 @@ public class Play extends GameState { public void render() { //Clear l'écran Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT); - //dessine le monde box2d b2dr.render(world, b2dCam.combined); } @@ -134,53 +142,43 @@ public class Play extends GameState { case 'J': new Platform((j*10)/PPM, ((hauteur-i)*10)/PPM, "bord_gauche", world); break; + case '1': + case '2': + new Joyau((j*10)/PPM, ((hauteur-i) * 10)/PPM, world); + break; + case 'Z': + new Panneau((j*10)/PPM, ((hauteur-i) * 10)/PPM, world); } } System.out.println(); } + } - /* for (int i = hauteur; i >= 0; i--){ - for (int j = 0; j < largeur; j++){ - char current = ligne[i-1].charAt(j); - System.out.print(current); - switch (current){ - *//*case 'J': - new Platform(j, i, "bord_gauche", world); - break; - case 'K': - new Platform(j, i, "rectangle", world); - break; - case 'L': - new Platform(j, i, "bord_droit", world); - break;*//* - case 'A': - case 'B': - case 'C': - case 'D': - case 'E': - case 'F': - case 'G': - case 'H': - case 'I': - new Platform((j*10)/PPM, ((i)*10)/PPM, "carre", world); - break; - *//*case 'W': - new Water(j, i, world); - break; - case 'Z': - //new Panneau(j, i, FabriqueUnite.getUnite(), world); - break; - case '1': - case '2': - //new Joyau(j, i, FabriqueUnite.getUnite(), world); - break;*//* + private void createPlayer(){ + BodyDef bdP = new BodyDef(); + bdP.type = BodyDef.BodyType.DynamicBody; + bdP.fixedRotation = true; + bdP.position.set(15/PPM, 20/PPM); - } - } - System.out.println(); - }*/ + player = world.createBody(bdP); + player.setUserData("player"); + + FixtureDef body = new FixtureDef(); + PolygonShape bodyShape = new PolygonShape(); + Vector2[] pts = new Vector2[]{new Vector2(0, (10/8)/PPM), new Vector2(2.5f/PPM, (30/8)/PPM), new Vector2(0, (70/8)/PPM), new Vector2(-2.5f/PPM, (30/8)/PPM)}; + bodyShape.set(pts); + body.shape = bodyShape; + player.createFixture(body).setUserData("body"); + + FixtureDef foot = new FixtureDef(); + CircleShape footShape = new CircleShape(); + footShape.setRadius((10/8)/PPM); + foot.shape = footShape; + foot.density = 0.5f; + foot.restitution = 0.1f; + player.createFixture(foot).setUserData("foot"); } } diff --git a/core/src/handlers/ContactListenerPerso.java b/core/src/handlers/ContactListenerPerso.java index fe6758a..ec52003 100644 --- a/core/src/handlers/ContactListenerPerso.java +++ b/core/src/handlers/ContactListenerPerso.java @@ -1,19 +1,38 @@ package handlers; +import com.badlogic.gdx.physics.box2d.Body; 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.Fixture; import com.badlogic.gdx.physics.box2d.Manifold; +import com.badlogic.gdx.utils.Array; public class ContactListenerPerso implements ContactListener { + private int numPlayerOnGround = 0; + Array<Body> bodyToRemove = new Array<Body>(); + /** * Appeler quand 2 fixtures rentre en collision * @param c */ @Override public void beginContact(Contact c) { - System.out.println("contact"); + Fixture fa = c.getFixtureA(); + Fixture fb = c.getFixtureB(); + + if (fb.getUserData() != null && fb.getUserData().equals("foot") && fa.getUserData() != null && fa.getUserData().equals("platform")){ + numPlayerOnGround++; + } + + if (fb.getUserData() != null && fb.getUserData().equals("joyau") && fa.getUserData() != null && fa.getBody().getUserData().equals("player")){ + if (!bodyToRemove.contains(fb.getBody(), false)){ + bodyToRemove.add(fb.getBody()); + System.out.println("à enlever"); + } + } + } /** @@ -22,7 +41,12 @@ public class ContactListenerPerso implements ContactListener { */ @Override public void endContact(Contact c) { + Fixture fa = c.getFixtureA(); + Fixture fb = c.getFixtureB(); + if (fb.getUserData() != null && fb.getUserData().equals("foot") && fa.getUserData() != null && fa.getUserData().equals("platform")){ + numPlayerOnGround--; + } } /** @@ -44,4 +68,12 @@ public class ContactListenerPerso implements ContactListener { public void postSolve(Contact c, ContactImpulse ContactImpulse) { } + + public boolean isPlayerOnGround() { + return numPlayerOnGround > 0; + } + + public Array<Body> getBodyToRemove() { + return bodyToRemove; + } } -- GitLab