2
0

gestureDemo.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 LeapToy::createGesturesLevel( %this )
  23. {
  24. // Create background.
  25. %this.createBackground();
  26. // Create the ground.
  27. %this.createGround();
  28. // Create the pyramid.
  29. %this.createPyramid();
  30. // Create a ball.
  31. %this.createBall();
  32. // Create circle gesture visual.
  33. %this.createCircleSprite();
  34. // Make sure no objects are still selected
  35. %this.selectedObjects.clear();
  36. // Se the gravity.
  37. SandboxScene.setGravity( 0, -9.8 );
  38. // Set the manipulation mode.
  39. Sandbox.useManipulation( pull );
  40. // Swap action maps
  41. FingerMap.pop();
  42. BreakoutMap.pop();
  43. GestureMap.push();
  44. }
  45. //-----------------------------------------------------------------------------
  46. function LeapToy::createPyramid( %this )
  47. {
  48. // Fetch the block count.
  49. %blockCount = LeapToy.BlockCount;
  50. // Sanity!
  51. if ( %blockCount < 2 )
  52. {
  53. echo( "Cannot have a pyramid block count less than two." );
  54. return;
  55. }
  56. // Set the block size.
  57. %blockSize = LeapToy.BlockSize;
  58. // Calculate a block building position.
  59. %posx = %blockCount * -0.5 * %blockSize;
  60. %posy = -8.8 + (%blockSize * 0.5);
  61. // Build the stack of blocks.
  62. for( %stack = 0; %stack < %blockCount; %stack++ )
  63. {
  64. // Calculate the stack position.
  65. %stackIndexCount = %blockCount - (%stack*2);
  66. %stackX = %posX + ( %stack * %blockSize );
  67. %stackY = %posY + ( %stack * %blockSize );
  68. // Build the stack.
  69. for ( %stackIndex = 0; %stackIndex < %stackIndexCount; %stackIndex++ )
  70. {
  71. // Calculate the block position.
  72. %blockX = %stackX + (%stackIndex*%blockSize);
  73. %blockY = %stackY;
  74. %blockFrames = "0 2 4 6";
  75. %randomNumber = getRandom(0, 4);
  76. // Create the sprite.
  77. %obj = new Sprite()
  78. {
  79. class = "Block";
  80. flipped = false;
  81. };
  82. %obj.setSceneLayer(3);
  83. %obj.setPosition( %blockX, %blockY );
  84. %obj.setSize( %blockSize );
  85. %obj.setImage( "LeapToy:objectsBlocks" );
  86. %obj.setImageFrame( getWord(%blockFrames, %randomNumber) );
  87. %obj.setDefaultFriction( 1.0 );
  88. %obj.createPolygonBoxCollisionShape( %blockSize, %blockSize );
  89. // Add to the scene.
  90. SandboxScene.add( %obj );
  91. }
  92. }
  93. }
  94. //-----------------------------------------------------------------------------
  95. function LeapToy::createBall( %this )
  96. {
  97. // Create the ball.
  98. %ball = new Sprite();
  99. %ball.Position = "5 5";
  100. %ball.Size = 4;
  101. %ball.Image = "LeapToy:widgetBall";
  102. %ball.setDefaultDensity( 1 );
  103. %ball.setDefaultRestitution( 0.5 );
  104. %ball.createCircleCollisionShape( 1.8 );
  105. %ball.CollisionCallback = true;
  106. %this.ball = %ball;
  107. // Add to the scene.
  108. SandboxScene.add( %ball );
  109. }
  110. //-----------------------------------------------------------------------------
  111. function LeapToy::createCircleSprite( %this )
  112. {
  113. // Create the circle.
  114. %circle = new Sprite();
  115. %circle.Position = "-10 5";
  116. %circle.setBodyType("Kinematic");
  117. %circle.Size = 5;
  118. %circle.Image = "LeapToy:circleThree";
  119. %circle.Visible = false;
  120. %circle.AngularVelocity = 180;
  121. %this.circleSprite = %circle;
  122. // Add to the scene.
  123. SandboxScene.add( %circle );
  124. }
  125. //-----------------------------------------------------------------------------
  126. // This will be callsed when the user makes a circle gesture with a Leap Motion
  127. //
  128. // %radius - How large the circle gesture was
  129. // %isClockwise - True if the motion was clockwise
  130. function LeapToy::showCircleSprite( %this, %radius, %isClockwise )
  131. {
  132. // If it isn't currently visible, show it
  133. if (!%this.circleSprite.visible)
  134. {
  135. %this.circleSprite.visible = true;
  136. // Find out where the cursor is currenty location
  137. %worldPosition = SandboxWindow.getWorldPoint(Canvas.getCursorPos());
  138. // Move the circle to that spot. This should be where the circle
  139. // gesture first started
  140. %this.circleSprite.position = %worldPosition;
  141. }
  142. // Resize the circle based on how big the radius was
  143. %this.circleSprite.size = %radius;
  144. // Rotate to the right if the circle is clockwise, left otherwise
  145. if (%isClockwise)
  146. %this.circleSprite.AngularVelocity = -180;
  147. else
  148. %this.circleSprite.AngularVelocity = 180;
  149. }
  150. //-----------------------------------------------------------------------------
  151. // This will be called when the user stops making a circle gesture
  152. function LeapToy::hideCircleSprite( %this )
  153. {
  154. // Hide the sprite
  155. %this.circleSprite.visible = 0;
  156. }
  157. //-----------------------------------------------------------------------------
  158. // This will be called when either the hand position or hand rotation is being tracked
  159. //
  160. // %horizontalSpeed - How fast to move in the X-axis
  161. // %verticalSpeed - How fast to move in the Y-axis
  162. // %angularSpeed - How fast to rotate on the Z-axis
  163. function LeapToy::accelerateBall( %this, %horizontalSpeed, %verticalSpeed )
  164. {
  165. %this.ball.setLinearVelocity(%horizontalSpeed, %verticalSpeed);
  166. }
  167. //-----------------------------------------------------------------------------
  168. // This is called when a user makes a swipe gesture with the Leap Motion
  169. //
  170. // %position - Where to spawn the asteroid
  171. // %direction - 3 point vector based on the direction the finger swiped
  172. // %speed - How fast the user's finger moved. Values will be quite large
  173. function LeapToy::createAsteroid( %this, %position, %direction, %speed )
  174. {
  175. // Size of the asteroid.
  176. %size = 3;
  177. // Reduce the speed of the swipe so it can be used for a reasonable
  178. // velocity in T2D.
  179. %reducedSpeed = mClamp((%speed / 8), 0, 55);
  180. %velocity = vectorScale(%direction, %reducedSpeed);
  181. // Create an asteroid.
  182. %object = new Sprite()
  183. {
  184. class = "Asteroid";
  185. };
  186. %object.Position = %position;
  187. %object.CollisionCallback = true;
  188. %object.Size = %size;
  189. %object.SceneLayer = 7;
  190. %object.Image = "LeapToy:widgetFruit";
  191. %object.setDefaultDensity( 3 );
  192. %object.createCircleCollisionShape( %size * 0.4 );
  193. %object.setLinearVelocity( %velocity._0, %velocity._1 );
  194. %object.setAngularVelocity( getRandom(-90,90) );
  195. %object.setLifetime( 10 );
  196. SandboxScene.add( %object );
  197. // Create fire trail.
  198. %player = new ParticlePlayer();
  199. %player.Particle = "LeapToy:digitalFire";
  200. %player.Position = %object.Position;
  201. %player.EmissionRateScale = 3;
  202. %player.SizeScale = 2;
  203. %player.SceneLayer = 0;
  204. %player.setLifetime( 10 );
  205. SandboxScene.add( %player );
  206. %jointId = SandboxScene.createRevoluteJoint( %object, %player );
  207. SandboxScene.setRevoluteJointLimit( %jointId, 0, 0 );
  208. // Assign the trail to the asteroid, used for cleanup later
  209. %object.Trail = %player;
  210. }
  211. //-----------------------------------------------------------------------------
  212. // Called when an object with a class of "Asteroid" collides with another body.
  213. // In this toy, it will delete the asteroid and create an explosion
  214. //
  215. // %object - What the asteroid collided with
  216. // %collisionDetails - Information about the collision
  217. function Asteroid::onCollision( %this, %object, %collisionDetails )
  218. {
  219. // Create explosion.
  220. %player = new ParticlePlayer();
  221. %player.BodyType = static;
  222. %player.Particle = "LeapToy:digitalExplosion";
  223. %player.Position = %this.Position;
  224. %player.SceneLayer = 0;
  225. SandboxScene.add( %player );
  226. %controller = new PointForceController();
  227. %controller.setControlLayers( 3 ); // Only affect blocks.
  228. %controller.Radius = 5;
  229. %controller.Force = -65;
  230. %controller.NonLinear = true;
  231. %controller.LinearDrag = 0.1;
  232. %controller.AngularDrag = 0;
  233. SandboxScene.Controllers.add( %controller );
  234. %controller.Position = %this.Position;
  235. %id = %controller.getID();
  236. // Delete the asteroid.
  237. %this.Trail.LinearVelocity = 0;
  238. %this.Trail.AngularVelocity = 0;
  239. %this.Trail.safeDelete();
  240. %this.safeDelete();
  241. LeapToy.schedule(500, "deleteController", %controller);
  242. }
  243. //-----------------------------------------------------------------------------
  244. function LeapToy::deleteController(%this, %controller)
  245. {
  246. SandboxScene.Controllers.remove(%controller);
  247. %controller.delete();
  248. }
  249. //-----------------------------------------------------------------------------
  250. function LeapToy::grabObjectsInCircle( %this )
  251. {
  252. %worldPosition = %this.circleSprite.getPosition();
  253. %size = %this.circleSprite.getSize();
  254. %radius = (%size._0 * 0.5);
  255. echo("Radius:" SPC %radius);
  256. %picked = SandboxScene.pickCircle(%worldPosition, %radius);
  257. // Finish if nothing picked.
  258. if ( %picked $= "" )
  259. return;
  260. // Fetch the pick count.
  261. %pickCount = %picked.Count;
  262. for( %n = 0; %n < %pickCount; %n++ )
  263. {
  264. // Fetch the picked object.
  265. %pickedObject = getWord( %picked, %n );
  266. // Skip if the object is static.
  267. if ( %pickedObject.getBodyType() $= "static" || %pickedObject.getBodyType() $= "kinematic")
  268. continue;
  269. if (%pickedObject.class $= "block")
  270. %pickedObject.flipFrame();
  271. }
  272. }
  273. //-----------------------------------------------------------------------------
  274. function Block::flipFrame( %this )
  275. {
  276. %currentFrame = %this.getImageFrame();
  277. if (%this.flipped == true)
  278. {
  279. %newFrame = %currentFrame - 1;
  280. %this.flipped = false;
  281. LeapToy.selectedObjects.remove(%this);
  282. }
  283. else
  284. {
  285. %newFrame = %currentFrame + 1;
  286. %this.flipped = true;
  287. LeapToy.selectedObjects.add(%this);
  288. }
  289. %this.setImageFrame(%newFrame);
  290. }
  291. //-----------------------------------------------------------------------------
  292. function LeapToy::createNewBlock( %this )
  293. {
  294. %worldPosition = SandboxWindow.getWorldPoint(Canvas.getCursorPos());
  295. // Create the sprite.
  296. %obj = new Sprite();
  297. %obj.setPosition( %worldPosition );
  298. %obj.setSize( LeapToy.blockSize );
  299. %obj.setImage( "ToyAssets:blocks" );
  300. %obj.setImageFrame( getRandom(0,55) );
  301. %obj.setDefaultFriction( 1.0 );
  302. %obj.createPolygonBoxCollisionShape( LeapToy.blockSize, LeapToy.blockSize );
  303. // Add to the scene.
  304. SandboxScene.add( %obj );
  305. }
  306. //-----------------------------------------------------------------------------
  307. function LeapToy::deleteSelectedObjects( %this )
  308. {
  309. while (%this.selectedObjects.getCount() > 0)
  310. %this.selectedObjects.getObject(0).delete();
  311. }