123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- Tower = function (game) {
- this.game = game;
- Phaser.Group.call(this, game);
- this.base = this.create( this.game.width/2, scaleValue(402), 'sprites', 'Base_2');
- this.base.anchor.set( 0.5, 1 );
- this.currenHeight = this.base.y - this.base.height + scaleValue(11);
- this.brickHeight = scaleValue(32);
- this.cutValue = scaleValue(15);
- this.counter = 1;
- //this.lastPartInfo = { x: this.base.x-this.base.width/2, width: this.base.width};
- this.parts = [ { x: this.base.x-scaleValue(174)/2, width: this.base.width + scaleValue(11)} ];
- this.emitter = game.add.emitter(0, 0, 40);
- this.emitter.makeParticles('star');
- this.emitter.minParticleSpeed.set(0, -100);
- this.emitter.maxParticleSpeed.set(0, -200);
- this.emitter.setRotation(-30, 30);
- this.emitter.setAlpha(0.3, 0.8);
- this.emitter.setScale(0.5, 0.5, 0.7, 0.7);
- this.emitter.gravity = 500;
- };
- Tower.prototype = Object.create(Phaser.Group.prototype);
- Tower.prototype.constructor = Tower;
- var p = Tower.prototype;
- p.placeBlock = function( x, width) {
- var cost = 0;
- var origX = x;
- lastPartInfo = this.parts[ this.parts.length - 1 ];
- var placeX = x;
- var deltaX = Math.abs(x-lastPartInfo.x);
- var birdPlace = 0;
- if( deltaX <= scaleValue(3) ){
- // идеально!
- placeX = lastPartInfo.x;
- cost = 10;
- }else if( deltaX >= scaleValue(4) && deltaX <= scaleValue(6) ) {
- // хорошо
- cost = 5;
- }else{
- // ну а тут пора обрезать
- // если вылезло слева
- var lives = JSON.parse( localStorage["TheTower.lives"] );
- var saveThis = false;
- if( x < lastPartInfo.x ){
- // не даем отризать маленькие куски
- if( deltaX < this.cutValue ){
- x = x-(this.cutValue - deltaX);
- deltaX = this.cutValue;
- }
- placeX = x+deltaX;
- width -= deltaX;
-
- // проверим жизни и смерть
- if( parseInt(width) <=0 && lives > 0 ){
- saveThis = true;
- width += deltaX;
- placeX = origX;
- lives--;
- localStorage["TheTower.lives"] = lives;
- this.game.gui.updateLives( this.lives );
- }else{
- birdPlace = (lastPartInfo.x + lastPartInfo.width);
- this.fallBrick( x, this.currenHeight - this.brickHeight, deltaX );
- }
- }
- // если вылезло справа
- else{
- // не даем отризать маленькие куски
- if( deltaX < this.cutValue ){
- x = x+(this.cutValue - deltaX);
- deltaX = this.cutValue;
- }
- placeX = x;
- width -= deltaX;
- // проверим жизни и смерть
- if( parseInt(width) <=0 && lives > 0 ){
- saveThis = true;
- width += deltaX;
- placeX = origX;
- lives--;
- localStorage["TheTower.lives"] = lives;
- this.game.gui.updateLives( this.lives );
- }else{
- birdPlace = lastPartInfo.x;
- this.fallBrick( x+width, this.currenHeight - this.brickHeight, deltaX );
- }
- }
- cost = 1;
- }
- width = parseInt( width );
- if( width > 0 || saveThis ){
- var part = new PartGenerator( this.game, width, this.brickHeight, this.counter);
- part.x = placeX - part.offsetX;
- part.y = this.currenHeight;
- this.add( part );
- this.parts.push( {x:placeX, width:width} );
- if( this.parts.length == 2 )
- this.bringToTop( this.children[0] );
- if( cost == 10 )
- this.placePerfect(x+width/2, width);
- else if( cost == 5 )
- this.placeGood(x+width/2, width);
- var mult = parseInt(this.counter/10)+1;
- cost *= mult;
- this.counter++;
- this.currenHeight -= this.brickHeight;
- }
- if( width<= 0)
- birdPlace = 0;
- return [width, cost, birdPlace];
- };
- p.backToSavePoint = function() {
- var steps = this.counter % 10;
- for( var i = 0; i < steps-1; i++ ){
- if( this.counter > 0)
- this.counter--;
- this.parts.pop();
- this.getTop().destroy();
- this.currenHeight += this.brickHeight;
- }
- };
- p.undoLastBlock = function() {
- if( this.counter > 0)
- this.counter--;
- this.parts.pop();
- var topPart = this.getTop();
- this.remove( topPart );
- this.game.world.add( topPart );
- topPart.goAway();
- this.currenHeight += this.brickHeight;
- };
- p.placeGood = function(x, width) {
- this.game.sndManager.playPerfect();
- var style_perfect = {font:'Bold '+scaleValue(30)+'px Arial', fill: '#ffffff', align: 'center'};
- var labelPerfect = this.game.add.text( x, this.currenHeight+scaleValue(5), Lang[lang][2], style_perfect );
- labelPerfect.anchor.set( 0.5, 0 );
- labelPerfect.tween = this.game.add.tween( labelPerfect );
- labelPerfect.tween.onComplete.add( function(){this.destroy()}, labelPerfect );
- labelPerfect.tween.to({alpha:0}, 600, null, true, 300);
- this.emitter.x = x;
- this.emitter.y = this.currenHeight;
- this.emitter.width = width;
- this.emitter.start(true, 1000, null, 10);
- };
- p.placePerfect = function(x, width) {
- this.game.sndManager.playPerfect();
- var style_perfect = {font:'Bold '+scaleValue(30)+'px Arial', fill: '#ffd700', align: 'center'};
- var labelPerfect = this.game.add.text( x, this.currenHeight+scaleValue(5), Lang[lang][1], style_perfect );
- labelPerfect.anchor.set( 0.5, 0 );
- labelPerfect.tween = this.game.add.tween( labelPerfect );
- labelPerfect.tween.onComplete.add( function(){this.destroy()}, labelPerfect );
- labelPerfect.tween.to({alpha:0}, 600, null, true, 300);
- this.emitter.x = x;
- this.emitter.y = this.currenHeight;
- this.emitter.width = width;
- this.emitter.start(true, 1000, null, 10);
- };
- p.fallBrick = function(x, y, width){
- var gr = this.game.add.graphics();
- gr.beginFill(0xfcc8a6);
- gr.drawRect( 0, 0, width, this.brickHeight );
- gr.endFill();
- gr.boundsPadding = 0;
- var brick = this.game.add.sprite( x, y, gr.generateTexture() );
- var tween = this.game.add.tween( brick );
- tween.onComplete.add( function() {
- this.destroy();
- }, brick );
- tween.to({alpha:0.2, y:brick.y + scaleValue(100), angle: 10}, 600, null, true );
- gr.destroy();
- };
|