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 // Initialize input
inputHandler = new InputHandler(); inputHandler = new InputHandler();
AppUtil.inputHandler = inputHandler;
Gdx.input.setInputProcessor(inputHandler); Gdx.input.setInputProcessor(inputHandler);
// Initialize states // Initialize states

View File

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

View File

@ -48,6 +48,15 @@ public class InGameState extends BaseState {
guiCreator = new InGameGUICreator(); guiCreator = new InGameGUICreator();
guiCreator.create(); 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 @Override
@ -94,6 +103,16 @@ public class InGameState extends BaseState {
@Override @Override
public void destroy() { 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 // Clear all entities that are left as they are no longer needed
AppUtil.engine.removeAllEntities(); AppUtil.engine.removeAllEntities();
// Clear GUI so there's nothing leftover for the next state // 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.ashley.core.Entity;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.saltosion.gladiator.gui.GUIManager; import com.saltosion.gladiator.gui.GUIManager;
import com.saltosion.gladiator.input.InputHandler;
import com.saltosion.gladiator.level.EntityFactory; import com.saltosion.gladiator.level.EntityFactory;
import com.saltosion.gladiator.level.LevelFactory; import com.saltosion.gladiator.level.LevelFactory;
@ -15,6 +16,7 @@ public class AppUtil {
public static EntityFactory entityFactory; public static EntityFactory entityFactory;
public static LevelFactory levelFactory; public static LevelFactory levelFactory;
public static GUIManager guiManager; public static GUIManager guiManager;
public static InputHandler inputHandler;
public static final int VPHEIGHT_CONST = 24; public static final int VPHEIGHT_CONST = 24;