====== al::Nerve ======
"Nerves" are a concept (State Machine in which every "Nerve" acts as a State) al uses to simplify distributing code in per-frame update functions. While this increases the amount of boilerplate code in the project, it makes code easier to read and understand.
Any class can inherit from the IUseNerve interface to provide a NerveKeeper. The NerveKeeper holds the current active nerve, and how many frames it has been running for. A "Nerve" is a member function of the class that will run every frame if it is active. For actors, nerves are often directly linked to skeletal animations the actor has, for example a "Jump" nerve that plays the "Jump" animation.
===== Example =====
The following is pseudocode on how an actor that flies up and down with an animation would be handled with and without the nerve concept.
// This is how it would be done in any generic game engine: //
void MyActor::update()
{
moveFrames++;
if (moveFrames >= 60) {
moveFrames = 0;
moveDirection = !moveDirection;
startAnim(this, moveDirection ? "MoveUp" : "MoveDown");
}
yPosition += moveDirection ? 10 : -10;
}
// This is how it would be done with nerves: //
void MyActor::exeMoveUp()
{
if (al::isFirstStep(this))
startAnim(this, "MoveUp");
if (al::isStep(this, 60))
al::setNerve(this, &nrvMyActorMoveDown);
yPosition += 10;
}
void MyActor::exeMoveDown()
{
if (al::isFirstStep(this))
startAnim(this, "MoveDown");
if (al::isStep(this, 60))
al::setNerve(this, &nrvMyActorMoveUp);
yPosition -= 10;
}