David Wallace
unread,Dec 21, 2011, 10:36:03 AM12/21/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Sign in to report message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to bost...@googlegroups.com
Chapter 5: Show me the GACT
These tests check individual keys or buttons.
public class KeyCheckTest : TestBase
{
Keys key;
InputService input;
string type;
public override bool Test(BehaviorArgs args)
{
switch (type)
{
case "Pressed":
return input.IsPressed(key);
case "Released":
return input.IsReleased(key);
case "Up":
return input.IsUp(key);
default:
return input.IsDown(key);
}
}
public KeyCheckTest(InputService gi, Keys data, string ktype)
{
key = data;
input = gi;
type = ktype;
}
public static TestBase Build(ContentService service, string source)
{
var ginput = service.Serve<InputService>();
if (ginput == null) return null;
Keys data;
var items = source.Split(',');
if (!Enum.TryParse<Keys>(items[0], out data))
throw new InvalidCastException("Invalid key " + items[0]);
var type = (items.Length > 1 ? items[1] : "Down");
return new KeyCheckTest(ginput, data, type);
}
}
I'm not using the ContentService to load content here but to call up the InputService that the test really needs. Note that the heavy lifting is done in the Build method. The constructor itself is quite basic. All of the scripts in the example use the default, or down, test but you could use Key(4,Released) to determine when the 4 key is released.
public class MouseCheckTest : TestBase
{
MouseButtons mouse;
InputService input;
string type;
public override bool Test(BehaviorArgs args)
{
switch (type) {
case "Pressed":
return input.IsPressed(mouse);
case "Released":
return input.IsReleased(mouse);
case "Up":
return input.IsUp(mouse);
default:
return input.IsDown(mouse);
}
}
public MouseCheckTest(InputService gi, MouseButtons mb, string mtype)
{
input = gi;
mouse = mb;
type = mtype;
}
public static TestBase Build(ContentService service, string source)
{
var ginput = service.Serve<InputService>();
if (ginput == null) return null;
MouseButtons mb;
var items = source.Split(',');
if (!Enum.TryParse<MouseButtons>(items[0], out mb))
throw new InvalidCastException("Invalid mouse " + items[0]);
var type = (items.Length > 1 ? items[1] : "Down");
return new MouseCheckTest(ginput, mb, type);
}
}
Aside from using the MouseButtons enum as opposed to Keys this method, as well as PadCheckTest with Buttons, is identical to KeyCheckTest. The question is one of simplifying code as opposed to script. Combining them would mean my script would have to be verbose like Input(Key(Tab),Up) or cryptic like Input(MLeft). I prefer my script to be as readable as possible even at the price of more code.
public class KeyPollTest : TestBase
{
Keys key;
InputService input;
public override bool Test(BehaviorArgs args)
{
var player = input.PollDown(key);
if (player.HasValue) input.Me = player.Value;
return player.HasValue;
}
public KeyPollTest(InputService gi, Keys k)
{
input = gi;
key = k;
}
public static TestBase Build(ContentService service, string source)
{
var ginput = service.Serve<InputService>();
if (ginput == null) return null;
Keys k;
if (!Enum.TryParse<Keys>(source, out k))
throw new InvalidCastException("Invalid key " + source);
return new KeyPollTest(ginput, k);
}
}
This test sets the active player as well as returns true if the PollDown method returns an actual index. I have an equivalent PadPollTest for the gamepad.
The rest of the input tests are in the next chapter.