Fixed an input bug by adding an "activated" boolean for inputs

This commit is contained in:
Allexit 2015-05-18 01:54:18 +03:00
parent a82ba237cf
commit e465931b2d
4 changed files with 42 additions and 10 deletions

View File

@ -61,6 +61,7 @@ public class GladiatorBrawler extends ApplicationAdapter {
// Initialize input
inputHandler = new InputHandler();
AppUtil.inputHandler = inputHandler;
Gdx.input.setInputProcessor(inputHandler);
// Initialize states

View File

@ -20,23 +20,33 @@ public class InputHandler implements InputProcessor {
public HashMap<Integer, String> keys = new HashMap<Integer, String>();
private final Array<String> hoveredUIElements = new Array<String>();
private final HashMap<String, Boolean> activatedInputs = new HashMap<String, Boolean>();
public InputHandler() {
keys.put(Keys.A, Name.MOVE_LEFT);
keys.put(Keys.D, Name.MOVE_RIGHT);
keys.put(Keys.SPACE, Name.JUMP);
keys.put(Keys.LEFT, Name.SWING_LEFT);
keys.put(Keys.RIGHT, Name.SWING_RIGHT);
keys.put(Keys.UP, Name.SWING_UP);
keys.put(Keys.DOWN, Name.SWING_DOWN);
keys.put(Keys.F2, Name.DEBUG);
addInput(Keys.A, Name.MOVE_LEFT, false);
addInput(Keys.D, Name.MOVE_RIGHT, false);
addInput(Keys.SPACE, Name.JUMP, false);
addInput(Keys.LEFT, Name.SWING_LEFT, false);
addInput(Keys.RIGHT, Name.SWING_RIGHT, false);
addInput(Keys.UP, Name.SWING_UP, false);
addInput(Keys.DOWN, Name.SWING_DOWN, false);
addInput(Keys.F2, Name.DEBUG, false);
}
private void addInput(int key, String action, boolean activated) {
keys.put(key, action);
activatedInputs.put(action, activated);
}
public void setInputEnabled(String action, boolean enabled) {
activatedInputs.put(action, enabled);
}
@Override
public boolean keyDown(int keycode) {
if (!keys.containsKey(keycode)) {
return false;
}
} if (!activatedInputs.get(keys.get(keycode))) { return false; }
String actionName = keys.get(keycode);
return InputReceivers.getReceiver(actionName).pressed();
}
@ -45,7 +55,7 @@ public class InputHandler implements InputProcessor {
public boolean keyUp(int keycode) {
if (!keys.containsKey(keycode)) {
return false;
}
} if (!activatedInputs.get(keys.get(keycode))) { return false; }
String actionName = keys.get(keycode);
return InputReceivers.getReceiver(actionName).released();
}

View File

@ -48,6 +48,15 @@ public class InGameState extends BaseState {
guiCreator = new InGameGUICreator();
guiCreator.create();
// Activate inputs
AppUtil.inputHandler.setInputEnabled(Name.JUMP, true);
AppUtil.inputHandler.setInputEnabled(Name.MOVE_LEFT, true);
AppUtil.inputHandler.setInputEnabled(Name.MOVE_RIGHT, true);
AppUtil.inputHandler.setInputEnabled(Name.SWING_DOWN, true);
AppUtil.inputHandler.setInputEnabled(Name.SWING_LEFT, true);
AppUtil.inputHandler.setInputEnabled(Name.SWING_RIGHT, true);
AppUtil.inputHandler.setInputEnabled(Name.SWING_UP, true);
}
@Override
@ -94,6 +103,16 @@ public class InGameState extends BaseState {
@Override
public void destroy() {
// Deactivate inputs
AppUtil.inputHandler.setInputEnabled(Name.JUMP, false);
AppUtil.inputHandler.setInputEnabled(Name.MOVE_LEFT, false);
AppUtil.inputHandler.setInputEnabled(Name.MOVE_RIGHT, false);
AppUtil.inputHandler.setInputEnabled(Name.SWING_DOWN, false);
AppUtil.inputHandler.setInputEnabled(Name.SWING_LEFT, false);
AppUtil.inputHandler.setInputEnabled(Name.SWING_RIGHT, false);
AppUtil.inputHandler.setInputEnabled(Name.SWING_UP, false);
// Clear all entities that are left as they are no longer needed
AppUtil.engine.removeAllEntities();
// Clear GUI so there's nothing leftover for the next state

View File

@ -4,6 +4,7 @@ import com.badlogic.ashley.core.Engine;
import com.badlogic.ashley.core.Entity;
import com.badlogic.gdx.math.Vector2;
import com.saltosion.gladiator.gui.GUIManager;
import com.saltosion.gladiator.input.InputHandler;
import com.saltosion.gladiator.level.EntityFactory;
import com.saltosion.gladiator.level.LevelFactory;
@ -15,6 +16,7 @@ public class AppUtil {
public static EntityFactory entityFactory;
public static LevelFactory levelFactory;
public static GUIManager guiManager;
public static InputHandler inputHandler;
public static final int VPHEIGHT_CONST = 24;