ExampleGamemodeScript.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. function ExampleGameMode::onCreateGame()
  21. {
  22. // Note: The Game object will be cleaned up by MissionCleanup. Therefore its lifetime is
  23. // limited to that of the mission.
  24. new ScriptObject(ExampleGameMode){};
  25. return ExampleGameMode;
  26. }
  27. //This function is called when the level finishes loading. It sets up the initial configuration, variables and
  28. //spawning and dynamic objects, timers or rules needed for the gamemode to run
  29. function ExampleGameMode::onMissionStart(%this)
  30. {
  31. //set up the game and game variables
  32. %this.initGameVars();
  33. if (%this.running)
  34. {
  35. error("onMissionStart: End the game first!");
  36. return;
  37. }
  38. // Start the game timer
  39. if (%this.duration)
  40. %this.gameSchedule = schedule(%this.duration * 1000, "onGameDurationEnd");
  41. %this.running = true;
  42. }
  43. //This function is called when the level ends. It can be envoked due to the gamemode ending
  44. //but is also kicked off when the game server is shut down as a form of cleanup for anything the gamemode
  45. //created or is managing like the above mentioned dynamic objects or timers
  46. function ExampleGameMode::onMissionEnded(%this)
  47. {
  48. if (!%this.running)
  49. {
  50. error("onMissionEnded: No game running!");
  51. return;
  52. }
  53. // Stop any game timers
  54. cancel(%this.gameSchedule);
  55. %this.running = false;
  56. }
  57. //This function is called in the event the server resets and is used to re-initialize the gamemode
  58. function ExampleGameMode::onMissionReset(%this)
  59. {
  60. // Called by resetMission(), after all the temporary mission objects
  61. // have been deleted.
  62. %this.initGameVars();
  63. }
  64. //This sets up our gamemode's duration time
  65. function ExampleGameMode::initGameVars(%this)
  66. {
  67. // Set the gameplay parameters
  68. %this.duration = 30 * 60;
  69. }
  70. //This is called when the timer runs out, allowing the gamemode to end
  71. function ExampleGameMode::onGameDurationEnd(%this)
  72. {
  73. //we don't end if we're currently editing the level
  74. if (%this.duration && !(EditorIsActive() && GuiEditorIsActive()))
  75. %this.onMissionEnded();
  76. }
  77. //This is called to actually spawn a control object for the player to utilize
  78. //A player character, spectator camera, etc.
  79. function ExampleGameMode::spawnControlObject(%this, %client)
  80. {
  81. //In this example, we just spawn a camera
  82. if (!isObject(%client.camera))
  83. {
  84. if(!isObject(Observer))
  85. {
  86. datablock CameraData(Observer)
  87. {
  88. mode = "Observer";
  89. };
  90. }
  91. %client.camera = spawnObject("Camera", Observer);
  92. }
  93. // If we have a camera then set up some properties
  94. if (isObject(%client.camera))
  95. {
  96. MissionCleanup.add( %this.camera );
  97. %client.camera.scopeToClient(%client);
  98. %client.setControlObject(%client.camera);
  99. %client.camera.setTransform("0 0 1 0 0 0 0");
  100. }
  101. }
  102. //This is called when the client has initially established a connection to the game server
  103. //It's used for setting up anything ahead of time for the client, such as loading in client-passed
  104. //config stuffs, saved data or the like that should be handled BEFORE the client has actually entered
  105. //the game itself
  106. function ExampleGameMode::onClientConnect(%this, %client)
  107. {
  108. }
  109. //This is called when a client enters the game server. It's used to spawn a player object
  110. //set up any client-specific properties such as saved configs, values, their name, etc
  111. //These callbacks are activated in core/clientServer/scripts/server/levelDownload.cs
  112. function ExampleGameMode::onClientEnterGame(%this, %client)
  113. {
  114. //Set the player name based on the client's connection data
  115. %client.setPlayerName(%client.connectData);
  116. %this.spawnControlObject(%client);
  117. }
  118. //This is called when the player leaves the game server. It's used to clean up anything that
  119. //was spawned or setup for the client when it connected, in onClientEnterGame
  120. //These callbacks are activated in core/clientServer/scripts/server/levelDownload.cs
  121. function ExampleGameMode::onClientLeaveGame(%this, %client)
  122. {
  123. // Cleanup the camera
  124. if (isObject(%client.camera))
  125. %client.camera.delete();
  126. }
  127. //This is called when the player has connected and finaly setup is done and control is handed
  128. //over to the client. It allows a point to special-case setting the client's canvas content
  129. //(Such as a gamemode-specific GUI) or setting up gamemode-specific keybinds/control schemes
  130. function ExampleGameMode::onInitialControlSet(%this)
  131. {
  132. }