ExampleGameMode.tscript 7.6 KB

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