Skip to content

Instantly share code, notes, and snippets.

@mueschm
Created August 22, 2012 17:38
Show Gist options
  • Save mueschm/3427818 to your computer and use it in GitHub Desktop.
Save mueschm/3427818 to your computer and use it in GitHub Desktop.
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