Game Cycling


The final feature we need to implement is the ability to cycle games when they are over—that is, when a player has reached either the score limit or the time limit.

First, add the following functions to the end of C:\koob\control\server\server.cs:

 function cycleGame() {    if (!$Game::Cycling) {       $Game::Cycling = true;       $Game::Schedule = schedule(0, 0, "onCycleExec");    } } function onCycleExec() {    endGame();    $Game::Schedule = schedule($Game::EndGamePause * 1000, 0, "onCyclePauseEnd"); } function onCyclePauseEnd() {    $Game::Cycling = false;    %search = $Server::MissionFileSpec;    for (%file = findFirstFile(%search); %file !$= "";          %file = findNextFile(%search)) {       if (%file $= $Server::MissionFile) {          %file = findNextFile(%search);          if (%file $= "")             %file = findFirstFile(%search);          break;       }    }    loadMission(%file); } 

The first function, cycleGame, schedules the actual cycling code to occur at some later point. In this case we do it right away after making sure that we aren't actually already cycling.

The function nCycleExec actually ends the game. The endGame function just stops when it finishes, not doing anything else. Further action is scheduled to be taken by the onCyclePauseEnd function. This allows us to put up a victory screen or other messages and leave them up for an appropriate viewing time before continuing on to the next game.

In order to provoke the actual cycleGame function into being, we do two things. First, when the game is launched, we schedule its demise based on $Game::Duration. To do this, locate the function StartGame farther up in the server.cs file, and add these lines to the beginning:

    if ($Game::Duration)       $Game::Schedule = schedule($Game::Duration * 1000, 0, "CycleGame" ); 

This starts the game timer running. When it expires, it invokes the cycleGame function.

The other thing we need to do is add some code that checks to see if a player has hit the $Game::MaxPoints limit.

Locate the function GameConnection::DoScore() and add this code to the top of the function:

    %client.score = (%client.lapsCompleted * $Game::Laps_Multiplier) +                    (%client.money * $Game::Money_Multiplier) +                    (%client.deaths * $Game::Deaths_Multiplier) +                    (%client.kills * $Game::Kills_Multiplier) ; 

This code accumulates the various scoring values into a single overall score. Now add the following code to the end of the same DoScore function:

    if (%client.score >= $Game::MaxPoints)      cycleGame(); 

This causes the game cycling activity to happen if any one player hits the score limit. Game cycling entails ending the game, loading a new map, and dropping the players into the game in the new map.




3D Game Programming All in One
3D Game Programming All in One (Course Technology PTR Game Development Series)
ISBN: 159200136X
EAN: 2147483647
Year: 2006
Pages: 197

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net