diff options
| author | Ben Beltran <ben@nsovocal.com> | 2013-11-10 22:11:08 -0600 |
|---|---|---|
| committer | Ben Beltran <ben@nsovocal.com> | 2013-11-10 22:11:08 -0600 |
| commit | 95d7601b7742ed560a9d8e00269217f62fc7ce32 (patch) | |
| tree | 66b3ac90c47c202c017e78d347bcd4c33121a28c /Super Polarity/InputController.cs | |
| parent | 9899ae5d21c559b98386be48c5a80e63db10552d (diff) | |
Chubas's House happened.
Diffstat (limited to 'Super Polarity/InputController.cs')
| -rw-r--r-- | Super Polarity/InputController.cs | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/Super Polarity/InputController.cs b/Super Polarity/InputController.cs new file mode 100644 index 0000000..405b3ab --- /dev/null +++ b/Super Polarity/InputController.cs @@ -0,0 +1,118 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Microsoft.Xna.Framework.Input; + +namespace SuperPolarity +{ + static class InputController + { + static Dictionary<string, List<Action<float>>> Listeners; + static Dictionary<string, List<Keys>> RegisteredKeys; + static Dictionary<string, List<Buttons>> RegisteredButtons; + + static GamePadState InputGamePadState; + static KeyboardState InputKeyboardState; + + /* + * Registered Events. + * + * You register: name of the event (ie. attack) and a key associated with it. + * or button... Left Stick /always/ dispatches move event. + */ + + static InputController() + { + Listeners = new Dictionary<string,List<Action<float>>>(); + InputKeyboardState = new KeyboardState(); + InputGamePadState = new GamePadState(); + } + + public static void UpdateInput() + { + Poll(); + DispatchMoveEvents(); + DispatchRegisteredEvents(); + } + + private static void Poll() + { + InputGamePadState = GamePad.GetState(Microsoft.Xna.Framework.PlayerIndex.One); + InputKeyboardState = Keyboard.GetState(); + } + + private static void DispatchRegisteredEvents() + { + } + + private static void DispatchMoveEvents() + { + float xMovement = 0.0f; + float yMovement = 0.0f; + // Dispatch the moveX / MoveY events every frame. + + xMovement = InputGamePadState.ThumbSticks.Left.X; + yMovement = -InputGamePadState.ThumbSticks.Left.Y; + + Console.WriteLine("Dispatching Input {0}", InputKeyboardState.IsKeyDown(Keys.Left)); + + if (InputKeyboardState.IsKeyDown(Keys.Left)) + { + xMovement = -1.0f; + } + + if (InputKeyboardState.IsKeyDown(Keys.Right)) + { + xMovement = 1.0f; + } + + if (InputKeyboardState.IsKeyDown(Keys.Up)) + { + yMovement = -1.0f; + } + + if (InputKeyboardState.IsKeyDown(Keys.Down)) + { + yMovement = 1.0f; + } + + Dispatch("moveX", xMovement); + Dispatch("moveY", yMovement); + } + + public static void Bind(string eventName, Action<float> listener) + { + List<Action<float>> newListenerList; + List<Action<float>> listenerList; + bool foundListeners; + + if (!Listeners.ContainsKey(eventName)) { + newListenerList = new List<Action<float>>(); + Listeners.Add(eventName, newListenerList); + } + + foundListeners = Listeners.TryGetValue(eventName, out listenerList); + + listenerList.Add(listener); + } + + public static void Dispatch(string eventName, float value) + { + List<Action<float>> listenerList; + bool foundListeners; + + foundListeners = Listeners.TryGetValue(eventName, out listenerList); + + if (!foundListeners) + { + return; + } + + foreach (Action<float> method in listenerList) + { + method(value); + } + } + } +} |