CPlayer.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. function CPlayer(refGame,oCartCoords){
  2. var _bBallGrabbed = false,
  3. _bWaitToThrow = false,
  4. _bEndIdling = false,
  5. _bFrameIncreasing = true, // frames increasing
  6. _iCurrFrame = undefined, // current played frame
  7. _iCurrScene = 1,
  8. _refGame = refGame;
  9. var _aSprites = [];
  10. this.init = function(oCartCoords){
  11. for (var i = 0; i <= 131; i++) {
  12. _aSprites.push(createBitmap(s_oSpriteLibrary.getSprite("pl" + i)));
  13. _aSprites[i].visible = false;
  14. _aSprites[i].regX = PLAYER_WIDTH/2;
  15. _aSprites[i].regY = PLAYER_HEIGHT/2;
  16. _aSprites[i].x = oCartCoords.x - PLAYER_WIDTH;
  17. _aSprites[i].y = oCartCoords.y - 0.55*PLAYER_HEIGHT;
  18. s_oStage.addChild(_aSprites[i]);
  19. };
  20. };
  21. this.nextScene = function(oCartCoords){
  22. _iCurrScene++;
  23. this.newBall();
  24. if (_iCurrScene < 5) {
  25. for (var i = 0; i <= 131; i++) {
  26. _aSprites[i].x = oCartCoords.x - PLAYER_WIDTH;
  27. _aSprites[i].y = oCartCoords.y - 0.55*PLAYER_HEIGHT;
  28. };
  29. } else {
  30. for (var i = 0; i <= 131; i++) {
  31. _aSprites[i].x = oCartCoords.x + 0.05*PLAYER_WIDTH;
  32. _aSprites[i].y = oCartCoords.y - 0.55*PLAYER_HEIGHT;
  33. _aSprites[i].scaleX = - 1;
  34. };
  35. };
  36. };
  37. this.idle1 = function(){
  38. this.playFrames(0,15,false);
  39. };
  40. this.grab1 = function(){
  41. if (_iCurrFrame < 15) {
  42. this.playFrames(0,15,true);
  43. } else if (_iCurrFrame <= 38){
  44. if (_iCurrFrame === 35) {
  45. _bBallGrabbed = true;
  46. };
  47. this.playFrames(16,39,true);
  48. } else if (_iCurrFrame === 39 && _bWaitToThrow === false){
  49. // waiting to throw
  50. _bWaitToThrow = true;
  51. _refGame.ballWaitToThrow();
  52. createjs.Tween.get(_aSprites[_iCurrFrame])
  53. .to({scaleY: 0.98, y: oCartCoords.y - 0.55*PLAYER_HEIGHT + 5}, 450,createjs.Ease.linear)
  54. .call(function(){
  55. createjs.Tween.get(_aSprites[_iCurrFrame])
  56. .to({scaleY: 1, y: oCartCoords.y - 0.55*PLAYER_HEIGHT}, 450,createjs.Ease.linear)
  57. .call(function(){
  58. _bWaitToThrow = false;
  59. });
  60. });
  61. };
  62. };
  63. this.grab2 = function(){
  64. if (!_bBallGrabbed || (_bBallGrabbed === true && _iCurrFrame < 61)) {
  65. this.playFrames(44, 61,true);
  66. if (_iCurrFrame === 57) {_bBallGrabbed = true;};
  67. } else if (_bBallGrabbed === true && _iCurrFrame >= 61 && _bWaitToThrow === false) {
  68. _bWaitToThrow = true;
  69. _refGame.ballWaitToThrow();
  70. createjs.Tween.removeTweens(_aSprites[_iCurrFrame]);
  71. createjs.Tween.get(_aSprites[_iCurrFrame])
  72. .to({scaleY: 0.98, y: oCartCoords.y - 0.55*PLAYER_HEIGHT + 5}, 450,createjs.Ease.linear)
  73. .call(function(){
  74. createjs.Tween.get(_aSprites[_iCurrFrame]).
  75. to({scaleY: 1, y: oCartCoords.y - 0.55*PLAYER_HEIGHT}, 450,createjs.Ease.linear).
  76. call(function(){
  77. _bWaitToThrow = false;
  78. });
  79. });
  80. };
  81. };
  82. this.throwing = function(){
  83. if (_iCurrFrame<43) {
  84. this.playFrames(40,43,true);
  85. } else {
  86. return true;
  87. };
  88. };
  89. this.endTurn = function(){
  90. if (!_bEndIdling) {
  91. this.playFrames(86,131,true);
  92. if(_iCurrFrame === 131){_bEndIdling = true;};
  93. } else {
  94. this.idle1();
  95. };
  96. };
  97. // bStartOver = true -> @ after last frame goes to first
  98. this.playFrames = function(iFirst,iLast,bStartOver){
  99. if(_iCurrFrame < iFirst || _iCurrFrame > iLast || _iCurrFrame === undefined){
  100. if(_aSprites[_iCurrFrame]){_aSprites[_iCurrFrame].visible = false};
  101. _iCurrFrame = iFirst;
  102. _aSprites[_iCurrFrame].visible = true;
  103. _bFrameIncreasing = true;
  104. } else {
  105. _aSprites[_iCurrFrame].visible = false;
  106. if (_iCurrFrame === iFirst && _bFrameIncreasing === false) {
  107. _bFrameIncreasing = true;
  108. _iCurrFrame += 1;
  109. _aSprites[_iCurrFrame].visible = true;
  110. } else if (_iCurrFrame === iLast) {
  111. if (bStartOver === true) {
  112. _iCurrFrame = iFirst;
  113. _aSprites[_iCurrFrame].visible = true;
  114. } else {
  115. _bFrameIncreasing = false;
  116. _iCurrFrame--;
  117. _aSprites[_iCurrFrame].visible = true;
  118. };
  119. } else {
  120. if (_bFrameIncreasing) {
  121. _iCurrFrame++;
  122. _aSprites[_iCurrFrame].visible = true;
  123. } else {
  124. _iCurrFrame--;
  125. _aSprites[_iCurrFrame].visible = true;
  126. };
  127. };
  128. };
  129. };
  130. // get player position
  131. this.getPlCoords = function(){
  132. return {x: _aSprites[39].x - PLAYER_WIDTH/8,y: _aSprites[39].y - PLAYER_HEIGHT/10 - BALL_SIZE/2};
  133. };
  134. this.isBallGrabbed = function(){
  135. return _bBallGrabbed;
  136. };
  137. this.newBall = function(){
  138. _bBallGrabbed = false;
  139. _bWaitToThrow = false;
  140. _bEndIdling = false;
  141. };
  142. this.init(oCartCoords);
  143. };