From e465931b2d80ff25344b4447179737edac89b185 Mon Sep 17 00:00:00 2001 From: Allexit Date: Mon, 18 May 2015 01:54:18 +0300 Subject: [PATCH] Fixed an input bug by adding an "activated" boolean for inputs --- .../saltosion/gladiator/GladiatorBrawler.java | 1 + .../gladiator/input/InputHandler.java | 30 ++++++++++++------- .../gladiator/state/InGameState.java | 19 ++++++++++++ .../com/saltosion/gladiator/util/AppUtil.java | 2 ++ 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/core/src/com/saltosion/gladiator/GladiatorBrawler.java b/core/src/com/saltosion/gladiator/GladiatorBrawler.java index c722c8c..4e0cde6 100644 --- a/core/src/com/saltosion/gladiator/GladiatorBrawler.java +++ b/core/src/com/saltosion/gladiator/GladiatorBrawler.java @@ -61,6 +61,7 @@ public class GladiatorBrawler extends ApplicationAdapter { // Initialize input inputHandler = new InputHandler(); + AppUtil.inputHandler = inputHandler; Gdx.input.setInputProcessor(inputHandler); // Initialize states diff --git a/core/src/com/saltosion/gladiator/input/InputHandler.java b/core/src/com/saltosion/gladiator/input/InputHandler.java index b05a6c4..ec0b1ca 100644 --- a/core/src/com/saltosion/gladiator/input/InputHandler.java +++ b/core/src/com/saltosion/gladiator/input/InputHandler.java @@ -20,23 +20,33 @@ public class InputHandler implements InputProcessor { public HashMap keys = new HashMap(); private final Array hoveredUIElements = new Array(); + private final HashMap activatedInputs = new HashMap(); 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(); } diff --git a/core/src/com/saltosion/gladiator/state/InGameState.java b/core/src/com/saltosion/gladiator/state/InGameState.java index af07fda..ba1716e 100644 --- a/core/src/com/saltosion/gladiator/state/InGameState.java +++ b/core/src/com/saltosion/gladiator/state/InGameState.java @@ -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 diff --git a/core/src/com/saltosion/gladiator/util/AppUtil.java b/core/src/com/saltosion/gladiator/util/AppUtil.java index 39b178c..e7a5a36 100644 --- a/core/src/com/saltosion/gladiator/util/AppUtil.java +++ b/core/src/com/saltosion/gladiator/util/AppUtil.java @@ -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;