Prototype By: Priyesh Dixit
Engineering By: Jason Hardman & Subhir Rao
DLL and Docs By: Jason Hardman
Summary:
DWController is an override controller system in C# for Xna.
It collects input from the Mouse, Keyboard, XBox Analog Controls, and XBox Digital Controls.
Key/Function pairs are added in override constructor.
Key-press events call Functions when triggered, passing the key_value and a user supplied object[] array via EventArg.
Download:
Includes demo project
http://flatline.darkwynter.com/compSci/DWControllers.zip
Usage:
To use DWController, override the Controller class, use the controller.Add() function with the key you wish to assign, and function you want to call, and the timer-delay for the key input. This time delay repeat inputs (from holding a key down) giving your application a consistent input rate across multiple PC hardware configurations. The key-delay argument is specified in milliseconds. To disable it, just pass in 0.0f.
It’s simple. To connect a key to a function, use commands like:
Add(new KeyboardControl(Keys.W, MoveForward, 10));
Add(new KeyboardControl(Keys.A, MoveLeft, 10));
Add(new MouseControl(ControlType.Motion, Rotate, 0));
Add(new MouseControl(ControlType.LeftButton, Attack, 0));
Installation:
- Import the dll into your project…
- Right-click References (in Project Explorer) and click Add…
- Locate the DWControllers dll and select.
Remember to include:using DarkWynter.Engine.Controllers;Override The Controller Class
using DarkWynter.Engine.Controllers;
public class GameController : Controller
{
public GameController(int player, PlayerIndex playerIndex)
: base(player, playerIndex)
{
// Add keyboard and mouse buttons
// Pass in the key you want to use, the function you want to call, and the refresh rate of the key
Add(new KeyboardControl(Keys.W, MoveForward, 10));
Add(new MouseControl(ControlType.LeftButton,Attack,0));
}
// Function called by controller to handle Move Forward event
private void MoveForward(ControllerBoolEventArgs args)
{
args.objectLibrary.humans[playerNumber].Translate(new Vector2(0, 10));
}
// Function called by controller to handle Attack event
private void Attack(ControllerBoolEventArgs args)
{
args.objectLibrary.humans[playerNumber].ChangeAttackAmount(0.5f);
}
}
Instantiate in your main Game file…
ControllerManager controllerManager;
List<object> args = new List<object>();
protected override void Initialize()
{
// Create a List of Child Controllers
List<Type> controllerTypes = new List<Type>();
controllerTypes.Add(typeof(MenuController));
controllerTypes.Add(typeof(GameController));
// Pass in Width and Height used by mouse.
controllerManager = new ControllerManager(
graphics.GraphicsDevice.Viewport.Width,
graphics.GraphicsDevice.Viewport.Height,
controllerTypes
);
// Pass “this” as EventArgs to Conroller delegates functions
args.Add(this);
base.Initialize();
}
And Update…
protected override void Update(GameTime gameTime)
{
// Checks if any controllers Added or Removed
controllerManager.Update();
// Update Appropriate Controller
if (gameMode == GameMode.Menu)
{
// Update all MenuControllers
List<Controller> controllers = controllerManager.GetControllers(typeof(MenuController));
for (int i = 0; i < controllers.Count; i++)
{
controllers[i].Update(ref args);
}
}
if (gameMode == GameMode.Game)
{
// Update all GameControllers
List<Controller> controllers = controllerManager.GetControllers(typeof(GameController));
for (int i = 0; i < controllers.Count; i++)
{
controllers[i].Update(ref args);
}
}
base.Update(gameTime);
}
Enjoy :-j