So far we've created the foundations for the player's level progression system, but currently it will stay at '0'. How does the player increase their level?
To solve that question, we're going to adapt the pre-existing highscore system!
First, let's have a look at score. The functions for it exist in WL_AGENT:
/*
===============
=
= DrawScore
=
===============
*/
void DrawScore(void)
{
if (viewsize == 21 && ingame)
return;
LatchNumber(6, 16, 6, gamestate.score);
}
/*
===============
=
= GivePoints
=
===============
*/
void GivePoints(int32_t points)
{
gamestate.score += points;
while (gamestate.score >= gamestate.nextextra)
{
gamestate.nextextra += EXTRAPOINTS;
GiveExtraMan();
}
DrawScore();
}
DrawScore() will draw the value of the player's current score on the statusbar, and GivePoints() is the function that rewards the player with score.
An example of the use of GivePoints() can be found in the KillActor() function inside WL_STATE, which triggers when an enemy dies:
case guardobj:
GivePoints(100);
NewState(ob, &s_grddie1);
PlaceItemType(bo_clip2, tilex, tiley);
break;
The above snippet is the code that initiates when a Brown Guard dies - The player is rewarded with 100 points, the enemy enters a fully dead state, and drops a small ammo clip.
Returning ourselves to the GivePoints() function, the game adds the amount specified (In the KillActor() snippet, this specified value is '100') to the player's score variable (gamestate.score). It then goes on to check the player's current score and if it has reached a milestone (stored in the 'gamestate.nextextra' variable), then it will reward a life and increase the value of gamestate.nextextra to the next milestone.
This behaviour mimicks everything we need for a level/experience system! Score can easily be treated as experience, and the 'gamestate.nextextra' milestone can be the requirement to level up!
All we need to do to get this working is change a single line in the function:
/*
===============
=
= GivePoints
=
===============
*/
void GivePoints(int32_t points)
{
gamestate.score += points;
while (gamestate.score >= gamestate.nextextra)
{
gamestate.lvl++; // Our changed line, while removing the call to GetExtraMan()
gamestate.nextextra += EXTRAPOINTS;
}
DrawScore();
}
Typically, the game awards an extra life when the player hits the milestone contained in 'gamestate.nextextra'. Now instead, the player's level will increase by one on that milestone.
How does the milestone work? Well, in the NewGame() function we visited earlier, we can see the line that sets the initial value:
gamestate.nextextra = EXTRAPOINTS;
But what is EXTRAPOINTS? We can find it in WL_DEF, under 'GLOBAL CONSTANTS':
#define EXTRAPOINTS 40000
This is a "define preprocessor", and basically works as a text-substitution for the value. This line makes it so if you reference 'EXTRAPOINTS' elsewhere in the source code, the game will replace that with a value of 40,000..
So the way we have set it up so far, the player will level up for every 40,000 points they score. However, that might be a bit much as it could take a while for the player to level up. For this guide, we'll make it 10,000.
#define EXTRAPOINTS 10000
For your personal game, you'll want to finetune this amount to match what you need in your game.