From 2317d753518b20039eca28a2e87241cb1e77f407 Mon Sep 17 00:00:00 2001 From: Santiago Faci Date: Tue, 6 Mar 2018 17:05:33 +0100 Subject: [PATCH] Added support to gamepad --- .../jumper2dx/characters/Player.java | 10 ++ .../jumper2dx/managers/SpriteManager.java | 126 ++++++++++++++++-- 2 files changed, 125 insertions(+), 11 deletions(-) diff --git a/core/src/com/codeandcoke/jumper2dx/characters/Player.java b/core/src/com/codeandcoke/jumper2dx/characters/Player.java index 2e2766d..3078867 100644 --- a/core/src/com/codeandcoke/jumper2dx/characters/Player.java +++ b/core/src/com/codeandcoke/jumper2dx/characters/Player.java @@ -231,6 +231,16 @@ public void jump(boolean sound) { canJump = false; isJumping = true; } + + /** + * Check if the player can jump. Then, jump. Otherwise player does nothing + */ + public void tryJump() { + Player.stuckPlatform = null; + if (canJump) { + jump(true); + } + } /** * El jugador muere diff --git a/core/src/com/codeandcoke/jumper2dx/managers/SpriteManager.java b/core/src/com/codeandcoke/jumper2dx/managers/SpriteManager.java index 1988788..8c5d08b 100644 --- a/core/src/com/codeandcoke/jumper2dx/managers/SpriteManager.java +++ b/core/src/com/codeandcoke/jumper2dx/managers/SpriteManager.java @@ -1,6 +1,10 @@ package com.codeandcoke.jumper2dx.managers; import com.badlogic.gdx.audio.Music; +import com.badlogic.gdx.controllers.Controller; +import com.badlogic.gdx.controllers.ControllerListener; +import com.badlogic.gdx.controllers.Controllers; +import com.badlogic.gdx.controllers.PovDirection; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.Texture; @@ -25,7 +29,7 @@ * @author Santiago Faci * @version Agosto 2014 */ -public class SpriteManager { +public class SpriteManager implements ControllerListener{ public Jumper2DX game; @@ -40,6 +44,11 @@ public class SpriteManager { public Music music; public LevelManager levelManager; + + enum PlayerState { + IDLE, LEFT, RIGHT, UP, DOWN + } + private PlayerState playerState; public SpriteManager(Jumper2DX game) { this.game = game; @@ -58,7 +67,10 @@ public SpriteManager(Jumper2DX game) { Gdx.gl.glCullFace(GL20.GL_CULL_FACE); loadCurrentLevel(); - } + + Controllers.addListener(this); + playerState = PlayerState.IDLE; + } /** * Carga el nivel actual @@ -233,7 +245,7 @@ private void checkCollisions() { private void handleInput() { // Se pulsa la teclad derecha - if (Gdx.input.isKeyPressed(Keys.RIGHT)) { + if ((Gdx.input.isKeyPressed(Keys.RIGHT)) || (playerState == PlayerState.RIGHT)) { player.isRunning = true; Player.stuckPlatform = null; player.velocity.x = Player.WALKING_SPEED; @@ -243,7 +255,7 @@ private void handleInput() { player.isRunning = true; } // Se pulsa la tecla izquierda - else if (Gdx.input.isKeyPressed(Keys.LEFT)) { + else if ((Gdx.input.isKeyPressed(Keys.LEFT)) || (playerState == PlayerState.LEFT)) { player.isRunning = true; Player.stuckPlatform = null; player.velocity.x = -Player.WALKING_SPEED; @@ -267,17 +279,14 @@ else if (Gdx.input.isKeyPressed(Keys.LEFT)) { // Se pulsa la tecla CONTROL IZQ (salto) if (Gdx.input.isKeyPressed(Keys.CONTROL_LEFT)) { - - Player.stuckPlatform = null; - if (player.canJump) { - player.jump(true); - } + + player.tryJump(); } - if (Gdx.input.isKeyPressed(Keys.UP)) { + if ((Gdx.input.isKeyPressed(Keys.UP)) || (playerState == PlayerState.UP)) { CAMERA_OFFSET += 40f * Gdx.graphics.getDeltaTime(); } - if (Gdx.input.isKeyPressed(Keys.DOWN)) { + if ((Gdx.input.isKeyPressed(Keys.DOWN)) || (playerState == PlayerState.DOWN)) { CAMERA_OFFSET -= 40f * Gdx.graphics.getDeltaTime(); } @@ -312,4 +321,99 @@ public void dispose() { levelManager.clearCharactersCurrentLevel(); } + + @Override + public void connected(Controller controller) { + + } + + @Override + public void disconnected(Controller controller) { + + } + + @Override + public boolean buttonDown(Controller controller, int buttonCode) { + + switch (buttonCode) { + case 0: + // Nothing + break; + case 1: + // Nothing + break; + case 2: + player.tryJump(); + break; + case 3: + // Nothing + break; + default: + break; + } + + return false; + } + + @Override + public boolean buttonUp(Controller controller, int buttonCode) { + return false; + } + + @Override + public boolean axisMoved(Controller controller, int axisCode, float value) { + + // Player moves around X axis + if (axisCode == 0) { + // Player pushes to go right + if (value == 1.0f) { + playerState = PlayerState.RIGHT; + } + // Player pushes to go left + else if (value == -1.0f) { + playerState = PlayerState.LEFT; + } + // Player release axis control + else { + playerState = PlayerState.IDLE; + } + } + // Player moves around Y axis + else { + // Player pushes to go up + if (value == 1.0f) { + playerState = PlayerState.DOWN; + } + // Player pushes to go down + else if (value == -1.0f) { + playerState = PlayerState.UP; + } + // Player release axis control + else { + playerState = PlayerState.IDLE; + } + } + + return false; + } + + @Override + public boolean povMoved(Controller controller, int povCode, PovDirection value) { + return false; + } + + @Override + public boolean xSliderMoved(Controller controller, int sliderCode, boolean value) { + return false; + } + + @Override + public boolean ySliderMoved(Controller controller, int sliderCode, boolean value) { + return false; + } + + @Override + public boolean accelerometerMoved(Controller controller, int accelerometerCode, Vector3 value) { + return false; + } } \ No newline at end of file