Created
August 22, 2012 17:38
-
-
Save mueschm/3427818 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Controller | |
{ | |
public class ControllerAction | |
{ | |
public delegate void KeyPressedDelegateFunction(); | |
private String name; | |
private Key key; | |
private KeyPressedDelegateFunction keyPressedFunction; | |
public ControllerAction(Key key, String name, KeyPressedDelegateFunction function) | |
{ | |
this.name = name; | |
this.key = key; | |
this.keyPressedFunction += function; | |
} | |
public void CheckKeyPress(KeyboardState kState) | |
{ | |
if(kState.IsKeyDown(key)) | |
if(keyPressedFunction != null) | |
keyPressedFunction(); | |
} | |
} | |
private static List<Dictionary<Key,ControllerAction>> controllers; | |
public static Initialize() | |
{ | |
controllers = new List<Dictionary<Key,ControllerAction>>(); | |
} | |
public static void update() | |
{ | |
int index = 0; | |
foreach(Dictionary<Key,ControllerAction> controller in controllers) | |
{ | |
KeyboardState kState = Keyboard.getState((PlayerIndex)i++); | |
foreach(KeyValuePair<Key,ControllerAction> entry in controller) | |
{ | |
entry.Value.CheckKeyPress(); | |
} | |
} | |
} | |
public static void AddPlayer() | |
{ | |
controllers.Add(new Dictionary<Key,ControllerAction>()); | |
} | |
public static void AddKey(int player, Key key, String name, ControllerAction.KeyPressedDelegateFunction function) | |
{ | |
controllers[i].Add(key,new ControllerAction(key,name,function)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment