ExampleGamemodeScript.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 to actually spawn a control object for the player to utilize
  73. //A player character, spectator camera, etc.
  74. function ExampleGameMode::spawnControlObject(%client)
  75. {
  76. //In this example, we just spawn a camera
  77. if (!isObject(%client.camera))
  78. {
  79. if(!isObject(Observer))
  80. {
  81. datablock CameraData(Observer)
  82. {
  83. mode = "Observer";
  84. };
  85. }
  86. %client.camera = spawnObject("Camera", Observer);
  87. }
  88. // If we have a camera then set up some properties
  89. if (isObject(%client.camera))
  90. {
  91. MissionCleanup.add( %this.camera );
  92. %client.camera.scopeToClient(%client);
  93. %client.setControlObject(%client.camera);
  94. %client.camera.setTransform("0 0 1 0 0 0 0");
  95. }
  96. }
  97. //This is called when the client has initially established a connection to the game server
  98. //It's used for setting up anything ahead of time for the client, such as loading in client-passed
  99. //config stuffs, saved data or the like that should be handled BEFORE the client has actually entered
  100. //the game itself
  101. function ExampleGameMode::onClientConnect(%client)
  102. {
  103. }
  104. //This is called when a client enters the game server. It's used to spawn a player object
  105. //set up any client-specific properties such as saved configs, values, their name, etc
  106. //These callbacks are activated in core/clientServer/scripts/server/levelDownload.cs
  107. function ExampleGameMode::onClientEnterGame(%client)
  108. {
  109. //Set the player name based on the client's connection data
  110. %client.setPlayerName(%client.connectData);
  111. ExampleGameMode::spawnControlObject(%client);
  112. }
  113. //This is called when the player leaves the game server. It's used to clean up anything that
  114. //was spawned or setup for the client when it connected, in onClientEnterGame
  115. //These callbacks are activated in core/clientServer/scripts/server/levelDownload.cs
  116. function ExampleGameMode::onClientLeaveGame(%client)
  117. {
  118. // Cleanup the camera
  119. if (isObject(%client.camera))
  120. %client.camera.delete();
  121. }
  122. //This is called when the player has connected and finaly setup is done and control is handed
  123. //over to the client. It allows a point to special-case setting the client's canvas content
  124. //(Such as a gamemode-specific GUI) or setting up gamemode-specific keybinds/control schemes
  125. function ExampleGameMode::onInitialControlSet()
  126. {
  127. }