CGame.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. function CGame(){
  2. var _bBallThrowing = false;
  3. var _bResetSelector = false;
  4. var _bBallGrabbed = false;
  5. var _bResettingTurn = true;
  6. var _bScored = false;
  7. var _bRimCollision = false;
  8. var _iGameState; // 0 = init, 1 = idle, 2 = grab, 3 = throwing, 4 = thrown, 5 = PrepForNextLevel
  9. var _iCurrentScreen;
  10. var _iShotsLeft;
  11. var _iPoints = 0;
  12. var _iShots = 0;
  13. var _iFrame = 0;
  14. var _fStartTime;
  15. var _fTimeLeft;
  16. var _szGameState;
  17. var _oBall = [];
  18. var _oWorld;
  19. var _oInterface;
  20. var _oHelpPanel;
  21. var _oEndPanel = null;
  22. var _oPlayer;
  23. var _oSceneStatic;
  24. var groundMaterial,
  25. ballMaterial,
  26. basketMaterial;
  27. this._init = function(){
  28. // init variables
  29. _iGameState = 0;
  30. _iCurrentScreen = 1;
  31. _iShotsLeft = NUM_SHOT_PER_SCENE;
  32. _fTimeLeft = TIME_AVAILABLE;
  33. // init cannon.js
  34. _oWorld = new CANNON.World();
  35. _oWorld.gravity.set(0,0,-400);
  36. _oWorld.broadphase = new CANNON.NaiveBroadphase();
  37. _oWorld.solver.iterations = 5;
  38. groundMaterial = new CANNON.Material();
  39. ballMaterial = new CANNON.Material();
  40. basketMaterial = new CANNON.Material();
  41. var ground_ground_cm = new CANNON.ContactMaterial(
  42. groundMaterial, groundMaterial, {
  43. friction: 0.20,
  44. restitution: 0.55
  45. });
  46. var ball_ground_cm = new CANNON.ContactMaterial(
  47. ballMaterial, groundMaterial, {
  48. friction: 0.05,
  49. restitution: 0.35
  50. });
  51. var ball_basket_cm = new CANNON.ContactMaterial(
  52. ballMaterial, basketMaterial, {
  53. friction: 0.25,
  54. restitution: 0.5
  55. });
  56. _oWorld.addContactMaterial(ground_ground_cm);
  57. _oWorld.addContactMaterial(ball_ground_cm);
  58. _oWorld.addContactMaterial(ball_basket_cm);
  59. // init floor
  60. var floorShape = new CANNON.Plane();
  61. var floorBody = new CANNON.Body({mass: 0, material: groundMaterial});
  62. floorBody.addShape(floorShape);
  63. _oWorld.add(floorBody);
  64. // init static scene (Background + Basket + Ball Cart)
  65. _oSceneStatic = new CSceneStatic(_oWorld,_iCurrentScreen,groundMaterial,basketMaterial);
  66. var _oCartCoords = _oSceneStatic.getCartCoords();
  67. // init player anims etc
  68. _oPlayer = new CPlayer(this,_oCartCoords);
  69. // init CBALL (1st screen)
  70. for (var i = 0; i < _iShotsLeft; i++) {
  71. _oBall.push(new CBall(_oWorld,groundMaterial,groundMaterial,_iCurrentScreen, i, _oCartCoords));
  72. };
  73. // init CInterface
  74. _oInterface = new CInterface(this);
  75. _oHelpPanel = new CHelpPanel();
  76. setVolume("soundtrack",SOUNDTRACK_VOLUME_IN_GAME);
  77. playSound("us_crowd", 1, true);
  78. $(s_oMain).trigger("start_level",1);
  79. };
  80. this.unload = function(){
  81. $(s_oMain).trigger("end_session", _iPoints);
  82. if(_iPoints > 0){
  83. $(s_oMain).trigger("share_event",_iPoints);
  84. }
  85. stopSound("us_crowd");
  86. // Unload CANNON.Js
  87. var iBodies = _oWorld.bodies.length;
  88. for (var i = 0; i < iBodies; i++) {
  89. _oWorld.remove( _oWorld.bodies[0] );
  90. };
  91. _oWorld = null;
  92. // Unload s_oStage objects (Create.Js)
  93. createjs.Tween.removeAllTweens();
  94. s_oStage.removeAllEventListeners();
  95. s_oStage.removeAllChildren();
  96. _oInterface.unload();
  97. if(_oEndPanel !== null){
  98. _oEndPanel.unload();
  99. }
  100. // Back to s_oMain (CMain.js)
  101. s_oMain.gotoMenu();
  102. };
  103. this.exitFromHelp = function(){
  104. _oHelpPanel.unload();
  105. // switch to IDLE
  106. _iGameState = 1;
  107. };
  108. this.getState = function(){
  109. return _oGameStates[_szGameState];
  110. };
  111. this.ballWaitToThrow = function() {
  112. if(_iShotsLeft>0){
  113. _oBall[_iShotsLeft - 1].waitToThrow();
  114. };
  115. };
  116. this.update = function(iCurTime){
  117. if (_iGameState > 1) {
  118. // CHECK TIME
  119. if (_fStartTime === undefined) {
  120. _fStartTime = iCurTime;
  121. } else {
  122. _fTimeLeft = Math.floor( TIME_AVAILABLE - (iCurTime - _fStartTime) );
  123. if (_fTimeLeft >= 0) {
  124. _oInterface.updateTime(_fTimeLeft);
  125. _oSceneStatic.updateTime(_fTimeLeft);
  126. } else {
  127. playSound("us_buzzer", 1, false);
  128. _oEndPanel = new CEndPanel(_fTimeLeft,_iPoints,_iShots,this);
  129. _iGameState = -1;
  130. };
  131. };
  132. };
  133. // IDLE - just used for 1st shot
  134. if (_iGameState === 1) {
  135. _iFrame++;
  136. _oPlayer.idle1();
  137. if (_iFrame>30){
  138. _iGameState = 2;
  139. };
  140. };
  141. // GRABBING BALL - just used for 1st shot
  142. if (_iGameState === 2) {
  143. _oInterface.listenForClick();
  144. _oPlayer.grab1();
  145. if (_oPlayer.isBallGrabbed() && !_bBallGrabbed) {
  146. _bBallGrabbed = true;
  147. _oBall[_iShotsLeft - 1].grab(_oPlayer.getPlCoords());
  148. for (var i = 0; i < _iShotsLeft - 1; i++) {
  149. _oBall[i].slide();
  150. };
  151. };
  152. if (_oInterface.isVectorAquired().state){
  153. _iGameState = 3;
  154. };
  155. };
  156. // THROWING
  157. if (_iGameState === 3) {
  158. if (!_bBallThrowing) {
  159. _bBallThrowing = true;
  160. _oBall[_iShotsLeft - 1].throwingBall(_oPlayer.getPlCoords(),
  161. _oInterface.isVectorAquired().vector,
  162. _oBall[_iShotsLeft - 1]);
  163. _oBall[_iShotsLeft - 1].update();
  164. };
  165. if(_oPlayer.throwing(_iCurrentScreen)) {
  166. _iGameState = 4;
  167. _bResettingTurn = true;
  168. _bBallThrowing = false;
  169. _bScored = false;
  170. _bRimCollision = false;
  171. };
  172. };
  173. // THROWN
  174. if (_iGameState === 4) {
  175. if (_bResettingTurn) {
  176. _bResettingTurn = false;
  177. _bBallGrabbed = false;
  178. _oPlayer.newBall();
  179. _oSceneStatic.newBall();
  180. _iShotsLeft--;
  181. _iShots++;
  182. _oInterface.updateShots(_iShots);
  183. };
  184. _oBall[_iShotsLeft].update();
  185. var bBasketState = _oSceneStatic.updateBasket(_oBall[_iShotsLeft].getPosition());
  186. if(bBasketState === true){
  187. _bScored = true;
  188. if(_oBall[_iShotsLeft].getCartPosition() === 0){
  189. _iPoints += POINT_FOR_SPECIAL_BALL;
  190. }else{
  191. _iPoints += POINT_FOR_BALL;
  192. }
  193. _oInterface.updateScore(_iPoints);
  194. playSound("us_cheer", 1, false);
  195. } else if (bBasketState === false && _bScored === false){
  196. _bRimCollision = true;
  197. };
  198. if(_bScored === true){
  199. _bScored = _oSceneStatic.scored();
  200. _bRimCollision = false;
  201. } else {
  202. if(_bRimCollision){
  203. _bRimCollision = _oSceneStatic.rimCollision();
  204. };
  205. };
  206. _oBall[_iShotsLeft].touchGround();
  207. if (_iShotsLeft > 0){
  208. _oPlayer.grab2();
  209. if (_oPlayer.isBallGrabbed() && !_bBallGrabbed) {
  210. _bBallGrabbed = true;
  211. _oBall[_iShotsLeft - 1].grab(_oPlayer.getPlCoords());
  212. for (var i = 0; i < _iShotsLeft - 1; i++) {
  213. _oBall[i].slide();
  214. };
  215. };
  216. } else {
  217. _oPlayer.endTurn();
  218. };
  219. if (_oBall[_iShotsLeft].touchedGround()) {
  220. if(!_bResetSelector){_oInterface.newBall(); _bResetSelector=true;};
  221. if(_iShotsLeft > 0){
  222. _oInterface.listenForClick();
  223. if (_oInterface.isVectorAquired().state) {_iGameState = 3; _bResetSelector=false;};
  224. } else if (_iShotsLeft === 0){
  225. _bResetSelector=false;
  226. _iGameState = 5;
  227. };
  228. };
  229. };
  230. // INIT NEXT SCENE
  231. if (_iGameState === 5){
  232. _iCurrentScreen++;
  233. if (_iCurrentScreen <= 5) {
  234. _iShotsLeft = NUM_SHOT_PER_SCENE;
  235. _oPlayer.endTurn();
  236. _oSceneStatic.nextScene();
  237. _oPlayer.nextScene(_oSceneStatic.getCartCoords());
  238. for (var i = 0; i < _iShotsLeft; i++) {
  239. _oBall[i].unload();
  240. _oBall[i] = new CBall(_oWorld,groundMaterial,groundMaterial,_iCurrentScreen, i, _oSceneStatic.getCartCoords());
  241. };
  242. _iGameState = 1;
  243. } else {
  244. _oEndPanel = new CEndPanel(_fTimeLeft,_iPoints,_iShots,this);
  245. _iGameState = -1;
  246. };
  247. };
  248. };
  249. s_oGame = this;
  250. this._init();
  251. }
  252. var s_oGame;