var EHDI = EHDI || Object.create(null); EHDI.components = EHDI.components || Object.create(null); EHDI.components.PatternManager = function(stage) { this.stage = stage; this.pool = []; for(var i = 0; i < 10; i++) { this.pool.push(new EHDI.components.AnimatedPatternObject()); this.stage.addChild(this.pool[i]); }; this.grid = []; this.gridRandomizer = []; this.numberOfElements = 1; this.numberOfDontClick = 0; this.inputIndex = 0; this.repeatCount = 1; this.randomPositions = this.createGridRandomizer(this.numberOfElements + this.numberOfDontClick); // this.createPattern(); }; EHDI.components.PatternManager.prototype = Object.create(null); EHDI.components.PatternManager.prototype.createGridRandomizer = function(size) { this.gridRandomizer = []; for(var i = 0; i < 6; i++) { for(var j = 0; j < 3; j++) { this.gridRandomizer.push(new PIXI.Point(i,j)); } } EHDI.NumberUtil.shuffleArray(this.gridRandomizer); return this.gridRandomizer.splice(0, size); }; EHDI.components.PatternManager.prototype.createPattern = function () { this.grid = []; for(var i = 0; i < this.numberOfElements; i++) { var circuitNumber = Math.floor(EHDI.NumberUtil.randomRange(1,4)); var newPatternObject = this.pool.pop(); newPatternObject.setPattern(circuitNumber, this.randomPositions[i].x, this.randomPositions[i].y, true); this.grid.push(newPatternObject); } if(this.numberOfDontClick > 0) { for(var i = this.numberOfElements; i < this.numberOfDontClick + this.numberOfElements; i++) { var circuitNumber = Math.floor(EHDI.NumberUtil.randomRange(1,4)); var newPatternObject = this.pool.pop(); newPatternObject.setPattern(circuitNumber, this.randomPositions[i].x, this.randomPositions[i].y, false); this.grid.push(newPatternObject); } } EHDI.NumberUtil.shuffleArray(this.grid); if(!this.grid[this.inputIndex].shouldClick) { this.checkIfNextRound(); } this.showPattern(); }; EHDI.components.PatternManager.prototype.showPattern = function () { this.showAnimation = new TimelineMax(); for(var i = 0; i < this.grid.length; i ++ ) { this.showAnimation.to(this.grid[i].sprite.scale, 0.25, {x : 1, y : 1, ease : Power0.easeNone}); this.showAnimation.to(this.grid[i].sprite, 0.25, {x : this.grid[i].xPos, y : this.grid[i].yPos, ease : Power2.easeIn}); this.showAnimation.add(this.playSpawnSFX); this.showAnimation.add(this.grid[i].showPattern.bind(this.grid[i]), "+=0.1"); this.showAnimation.add(this.doNothing, "+=0.3"); }; this.showAnimation.add(EHDI.GAME.gameScene.lowerGlowIntensity.bind(EHDI.GAME.gameScene)); this.showAnimation.add(this.activateInteractions.bind(this)); if(this.showAnimation && EHDI.GAME.pauseButton.isPaused) this.showAnimation.pause(); }; EHDI.components.PatternManager.prototype.playSpawnSFX = function() { EHDI.GAME.soundManager.playSFX("spawn_pattern"); }; EHDI.components.PatternManager.prototype.doNothing = function() { }; EHDI.components.PatternManager.prototype.activateInteractions = function() { this.showAnimation.kill(); this.showAnimation = null; for(var i = 0; i < this.grid.length; i++) { this.grid[i].interactive = true; TweenLite.to(this.grid[i].sprite, 0.1, {alpha : 1}); } }; EHDI.components.PatternManager.prototype.processInput = function(input) { if(this.grid[this.inputIndex] === input) { return true; } return false; }; EHDI.components.PatternManager.prototype.checkIfNextRound = function() { this.inputIndex++; if(this.inputIndex >= this.numberOfElements + this.numberOfDontClick) { for(var i = 0; i < this.grid.length; i++) { this.grid[i].hideObject(false, true); } this.backToPool(); EHDI.GAME.gizmoArmature.animation.gotoAndPlay("correct", -1, -1, 1); EHDI.GAME.scoreManager.addScore(10 * (this.numberOfElements + this.numberOfDontClick)); } else if(!this.grid[this.inputIndex].shouldClick) this.checkIfNextRound(); }; EHDI.components.PatternManager.prototype.nextRound = function() { this.inputIndex = 0; EHDI.GAME.gizmoArmature.animation.gotoAndPlay("idle", -1, -1, 0); if(this.repeatCount >= 3 || (this.numberOfElements === 1 && this.numberOfDontClick === 0)) { this.repeatCount = 1; if(this.numberOfElements === 5) { this.numberOfDontClick++; if(this.numberOfDontClick >= 5) this.numberOfDontClick = 5; } else if(this.numberOfDontClick === 1 || (this.numberOfElements === 1 && this.numberOfDontClick === 0)) { this.numberOfElements = this.numberOfElements + this.numberOfDontClick + 1; this.numberOfDontClick = 0; if(this.numberOfElements >= 5) this.numberOfElements = 5; } else if(this.numberOfDontClick === 0) { this.numberOfElements--; this.numberOfDontClick++; } } else { this.repeatCount++; } this.randomPositions = this.createGridRandomizer(this.numberOfElements + this.numberOfDontClick); EHDI.GAME.gameScene.higherGlowIntensity(); }; EHDI.components.PatternManager.prototype.endGame = function() { EHDI.GAME.pauseButton.isEndGame = true; EHDI.GAME.gizmoArmature.animation.gotoAndPlay("wrong", -1, -1, 1); EHDI.GAME.gameScene.error(); for(var i = 0; i < this.grid.length; i++) { this.grid[i].interactive = false; if(this.grid[this.inputIndex] !== this.grid[i]) this.grid[i].break(); } this.postGameTimeline = new TimelineMax({onComplete: EHDI.GAME.gameScene.playOutro.bind(EHDI.GAME.gameScene), repeat: 3}); this.postGameTimeline.to(this.grid[this.inputIndex].sprite.scale, 0.1, {x: 1.2, y : 1.2}); this.postGameTimeline.to(this.grid[this.inputIndex].sprite.scale, 0.1, {x: 1, y : 1}); }; EHDI.components.PatternManager.prototype.pauseAnimations = function() { if(this.showAnimation) this.showAnimation.pause(); if(this.postGameTimeline) this.postGameTimeline.pause(); for(var i = 0; i < this.grid.length; i++) { this.grid[i].pauseTimeline(); } }; EHDI.components.PatternManager.prototype.resumeAnimations = function() { if(this.showAnimation) this.showAnimation.play(); if(this.postGameTimeline) this.postGameTimeline.play(); for(var i = 0; i < this.grid.length; i++) { this.grid[i].resumeTimeline(); } }; EHDI.components.PatternManager.prototype.hideAllPatterns = function() { for(var i = 0; i < this.grid.length; i++) { this.grid[i].hideObject(false, true); //this.backToPool(this.grid[i]); } }; EHDI.components.PatternManager.prototype.backToPool = function() { for(var i = 0; i < this.grid.length; i++) { this.pool.push(this.grid[i]); } this.grid.splice(0, this.grid.length); } EHDI.components.PatternManager.prototype.goToPostGame = function() { var score = EHDI.GAME.scoreManager.getScore(); var star = 0; if(score > 0) star++; if(score > 300) star++; if(score > 600) star++; this.postGameTimeline.kill(); this.postGameTimeline = null; EHDI.GAME.pauseButton.endGame(); //EHDI.GAME.sceneManager.pushPopUp(new EHDI.popup.PostGamePopUp(star, score), {y : new EHDI.scene.TransitionParameter(-EHDI.GAME.sceneManager.getStageHeight(), EHDI.GAME.sceneManager.getStageHeight() * 0.5), duration : 0.5}); if(score > EHDI.GAME.saveData.highScore) { EHDI.GAME.saveData.highScore = score; EHDI.sbGame.saveGameData(EHDI.GAME.saveData, "DEFAULT", function(){ console.log("data saved."); }); } EHDI.sbGame.end(score); };