Skip to content
Snippets Groups Projects
Commit 703be6f8 authored by timeo's avatar timeo
Browse files

Création Classes pour gérer les contacts et les Inputs

parent 23281d86
No related branches found
No related tags found
No related merge requests found
......@@ -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();
}
}
......
......@@ -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();
......
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) {
}
}
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];
}
}
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;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment