ExampleGameMode.tscript 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. function ExampleGameMode::onCreateGame()
  2. {
  3. // Note: The Game object will be cleaned up by MissionCleanup. Therefore its lifetime is
  4. // limited to that of the mission.
  5. new ScriptObject(ExampleGameMode){};
  6. return ExampleGameMode;
  7. }
  8. //-----------------------------------------------------------------------------
  9. // The server has started up so do some game start up
  10. //-----------------------------------------------------------------------------
  11. function ExampleGameMode::onMissionStart(%this)
  12. {
  13. //set up the game and game variables
  14. %this.initGameVars();
  15. if (%this.Running)
  16. {
  17. error("onMissionStart: End the game first!");
  18. return;
  19. }
  20. // Inform the client we're starting up
  21. for (%clientIndex = 0; %clientIndex < ClientGroup.getCount(); %clientIndex++)
  22. {
  23. %cl = ClientGroup.getObject(%clientIndex);
  24. commandToClient(%cl, 'GameStart');
  25. }
  26. %this.Running = true;
  27. }
  28. function ExampleGameMode::onMissionEnded(%this)
  29. {
  30. if (!%this.Running)
  31. {
  32. error("onMissionEnded: No game running!");
  33. return;
  34. }
  35. for (%clientIndex = 0; %clientIndex < ClientGroup.getCount(); %clientIndex++)
  36. {
  37. %cl = ClientGroup.getObject(%clientIndex);
  38. commandToClient(%cl, 'GameEnd', %this.EndGamePause);
  39. }
  40. %this.Running = false;
  41. }
  42. function ExampleGameMode::onMissionReset(%this)
  43. {
  44. // Called by resetMission(), after all the temporary mission objects
  45. // have been deleted.
  46. %this.initGameVars();
  47. }
  48. function ExampleGameMode::initGameVars(%this)
  49. {
  50. //-----------------------------------------------------------------------------
  51. // What kind of "camera" is spawned is either controlled directly by the
  52. // SpawnSphere or it defaults back to the values set here. This also controls
  53. // which SimGroups to attempt to select the spawn sphere's from by walking down
  54. // the list of SpawnGroups till it finds a valid spawn object.
  55. // These override the values set in core/scripts/server/spawn.cs
  56. //-----------------------------------------------------------------------------
  57. %this.defaultCameraClass = "Camera";
  58. %this.defaultCameraDataBlock = "Observer";
  59. %this.defaultCameraSpawnGroups = "CameraSpawnPoints PlayerSpawnPoints PlayerDropPoints";
  60. }
  61. function ExampleGameMode::onGameDurationEnd(%this)
  62. {
  63. }
  64. function ExampleGameMode::onClientEnterGame(%this, %client)
  65. {
  66. // This function currently relies on some helper functions defined in
  67. // core/scripts/spawn.cs. For custom spawn behaviors one can either
  68. // override the properties on the SpawnSphere's or directly override the
  69. // functions themselves.
  70. //echo (%game @"\c4 -> "@ %game.class @" -> GameCore::onClientEntergame");
  71. // Sync the client's clocks to the server's
  72. commandToClient(%client, 'SyncClock', $Sim::Time - %this.StartTime);
  73. //Set the player name based on the client's connection data
  74. %client.setPlayerName(%client.connectData);
  75. // Find a spawn point for the camera
  76. // This function currently relies on some helper functions defined in
  77. // core/scripts/server/spawn.cs. For custom spawn behaviors one can either
  78. // override the properties on the SpawnSphere's or directly override the
  79. // functions themselves.
  80. %cameraSpawnPoint = %this.pickCameraSpawnPoint(%this.DefaultCameraSpawnGroups);
  81. // Spawn a camera for this client using the found %spawnPoint
  82. %this.spawnCamera(%client, %cameraSpawnPoint);
  83. // Inform the client of all the other clients
  84. %count = ClientGroup.getCount();
  85. for (%cl = 0; %cl < %count; %cl++)
  86. {
  87. %other = ClientGroup.getObject(%cl);
  88. if ((%other != %client))
  89. {
  90. // These should be "silent" versions of these messages...
  91. messageClient(%client, 'MsgClientJoin', "",
  92. %other.playerName,
  93. %other,
  94. %other.sendGuid,
  95. %other.team,
  96. %other.score,
  97. %other.kills,
  98. %other.deaths,
  99. %other.isAIControlled(),
  100. %other.isAdmin,
  101. %other.isSuperAdmin);
  102. }
  103. }
  104. // Inform the client we've joined up
  105. messageClient(%client,
  106. 'MsgClientJoin', '\c2Welcome to the Torque demo app %1.',
  107. %client.playerName,
  108. %client,
  109. %client.sendGuid,
  110. %client.team,
  111. %client.score,
  112. %client.kills,
  113. %client.deaths,
  114. %client.isAiControlled(),
  115. %client.isAdmin,
  116. %client.isSuperAdmin);
  117. // Inform all the other clients of the new guy
  118. messageAllExcept(%client, -1, 'MsgClientJoin', '\c1%1 joined the game.',
  119. %client.playerName,
  120. %client,
  121. %client.sendGuid,
  122. %client.team,
  123. %client.score,
  124. %client.kills,
  125. %client.deaths,
  126. %client.isAiControlled(),
  127. %client.isAdmin,
  128. %client.isSuperAdmin);
  129. }
  130. function ExampleGameMode::onClientLeaveGame(%this, %client)
  131. {
  132. // Cleanup the camera
  133. if (isObject(%client.camera))
  134. %client.camera.delete();
  135. }
  136. function ExampleGameMode::onInitialControlSet(%this)
  137. {
  138. }
  139. function ExampleGameMode::spawnCamera(%this, %client, %spawnPoint)
  140. {
  141. // Set the control object to the default camera
  142. if (!isObject(%client.camera))
  143. {
  144. if (%this.defaultCameraClass !$= "")
  145. %client.camera = spawnObject(%this.defaultCameraClass, %this.defaultCameraDataBlock);
  146. }
  147. // If we have a camera then set up some properties
  148. if (isObject(%client.camera))
  149. {
  150. MissionCleanup.add( %client.camera );
  151. %client.camera.scopeToClient(%client);
  152. %client.setControlObject(%client.camera);
  153. if(!isObject(%spawnPoint))
  154. %spawnPoint = %this.pickCameraSpawnPoint(%this.defaultCameraSpawnGroups);
  155. if (isObject(%spawnPoint))
  156. {
  157. // Attempt to treat %spawnPoint as an object
  158. if (getWordCount(%spawnPoint) == 1 && isObject(%spawnPoint))
  159. {
  160. %client.camera.setTransform(%spawnPoint.getTransform());
  161. }
  162. else
  163. {
  164. // Treat %spawnPoint as an AxisAngle transform
  165. %client.camera.setTransform(%spawnPoint);
  166. }
  167. }
  168. }
  169. }
  170. //-----------------------------------------------------------------------------
  171. // pickCameraSpawnPoint() is responsible for finding a valid spawn point for a
  172. // camera.
  173. //-----------------------------------------------------------------------------
  174. function ExampleGameMode::pickCameraSpawnPoint(%this, %spawnGroups)
  175. {
  176. // Walk through the groups until we find a valid object
  177. for (%i = 0; %i < getWordCount(%spawnGroups); %i++)
  178. {
  179. %group = getWord(%spawnGroups, %i);
  180. %count = getWordCount(%group);
  181. if (isObject(%group))
  182. %spawnPoint = %group.getRandom();
  183. if (isObject(%spawnPoint))
  184. return %spawnPoint;
  185. }
  186. // Didn't find a spawn point by looking for the groups
  187. // so let's return the "default" SpawnSphere
  188. // First create it if it doesn't already exist
  189. if (!isObject(DefaultCameraSpawnSphere))
  190. {
  191. %spawn = new SpawnSphere(DefaultCameraSpawnSphere)
  192. {
  193. dataBlock = "SpawnSphereMarker";
  194. spawnClass = $Game::DefaultCameraClass;
  195. spawnDatablock = $Game::DefaultCameraDataBlock;
  196. };
  197. // Add it to the MissionCleanup group so that it
  198. // doesn't get saved to the Mission (and gets cleaned
  199. // up of course)
  200. MissionCleanup.add(%spawn);
  201. }
  202. return DefaultCameraSpawnSphere;
  203. }