Tower.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. Tower = function (game) {
  2. this.game = game;
  3. Phaser.Group.call(this, game);
  4. this.base = this.create( this.game.width/2, scaleValue(402), 'sprites', 'Base_2');
  5. this.base.anchor.set( 0.5, 1 );
  6. this.currenHeight = this.base.y - this.base.height + scaleValue(11);
  7. this.brickHeight = scaleValue(32);
  8. this.cutValue = scaleValue(15);
  9. this.counter = 1;
  10. //this.lastPartInfo = { x: this.base.x-this.base.width/2, width: this.base.width};
  11. this.parts = [ { x: this.base.x-scaleValue(174)/2, width: this.base.width + scaleValue(11)} ];
  12. this.emitter = game.add.emitter(0, 0, 40);
  13. this.emitter.makeParticles('star');
  14. this.emitter.minParticleSpeed.set(0, -100);
  15. this.emitter.maxParticleSpeed.set(0, -200);
  16. this.emitter.setRotation(-30, 30);
  17. this.emitter.setAlpha(0.3, 0.8);
  18. this.emitter.setScale(0.5, 0.5, 0.7, 0.7);
  19. this.emitter.gravity = 500;
  20. };
  21. Tower.prototype = Object.create(Phaser.Group.prototype);
  22. Tower.prototype.constructor = Tower;
  23. var p = Tower.prototype;
  24. p.placeBlock = function( x, width) {
  25. var cost = 0;
  26. var origX = x;
  27. lastPartInfo = this.parts[ this.parts.length - 1 ];
  28. var placeX = x;
  29. var deltaX = Math.abs(x-lastPartInfo.x);
  30. var birdPlace = 0;
  31. if( deltaX <= scaleValue(3) ){
  32. // идеально!
  33. placeX = lastPartInfo.x;
  34. cost = 10;
  35. }else if( deltaX >= scaleValue(4) && deltaX <= scaleValue(6) ) {
  36. // хорошо
  37. cost = 5;
  38. }else{
  39. // ну а тут пора обрезать
  40. // если вылезло слева
  41. var lives = JSON.parse( localStorage["TheTower.lives"] );
  42. var saveThis = false;
  43. if( x < lastPartInfo.x ){
  44. // не даем отризать маленькие куски
  45. if( deltaX < this.cutValue ){
  46. x = x-(this.cutValue - deltaX);
  47. deltaX = this.cutValue;
  48. }
  49. placeX = x+deltaX;
  50. width -= deltaX;
  51. // проверим жизни и смерть
  52. if( parseInt(width) <=0 && lives > 0 ){
  53. saveThis = true;
  54. width += deltaX;
  55. placeX = origX;
  56. lives--;
  57. localStorage["TheTower.lives"] = lives;
  58. this.game.gui.updateLives( this.lives );
  59. }else{
  60. birdPlace = (lastPartInfo.x + lastPartInfo.width);
  61. this.fallBrick( x, this.currenHeight - this.brickHeight, deltaX );
  62. }
  63. }
  64. // если вылезло справа
  65. else{
  66. // не даем отризать маленькие куски
  67. if( deltaX < this.cutValue ){
  68. x = x+(this.cutValue - deltaX);
  69. deltaX = this.cutValue;
  70. }
  71. placeX = x;
  72. width -= deltaX;
  73. // проверим жизни и смерть
  74. if( parseInt(width) <=0 && lives > 0 ){
  75. saveThis = true;
  76. width += deltaX;
  77. placeX = origX;
  78. lives--;
  79. localStorage["TheTower.lives"] = lives;
  80. this.game.gui.updateLives( this.lives );
  81. }else{
  82. birdPlace = lastPartInfo.x;
  83. this.fallBrick( x+width, this.currenHeight - this.brickHeight, deltaX );
  84. }
  85. }
  86. cost = 1;
  87. }
  88. width = parseInt( width );
  89. if( width > 0 || saveThis ){
  90. var part = new PartGenerator( this.game, width, this.brickHeight, this.counter);
  91. part.x = placeX - part.offsetX;
  92. part.y = this.currenHeight;
  93. this.add( part );
  94. this.parts.push( {x:placeX, width:width} );
  95. if( this.parts.length == 2 )
  96. this.bringToTop( this.children[0] );
  97. if( cost == 10 )
  98. this.placePerfect(x+width/2, width);
  99. else if( cost == 5 )
  100. this.placeGood(x+width/2, width);
  101. var mult = parseInt(this.counter/10)+1;
  102. cost *= mult;
  103. this.counter++;
  104. this.currenHeight -= this.brickHeight;
  105. }
  106. if( width<= 0)
  107. birdPlace = 0;
  108. return [width, cost, birdPlace];
  109. };
  110. p.backToSavePoint = function() {
  111. var steps = this.counter % 10;
  112. for( var i = 0; i < steps-1; i++ ){
  113. if( this.counter > 0)
  114. this.counter--;
  115. this.parts.pop();
  116. this.getTop().destroy();
  117. this.currenHeight += this.brickHeight;
  118. }
  119. };
  120. p.undoLastBlock = function() {
  121. if( this.counter > 0)
  122. this.counter--;
  123. this.parts.pop();
  124. var topPart = this.getTop();
  125. this.remove( topPart );
  126. this.game.world.add( topPart );
  127. topPart.goAway();
  128. this.currenHeight += this.brickHeight;
  129. };
  130. p.placeGood = function(x, width) {
  131. this.game.sndManager.playPerfect();
  132. var style_perfect = {font:'Bold '+scaleValue(30)+'px Arial', fill: '#ffffff', align: 'center'};
  133. var labelPerfect = this.game.add.text( x, this.currenHeight+scaleValue(5), Lang[lang][2], style_perfect );
  134. labelPerfect.anchor.set( 0.5, 0 );
  135. labelPerfect.tween = this.game.add.tween( labelPerfect );
  136. labelPerfect.tween.onComplete.add( function(){this.destroy()}, labelPerfect );
  137. labelPerfect.tween.to({alpha:0}, 600, null, true, 300);
  138. this.emitter.x = x;
  139. this.emitter.y = this.currenHeight;
  140. this.emitter.width = width;
  141. this.emitter.start(true, 1000, null, 10);
  142. };
  143. p.placePerfect = function(x, width) {
  144. this.game.sndManager.playPerfect();
  145. var style_perfect = {font:'Bold '+scaleValue(30)+'px Arial', fill: '#ffd700', align: 'center'};
  146. var labelPerfect = this.game.add.text( x, this.currenHeight+scaleValue(5), Lang[lang][1], style_perfect );
  147. labelPerfect.anchor.set( 0.5, 0 );
  148. labelPerfect.tween = this.game.add.tween( labelPerfect );
  149. labelPerfect.tween.onComplete.add( function(){this.destroy()}, labelPerfect );
  150. labelPerfect.tween.to({alpha:0}, 600, null, true, 300);
  151. this.emitter.x = x;
  152. this.emitter.y = this.currenHeight;
  153. this.emitter.width = width;
  154. this.emitter.start(true, 1000, null, 10);
  155. };
  156. p.fallBrick = function(x, y, width){
  157. var gr = this.game.add.graphics();
  158. gr.beginFill(0xfcc8a6);
  159. gr.drawRect( 0, 0, width, this.brickHeight );
  160. gr.endFill();
  161. gr.boundsPadding = 0;
  162. var brick = this.game.add.sprite( x, y, gr.generateTexture() );
  163. var tween = this.game.add.tween( brick );
  164. tween.onComplete.add( function() {
  165. this.destroy();
  166. }, brick );
  167. tween.to({alpha:0.2, y:brick.y + scaleValue(100), angle: 10}, 600, null, true );
  168. gr.destroy();
  169. };