spawn.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. //-----------------------------------------------------------------------------
  28. // Leave $Game::defaultPlayerClass and $Game::defaultPlayerDataBlock as empty strings ("")
  29. // to spawn a the $Game::defaultCameraClass as the control object.
  30. $Game::DefaultPlayerClass = "Player";
  31. $Game::DefaultPlayerDataBlock = "DefaultPlayerData";
  32. $Game::DefaultPlayerSpawnGroups = "PlayerSpawnPoints";
  33. //-----------------------------------------------------------------------------
  34. // What kind of "camera" is spawned is either controlled directly by the
  35. // SpawnSphere or it defaults back to the values set here. This also controls
  36. // which SimGroups to attempt to select the spawn sphere's from by walking down
  37. // the list of SpawnGroups till it finds a valid spawn object.
  38. //-----------------------------------------------------------------------------
  39. $Game::DefaultCameraClass = "Camera";
  40. $Game::DefaultCameraDataBlock = "Observer";
  41. $Game::DefaultCameraSpawnGroups = "CameraSpawnPoints PlayerSpawnPoints";
  42. //-----------------------------------------------------------------------------
  43. // pickCameraSpawnPoint() is responsible for finding a valid spawn point for a
  44. // camera.
  45. //-----------------------------------------------------------------------------
  46. function pickCameraSpawnPoint(%spawnGroups)
  47. {
  48. // Walk through the groups until we find a valid object
  49. for (%i = 0; %i < getWordCount(%spawnGroups); %i++)
  50. {
  51. %group = getWord(%spawnGroups, %i);
  52. %count = getWordCount(%group);
  53. if (isObject(%group))
  54. %spawnPoint = %group.getRandom();
  55. if (isObject(%spawnPoint))
  56. return %spawnPoint;
  57. }
  58. // Didn't find a spawn point by looking for the groups
  59. // so let's return the "default" SpawnSphere
  60. // First create it if it doesn't already exist
  61. if (!isObject(DefaultCameraSpawnSphere))
  62. {
  63. %spawn = new SpawnSphere(DefaultCameraSpawnSphere)
  64. {
  65. dataBlock = "SpawnSphereMarker";
  66. spawnClass = $Game::DefaultCameraClass;
  67. spawnDatablock = $Game::DefaultCameraDataBlock;
  68. };
  69. // Add it to the MissionCleanup group so that it
  70. // doesn't get saved to the Mission (and gets cleaned
  71. // up of course)
  72. MissionCleanup.add(%spawn);
  73. }
  74. return DefaultCameraSpawnSphere;
  75. }
  76. //-----------------------------------------------------------------------------
  77. // pickPlayerSpawnPoint() is responsible for finding a valid spawn point for a
  78. // player.
  79. //-----------------------------------------------------------------------------
  80. function pickPlayerSpawnPoint(%spawnGroups)
  81. {
  82. // Walk through the groups until we find a valid object
  83. for (%i = 0; %i < getWordCount(%spawnGroups); %i++)
  84. {
  85. %group = getWord(%spawnGroups, %i);
  86. if (isObject(%group))
  87. %spawnPoint = %group.getRandom();
  88. if (isObject(%spawnPoint))
  89. return %spawnPoint;
  90. }
  91. // Didn't find a spawn point by looking for the groups
  92. // so let's return the "default" SpawnSphere
  93. // First create it if it doesn't already exist
  94. if (!isObject(DefaultPlayerSpawnSphere))
  95. {
  96. %spawn = new SpawnSphere(DefaultPlayerSpawnSphere)
  97. {
  98. dataBlock = "SpawnSphereMarker";
  99. spawnClass = $Game::DefaultPlayerClass;
  100. spawnDatablock = $Game::DefaultPlayerDataBlock;
  101. };
  102. // Add it to the MissionCleanup group so that it
  103. // doesn't get saved to the Mission (and gets cleaned
  104. // up of course)
  105. MissionCleanup.add(%spawn);
  106. }
  107. return DefaultPlayerSpawnSphere;
  108. }
  109. //-----------------------------------------------------------------------------
  110. // GameConnection::spawnCamera() is responsible for spawning a camera for a
  111. // client
  112. //-----------------------------------------------------------------------------
  113. //function GameConnection::spawnCamera(%this, %spawnPoint)
  114. //{
  115. //// Set the control object to the default camera
  116. //if (!isObject(%this.camera))
  117. //{
  118. //if (isDefined("$Game::DefaultCameraClass"))
  119. //%this.camera = spawnObject($Game::DefaultCameraClass, $Game::DefaultCameraDataBlock);
  120. //}
  121. //
  122. //if(!isObject(%this.PathCamera))
  123. //{
  124. //// Create path camera
  125. //%this.PathCamera = spawnObject("PathCamera", "LoopingCam");
  126. ////%this.PathCamera = new PathCamera() {
  127. ////dataBlock = LoopingCam;
  128. ////position = "0 0 300 1 0 0 0";
  129. ////};
  130. //}
  131. //if(isObject(%this.PathCamera))
  132. //{
  133. //%this.PathCamera.setPosition("-54.0187 1.81237 5.14039");
  134. //%this.PathCamera.followPath(MenuPath);
  135. //MissionCleanup.add( %this.PathCamera);
  136. //%this.PathCamera.scopeToClient(%this);
  137. //%this.setControlObject(%this.PathCamera);
  138. //}
  139. //// If we have a camera then set up some properties
  140. //if (isObject(%this.camera))
  141. //{
  142. //MissionCleanup.add( %this.camera );
  143. //%this.camera.scopeToClient(%this);
  144. //
  145. ////%this.setControlObject(%this.camera);
  146. ////%this.setControlObject(%this.PathCamera);
  147. //
  148. //if (isDefined("%spawnPoint"))
  149. //{
  150. //// Attempt to treat %spawnPoint as an object
  151. //if (getWordCount(%spawnPoint) == 1 && isObject(%spawnPoint))
  152. //{
  153. //%this.camera.setTransform(%spawnPoint.getTransform());
  154. //}
  155. //else
  156. //{
  157. //// Treat %spawnPoint as an AxisAngle transform
  158. //%this.camera.setTransform(%spawnPoint);
  159. //}
  160. //}
  161. //}
  162. //}
  163. function GameConnection::spawnCamera(%this, %spawnPoint)
  164. {
  165. // Set the control object to the default camera
  166. if (!isObject(%this.camera))
  167. {
  168. if (isDefined("$Game::DefaultCameraClass"))
  169. %this.camera = spawnObject($Game::DefaultCameraClass, $Game::DefaultCameraDataBlock);
  170. }
  171. // If we have a camera then set up some properties
  172. if (isObject(%this.camera))
  173. {
  174. MissionCleanup.add( %this.camera );
  175. %this.camera.scopeToClient(%this);
  176. %this.setControlObject(%this.camera);
  177. if (isDefined("%spawnPoint"))
  178. {
  179. // Attempt to treat %spawnPoint as an object
  180. if (getWordCount(%spawnPoint) == 1 && isObject(%spawnPoint))
  181. {
  182. %this.camera.setTransform(%spawnPoint.getTransform());
  183. }
  184. else
  185. {
  186. // Treat %spawnPoint as an AxisAngle transform
  187. %this.camera.setTransform(%spawnPoint);
  188. }
  189. }
  190. }
  191. }
  192. //-----------------------------------------------------------------------------
  193. // GameConnection::spawnPlayer() is responsible for spawning a player for a
  194. // client
  195. //-----------------------------------------------------------------------------
  196. function GameConnection::spawnPlayer(%this, %spawnPoint, %noControl)
  197. {
  198. if (isObject(%this.player))
  199. {
  200. // The client should not already have a player. Assigning
  201. // a new one could result in an uncontrolled player object.
  202. error("Attempting to create a player for a client that already has one!");
  203. }
  204. // Attempt to treat %spawnPoint as an object
  205. if (getWordCount(%spawnPoint) == 1 && isObject(%spawnPoint))
  206. {
  207. // Defaults
  208. %spawnClass = $Game::DefaultPlayerClass;
  209. %spawnDataBlock = $Game::DefaultPlayerDataBlock;
  210. // Overrides by the %spawnPoint
  211. if (isDefined("%spawnPoint.spawnClass"))
  212. {
  213. %spawnClass = %spawnPoint.spawnClass;
  214. %spawnDataBlock = %spawnPoint.spawnDatablock;
  215. }
  216. // This may seem redundant given the above but it allows
  217. // the SpawnSphere to override the datablock without
  218. // overriding the default player class
  219. if (isDefined("%spawnPoint.spawnDatablock"))
  220. %spawnDataBlock = %spawnPoint.spawnDatablock;
  221. %spawnProperties = %spawnPoint.spawnProperties;
  222. %spawnScript = %spawnPoint.spawnScript;
  223. // Spawn with the engine's Sim::spawnObject() function
  224. %player = spawnObject(%spawnClass, %spawnDatablock, "",
  225. %spawnProperties, %spawnScript);
  226. // If we have an object do some initial setup
  227. if (isObject(%player))
  228. {
  229. // Set the transform to %spawnPoint's transform
  230. %player.setTransform(%spawnPoint.getTransform());
  231. }
  232. else
  233. {
  234. // If we weren't able to create the player object then warn the user
  235. if (isDefined("%spawnDatablock"))
  236. {
  237. MessageBoxOK("Spawn Player Failed",
  238. "Unable to create a player with class " @ %spawnClass @
  239. " and datablock " @ %spawnDatablock @ ".\n\nStarting as an Observer instead.",
  240. %this @ ".spawnCamera();");
  241. }
  242. else
  243. {
  244. MessageBoxOK("Spawn Player Failed",
  245. "Unable to create a player with class " @ %spawnClass @
  246. ".\n\nStarting as an Observer instead.",
  247. %this @ ".spawnCamera();");
  248. }
  249. }
  250. }
  251. else
  252. {
  253. // Create a default player
  254. %player = spawnObject($Game::DefaultPlayerClass, $Game::DefaultPlayerDataBlock);
  255. if (!%player.isMemberOfClass("Player"))
  256. warn("Trying to spawn a class that does not derive from Player.");
  257. //Ensure we have a valid spawn point
  258. if(%spawnPoint $= "")
  259. {
  260. %spawnPoint = pickPlayerSpawnPoint($Game::DefaultPlayerSpawnGroups).getTransform();
  261. }
  262. // Treat %spawnPoint as a transform
  263. %player.setTransform(%spawnPoint);
  264. }
  265. // If we didn't actually create a player object then bail
  266. if (!isObject(%player))
  267. {
  268. // Make sure we at least have a camera
  269. %this.spawnCamera(%spawnPoint);
  270. return;
  271. }
  272. // Update the default camera to start with the player
  273. if (isObject(%this.camera))
  274. {
  275. if (%player.getClassname() $= "Player")
  276. %this.camera.setTransform(%player.getEyeTransform());
  277. else
  278. %this.camera.setTransform(%player.getTransform());
  279. }
  280. // Add the player object to MissionCleanup so that it
  281. // won't get saved into the level files and will get
  282. // cleaned up properly
  283. MissionCleanup.add(%player);
  284. // Store the client object on the player object for
  285. // future reference
  286. %player.client = %this;
  287. // Player setup...
  288. if (%player.isMethod("setShapeName"))
  289. %player.setShapeName(%this.playerName);
  290. if (%player.isMethod("setEnergyLevel"))
  291. %player.setEnergyLevel(%player.getDataBlock().maxEnergy);
  292. // Give the client control of the player
  293. %this.player = %player;
  294. // Give the client control of the camera if in the editor
  295. if( $startWorldEditor )
  296. {
  297. %control = %this.camera;
  298. %control.mode = toggleCameraFly;
  299. EditorGui.syncCameraGui();
  300. }
  301. else
  302. %control = %player;
  303. if(!isDefined("%noControl"))
  304. %this.setControlObject(%control);
  305. }