main.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. function TruckToy::create( %this )
  23. {
  24. TruckToy.ObstacleFriction = 1.5;
  25. TruckToy.CameraWidth = 20;
  26. TruckToy.CameraHeight = 15;
  27. TruckToy.WorldWidth = TruckToy.CameraWidth * 10;
  28. TruckToy.WorldLeft = TruckToy.WorldWidth * -0.5;
  29. TruckToy.WorldRight = TruckToy.WorldWidth * 0.5;
  30. TruckToy.FloorLevel = -4.5;
  31. TruckToy.BackdropDomain = 31;
  32. TruckToy.BackgroundDomain = 25;
  33. TruckToy.TruckDomain = 20;
  34. TruckToy.GroundDomain = 18;
  35. TruckToy.ObstacleDomain = 15;
  36. TruckToy.ProjectileDomain = 16;
  37. TruckToy.ForegroundDomain = 10;
  38. TruckToy.WheelSpeed = 400;
  39. TruckToy.WheelFriction = 1;
  40. TruckToy.FrontWheelDensity = 6;
  41. TruckToy.RearWheelDensity = 3;
  42. TruckToy.FrontWheelDrive = true;
  43. TruckToy.RearWheelDrive = true;
  44. TruckToy.ProjectileRate = 3000;
  45. TruckToy.ExplosionScale = 1;
  46. TruckToy.RotateCamera = true;
  47. // Add the custom controls.
  48. addNumericOption( "Wheel Speed", 100, 1000, 50, "setWheelSpeed", TruckToy.WheelSpeed, false, "Sets the rotational speed of the wheel when it is put into drive." );
  49. addNumericOption( "Wheel Friction", 0, 10, 1, "setWheelFriction", TruckToy.WheelFriction, true, "Sets the friction for the surface of each wheel." );
  50. addNumericOption( "Front Wheel Density", 1, 20, 1, "setFrontWheelDensity", TruckToy.FrontWheelDensity, true, "Sets the density of the front wheel." );
  51. addNumericOption( "Rear Wheel Density", 1, 20, 1, "setFrontWheelDensity", TruckToy.RearWheelDensity, true, "Sets the density of the rear wheel." );
  52. addNumericOption( "Projectile Rate (ms)", 100, 60000, 100, "setProjectileRate", TruckToy.ProjectileRate, false, "Sets the time interval in-between projectiles appearing." );
  53. addNumericOption( "Explosion Scale", 1, 11, 1, "setExplosionScale", TruckToy.ExplosionScale, false, "Sets the size scale of the explosions caused by a projectile landing." );
  54. addFlagOption("Front Wheel Drive", "setFrontWheelDrive", TruckToy.FrontWheelDrive, false, "Whether the motor on the front wheel is active or not." );
  55. addFlagOption("Rear Wheel Drive", "setRearWheelDrive", TruckToy.RearWheelDrive, false, "Whether the motor on the rear wheel is active or not." );
  56. addFlagOption("Rotate Camera", "setRotateCamera", TruckToy.RotateCamera, true, "Whether the rotate the camera that is mounted to the truck or not." );
  57. // Reset the toy.
  58. %this.reset();
  59. }
  60. //-----------------------------------------------------------------------------
  61. function TruckToy::destroy( %this )
  62. {
  63. // Deactivate the package.
  64. deactivatePackage( TruckToyPackage );
  65. }
  66. //-----------------------------------------------------------------------------
  67. function TruckToy::reset( %this )
  68. {
  69. // Clear the scene.
  70. SandboxScene.clear();
  71. // Set a typical Earth gravity.
  72. SandboxScene.setGravity( 0, -9.8 );
  73. // Camera Configuration
  74. SandboxWindow.setCameraPosition( TruckToy.WorldLeft + (TruckToy.CameraWidth/2) - 10, 0 );
  75. SandboxWindow.setCameraAngle( 0 );
  76. SandboxWindow.setCameraSize( TruckToy.CameraWidth, TruckToy.CameraHeight );
  77. SandboxWindow.setViewLimitOn( TruckToy.WorldLeft, TruckToy.CameraHeight/-2, TruckToy.WorldRight, TruckToy.CameraHeight/2 );
  78. // Create the scene contents in a roughly left to right order.
  79. // Background.
  80. %this.createBackground();
  81. // Floor.
  82. %this.createFloor();
  83. // Wrecked cars at start.
  84. %this.createWreckedCar( 1, -90, TruckToy.FloorLevel + 0.75, 0, true );
  85. %this.createWreckedCar( 2, -85, TruckToy.FloorLevel + 0.75, 0, true );
  86. %this.createWreckedCar( 3, -82, TruckToy.FloorLevel + 0.75, 0, true );
  87. %this.createWreckedCar( 1, -87.123, -2.478, 2.537, true );
  88. %this.createBrick( 3, -87.5, TruckToy.FloorLevel + 0.25, true );
  89. %this.createBrick( 4, -87.5, TruckToy.FloorLevel + 0.75, true );
  90. %this.createBrick( 2, -79, TruckToy.FloorLevel + 0.25, true );
  91. %this.createBonfire( -91.5, TruckToy.FloorLevel + 0.5, 1, TruckToy.BackgroundDomain-1 );
  92. // Building with chains.
  93. %this.createForegroundWall( 2, -99, -5 );
  94. %this.createForegroundWall( 1, -75.5, -6.5 );
  95. %this.createBrokenCementWall( -78, -1.5 );
  96. %this.createWreckedBuilding( -71.5, -1 );
  97. %this.createWoodPile( -65, -2.5 );
  98. %this.createBrickPile( -67, TruckToy.FloorLevel + 0.45 );
  99. %this.createForegroundBrickWall( 1, -61, -6 );
  100. %this.createBonfire( -82, TruckToy.FloorLevel + 0.5, 1.5, TruckToy.ObstacleDomain+1 );
  101. // Start of bridge.
  102. %this.createPlank( 1, -53, TruckToy.FloorLevel + 0.5, 0, true );
  103. %this.createPlank( 1, -50.1522, -2.3, 21.267, true );
  104. %this.createWreckedCar( 2, -47, TruckToy.FloorLevel + 1.9, -100, true );
  105. %this.createWreckedCar( 3, -45.5, TruckToy.FloorLevel + 1.9, 100, true );
  106. %this.createPlank( 2, -44, TruckToy.FloorLevel + 2, -90, true );
  107. %this.createPlank( 1, -43, TruckToy.FloorLevel + 2, -90, true );
  108. %this.createPlank( 2, -42, TruckToy.FloorLevel + 2, -90, true );
  109. %this.createPlank( 1, -41, TruckToy.FloorLevel + 2, -90, true );
  110. %this.createForegroundWall( 2, -42, -4.5 );
  111. %this.createBridge( -41, TruckToy.FloorLevel + 4, 40 );
  112. for ( %n = 0; %n < 10; %n++ )
  113. {
  114. %brick = %this.createBrickStack( getRandom(1,5), -39 + getRandomF(0,16), TruckToy.FloorLevel + 5, false );
  115. %brick.setAwake(true);
  116. }
  117. %this.createPlank( 1, -20.5, TruckToy.FloorLevel + 1.5, -90, true );
  118. %this.createPlank( 3, -19, TruckToy.FloorLevel + 4, 0, true );
  119. %this.createPlank( 1, -16.5, TruckToy.FloorLevel + 1.5, -90, true );
  120. %this.createForegroundBrickWall( 2, -19, -6 );
  121. %this.createBonfire( -46.5, TruckToy.FloorLevel, 3, TruckToy.BackgroundDomain-1 );
  122. %this.createBonfire( -18.7, TruckToy.FloorLevel + 1, 2, TruckToy.BackgroundDomain-1 );
  123. // More wrecked cars.
  124. %this.createWreckedCar( 1, -12, TruckToy.FloorLevel + 0.75, 0, true );
  125. %this.createWreckedCar( 2, -7, TruckToy.FloorLevel + 0.75, 0, true );
  126. %this.createWreckedCar( 3, -4, TruckToy.FloorLevel + 0.75, 0, true );
  127. %this.createBonfire( -5, TruckToy.FloorLevel + 0.5, 1, TruckToy.BackgroundDomain-1 );
  128. // ************************************************************************
  129. // Start of pyramid.
  130. // ************************************************************************
  131. %this.createPyramid( 2, TruckToy.FloorLevel + 0.25, 19, true );
  132. %this.createForegroundWall( 1, 9, -6 );
  133. %this.createPyramid( 2+21, TruckToy.FloorLevel + 0.25, 13, true );
  134. %this.createForegroundBrickWall( 1, 9, -7 );
  135. %this.createBonfire( 21, TruckToy.FloorLevel, 3, TruckToy.BackgroundDomain-1 );
  136. // ************************************************************************
  137. // Start of brick stacks.
  138. // ************************************************************************
  139. %this.createBrickStack( 45, TruckToy.FloorLevel + 0.25, 10, false );
  140. %this.createBrickStack( 47, TruckToy.FloorLevel + 0.25, 1, true );
  141. %this.createBrickStack( 49, TruckToy.FloorLevel + 0.25, 10, false );
  142. %this.createBrickStack( 72, TruckToy.FloorLevel + 0.25, 1, true );
  143. %this.createBrickStack( 74, TruckToy.FloorLevel + 0.25, 10, false );
  144. %this.createBrickStack( 76, TruckToy.FloorLevel + 0.25, 1, true );
  145. %this.createBrickStack( 78, TruckToy.FloorLevel + 0.25, 10, false );
  146. %this.createBonfire( 71, TruckToy.FloorLevel, 1, TruckToy.BackgroundDomain-1 );
  147. %this.createBonfire( 85, TruckToy.FloorLevel, 1.5, TruckToy.BackgroundDomain-1 );
  148. // Truck.
  149. %truckStartX = TruckToy.WorldLeft + (TruckToy.CameraWidth/6);
  150. %truckStartY = 3;
  151. %this.createTruck( %truckStartX, %truckStartY );
  152. // Start the timer.
  153. TruckToy.startTimer( "createProjectile", TruckToy.ProjectileRate );
  154. }
  155. // -----------------------------------------------------------------------------
  156. function TruckToy::createBackground(%this)
  157. {
  158. // Atmosphere
  159. %obj = new Sprite();
  160. %obj.setBodyType( "static" );
  161. %obj.setImage( "ToyAssets:highlightBackground" );
  162. %obj.BlendColor = DarkGray;
  163. %obj.setSize( TruckToy.WorldWidth * (TruckToy.CameraWidth*2), 75 );
  164. %obj.setSceneLayer( TruckToy.BackdropDomain );
  165. %obj.setSceneGroup( TruckToy.BackdropDomain );
  166. %obj.setCollisionSuppress();
  167. %obj.setAwake( false );
  168. %obj.setActive( false );
  169. SandboxScene.add( %obj );
  170. // Industrial Background
  171. %obj = new Scroller();
  172. %obj.setBodyType( "static" );
  173. %obj.setImage( "TruckToy:industrial_02" );
  174. %obj.setPosition( 0, -1 );
  175. %obj.setSize( TruckToy.WorldWidth, 8 );
  176. %obj.setRepeatX( TruckToy.WorldWidth / 8 );
  177. %obj.setSceneLayer( TruckToy.BackgroundDomain);
  178. %obj.setSceneGroup( TruckToy.BackgroundDomain);
  179. %obj.setCollisionSuppress();
  180. %obj.setAwake( false );
  181. %obj.setActive( false );
  182. SandboxScene.add( %obj );
  183. }
  184. // -----------------------------------------------------------------------------
  185. function TruckToy::createFloor(%this)
  186. {
  187. // Ground
  188. %obj = new Scroller();
  189. %obj.setBodyType( "static" );
  190. %obj.setImage( "ToyAssets:woodGround" );
  191. %obj.setSize( TruckToy.WorldWidth, 3 );
  192. %obj.setPosition( 0, TruckToy.FloorLevel - (%obj.getSizeY()/2) );
  193. %obj.setRepeatX( TruckToy.WorldWidth / 12 );
  194. %obj.setSceneLayer( TruckToy.ObstacleDomain );
  195. %obj.setSceneGroup( TruckToy.GroundDomain );
  196. %obj.setDefaultFriction( TruckToy.ObstacleFriction );
  197. %obj.setCollisionGroups( none );
  198. %obj.createEdgeCollisionShape( TruckToy.WorldWidth/-2, 1.5, TruckToy.WorldWidth/2, 1.5 );
  199. %obj.createEdgeCollisionShape( TruckToy.WorldWidth/-2, 3, TruckToy.WorldWidth/-2, 50 );
  200. %obj.createEdgeCollisionShape( TruckToy.WorldWidth/2, 3, TruckToy.WorldWidth/2, 50 );
  201. %obj.CollisionCallback = true;
  202. %obj.setAwake( false );
  203. SandboxScene.add( %obj );
  204. }
  205. // -----------------------------------------------------------------------------
  206. function TruckToy::createBrokenCementWall( %this, %posX, %posY )
  207. {
  208. %obj = new Sprite();
  209. %obj.setBodyType( "static" );
  210. %obj.setImage( "TruckToy:brokenCementWall" );
  211. %obj.setPosition( %posX, %posY );
  212. %obj.setSize( 6, 6 );
  213. %obj.setSceneLayer( TruckToy.BackgroundDomain-2 );
  214. %obj.setSceneGroup( TruckToy.BackgroundDomain);
  215. %obj.setCollisionSuppress();
  216. %obj.setAwake( false );
  217. %obj.setActive( false );
  218. SandboxScene.add( %obj );
  219. return %obj;
  220. }
  221. // -----------------------------------------------------------------------------
  222. function TruckToy::createWoodPile( %this, %posX, %posY )
  223. {
  224. %obj = new Sprite();
  225. %obj.setBodyType( "static" );
  226. %obj.setImage( "TruckToy:woodPile" );
  227. %obj.setPosition( %posX, %posY );
  228. %obj.setSize( 8, 5 );
  229. %obj.setSceneLayer( TruckToy.BackgroundDomain-2 );
  230. %obj.setSceneGroup( TruckToy.BackgroundDomain);
  231. %obj.setCollisionSuppress();
  232. %obj.setAwake( false );
  233. %obj.setActive( false );
  234. SandboxScene.add( %obj );
  235. return %obj;
  236. }
  237. // -----------------------------------------------------------------------------
  238. function TruckToy::createBrickStack( %this, %posX, %posY, %brickCount, %static )
  239. {
  240. for ( %n = 0; %n < %brickCount; %n++ )
  241. {
  242. %this.createBrick( getRandom(1,5), %posX, %posY + (%n*0.5), %static );
  243. }
  244. }
  245. // -----------------------------------------------------------------------------
  246. function TruckToy::createPyramid( %this, %posX, %posY, %brickBaseCount, %static )
  247. {
  248. if ( %brickBaseCount < 2 )
  249. {
  250. echo( "Invalid pyramid brick base count of" SPC %brickBaseCount );
  251. return;
  252. }
  253. for( %stack = 0; %stack < %brickBaseCount; %stack++ )
  254. {
  255. %stackIndexCount = %brickBaseCount - (%stack*2);
  256. %stackX = %posX + ( %stack * 1.0 ) + getRandomF(-0.3, 0.3 );
  257. %stackY = %posY + ( %stack * 0.5 );
  258. for ( %stackIndex = 0; %stackIndex < %stackIndexCount; %stackIndex++ )
  259. {
  260. %this.createBrick( getRandom(1, 5), %stackX + %stackIndex, %stackY, %static );
  261. }
  262. }
  263. }
  264. // -----------------------------------------------------------------------------
  265. function TruckToy::createBridge( %this, %posX, %posY, %linkCount )
  266. {
  267. %linkWidth = 0.5;
  268. %linkHeight = %linkWidth * 0.5;
  269. %halfLinkWidth = %linkWidth * 0.5;
  270. %rootObj = new Sprite();
  271. %rootObj.setBodyType( "static" );
  272. %rootObj.setImage( "ToyAssets:cable" );
  273. %rootObj.setPosition( %posX, %posY );
  274. %rootObj.setSize( %linkWidth, %linkHeight );
  275. %rootObj.setSceneLayer( TruckToy.BackgroundDomain-3 );
  276. %rootObj.setSceneGroup( TruckToy.ObstacleDomain );
  277. %rootObj.setCollisionSuppress();
  278. SandboxScene.add( %rootObj );
  279. %lastLinkObj = %rootObj;
  280. for ( %n = 1; %n <= %linkCount; %n++ )
  281. {
  282. %obj = new Sprite();
  283. %obj.setImage( "ToyAssets:cable" );
  284. %obj.setPosition( %posX + (%n*%linkWidth), %posY );
  285. %obj.setSize( %linkWidth, %linkHeight );
  286. %obj.setSceneLayer( TruckToy.BackgroundDomain-3 );
  287. %obj.setSceneGroup( TruckToy.ObstacleDomain );
  288. if ( %n == %linkCount )
  289. {
  290. %obj.setBodyType( "static" );
  291. %obj.setCollisionSuppress();
  292. }
  293. else
  294. {
  295. %obj.setCollisionGroups( none );
  296. %obj.setDefaultDensity( 1 );
  297. %obj.setDefaultFriction( TruckToy.ObstacleFriction );
  298. %obj.createPolygonBoxCollisionShape( %linkWidth, %linkHeight );
  299. %obj.setAngularDamping( 1.0 );
  300. %obj.setLinearDamping( 1.0 );
  301. }
  302. //%obj.setDebugOn( 5 );
  303. SandboxScene.add( %obj );
  304. SandboxScene.createRevoluteJoint( %lastLinkObj, %obj, %halfLinkWidth, 0, -%halfLinkWidth, 0 );
  305. %joint = SandboxScene.createMotorJoint( %lastLinkObj, %obj );
  306. SandboxScene.setMotorJointMaxForce( %joint, 1000 );
  307. %obj.setAwake( false );
  308. %lastLinkObj.setAwake( false );
  309. //%obj.setDebugOn( 5 );
  310. %lastLinkObj = %obj;
  311. }
  312. return %lastLinkObj;
  313. }
  314. // -----------------------------------------------------------------------------
  315. function TruckToy::createChain( %this, %posX, %posY, %linkCount )
  316. {
  317. %linkWidth = 0.25;
  318. %linkHeight = %linkWidth * 2;
  319. %halfLinkHeight = %linkHeight * 0.5;
  320. %rootObj = new Sprite();
  321. %rootObj.setBodyType( "static" );
  322. %rootObj.setImage( "ToyAssets:chain" );
  323. %rootObj.setPosition( %posX, %posY );
  324. %rootObj.setSize( %linkWidth, %linkHeight );
  325. %rootObj.setSceneLayer( TruckToy.BackgroundDomain-1 );
  326. %rootObj.setSceneGroup( TruckToy.ObstacleDomain );
  327. %rootObj.setCollisionSuppress();
  328. SandboxScene.add( %rootObj );
  329. %lastLinkObj = %rootObj;
  330. for ( %n = 1; %n <= %linkCount; %n++ )
  331. {
  332. if ( %n < %linkCount )
  333. {
  334. %obj = new Sprite();
  335. %obj.setImage( "ToyAssets:chain" );
  336. %obj.setPosition( %posX, %posY - (%n*%linkHeight) );
  337. %obj.setSize( %linkWidth, %linkHeight );
  338. SandboxScene.add( %obj );
  339. }
  340. else
  341. {
  342. %obj = %this.createBonfire( %posX, %posY - (%n*%linkHeight), 0.5, TruckToy.BackgroundDomain-1 );
  343. %obj.BodyType = dynamic;
  344. }
  345. %obj.setSceneLayer( TruckToy.BackgroundDomain-1 );
  346. %obj.setSceneGroup( TruckToy.ObstacleDomain );
  347. %obj.setCollisionGroups( none );
  348. %obj.setDefaultDensity( 1 );
  349. %obj.setDefaultFriction( 0.2 );
  350. %obj.createPolygonBoxCollisionShape( %linkWidth, %linkHeight );
  351. %obj.setAngularDamping( 1.0 );
  352. %obj.setLinearDamping( 0.5 );
  353. SandboxScene.createRevoluteJoint( %lastLinkObj, %obj, 0, -%halfLinkHeight, 0, %halfLinkHeight, false );
  354. %lastLinkObj = %obj;
  355. }
  356. %lastLinkObj.setAwake(false);
  357. return %lastLinkObj;
  358. }
  359. // -----------------------------------------------------------------------------
  360. function TruckToy::createWreckedBuilding( %this, %posX, %posY )
  361. {
  362. %obj = new Sprite();
  363. %obj.setBodyType( "static" );
  364. %obj.setImage( "TruckToy:wreckedBuilding" );
  365. %obj.setPosition( %posX, %posY );
  366. %obj.setSize( 9, 8 );
  367. %obj.setSceneLayer( TruckToy.BackgroundDomain-1 );
  368. %obj.setSceneGroup( TruckToy.BackgroundDomain);
  369. %obj.setCollisionSuppress();
  370. %obj.setAwake( false );
  371. %obj.setActive( false );
  372. SandboxScene.add( %obj );
  373. %this.createChain( %posX - 3, %posY + 3.4, 10 );
  374. %this.createChain( %posX - 1, %posY + 3.2, 10 );
  375. %this.createChain( %posX + 1, %posY + 3.0, 10 );
  376. %this.createChain( %posX + 3, %posY + 2.8, 10 );
  377. }
  378. // -----------------------------------------------------------------------------
  379. function TruckToy::createForegroundBrickWall( %this, %wallNumber, %posX, %posY )
  380. {
  381. if ( %wallNumber < 1 || %wallNumber > 2 )
  382. {
  383. echo( "Invalid foreground wall no of" SPC %wallNumber );
  384. return;
  385. }
  386. %image = "TruckToy:brickWall_0" @ %wallNumber;
  387. %obj = new Sprite();
  388. %obj.setBodyType( "static" );
  389. %obj.setImage( %image );
  390. %obj.setPosition( %posX, %posY );
  391. %obj.setSize( 10, 5 );
  392. %obj.setSceneLayer( TruckToy.ForegroundDomain-1 );
  393. %obj.setSceneGroup( TruckToy.ForegroundDomain );
  394. %obj.setCollisionSuppress();
  395. %obj.setAwake( false );
  396. %obj.setActive( false );
  397. SandboxScene.add( %obj );
  398. return %obj;
  399. }
  400. // -----------------------------------------------------------------------------
  401. function TruckToy::createForegroundWall( %this, %wallNumber, %posX, %posY )
  402. {
  403. if ( %wallNumber < 1 || %wallNumber > 2 )
  404. {
  405. echo( "Invalid foreground wall no of" SPC %wallNumber );
  406. return;
  407. }
  408. %image = "TruckToy:foregroundWall_0" @ %wallNumber;
  409. %obj = new Sprite();
  410. %obj.setBodyType( "static" );
  411. %obj.setImage( %image );
  412. %obj.setPosition( %posX, %posY );
  413. %obj.setSize( 6, 6 );
  414. %obj.setSceneLayer( TruckToy.ForegroundDomain-1 );
  415. %obj.setSceneGroup( TruckToy.ForegroundDomain );
  416. %obj.setCollisionSuppress();
  417. %obj.setAwake( false );
  418. %obj.setActive( false );
  419. SandboxScene.add( %obj );
  420. return %obj;
  421. }
  422. // -----------------------------------------------------------------------------
  423. function TruckToy::createBrick( %this, %brickNumber, %posX, %posY, %static )
  424. {
  425. if ( %brickNumber < 1 || %brickNumber > 5 )
  426. {
  427. echo( "Invalid brick no of" SPC %brickNumber );
  428. return;
  429. }
  430. %image = "ToyAssets:brick_0" @ %brickNumber;
  431. %obj = new Sprite();
  432. if ( %static ) %obj.setBodyType( "static" );
  433. %obj.setImage( %image );
  434. %obj.setPosition( %posX, %posY );
  435. %obj.setSize( 1, 0.5 );
  436. %obj.setSceneLayer( TruckToy.ObstacleDomain );
  437. %obj.setSceneGroup( TruckToy.ObstacleDomain );
  438. %obj.setCollisionGroups( TruckToy.GroundDomain, TruckToy.ObstacleDomain );
  439. %obj.setDefaultFriction( TruckToy.ObstacleFriction );
  440. %obj.createPolygonBoxCollisionShape( 1, 0.5 );
  441. %obj.setAwake( false );
  442. SandboxScene.add( %obj );
  443. return %obj;
  444. }
  445. // -----------------------------------------------------------------------------
  446. function TruckToy::createBrickPile( %this, %posX, %posY )
  447. {
  448. %obj = new Sprite();
  449. %obj.setBodyType( "static" );
  450. %obj.setImage( "TruckToy:brickPile" );
  451. %obj.setPosition( %posX, %posY );
  452. %obj.setSize( 4, 1 );
  453. %obj.setSceneLayer( TruckToy.BackgroundDomain-3 );
  454. %obj.setSceneGroup( TruckToy.BackgroundDomain);
  455. %obj.setCollisionSuppress();
  456. %obj.setAwake( false );
  457. %obj.setActive( false );
  458. SandboxScene.add( %obj );
  459. }
  460. // -----------------------------------------------------------------------------
  461. function TruckToy::createPlank( %this, %plankNumber, %posX, %posY, %angle, %static )
  462. {
  463. if ( %plankNumber < 1 || %plankNumber > 3 )
  464. {
  465. echo( "Invalid plank no of" SPC %plankNumber );
  466. return;
  467. }
  468. %image = "TruckToy:plank_0" @ %plankNumber;
  469. %obj = new Sprite();
  470. if ( %static ) %obj.setBodyType( "static" );
  471. %obj.setImage( %image );
  472. %obj.setAngle( %angle );
  473. %obj.setPosition( %posX, %posY );
  474. %obj.setSize( 5, 1 );
  475. %obj.setSceneLayer( TruckToy.ObstacleDomain );
  476. %obj.setSceneGroup( TruckToy.ObstacleDomain );
  477. %obj.setDefaultFriction( TruckToy.ObstacleFriction );
  478. %obj.setCollisionGroups( TruckToy.GroundDomain, TruckToy.ObstacleDomain );
  479. %obj.setAwake( false );
  480. %obj.setDefaultFriction( 1.0 );
  481. switch$( %plankNumber )
  482. {
  483. case 1:
  484. %obj.createPolygonCollisionShape( "-2.5 -0.5 2.5 -0.5 2.5 -0.2 2.0 0.5 -2.5 -0.2" );
  485. case 2:
  486. %obj.createPolygonCollisionShape( "-2.5 -0.4 2.4 -0.5 2.4 0.5 0 0.5 -2.1 0.1 -2.5 -0.2" );
  487. case 3:
  488. %obj.createPolygonCollisionShape( "-2.5 -0.4 2.5 -0.5 1.9 0.5 -1.8 0.5 -2.5 0" );
  489. }
  490. SandboxScene.add( %obj );
  491. return %obj;
  492. }
  493. // -----------------------------------------------------------------------------
  494. function TruckToy::createWreckedCar( %this, %carNumber, %posX, %posY, %angle, %static )
  495. {
  496. if ( %carNumber < 1 || %carNumber > 3 )
  497. {
  498. echo( "Invalid brick no of" SPC %brickNumber );
  499. return;
  500. }
  501. %image = "TruckToy:wreckedCar_0" @ %carNumber;
  502. %obj = new Sprite();
  503. if ( %static ) %obj.setBodyType( "static" );
  504. %obj.setImage( %image );
  505. %obj.setAngle( %angle );
  506. %obj.setPosition( %posX, %posY );
  507. %obj.setSize( 4, 1.5 );
  508. %obj.setSceneLayer( TruckToy.ObstacleDomain );
  509. %obj.setSceneGroup( TruckToy.ObstacleDomain );
  510. %obj.setCollisionGroups( TruckToy.GroundDomain, TruckToy.ObstacleDomain );
  511. %obj.setAwake( false );
  512. %obj.setDefaultFriction( TruckToy.ObstacleFriction );
  513. switch$( %carNumber )
  514. {
  515. case 1:
  516. %obj.createPolygonCollisionShape( "-2 -0.65 0.5 -0.75 2 -0.45 1.9 0.2 0.5 0.65 -0.5 0.6 -2 -0.3" );
  517. case 2:
  518. %obj.createPolygonCollisionShape( "-2 -0.75 2 -0.75 2 -0.2 0.4 0.65 -0.9 0.7 -2 0.0" );
  519. case 3:
  520. %obj.createPolygonCollisionShape( "-2 -0.65 0 -0.75 2 -0.55 1.8 0.3 0.5 0.75 -0.5 0.75 -2 0.1" );
  521. }
  522. SandboxScene.add( %obj );
  523. return %obj;
  524. }
  525. // -----------------------------------------------------------------------------
  526. function TruckToy::createBonfire(%this, %x, %y, %scale, %layer)
  527. {
  528. // Create an impact explosion at the projectiles position.
  529. %particlePlayer = new ParticlePlayer();
  530. %particlePlayer.BodyType = static;
  531. %particlePlayer.SetPosition( %x, %y );
  532. %particlePlayer.SceneLayer = %layer;
  533. %particlePlayer.ParticleInterpolation = true;
  534. %particlePlayer.Particle = "ToyAssets:bonfire";
  535. %particlePlayer.SizeScale = %scale;
  536. %particlePlayer.CameraIdleDistance = TruckToy.CameraWidth * 0.8;
  537. SandboxScene.add( %particlePlayer );
  538. return %particlePlayer;
  539. }
  540. // -----------------------------------------------------------------------------
  541. function TruckToy::createProjectile(%this)
  542. {
  543. // Fetch the truck position.
  544. %truckPositionX = TruckToy.TruckBody.Position.x;
  545. %projectile = new Sprite() { class = "TruckProjectile"; };
  546. %projectile.Animation = "ToyAssets:Projectile_FireballAnim";
  547. %projectile.setPosition( getRandom( %truckPositionX - (TruckToy.CameraWidth * 0.2), %truckPositionX + (TruckToy.CameraWidth * 0.5) ), 12 );
  548. %projectile.setSceneLayer( TruckToy.BackgroundDomain-2 );
  549. %projectile.setSceneGroup( TruckToy.ProjectileDomain );
  550. %projectile.FlipY = true;
  551. %projectile.Size = getRandom(0.5, 2);
  552. %projectile.Lifetime = 2.5;
  553. %projectile.createCircleCollisionShape( 0.2 );
  554. %projectile.setCollisionGroups( TruckToy.GroundDomain );
  555. %projectile.CollisionCallback = true;
  556. SandboxScene.add( %projectile );
  557. }
  558. // -----------------------------------------------------------------------------
  559. function TruckProjectile::onCollision(%this, %object, %collisionDetails)
  560. {
  561. // Create an impact explosion at the projectiles position.
  562. %particlePlayer = new ParticlePlayer();
  563. %particlePlayer.BodyType = Static;
  564. %particlePlayer.Position = %this.Position;
  565. %particlePlayer.Size = 10;
  566. %particlePlayer.SceneLayer = TruckToy.BackgroundDomain-1;
  567. %particlePlayer.ParticleInterpolation = true;
  568. %particlePlayer.Particle = "ToyAssets:ImpactExplosion";
  569. %particlePlayer.SizeScale = getRandom(TruckToy.ExplosionScale, TruckToy.ExplosionScale * 1.5);
  570. SandboxScene.add( %particlePlayer );
  571. // Start the camera shaking.
  572. SandboxWindow.startCameraShake( 10 + (3 * TruckToy.ExplosionScale), 1 );
  573. // Delete the projectile.
  574. %this.safeDelete();
  575. }
  576. // -----------------------------------------------------------------------------
  577. function TruckToy::createTruck( %this, %posX, %posY )
  578. {
  579. // Truck Body.
  580. %exhaustParticles = new ParticlePlayer();
  581. %exhaustParticles.setPosition( %posX-3, %posY );
  582. %exhaustParticles.setSceneLayer( TruckToy.TruckDomain );
  583. %exhaustParticles.Particle = "TruckToy:exhaust";
  584. %exhaustParticles.SizeScale = 0.1;
  585. %exhaustParticles.ForceScale = 0.4;
  586. %exhaustParticles.EmissionRateScale = 4;
  587. SandboxScene.add( %exhaustParticles );
  588. %exhaustParticles.play();
  589. TruckToy.TruckExhaust = %exhaustParticles;
  590. TruckToy.TruckBody = new Sprite();
  591. TruckToy.TruckBody.setPosition( %posX, %posY );
  592. TruckToy.TruckBody.setImage( "TruckToy:truckBody" );
  593. TruckToy.TruckBody.setSize( 5, 2.5 );
  594. TruckToy.TruckBody.setSceneLayer( TruckToy.TruckDomain );
  595. TruckToy.TruckBody.setSceneGroup( TruckToy.ObstacleDomain);
  596. TruckToy.TruckBody.setCollisionGroups( TruckToy.ObstacleDomain, TruckToy.ObstacleDomain-1, TruckToy.GroundDomain );
  597. TruckToy.TruckBody.createPolygonCollisionShape( "-2 0.2 -2 -0.5 0 -.95 2 -0.5 2 0.0 0 0.7 -1.5 0.7" );
  598. //TruckToy.TruckBody.setDebugOn( 5 );
  599. SandboxScene.add( TruckToy.TruckBody );
  600. // Attach the exhaust output to the truck body.
  601. %joint = SandboxScene.createRevoluteJoint( TruckToy.TruckBody, TruckToy.TruckExhaust, "-2.3 -0.6", "0 0" );
  602. SandboxScene.setRevoluteJointLimit( %joint, 0, 0 );
  603. // Mount camera to truck body.
  604. SandboxWindow.mount( TruckToy.TruckBody, "0 0", 3, true, TruckToy.RotateCamera );
  605. // Tires.
  606. // Suspension = -1.0 : -1.5
  607. // Rear tire.
  608. %tireRear = new Sprite();
  609. %tireRear.setPosition( %posX-1.4, %posY-1.0 );
  610. %tireRear.setImage( "ToyAssets:tires" );
  611. %tireRear.setSize( 1.7, 1.7 );
  612. %tireRear.setSceneLayer( TruckToy.TruckDomain-1 );
  613. %tireRear.setSceneGroup( TruckToy.ObstacleDomain );
  614. %tireRear.setCollisionGroups( TruckToy.ObstacleDomain, TruckToy.GroundDomain );
  615. %tireRear.setDefaultFriction( TruckToy.WheelFriction );
  616. %tireRear.setDefaultDensity( TruckToy.RearWheelDensity );
  617. %tireRear.createCircleCollisionShape( 0.8 );
  618. SandboxScene.add( %tireRear );
  619. TruckToy.RearWheel = %tireRear;
  620. // Front tire.
  621. %tireFront = new Sprite();
  622. %tireFront.setPosition( %posX+1.7, %posY-1.0 );
  623. %tireFront.setImage( "ToyAssets:tires" );
  624. %tireFront.setSize( 1.7, 1.7 );
  625. %tireFront.setSceneLayer( TruckToy.TruckDomain-1 );
  626. %tireFront.setSceneGroup( TruckToy.ObstacleDomain );
  627. %tireFront.setCollisionGroups( TruckToy.ObstacleDomain, TruckToy.GroundDomain );
  628. %tireFront.setDefaultFriction( TruckToy.WheelFriction );
  629. %tireFront.setDefaultDensity( TruckToy.FrontWheelDensity );
  630. %tireFront.createCircleCollisionShape( 0.8 );
  631. SandboxScene.add( %tireFront );
  632. TruckToy.FrontWheel = %tireFront;
  633. // Suspension joints.
  634. TruckToy.RearMotorJoint = SandboxScene.createWheelJoint( TruckToy.TruckBody, %tireRear, "-1.4 -1.25", "0 0", "0 1" );
  635. TruckToy.FrontMotorJoint = SandboxScene.createWheelJoint( TruckToy.TruckBody, %tireFront, "1.7 -1.25", "0 0", "0 1" );
  636. }
  637. // -----------------------------------------------------------------------------
  638. function truckForward(%val)
  639. {
  640. if(%val)
  641. {
  642. if ( !TruckToy.TruckMoving )
  643. {
  644. %driveActive = false;
  645. if ( TruckToy.FrontWheelDrive )
  646. {
  647. SandboxScene.setWheelJointMotor( TruckToy.FrontMotorJoint, true, -TruckToy.WheelSpeed, 10000 );
  648. %driveActive = true;
  649. }
  650. else
  651. {
  652. SandboxScene.setWheelJointMotor( TruckToy.FrontMotorJoint, false );
  653. }
  654. if ( TruckToy.RearWheelDrive )
  655. {
  656. SandboxScene.setWheelJointMotor( TruckToy.RearMotorJoint, true, -TruckToy.WheelSpeed, 10000 );
  657. %driveActive = true;
  658. }
  659. else
  660. {
  661. SandboxScene.setWheelJointMotor( TruckToy.RearMotorJoint, false );
  662. }
  663. if ( %driveActive )
  664. {
  665. TruckToy.TruckExhaust.SizeScale *= 4;
  666. TruckToy.TruckExhaust.ForceScale /= 2;
  667. }
  668. }
  669. TruckToy.TruckMoving = true;
  670. }
  671. else
  672. {
  673. truckStop();
  674. }
  675. }
  676. // -----------------------------------------------------------------------------
  677. function truckReverse(%val)
  678. {
  679. if(%val)
  680. {
  681. if ( !TruckToy.TruckMoving )
  682. {
  683. %driveActive = false;
  684. if ( TruckToy.FrontWheelDrive )
  685. {
  686. SandboxScene.setWheelJointMotor( TruckToy.FrontMotorJoint, true, TruckToy.WheelSpeed, 10000 );
  687. %driveActive = true;
  688. }
  689. else
  690. {
  691. SandboxScene.setWheelJointMotor( TruckToy.FrontMotorJoint, false );
  692. }
  693. if ( TruckToy.RearWheelDrive )
  694. {
  695. SandboxScene.setWheelJointMotor( TruckToy.RearMotorJoint, true, TruckToy.WheelSpeed, 10000 );
  696. %driveActive = true;
  697. }
  698. else
  699. {
  700. SandboxScene.setWheelJointMotor( TruckToy.RearMotorJoint, false );
  701. }
  702. if ( %driveActive )
  703. {
  704. TruckToy.TruckExhaust.SizeScale *= 4;
  705. TruckToy.TruckExhaust.ForceScale /= 2;
  706. }
  707. }
  708. TruckToy.TruckMoving = true;
  709. }
  710. else
  711. {
  712. truckStop();
  713. }
  714. }
  715. //-----------------------------------------------------------------------------
  716. function TruckToy::truckStop(%this)
  717. {
  718. // Finish if truck is not moving.
  719. if ( !TruckToy.TruckMoving )
  720. return;
  721. // Stop truck moving.
  722. SandboxScene.setWheelJointMotor( TruckToy.RearMotorJoint, true, 0, 10000 );
  723. SandboxScene.setWheelJointMotor( TruckToy.FrontMotorJoint, true, 0, 10000 );
  724. TruckToy.TruckExhaust.SizeScale /= 4;
  725. TruckToy.TruckExhaust.ForceScale *= 2;
  726. // Flag truck as not moving.
  727. TruckToy.TruckMoving = false;
  728. }
  729. //-----------------------------------------------------------------------------
  730. function TruckToy::setWheelSpeed( %this, %value )
  731. {
  732. %this.WheelSpeed = %value;
  733. }
  734. //-----------------------------------------------------------------------------
  735. function TruckToy::setWheelFriction( %this, %value )
  736. {
  737. %this.WheelFriction = %value;
  738. }
  739. //-----------------------------------------------------------------------------
  740. function TruckToy::setFrontWheelDensity( %this, %value )
  741. {
  742. %this.FrontWheelDensity = %value;
  743. }
  744. //-----------------------------------------------------------------------------
  745. function TruckToy::setRearWheelDensity( %this, %value )
  746. {
  747. %this.RearWheelDensity = %value;
  748. }
  749. //-----------------------------------------------------------------------------
  750. function TruckToy::setFrontWheelDrive( %this, %value )
  751. {
  752. %this.FrontWheelDrive = %value;
  753. }
  754. //-----------------------------------------------------------------------------
  755. function TruckToy::setRearWheelDrive( %this, %value )
  756. {
  757. %this.RearWheelDrive = %value;
  758. }
  759. //-----------------------------------------------------------------------------
  760. function TruckToy::setProjectileRate( %this, %value )
  761. {
  762. %this.ProjectileRate = %value;
  763. // Start the timer.
  764. TruckToy.startTimer( "createProjectile", TruckToy.ProjectileRate );
  765. }
  766. //-----------------------------------------------------------------------------
  767. function TruckToy::setExplosionScale( %this, %value )
  768. {
  769. %this.ExplosionScale = %value;
  770. }
  771. //-----------------------------------------------------------------------------
  772. function TruckToy::setRotateCamera( %this, %value )
  773. {
  774. %this.RotateCamera = %value;
  775. }
  776. //-----------------------------------------------------------------------------
  777. function TruckToy::onTouchDown(%this, %touchID, %worldPosition)
  778. {
  779. // Finish if truck is already moving.
  780. if ( TruckToy.TruckMoving )
  781. return;
  782. // If we touch in-front of the truck then move forward else reverse.
  783. if ( %worldPosition.x >= TruckToy.TruckBody.Position.x )
  784. {
  785. truckForward( true );
  786. }
  787. else
  788. {
  789. truckReverse( true );
  790. }
  791. }
  792. //-----------------------------------------------------------------------------
  793. function TruckToy::onTouchUp(%this, %touchID, %worldPosition)
  794. {
  795. // Stop the truck.
  796. TruckToy.truckStop();
  797. }