Today I wrote code for doing movement combos like console fighting games often have. It's very versatile... some test combinations I did were pressing W, then releasing it while pressing A and D at the same time... or press A, then S, then D, then S again, then A again... you can test them by
playing this; either one will fire a fireball. Using the arrow keys to move, up arrow to jump.
Anyway I figured other people might find the code useful... it won't work "out of the box" since it uses too many of my other files, but it's easy to adapt. Here's
MoveCombo.h which has most of it. The sample game uses this code:
using namespace move;
MoveCombo[0] = combo( 1,
2,
step(1, keypress(KEY_W)),
step(3, keyrelease(KEY_W), keypress(KEY_A), keypress(KEY_D)) );
MoveCombo[1] = combo( 1,
5,
step(1, keypress(KEY_A)),
step(2, keypress(KEY_S), keyrelease(KEY_A)),
step(2, keypress(KEY_D), keyrelease(KEY_S)),
step(2, keypress(KEY_S), keyrelease(KEY_D)),
step(2, keypress(KEY_A), keyrelease(KEY_S)) );
What that actually means, I'll describe line-by-line for the first combination:
MoveCombo[0] = combo( 1,
'1' means one second -- each step of the move needs to be completed within one second of the previous step.
2,
There are 2 steps.
step(1, keypress(KEY_W)),
The first step is for the player to press the W key. The '1' indicates how many keys are in this step.
step(3, keyrelease(KEY_W), keypress(KEY_A), keypress(KEY_D)) );
This means the player should then release W, press A, and press D, all in the same step. That means they all need to be entered simultaneously (within 1/8 of a second). So first the player presses W, then less than a second later he needs to release W and press A and D, all at the same time. If he released W, then pressed A, then pressed D, it wouldn't work (unless he does it really quickly, since there's 1/8 second tolerance).
The code for casting the fireball is:
if( MoveCombo[0].IsEntered(Keyboard) ||
MoveCombo[1].IsEntered(Keyboard) )
{
// fire the fireball
}
As for MoveCombo.h, it uses my keyboard class for input... all you need to know is:
- 'Time' is a static global (of the base 'object' class) that's just "float(timeGetTime()) * 0.001f" which means it's the current time in seconds.
- The keycodes (KEY_W, etc.) are identical to the already defined codes (VK_W, etc.).
- Keyboard->PressTime and Keyboard->ReleaseTime are arrays that indicate the time when a given key was pressed or released. They actually remember the last 8 times every key was pressed and released... PressTime[KEY_W][0] returns the last time the W key was pressed, whereas PressTime[KEY_W][1] returns the time of the previous time it was pressed. If it's only been pressed once, this will be 0. So for the A-S-D-S-A combo, A and S are pressed twice, so it has to know the times of each of those presses.
Let me know if anyone finds any use from this, or has any questions.