ExampleGamemodeScript.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //-----------------------------------------------------------------------------
  2. // The server has started up so do some game start up
  3. //-----------------------------------------------------------------------------
  4. //This file implements game mode logic for an Example gamemode. The primary functions:
  5. //ExampleGameMode::onMissionStart
  6. //ExampleGameMode::onMissionReset
  7. //ExampleGameMode::onMissionEnd
  8. //Are the primary hooks for the server to start, restart and end any active gamemodes
  9. //onMissionStart, for example is called from core/clientServer/scripts/server/levelLoad.cs
  10. //It's called once the server has successfully loaded the level, and has parsed
  11. //through any active scenes to get GameModeNames defined by them. It then iterates
  12. //over them and calls these callbacks to envoke gamemode behaviors. This allows multiple
  13. //gamemodes to be in effect at one time. Modules can implement as many gamemodes as you want.
  14. //
  15. //For levels that can be reused for multiple gammodes, the general setup would be a primary level file
  16. //with the Scene in it having the main geometry, weapons, terrain, etc. You would then have subScenes that
  17. //each contain what's necessary for the given gamemode, such as a subScene that just adds the flags and capture
  18. //triggers for a CTF mode. The subscene would then have it's GameModeName defined to run the CTF gamemode logic
  19. //and the levelLoad code will execute it.
  20. //This function is called when the level finishes loading. It sets up the initial configuration, variables and
  21. //spawning and dynamic objects, timers or rules needed for the gamemode to run
  22. function ExampleGameMode::onMissionStart()
  23. {
  24. //set up the game and game variables
  25. ExampleGameMode::initGameVars();
  26. if ($Game::Running)
  27. {
  28. error("onMissionStart: End the game first!");
  29. return;
  30. }
  31. // Start the game timer
  32. if ($Game::Duration)
  33. $Game::Schedule = schedule($Game::Duration * 1000, "onGameDurationEnd");
  34. $Game::Running = true;
  35. $Game = ExampleGameMode;
  36. }
  37. //This function is called when the level ends. It can be envoked due to the gamemode ending
  38. //but is also kicked off when the game server is shut down as a form of cleanup for anything the gamemode
  39. //created or is managing like the above mentioned dynamic objects or timers
  40. function ExampleGameMode::onMissionEnded()
  41. {
  42. if (!$Game::Running)
  43. {
  44. error("onMissionEnded: No game running!");
  45. return;
  46. }
  47. // Stop any game timers
  48. cancel($Game::Schedule);
  49. $Game::Running = false;
  50. $Game = "";
  51. }
  52. //This function is called in the event the server resets and is used to re-initialize the gamemode
  53. function ExampleGameMode::onMissionReset()
  54. {
  55. // Called by resetMission(), after all the temporary mission objects
  56. // have been deleted.
  57. ExampleGameMode::initGameVars();
  58. }
  59. //This sets up our gamemode's duration time
  60. function ExampleGameMode::initGameVars()
  61. {
  62. // Set the gameplay parameters
  63. $Game::Duration = 30 * 60;
  64. }
  65. //This is called when the timer runs out, allowing the gamemode to end
  66. function ExampleGameMode::onGameDurationEnd()
  67. {
  68. //we don't end if we're currently editing the level
  69. if ($Game::Duration && !(EditorIsActive() && GuiEditorIsActive()))
  70. ExampleGameMode::onMissionEnded();
  71. }
  72. //This is called when a client enters the game server. It's used to spawn a player object
  73. //set up any client-specific properties such as saved configs, values, their name, etc
  74. //These callbacks are activated in core/clientServer/scripts/server/levelDownload.cs
  75. function ExampleGameMode::onClientEnterGame(%client)
  76. {
  77. //Set the player name based on the client's connection data
  78. %client.setPlayerName(%client.connectData);
  79. //In this example, we just spawn a camera
  80. if (!isObject(%client.camera))
  81. {
  82. if(!isObject(Observer))
  83. {
  84. datablock CameraData(Observer)
  85. {
  86. mode = "Observer";
  87. };
  88. }
  89. %client.camera = spawnObject("Camera", Observer);
  90. }
  91. // If we have a camera then set up some properties
  92. if (isObject(%client.camera))
  93. {
  94. MissionCleanup.add( %this.camera );
  95. %client.camera.scopeToClient(%client);
  96. %client.setControlObject(%client.camera);
  97. %client.camera.setTransform("0 0 1 0 0 0 0");
  98. }
  99. }
  100. //This is called when the player leaves the game server. It's used to clean up anything that
  101. //was spawned or setup for the client when it connected, in onClientEnterGame
  102. //These callbacks are activated in core/clientServer/scripts/server/levelDownload.cs
  103. function ExampleGameMode::onClientLeaveGame(%client)
  104. {
  105. // Cleanup the camera
  106. if (isObject(%client.camera))
  107. %client.camera.delete();
  108. }