CBall.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. function CBall( refWorld, refBallMaterial, refStdMaterial, iScene, iPosition, oCartCoords){
  2. var aProj = [
  3. [-1.324419465111331,0.07007920010441403,0.1938814229049522,0.1928501356834461,-0.2649754991389736,-0.29199768911624474,-0.9738954395935614,-0.9687151293457442,0.00813866187260608,1.8973661726282403,-0.15703993365948804,-0.1562046123872247,172.14460555118495,-57.92280926049329,268.64013255353115,275.1899143092173],
  4. [-1.0311847252524577,-0.17599844916970192,-0.6427545415461143,-0.6393356242752912,0.8719967238119737,-0.26330126009450605,-0.7552979557156582,-0.7512804014885001,0.025392432048756682,1.8946950915444967,-0.16466751447142178,-0.16379162084052554,74.75179565415395,-37.2124724898568,323.023333753347,329.2838423047125],
  5. [-0.04192064711782405,-0.28804391554765474,-0.9934914290013894,-0.9882068844583713,1.3500256223076117,-0.017671886269370676,-0.030155903522676906,-0.02999549930503738,0.006203995474932445,1.8991816460801592,-0.15096085318711913,-0.15015786754513807,6.84660099873786,-40.15800737796026,335.8181997013088,342.0106502533296],
  6. [1.0287322954933102,-0.20296283656350578,-0.6427545605150148,-0.6393356431432929,0.8752566449130996,0.2404296293623392,0.7552979421456383,0.7512803879906613,-0.0008671184282911734,1.895038730402846,-0.1646675026722602,-0.16379160910412566,-75.22073485514608,-35.249324074836075,323.0233348288672,329.2838433745118],
  7. [1.3232711036898168,-0.00901517234163781,0.201487851298251,0.20041610423131065,-0.2666922478682122,0.28881907281462693,0.9738954354986644,0.9687151252726287,0.04684016896142928,1.8991247445227672,-0.14715365255419113,-0.14637091803943836,-176.51195787275645,-53.18780012690175,267.37304986600344,273.9295714431983]
  8. ];
  9. var _bBallGrabbed = false,
  10. _bBallWaitToThrow = false,
  11. _bBallThrowing = false,
  12. _bTouchGround = false,
  13. _iCartPosition = iPosition,
  14. _iScene = iScene,
  15. _fMass = 1, // CANNON.Js ball mass
  16. _fRadius = 7, // CANNON.Js ball radius
  17. _fAngle = 0,
  18. _v3IniPos = [0,new CANNON.Vec3(97,150,86),new CANNON.Vec3(178,80,86),new CANNON.Vec3(195,-26,86),new CANNON.Vec3(130,-110,86),new CANNON.Vec3(112,-150,87)],
  19. _v3IniPosStand = [0,new CANNON.Vec3(97,150,78.5),new CANNON.Vec3(178,80,78.5),new CANNON.Vec3(195,-26,78.5),new CANNON.Vec3(130,-110,78.5),new CANNON.Vec3(112,-150,79.5)],
  20. _v3Winning = [0,new CANNON.Vec3(-1292.5,-5287.5, 6697.5),new CANNON.Vec3(-3995,-2702.5,6697.5),new CANNON.Vec3(-4935,+940,6580),new CANNON.Vec3(-2398.5,+4036.5,6552),new CANNON.Vec3(-1755,+5206.5, 6669)],
  21. _v2IniPos = {x: oCartCoords.x - BALL_SIZE, y: oCartCoords.y - 0.4*CANVAS_HEIGHT},
  22. _v2Position = {x:0, y:0},
  23. _v2PositionLag = {x:0, y:0},
  24. _oWorld = refWorld;
  25. this._oSphereBody;
  26. this._oStandBody;
  27. this._oSprite;
  28. this._oSpriteShadow;
  29. this._oContainer = undefined;
  30. this._v2SlideDestination = {x:0,y:0};
  31. // Ball initialized but not added to CANNON.Js world
  32. var _oSphereShape = new CANNON.Sphere(_fRadius);
  33. this._oSphereBody = new CANNON.Body({mass: _fMass,material: refBallMaterial});
  34. this._oSphereBody.position.copy(_v3IniPos[iScene]);
  35. this._oSphereBody.addShape(_oSphereShape);
  36. // Stand initialized but not added to CANNON.Js world
  37. var _oStandShape = new CANNON.Box(new CANNON.Vec3(2.5,2.5,0.5));
  38. this._oStandBody = new CANNON.Body({mass: 0,material: refStdMaterial});
  39. this._oStandBody.position.copy(_v3IniPosStand[iScene]);
  40. this._oStandBody.addShape(_oStandShape);
  41. this._oSpriteShadow = createBitmap(s_oSpriteLibrary.getSprite("shadow"));
  42. this._oSpriteShadow.regX = 104/2;
  43. this._oSpriteShadow.regY = 44/2;
  44. this._oSpriteShadow.visible = false;
  45. var szBallType;
  46. if(_iCartPosition === 0){
  47. szBallType = "ball_2";
  48. }else{
  49. szBallType = "ball_1";
  50. }
  51. this._oSprite = createBitmap(s_oSpriteLibrary.getSprite(szBallType));
  52. this._oSprite.regX = BALL_SIZE/2;
  53. this._oSprite.regY = BALL_SIZE/2;
  54. if (_iScene < 5) {
  55. this._oSprite.x = _v2IniPos.x - (_iCartPosition*BALL_SIZE*0.75);
  56. } else {
  57. this._oSprite.x = _v2IniPos.x + (_iCartPosition-(NUM_SHOT_PER_SCENE-1))*BALL_SIZE*0.75;
  58. };
  59. this._oSprite.y = _v2IniPos.y;
  60. this._oContainer = new createjs.Container();
  61. this._oContainer.addChild(this._oSprite,this._oSpriteShadow);
  62. if (_iScene === 1) {
  63. s_oStage.addChildAt(this._oContainer,CART_DEPTH_INDEX + 1);
  64. } else {
  65. s_oStage.addChildAt(this._oContainer,CART_DEPTH_INDEX + 1);
  66. };
  67. this.unload = function(){
  68. _oWorld.remove(this._oSphereBody);
  69. _oWorld.remove(this._oStandBody);
  70. s_oStage.removeChild(this._oContainer);
  71. };
  72. this.update = function(){
  73. if (!_bBallThrowing) {
  74. _oWorld.step(1/30);
  75. _v2PositionLag.x = _v2Position.x;
  76. _v2PositionLag.y = _v2Position.y;
  77. _v2Position = this._projCoordScaling(this._oSphereBody.position);
  78. var speed = _v2Position.x - _v2PositionLag.x,scaling = _v2Position.diam/BALL_SIZE;
  79. if (scaling>1) {scaling=1;};
  80. this._oSprite.x = _v2Position.x;
  81. this._oSprite.y = _v2Position.y;
  82. this._oSprite.scaleY = this._oSprite.scaleX = scaling;
  83. _fAngle += speed*BALL_ROTATION_SPEED;
  84. this._oSprite.rotation = _fAngle;
  85. this._oSpriteShadow.visible = true;
  86. this._oSpriteShadow.x = _v2Position.shadowX;
  87. this._oSpriteShadow.y = _v2Position.shadowY;
  88. this._oSpriteShadow.scaleX = this._oSpriteShadow.x.scaleY = scaling*0.75;
  89. this._oSpriteShadow.alpha = (this._oSprite.y/CANVAS_HEIGHT)*0.66 + 0.33;
  90. };
  91. };
  92. this.grab = function(v2Dest){
  93. if(!_bBallGrabbed){
  94. _bBallGrabbed = true;
  95. createjs.Tween.removeTweens(this._oSprite);
  96. if (_iScene<5) {
  97. createjs.Tween.get(this._oSprite).to({x:v2Dest.x,y:v2Dest.y}, 80,createjs.Ease.cubicIn);
  98. } else {
  99. createjs.Tween.get(this._oSprite).to({x:v2Dest.x + 100,y:v2Dest.y}, 80,createjs.Ease.cubicIn);
  100. };
  101. };
  102. };
  103. this.slide = function(){
  104. _fAngle -= 75;
  105. if (_iScene<5) {
  106. createjs.Tween.get(this._oSprite).to({x: this._oSprite.x - BALL_SIZE*0.8, rotation: _fAngle}, 500,createjs.Ease.linear);
  107. } else {
  108. createjs.Tween.get(this._oSprite).to({x: this._oSprite.x + BALL_SIZE*0.8, rotation: _fAngle}, 500,createjs.Ease.linear)
  109. };
  110. };
  111. this.waitToThrow = function(){
  112. if (!_bBallWaitToThrow && !_bBallThrowing) {
  113. _bBallWaitToThrow = true;
  114. var that = this;
  115. createjs.Tween.get(this._oSprite).to({y: this._oSprite.y + 7}, 450,createjs.Ease.linear).call(function(){
  116. createjs.Tween.get(that._oSprite)
  117. .to({y: that._oSprite.y - 7}, 450,createjs.Ease.linear)
  118. .call(function(){
  119. _bBallWaitToThrow = false;
  120. });
  121. },that);
  122. };
  123. };
  124. this._projCoord = function(v3){
  125. var pos = {x: v3.x, y: v3.y, z: v3.z},
  126. x = pos.x, y = pos.y, z = pos.z,
  127. e = aProj[_iScene - 1];
  128. var d = 1 / ( e[ 3 ] * x + e[ 7 ] * y + e[ 11 ] * z + e[ 15 ] );
  129. pos.x = ( e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ] ) * d;
  130. pos.y = ( e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ] ) * d;
  131. pos.z = ( e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ] ) * d;
  132. pos.x = (pos.x + 1) * CANVAS_WIDTH/2;
  133. pos.y = (-pos.y + 1) * CANVAS_HEIGHT/2;
  134. return {x: pos.x, y: pos.y};
  135. };
  136. this._projCoordScaling = function(v3){
  137. var pos1 = this._projCoord( new CANNON.Vec3( v3.x , v3.y , v3.z+_fRadius ) ),
  138. pos2 = this._projCoord( new CANNON.Vec3( v3.x , v3.y , v3.z-_fRadius ) ),
  139. shdw = this._projCoord( new CANNON.Vec3( v3.x , v3.y , 0 ) );
  140. var diameter = Math.abs(pos1.y - pos2.y);
  141. return {x: (pos1.x + pos2.x)/2, y: (pos1.y + pos2.y)/2, diam: diameter, shadowX: shdw.x, shadowY: shdw.y};
  142. };
  143. this.throwingBall = function(v2Dest,v2Error,refBall){
  144. var that = this;
  145. var _v2Dest;
  146. if (_iScene < 5) {
  147. _v2Dest = {x: v2Dest.x-40, y: v2Dest.y-172};
  148. } else {
  149. _v2Dest = {x: v2Dest.x+145, y: v2Dest.y-172};
  150. };
  151. if(!_bBallThrowing){
  152. _bBallThrowing = true;
  153. createjs.Tween.removeTweens(this._oSprite);
  154. createjs.Tween.get(this._oSprite).to({x:_v2Dest.x,y:_v2Dest.y}, 100,createjs.Ease.linear).call(function(){
  155. var v3Force;
  156. if (_iScene===1) {
  157. v3Force = new CANNON.Vec3(_v3Winning[1].x - v2Error.x * ERROR_MULT,
  158. _v3Winning[1].y + v2Error.y * ERROR_MULT,
  159. _v3Winning[1].z)
  160. } else if (_iScene===2) {
  161. v3Force = new CANNON.Vec3(_v3Winning[2].x - v2Error.x * ERROR_MULT,
  162. _v3Winning[2].y + v2Error.y * ERROR_MULT,
  163. _v3Winning[2].z)
  164. } else if (_iScene===3) {
  165. v3Force = new CANNON.Vec3(_v3Winning[3].x + v2Error.y * ERROR_MULT,
  166. _v3Winning[3].y + v2Error.x * ERROR_MULT,
  167. _v3Winning[3].z)
  168. } else if (_iScene===4) {
  169. v3Force = new CANNON.Vec3(_v3Winning[4].x + v2Error.x * ERROR_MULT,
  170. _v3Winning[4].y - v2Error.y * ERROR_MULT,
  171. _v3Winning[4].z);
  172. } else if (_iScene===5) {
  173. v3Force = new CANNON.Vec3(_v3Winning[5].x + v2Error.x * ERROR_MULT,
  174. _v3Winning[5].y - v2Error.y * ERROR_MULT,
  175. _v3Winning[5].z);
  176. };
  177. _oWorld.add(refBall._oSphereBody);
  178. _oWorld.add(refBall._oStandBody);
  179. that._oSphereBody.applyForce(
  180. v3Force,
  181. that._oSphereBody.position);
  182. },that);
  183. that.update();
  184. _bBallThrowing = false;
  185. };
  186. };
  187. this.touchGround = function(){
  188. var that = this;
  189. if(this._oSphereBody.position.z <= 7.01 && _bTouchGround === false){
  190. playSound("us_bounce", 1, false);
  191. _bTouchGround = true;
  192. createjs.Tween.get(that._oContainer).to({alpha:0}, 1000, createjs.Ease.circIn).call(function(){
  193. that.unload();
  194. },that);
  195. };
  196. };
  197. this.touchedGround = function(){
  198. return _bTouchGround;
  199. };
  200. this.getPosition = function(){
  201. return this._oSphereBody.position;
  202. };
  203. this.getCartPosition = function(){
  204. return _iCartPosition;
  205. };
  206. };