Server


The control/server.cs module is where game-specific server code is located. Most of the functionality that is carried in this module is found in the form of methods for the GameConnection class. Here is the control/server.cs module. Type it in and save it as Emaga4\control\server.cs.

 //============================================================================ // control/server.cs // // server-side game specific module for 3DGPAI1 emaga4 tutorial game // provides client connection management and player/avatar spawning // // Copyright (c) 2003 by Kenneth C. Finney. //============================================================================ function OnServerCreated() //---------------------------------------------------------------------------- // Once the engine has fired up the server, this function is called //---------------------------------------------------------------------------- {    Exec("./player.cs"); // Load the player datablocks and methods } //============================================================================ // GameConnection Methods // Extensions to the GameConnection class. Here we add some methods // to handle player spawning and creation. //============================================================================ function GameConnection::OnClientEnterGame(%this) //---------------------------------------------------------------------------- // Called when the client has been accepted into the game by the server. //---------------------------------------------------------------------------- {    // Create a player object.    %this.spawnPlayer(); } function GameConnection::SpawnPlayer(%this) //---------------------------------------------------------------------------- // This is where we place the player spawn decision code. // It might also call a function that would figure out the spawn // point transforms by looking up spawn markers. // Once we know where the player will spawn, then we create the avatar. //---------------------------------------------------------------------------- {    %this.createPlayer("0 0 220 1 0 0 0"); } function GameConnection::CreatePlayer(%this, %spawnPoint) //---------------------------------------------------------------------------- // Create the player's avatar object, set it up, and give the player control // of it. //---------------------------------------------------------------------------- {    if (%this.player > 0)//The player should NOT already have an avatar object.    {                              // If he does, that's a Bad Thing.       Error( "Attempting to create an angus ghost!" );    }    // Create the player object       %player = new Player() {       dataBlock = HumanMaleAvatar; // defined in player.cs       client = %this;            // the avatar will have a pointer to its    }; // owner's connection    // Player setup...    %player.setTransform(%spawnPoint); // where to put it    // Give the client control of the player    %this.player = %player;    %this.setControlObject(%player); } //============================================================================ // The following functions are called from the server common code modules. // These stubs are added here to prevent warning messages from cluttering // up the log file. //============================================================================ function ClearCenterPrintAll() { } function ClearBottomPrintAll() { } 

The first function, OnServerCreated, manages what happens immediately after the server is up and running. In our case we need the player-avatar datablocks and methods to be loaded up so they can be transmitted to the client.

Then we define some GameConnection methods. The first one, OnClientEnterGame, simply calls the SpawnPlayer method, which then calls the CreatePlayer method using the hard-coded transform provided.

CreatePlayer then creates a new player object using the player datablock defined in control/player.cs (which we will review shortly). It then applies the transform (which we created manually earlier) to the player's avatar and then transfers control to the player.

Finally, there are a couple more stub routines. That's the end of them—for now—I promise!




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