builderDemo.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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::createBuilderLevel( %this )
  23. {
  24. // Create background.
  25. %this.createBackground();
  26. // Create the ground.
  27. %this.createGround();
  28. // Create the pyramid.
  29. %this.createPyramid();
  30. // Create circle gesture visual.
  31. %this.createCircleSprite();
  32. // Create circle gesture visual.
  33. %this.createBuilderFingers();
  34. // Set the gravity.
  35. SandboxScene.setGravity( 0, -9.8 );
  36. // Set the manipulation mode.
  37. Sandbox.useManipulation( off );
  38. // Create the help text scene
  39. %helpText = new SimSet();
  40. %helpText.add(new ScriptObject() { Text = "Press TAB to toggle between Cursor and Finger control."; });
  41. %helpText.add(new ScriptObject() { Text = " "; });
  42. %helpText.add(new ScriptObject() { Text = "Finger Control: "; });
  43. %helpText.add(new ScriptObject() { Text = " Reach in to enable finger collision."; });
  44. %helpText.add(new ScriptObject() { Text = " Fingers will turn yellow when collision is enabled."; });
  45. %helpText.add(new ScriptObject() { Text = " "; });
  46. %helpText.add(new ScriptObject() { Text = "Cursor Control: "; });
  47. %helpText.add(new ScriptObject() { Text = " Key tap creates a new block."; });
  48. %helpText.add(new ScriptObject() { Text = " Circle Gesture selects blocks within the circle."; });
  49. %helpText.add(new ScriptObject() { Text = " Screen Tap deletes the selected blocks."; });
  50. %helpText.add(new ScriptObject() { Text = " "; });
  51. %helpText.add(new ScriptObject() { Text = "Press H to return to the demo."; });
  52. %this.createHelpTextScene(%helpText);
  53. %helpText.delete();
  54. %this.CenterZ = 125;
  55. // Swap action maps
  56. FingerMap.pop();
  57. BreakoutMap.pop();
  58. GestureMap.pop();
  59. // Enable builder map
  60. BuilderMap.push();
  61. }
  62. function LeapToy::createBuilderBlock(%this, %position)
  63. {
  64. // Set the block size.
  65. %blockSize = LeapToy.BlockSize;
  66. %blockFrames = "0 2 4 6";
  67. %randomNumber = getRandom(0, 4);
  68. // Create the sprite.
  69. %obj = new Sprite()
  70. {
  71. class = "Block";
  72. flipped = false;
  73. };
  74. %obj.setSceneLayer(3);
  75. %obj.setPosition( %position.x, %position.y );
  76. %obj.setSize( %blockSize );
  77. %obj.setImage( "LeapToy:objectsBlocks" );
  78. %obj.setImageFrame( getWord(%blockFrames, %randomNumber) );
  79. %obj.setDefaultFriction( 1.0 );
  80. %obj.createPolygonBoxCollisionShape( %blockSize, %blockSize );
  81. // Add to the scene.
  82. SandboxScene.add( %obj );
  83. }
  84. //-----------------------------------------------------------------------------
  85. // Called when the user makes a circle with their finger(s)
  86. //
  87. // %id - Finger ID, based on the order the finger was added to the tracking
  88. // %progress - How much of the circle has been completed
  89. // %radius - Radius of the circle created by the user's motion
  90. // %isClockwise - Toggle based on the direction the user made the circle
  91. // %state - State of the gesture progress: 1: Started, 2: Updated, 3: Stopped
  92. function LeapToy::reactToCircleBuilder(%this, %id, %progress, %radius, %isClockwise, %state)
  93. {
  94. if (!%this.enableCircleGesture)
  95. return;
  96. if (isLeapCursorControlled())
  97. {
  98. if (%progress > 0 && %state == 3)
  99. {
  100. %this.grabObjectsInCircle();
  101. %this.schedule(300, "hideCircleSprite");
  102. }
  103. else if (%progress > 0 && %state != 3)
  104. {
  105. %this.showCircleSprite(%radius/5, %isClockwise);
  106. }
  107. }
  108. }
  109. //-----------------------------------------------------------------------------
  110. // Called when the user makes a swipe with their finger(s)
  111. //
  112. // %id - Finger ID, based on the order the finger was added to the tracking
  113. // %state - State of the gesture progress: 1: Started, 2: Updated, 3: Stopped
  114. // %direction - 3 point vector based on the direction the finger swiped
  115. // %speed - How fast the user's finger moved. Values will be quite large
  116. function LeapToy::reactToSwipeBuilder(%this, %id, %state, %direction, %speed)
  117. {
  118. if (!%this.enableSwipeGesture)
  119. return;
  120. }
  121. //-----------------------------------------------------------------------------
  122. // Called when the user makes a screen tap gesture with their finger(s)
  123. //
  124. // %id - Finger ID, based on the order the finger was added to the tracking
  125. // %position - 3 point vector based on where the finger was located in "Leap Space"
  126. // %direction - 3 point vector based on the direction the finger motion
  127. function LeapToy::reactToScreenTapBuilder(%this, %id, %position, %direction)
  128. {
  129. if (!%this.enableScreenTapGesture)
  130. return;
  131. %this.deleteSelectedObjects();
  132. }
  133. //-----------------------------------------------------------------------------
  134. // Called when the user makes a key tap gesture with their finger(s)
  135. //
  136. // %id - Finger ID, based on the order the finger was added to the tracking
  137. // %position - 3 point vector based on where the finger was located in "Leap Space"
  138. // %direction - 3 point vector based on the direction the finger tap
  139. function LeapToy::reactToKeyTapBuilder(%this, %id, %position, %direction)
  140. {
  141. if (!%this.enableKeyTapGesture)
  142. return;
  143. if (isLeapCursorControlled())
  144. {
  145. %worldPosition = SandboxWindow.getWorldPoint(Canvas.getCursorPos());
  146. %this.createBuilderBlock(%worldPosition);
  147. }
  148. }
  149. //-----------------------------------------------------------------------------
  150. // Constantly polling callback based on the finger position on a hand
  151. // %id - Ordered hand ID based on when it was added to the tracking device
  152. // %position - 3 point vector based on where the finger is located in "Leap Space"
  153. function LeapToy::trackFingerPosBuilder(%this, %ids, %fingersX, %fingersY, %fingersZ)
  154. {
  155. if (!%this.enableFingerTracking)
  156. return;
  157. for(%i = 0; %i < getWordCount(%ids); %i++)
  158. {
  159. %id = getWord(%ids, %i);
  160. // The next two lines of code use projection. To use intersection
  161. // comment out both lines, then uncomment the getPointFromIntersection call.
  162. %position = getWord(%fingersX, %i) SPC getWord(%fingersY, %i) SPC getWord(%fingersZ, %i);
  163. %screenPosition = getPointFromProjection(%position);
  164. // This uses intersection
  165. //%screenPosition = getPointFromIntersection(%id);
  166. %worldPosition = SandboxWindow.getWorldPoint(%screenPosition);
  167. %this.showFingerBuilder(%id, %worldPosition, %position.z);
  168. }
  169. }
  170. //-----------------------------------------------------------------------------
  171. function LeapToy::showFingerBuilder(%this, %id, %worldPosition, %zpos)
  172. {
  173. echo("Finger " SPC %id SPC ":" SPC %worldPosition);
  174. %finger = %this.fingers[%id];
  175. if (!isObject(%finger))
  176. return;
  177. if (!%finger.visible)
  178. {
  179. %finger.visible = true;
  180. %finger.setPosition(%worldPosition);
  181. %this.movePosition[%id] = %worldPosition;
  182. }
  183. else
  184. {
  185. %distance = VectorSub(%finger.getPosition(), %worldPosition);
  186. %finger.moveTo(%worldPosition, VectorLen(%distance) * 10, true, false);
  187. %this.movePosition[%id] = %worldPosition;
  188. }
  189. %size = 2;
  190. if( %zpos > LeapToy.CenterZ )
  191. {
  192. %finger.setCollisionSuppress(true);
  193. // set the size to be bigger to give the illusion it is in front of the play area
  194. %size = (%zpos/LeapToy.CenterZ) * 2;
  195. %finger.setBlendColor(1.0, 1.0, 1.0, 0.5);
  196. }
  197. else
  198. {
  199. %finger.setCollisionSuppress(false);
  200. %finger.setBlendColor("Yellow");
  201. }
  202. %finger.setSize(%size, %size);
  203. %this.schedule(200, "checkFingerBuilder", %id, %worldPosition);
  204. }
  205. //-----------------------------------------------------------------------------
  206. function LeapToy::checkFingerBuilder(%this, %id, %worldPosition)
  207. {
  208. %finger = %this.fingers[%id];
  209. // Likely no activity, since the device is sensitive
  210. if (%this.movePosition[%id] $= %worldPosition)
  211. {
  212. %finger.visible = false;
  213. %finger.setCollisionSuppress(true);
  214. }
  215. }
  216. //-----------------------------------------------------------------------------
  217. function LeapToy::createBuilderFingers(%this)
  218. {
  219. for (%i = 0; %i < 10; %i++)
  220. {
  221. %name = "Finger" @ %i;
  222. // Create the circle.
  223. %finger = new Sprite();
  224. %finger.setName(%name);
  225. %finger.Position = "-10 5";
  226. %finger.Size = 2;
  227. %finger.Image = "LeapToy:circleThree";
  228. %finger.Visible = false;
  229. %finger.createCircleCollisionShape(1.0, "0 0");
  230. %finger.FixedAngle = true;
  231. %finger.setDefaultFriction(1.0, true);
  232. %finger.CollisionSuppress = true;
  233. %finger.setSceneLayer(0);
  234. %finger.setCollisionLayers("1 2 3 4 5");
  235. %this.fingers[%i] = %finger;
  236. // Add to the scene.
  237. SandboxScene.add( %finger );
  238. }
  239. }