game.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. //-----------------------------------------------------------------------------
  23. // What kind of "player" is spawned is either controlled directly by the
  24. // SpawnSphere or it defaults back to the values set here. This also controls
  25. // which SimGroups to attempt to select the spawn sphere's from by walking down
  26. // the list of SpawnGroups till it finds a valid spawn object.
  27. // These override the values set in core/scripts/server/spawn.cs
  28. //-----------------------------------------------------------------------------
  29. // Leave $Game::defaultPlayerClass and $Game::defaultPlayerDataBlock as empty strings ("")
  30. // to spawn a the $Game::defaultCameraClass as the control object.
  31. $Game::DefaultPlayerClass = "";
  32. $Game::DefaultPlayerDataBlock = "";
  33. $Game::DefaultPlayerSpawnGroups = "CameraSpawnPoints PlayerSpawnPoints PlayerDropPoints";
  34. //-----------------------------------------------------------------------------
  35. // What kind of "camera" is spawned is either controlled directly by the
  36. // SpawnSphere or it defaults back to the values set here. This also controls
  37. // which SimGroups to attempt to select the spawn sphere's from by walking down
  38. // the list of SpawnGroups till it finds a valid spawn object.
  39. // These override the values set in core/scripts/server/spawn.cs
  40. //-----------------------------------------------------------------------------
  41. $Game::DefaultCameraClass = "Camera";
  42. $Game::DefaultCameraDataBlock = "Observer";
  43. $Game::DefaultCameraSpawnGroups = "CameraSpawnPoints PlayerSpawnPoints PlayerDropPoints";
  44. // Global movement speed that affects all Cameras
  45. $Camera::MovementSpeed = 30;
  46. //-----------------------------------------------------------------------------
  47. // GameConnection manages the communication between the server's world and the
  48. // client's simulation. These functions are responsible for maintaining the
  49. // client's camera and player objects.
  50. //-----------------------------------------------------------------------------
  51. //-----------------------------------------------------------------------------
  52. // This is the main entry point for spawning a control object for the client.
  53. // The control object is the actual game object that the client is responsible
  54. // for controlling in the client and server simulations. We also spawn a
  55. // convenient camera object for use as an alternate control object. We do not
  56. // have to spawn this camera object in order to function in the simulation.
  57. //
  58. // Called for each client after it's finished downloading the mission and is
  59. // ready to start playing.
  60. //-----------------------------------------------------------------------------
  61. function GameConnection::onClientEnterGame(%this)
  62. {
  63. // This function currently relies on some helper functions defined in
  64. // core/scripts/spawn.cs. For custom spawn behaviors one can either
  65. // override the properties on the SpawnSphere's or directly override the
  66. // functions themselves.
  67. // Find a spawn point for the camera
  68. %cameraSpawnPoint = pickCameraSpawnPoint($Game::DefaultCameraSpawnGroups);
  69. // Spawn a camera for this client using the found %spawnPoint
  70. %this.spawnCamera(%cameraSpawnPoint);
  71. // Find a spawn point for the player
  72. %playerSpawnPoint = pickPlayerSpawnPoint($Game::DefaultPlayerSpawnGroups);
  73. // Spawn a camera for this client using the found %spawnPoint
  74. %this.spawnPlayer(%playerSpawnPoint);
  75. }
  76. //-----------------------------------------------------------------------------
  77. // Clean up the client's control objects
  78. //-----------------------------------------------------------------------------
  79. function GameConnection::onClientLeaveGame(%this)
  80. {
  81. // Cleanup the camera
  82. if (isObject(%this.camera))
  83. %this.camera.delete();
  84. // Cleanup the player
  85. if (isObject(%this.player))
  86. %this.player.delete();
  87. }
  88. //-----------------------------------------------------------------------------
  89. // Handle a player's death
  90. //-----------------------------------------------------------------------------
  91. function GameConnection::onDeath(%this, %sourceObject, %sourceClient, %damageType, %damLoc)
  92. {
  93. // Clear out the name on the corpse
  94. if (isObject(%this.player))
  95. {
  96. if (%this.player.isMethod("setShapeName"))
  97. %this.player.setShapeName("");
  98. }
  99. // Switch the client over to the death cam
  100. if (isObject(%this.camera) && isObject(%this.player))
  101. {
  102. %this.camera.setMode("Corpse", %this.player);
  103. %this.setControlObject(%this.camera);
  104. }
  105. // Unhook the player object
  106. %this.player = 0;
  107. }
  108. //-----------------------------------------------------------------------------
  109. // Server, mission, and game management
  110. //-----------------------------------------------------------------------------
  111. //-----------------------------------------------------------------------------
  112. // The server has started up so do some game start up
  113. //-----------------------------------------------------------------------------
  114. function onServerCreated()
  115. {
  116. // Server::GameType is sent to the master server.
  117. // This variable should uniquely identify your game and/or mod.
  118. $Server::GameType = "Torque 3D";
  119. // Server::MissionType sent to the master server. Clients can
  120. // filter servers based on mission type.
  121. $Server::MissionType = "pureLIGHT";
  122. // GameStartTime is the sim time the game started. Used to calculated
  123. // game elapsed time.
  124. $Game::StartTime = 0;
  125. // Create the server physics world.
  126. physicsInitWorld( "server" );
  127. // Load up any objects or datablocks saved to the editor managed scripts
  128. %datablockFiles = new ArrayObject();
  129. %datablockFiles.add( "art/ribbons/ribbonExec.cs" );
  130. %datablockFiles.add( "art/particles/managedParticleData.cs" );
  131. %datablockFiles.add( "art/particles/managedParticleEmitterData.cs" );
  132. %datablockFiles.add( "art/decals/managedDecalData.cs" );
  133. %datablockFiles.add( "art/datablocks/managedDatablocks.cs" );
  134. %datablockFiles.add( "art/forest/managedItemData.cs" );
  135. %datablockFiles.add( "art/datablocks/datablockExec.cs" );
  136. loadDatablockFiles( %datablockFiles, true );
  137. // Run the other gameplay scripts in this folder
  138. exec("./scriptExec.cs");
  139. // Keep track of when the game started
  140. $Game::StartTime = $Sim::Time;
  141. }
  142. //-----------------------------------------------------------------------------
  143. // This function is called as part of a server shutdown
  144. //-----------------------------------------------------------------------------
  145. function onServerDestroyed()
  146. {
  147. // Destroy the server physcis world
  148. physicsDestroyWorld( "server" );
  149. }
  150. //-----------------------------------------------------------------------------
  151. // Called by loadMission() once the mission is finished loading
  152. //-----------------------------------------------------------------------------
  153. function onMissionLoaded()
  154. {
  155. // Start the server side physics simulation
  156. physicsStartSimulation( "server" );
  157. // Nothing special for now, just start up the game play
  158. startGame();
  159. }
  160. //-----------------------------------------------------------------------------
  161. // Called by endMission(), right before the mission is destroyed
  162. //-----------------------------------------------------------------------------
  163. function onMissionEnded()
  164. {
  165. // Stop the server physics simulation
  166. physicsStopSimulation( "server" );
  167. // Normally the game should be ended first before the next
  168. // mission is loaded, this is here in case loadMission has been
  169. // called directly. The mission will be ended if the server
  170. // is destroyed, so we only need to cleanup here.
  171. $Game::Running = false;
  172. }
  173. //-----------------------------------------------------------------------------
  174. // Called once the game has started
  175. //-----------------------------------------------------------------------------
  176. function startGame()
  177. {
  178. if ($Game::Running)
  179. {
  180. error("startGame(): End the game first!");
  181. return;
  182. }
  183. $Game::Running = true;
  184. }
  185. //-----------------------------------------------------------------------------
  186. // Called once the game has ended
  187. //-----------------------------------------------------------------------------
  188. function endGame()
  189. {
  190. if (!$Game::Running)
  191. {
  192. error("endGame(): No game running!");
  193. return;
  194. }
  195. // Inform the client the game is over
  196. for( %clientIndex = 0; %clientIndex < ClientGroup.getCount(); %clientIndex++ )
  197. {
  198. %cl = ClientGroup.getObject( %clientIndex );
  199. commandToClient(%cl, 'GameEnd');
  200. }
  201. // Delete all the temporary mission objects
  202. resetMission();
  203. $Game::Running = false;
  204. }