Now that we have our knifetimer variable, we want it to activate if the player makes a kill with the knife.
Most things to do with enemies are programmed in WL_ACT2 and WL_STATE, which is where we'll look. Have a look at the KillActor() function in WL_STATE:
/*
===============
=
= KillActor
=
===============
*/
void KillActor (objtype *ob)
{
int tilex,tiley;
tilex = ob->x >> TILESHIFT; // drop item on center
tiley = ob->y >> TILESHIFT;
switch (ob->obclass)
{
case guardobj:
GivePoints (100);
NewState (ob,&s_grddie1);
PlaceItemType (bo_clip2,tilex,tiley);
break;
case officerobj:
This function is what activates when an enemy is damaged enough that they lose all their health. Points and items are rewarded while the enemy goes into it's death animation.
At the bottom of the function, the killcount is increased and enemy properties are changed to reflect that they are dead.
Here, we'll want to set the killtimer to go for 10 seconds, but only if the player made the kill with the knife. The easiest way to do this will be by simply checking the player's weapon at the time of the kill. To do that, we'll add an if statement checking the player's current weapon.
case deathobj:
GivePoints (5000);
NewState (ob,&s_deathdie1);
PlaceItemType (bo_key1,tilex,tiley);
break;
#endif
}
if (gamestate.weapon == wp_knife)
gamestate.knifetimer = 700;
gamestate.killcount++;
ob->flags &= ~FL_SHOOTABLE;
actorat[ob->tilex][ob->tiley] = NULL;
ob->flags |= FL_NONMARK;
}
Here, when there is a kill, we are telling Wolf3D to check if the current weapon is a knife, and if it is, increase our knifetimer to a value of 700. Why 700? Well in Wolfenstein 3D, time is measured in tics, which represent 1/70 of a second. So, to do 10 seconds, we must multiply by 70.
Alternatively, if you find it easier to read, you could make one small visual change
if (gamestate.currentweapon == wp_knife)
gamestate.knifetimer = 10 * 70;
Here, we have used the format seconds * 70, which in the above example will still result in 350, but at a glance you can know how many seconds are assigned to knifetimer.