var EHDI = EHDI || Object.create(null); EHDI.components = EHDI.components || Object.create(null); EHDI.components.CustomerSpawner = function(stage) { EHDI.aka.Container.call(this); this.entering = null; this.customerCount = 0; this.lastNumbers = []; this.upcoming = this.createCustomer(stage); this.current = null; this.done = null; this.forDisposal = []; this.stage = stage; // this.upcoming.position.set(EHDI.GAME.sceneManager.getStageWidth() * 0.5, EHDI.GAME.sceneManager.getStageHeight() * 0.9); this.upcoming.position.set(EHDI.GAME.sceneManager.getStageWidth() * 0.9, EHDI.GAME.sceneManager.getStageHeight() * 0.9); this.moveCustomer(stage); }; EHDI.components.CustomerSpawner.prototype = Object.create(EHDI.aka.Container.prototype); EHDI.components.CustomerSpawner.prototype.createCustomer = function(stage) { var min = 1, max = 3, bread = 0, fish = 0, score = EHDI.GAME.scoreManager.getScore(); if(score >= 100) max++; if(score >= 200) max++; if(score >= 300) max++; if(Math.random() > 0.49) { bread = Math.floor(EHDI.NumberUtil.randomRange(min, max)); fish = Math.floor(EHDI.NumberUtil.randomRange(0, bread)); } else { fish = Math.floor(EHDI.NumberUtil.randomRange(min, max)); bread = Math.floor(EHDI.NumberUtil.randomRange(0, bread)); } /*var isBread = false, isFish = false, isRotate = false; if(Math.random() > 0.49) isBread = true; else isFish = true; if(EHDI.GAME.scoreManager.getScore() >= 100 && Math.random() > 0.49) { isBread = false; isFish = false; } if(EHDI.GAME.scoreManager.getScore() >= 200 && Math.random() > 0.49) { isBread = true; isBread = true; } if(EHDI.GAME.scoreManager.getScore() >= 300 && Math.random() < 0.24) { isRotate = true; }*/ this.customerCount++; var randomNumber = Math.floor(EHDI.NumberUtil.randomRange(1,11)); while(this.lastNumbers.indexOf(randomNumber) !== -1) { randomNumber = Math.floor(EHDI.NumberUtil.randomRange(1,11)); } if(this.lastNumbers.length > 1) { this.lastNumbers.splice(0, 1); } this.lastNumbers.push(randomNumber); var bonus = this.customerCount % 5 === 0 && this.customerCount !== 0; var customer = new EHDI.components.Character(bread, fish, bonus, randomNumber); stage.addChild(customer); return customer; }; EHDI.components.CustomerSpawner.prototype.moveCustomer = function(stage) { if(this.moveTimeline) this.moveTimeline.kill(); this.moveTimeline = new TimelineMax(); if(this.current) { this.forDisposal.push(this.current); this.moveTimeline.to(this.current, 0.75, {x : -this.current.width * 0.5}); this.moveTimeline.addCallback(this.disposeCustomers.bind(this)); this.current = null; } if(this.upcoming) { this.current = this.upcoming; this.moveTimeline.to(this.current, 0.3, {x : EHDI.GAME.sceneManager.getStageWidth() * 0.5}, 0.25); this.moveTimeline.addCallback(this.toggleInteraction.bind(this), "-=0.1"); this.moveTimeline.to(this.current.plate.scale, 0.25, {x : 1.1, y : 1.1, ease : Back.easeOut}); this.moveTimeline.to(this.current.plate, 0.25, { y : this.current.plate.y + EHDI.GAME.sceneManager.getStageHeight() * 0.01, ease : Back.easeOut}, "-=0.25"); this.moveTimeline.addCallback(this.activeCustomer.bind(this)); this.upcoming = this.createCustomer(this.stage); this.moveTimeline.to(this.upcoming, 0.3, {x : EHDI.GAME.sceneManager.getStageWidth() * 0.9}); this.upcoming.position.set(EHDI.GAME.sceneManager.getStageWidth() + this.upcoming.width * 0.6, EHDI.GAME.sceneManager.getStageHeight()* 0.9); } }; EHDI.components.CustomerSpawner.prototype.activeCustomer = function() { if(this.current != null) this.current.interactive = true; }; EHDI.components.CustomerSpawner.prototype.disposeCustomers = function() { for(i = 0; i < this.forDisposal.length; i++) { if(this.forDisposal[i].reactionTimeline) this.forDisposal[i].reactionTimeline.kill(); this.stage.removeChild(this.forDisposal[i]); } this.forDisposal.splice(0, this.forDisposal.length); }; EHDI.components.CustomerSpawner.prototype.toggleInteraction = function() { this.interaction = !this.interaction; if(this.customerCount - 2 < 3) this.current.showHint(); EHDI.GAME.gameScene.bowlShadow.visible = this.interaction; }; EHDI.components.CustomerSpawner.prototype.pause = function() { if(this.current) this.current.pause(); this.moveTimeline.pause(); }; EHDI.components.CustomerSpawner.prototype.resume = function() { this.moveTimeline.play(); if(this.current) this.current.resume(); }; EHDI.components.CustomerSpawner.prototype.goToPostGame = function() { EHDI.GAME.keyboard.leftArrow.deleteListeners(); EHDI.GAME.keyboard.rightArrow.deleteListeners(); EHDI.GAME.keyboard.space.deleteListeners(); EHDI.GAME.sceneManager.changeScene(new EHDI.scene.PostGameScene(), {alpha : new EHDI.scene.TransitionParameter(0, 1), duration : 0.25}); };