Skip to content

Commit

Permalink
Added support to gamepad
Browse files Browse the repository at this point in the history
  • Loading branch information
sfaci committed Mar 6, 2018
1 parent 8c5a3a3 commit 2317d75
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 11 deletions.
10 changes: 10 additions & 0 deletions core/src/com/codeandcoke/jumper2dx/characters/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
126 changes: 115 additions & 11 deletions core/src/com/codeandcoke/jumper2dx/managers/SpriteManager.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -25,7 +29,7 @@
* @author Santiago Faci
* @version Agosto 2014
*/
public class SpriteManager {
public class SpriteManager implements ControllerListener{

public Jumper2DX game;

Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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();
}

Expand Down Expand Up @@ -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;
}
}

0 comments on commit 2317d75

Please sign in to comment.