main.cs 33 KB

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