Client


The control/client.cs module is chock-full of good stuff. This is another module that will need to have some of its code divested when it grows in later chapters. The main activities taking place in here are as follows:

  • Creation of a key map with key bindings

  • Definition of a callback that gets called from with Torque to generate a 3D view

  • Definition of an interface to hold the 3D view

  • Definition of a series of functions that hook key commands to avatar motion

  • A series of stub routines

Here is the control/client.cs module. Type it in and save it as Emaga4\control\client.cs.

 //============================================================================ // control/client.cs // // This module contains client specific code for handling // the setup and operation of the player's in-game interface. // // 3DGPAI1 emaga4 tutorial game // // Copyright (c) 2003 by Kenneth C. Finney. //============================================================================ if ( IsObject( playerKeymap ) ) // If we already have a player key map,    playerKeymap.delete();       // delete it so that we can make a new one new ActionMap(playerKeymap); $movementSpeed = 1;             // m/s for use by movement functions //---------------------------------------------------------------------------- // The player sees the game via this control //---------------------------------------------------------------------------- new GameTSCtrl(PlayerInterface) {    profile = "GuiContentProfile";    noCursor = "1"; }; function PlayerInterface::onWake(%this) //---------------------------------------------------------------------------- // When PlayerInterface is activated, this function is called. //---------------------------------------------------------------------------- {    $enableDirectInput = "1";    activateDirectInput();    // restore the player's key mappings    playerKeymap.push(); } function GameConnection::InitialControlSet(%this) //---------------------------------------------------------------------------- // This callback is called directly from inside the Torque Engine // during server initialization. //---------------------------------------------------------------------------- {    Echo ("Setting Initial Control Object");    // The first control object has been set by the server    // and we are now ready to go.    Canvas.SetContent(PlayerInterface); } //============================================================================ // Motion Functions //============================================================================ function GoLeft(%val) //---------------------------------------------------------------------------- // "strafing" //---------------------------------------------------------------------------- {    $mvLeftAction = %val; } function GoRight(%val) //---------------------------------------------------------------------------- // "strafing" //---------------------------------------------------------------------------- {    $mvRightAction = %val; } function GoAhead(%val) //---------------------------------------------------------------------------- // running forward //---------------------------------------------------------------------------- {    $mvForwardAction = %val; } function BackUp(%val) //---------------------------------------------------------------------------- // running backwards //---------------------------------------------------------------------------- {    $mvBackwardAction = %val; } function DoYaw(%val) //---------------------------------------------------------------------------- // looking, spinning or aiming horizontally by mouse or joystick control //---------------------------------------------------------------------------- {    $mvYaw += %val * ($cameraFov / 90) * 0.02; } function DoPitch(%val) //---------------------------------------------------------------------------- // looking vertically by mouse or joystick control //---------------------------------------------------------------------------- {    $mvPitch += %val * ($cameraFov / 90) * 0.02; } function DoJump(%val) //---------------------------------------------------------------------------- // momentary upward movement, with character animation //---------------------------------------------------------------------------- {    $mvTriggerCount2++; } //============================================================================ // View Functions //============================================================================ function Toggle3rdPPOVLook( %val ) //---------------------------------------------------------------------------- // Enable the "free look" feature. As long as the mapped key is pressed, // the player can view his avatar by moving the mouse around. //---------------------------------------------------------------------------- {    if ( %val )       $mvFreeLook = true;    else       $mvFreeLook = false; } function Toggle1stPPOV(%val) //---------------------------------------------------------------------------- // switch between 1st and 3rd person point-of-views. //---------------------------------------------------------------------------- {    if (%val)    {       $firstPerson = !$firstPerson;    } } //============================================================================ // keyboard control mappings //============================================================================ // these ones available when player is in game playerKeymap.Bind(keyboard, up, GoAhead); playerKeymap.Bind(keyboard, down, BackUp); playerKeymap.Bind(keyboard, left, GoLeft); playerKeymap.Bind(keyboard, right, GoRight); playerKeymap.Bind( keyboard, numpad0, DoJump ); playerKeymap.Bind( mouse, xaxis, DoYaw ); playerKeymap.Bind( mouse, yaxis, DoPitch ); playerKeymap.Bind( keyboard, z, Toggle3rdPPOVLook ); playerKeymap.Bind( keyboard, tab, Toggle1stPPOV ); // these ones are always available GlobalActionMap.BindCmd(keyboard, escape, "", "quit();"); GlobalActionMap.Bind(keyboard, tilde, ToggleConsole); //============================================================================ // The following functions are called from the client common code modules. // These stubs are added here to prevent warning messages from cluttering // up the log file. //============================================================================ function onServerMessage() { } function onMissionDownloadPhase1() { } function onPhase1Progress() { } function onPhase1Complete() { } function onMissionDownloadPhase2() { } function onPhase2Progress() { } function onPhase2Complete() { } function onPhase3Complete() { } function onMissionDownloadComplete() { } 

Right off the bat, a new ActionMap called playerKeymap is created. This is a structure that holds the mapping of key commands to functions that will be performed—a mechanism often called key binding, or key mapping. We create the new ActionMap with the intent to populate it later in the module.

Then we define the 3D control (TS, or ThreeSpace) we call PlayerInterface (because that's what it is), which will contain our view into the 3D world. It's not a complex definition. It basically uses a profile defined in the common code—something we'll explore in a later chapter. If we want to use our mouse to provide view manipulation, we must set the noCursor property of the control to 1, or true.

Then we define a method for the PlayerInterface control that describes what to do when the control becomes active ("wakes up"). It's not much, but what it does is activate DirectInput in order to grab any user inputs at the keyboard or mouse and then make the playerKeymap bindings active.

Next, we define a callback method for the GameConnection object (you know, the one we created back there in control/main.cs). The engine invokes this method internally when the server has established the connection and is ready to hand control over to us. In this method we assign our player interface control to the Canvas we created earlier in the InitializeClient() function in the control/initialize.cs module.

After that, we define a whole raft of motion functions to which we will later bind keys. Notice that they employ global variables, such as $mvLeftAction. This variable and others like it, each of which starts with $mv, are seen and used internally by the engine.

Then there is a list of key bindings. Notice that there are several variations of the Bind calls. First, there are binds to our playerKeymap, which makes sense. Then there are binds to the GlobalActionMap; these bindings are available at all times when the program is running, not just when an actual game simulation is under way, which is the case with a normal action map.

Finally, there is a list of stub routines. All of these routines are called from within the common code package. We don't need them to do anything yet, but as before, in order to minimize log file warnings, we create stub routines for the functions.




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