var EHDI = EHDI || Object.create(null); EHDI.scene = EHDI.scene || Object.create(null); EHDI.scene.GameScene = function (){ PIXI.Container.call(this); EHDI.GAME.gameScene = this; this.gameLayer = new EHDI.aka.Container(); this.guiLayer = new EHDI.aka.Container(); this.addChild(this.gameLayer); this.addChild(this.guiLayer); this.isMoving = false; this.ignoreInput = false; }; EHDI.scene.GameScene.prototype = Object.create(EHDI.aka.Container.prototype); EHDI.scene.GameScene.prototype.screenWillAppear = function() { this.bg = new EHDI.components.Background(EHDI.Assets.images["lostsheep_bg"]); this.gameLayer.addChild(this.bg); }; EHDI.scene.GameScene.prototype.screenDidAppear = function() { //this.setupPauseButton(); var pauseButton = new EHDI.components.pauseButton(); this.guiLayer.addChild(pauseButton); this.bg.interactive = true; this.hand = new EHDI.components.Hand(); this.gameLayer.addChild(this.hand); this.obstacleSpawner = new EHDI.components.ObstacleSpawner(this.hand.handCollider, this.gameLayer); this.gameLayer.addChild(this.obstacleSpawner); this.bg.mousedown = this.pointerDown.bind(this); this.bg.touchstart = this.pointerDown.bind(this); this.bg.mousemove = this.move.bind(this); this.bg.touchmove = this.move.bind(this); this.bg.touchend = this.pointerUp.bind(this); this.bg.touchendoutside = this.pointerUp.bind(this); this.bg.mouseupoutside = this.pointerUp.bind(this); this.bg.mouseup = this.pointerUp.bind(this); EHDI.GAME.scoreManager = new EHDI.components.ScoreManager(this.guiLayer); EHDI.GAME.scoreManager.setXY(EHDI.GAME.sceneManager.getStageWidth() * 0.025, EHDI.GAME.sceneManager.getStageHeight() * 0.015); EHDI.GAME.grassManager = new EHDI.components.GrassManager(this.guiLayer); EHDI.GAME.grassManager.setXY(EHDI.GAME.sceneManager.getStageWidth() * 0.3, EHDI.GAME.sceneManager.getStageHeight() * 0.015); this.updateFunction = this.updateDistance.bind(this); EHDI.GAME.updateManager.addFrameListener(this.updateFunction); EHDI.GAME.updateManager.addFrameListener(this.hand.updateFunction); this.createDragonBones(); this.createParticles(); this.hand.enter(); var cache = EHDI.GAME.saveData; if(cache.isFirstTimePlay) { EHDI.GAME.pauseButton.isPaused = true; EHDI.GAME.sceneManager.pushPopUp(new EHDI.popup.HTPPopUp(), {y : new EHDI.scene.TransitionParameter(-EHDI.GAME.sceneManager.getStageHeight(), EHDI.GAME.sceneManager.getStageHeight() * 0.5), duration : 0.25}); cache.isFirstTimePlay = false; EHDI.sbGame.saveGameData(EHDI.GAME.saveData, "DEFAULT", function(){ console.log("data saved."); }); } }; EHDI.scene.GameScene.prototype.startGame = function() { this.isMoving = true; }; EHDI.scene.GameScene.prototype.createDragonBones = function() { EHDI.GAME.dbFactory = new dragonBones.PixiFactory(); var textureImage = EHDI.Assets.images["lostsheep_vfx_tex"].baseTexture.source; var textureData = EHDI.Assets.cjAssets.getResult("lostsheep_vfx_tex"); EHDI.GAME.dbFactory.addTextureAtlas(new dragonBones.TextureAtlas(textureImage, textureData)); this.skeleton = EHDI.Assets.cjAssets.getResult("lostsheep_vfx_ske"); EHDI.GAME.dbFactory.addDragonBonesData(dragonBones.DataParser.parseDragonBonesData(this.skeleton)); EHDI.GAME.updateManager.addFrameListener(this.animateAnimations); }; EHDI.scene.GameScene.prototype.animateAnimations = function (dt) { if(EHDI.GAME.pauseButton.isPaused || EHDI.GAME.pauseButton.isEndGame) return; dragonBones.WorldClock.clock.advanceTime(dt * 0.001); }; EHDI.scene.GameScene.prototype.createParticles = function() { this.particleContainer = new EHDI.aka.Container(); this.gameLayer.addChild(this.particleContainer); this.explosions = new EHDI.aka.Sprite(EHDI.Assets.images["fx_explode-fs8"]); this.explosions.anchor.set(0.5,0.5); this.explosions.scale.set(0, 0); this.particleContainer.addChild(this.explosions); this.particleContainer.zIndex = this.hand.handCollider.y; } EHDI.scene.GameScene.prototype.animateMalfunction = function(obstacle) { this.hand.hideHint(); this.sparkArmature = EHDI.GAME.dbFactory.buildArmature("sparks"); this.sparkArmature.animation.gotoAndPlay("sparks_an", -1, -1, 0); dragonBones.WorldClock.clock.add(this.sparkArmature); this.spark = this.sparkArmature.getDisplay(); this.particleContainer.addChild(this.spark); this.cogsArmature = EHDI.GAME.dbFactory.buildArmature("cogs"); this.cogsArmature.animation.gotoAndPlay("cogs_an", -1, -1, 1); dragonBones.WorldClock.clock.add(this.cogsArmature); this.cogs = this.cogsArmature.getDisplay(); this.particleContainer.addChild(this.cogs); this.smokeArmature = EHDI.GAME.dbFactory.buildArmature("smoke"); this.smokeArmature.animation.gotoAndPlay("smoke_an", -1, -1, 0); dragonBones.WorldClock.clock.add(this.smokeArmature); this.smoke = this.smokeArmature.getDisplay(); this.gameLayer.addChild(this.smoke); this.smoke.zIndex = this.hand.zIndex - 1; this.spark.position.set(this.hand.hand.x + this.hand.hand.width * 0.5, this.hand.hand.y); this.cogs.position.set(this.hand.hand.x + this.hand.hand.width * 0.5, this.hand.hand.y); this.smoke.position.set(this.hand.hand.x + this.hand.hand.width * 0.5, this.hand.hand.y - this.smoke.height * 0.5); var xPos = this.hand.x, yPos = this.hand.y; this.shakeTimeline = new TimelineMax({repeat : 3, onComplete : this.endGame.bind(this)}); this.shakeTimeline.to(this.hand, 0.06, {x : xPos + 2, y : yPos -2, ease: Power0.easeNone}); this.shakeTimeline.to(this.hand, 0.06, {x : xPos, y : yPos -2, ease: Power0.easeNone}); this.shakeTimeline.to(this.hand, 0.06, {x : xPos - 2, y : yPos +2, ease: Power0.easeNone}); this.shakeTimeline.to(this.hand, 0.06, {x : xPos, y : yPos + 2, ease: Power0.easeNone}); }; EHDI.scene.GameScene.prototype.showSmoke = function(obstacle) { var midpoint = this.hand.hand.x + this.hand.hand.width * 0.5 + obstacle.x; midpoint /= 2; this.smoke.position.set(midpoint, this.hand.hand.y); this.smokeAnimation = new TimelineMax(); this.smokeAnimation.to(this.smoke.scale, 0.2, {x : 1, y : 1, ease: Power0.easeNone}); this.smokeAnimation.to(this.smoke, 0.5, {alpha : 0, ease : Power0.easeNone }, "-=0.2"); }; EHDI.scene.GameScene.prototype.showExplosion = function() { this.explosions.position.set(this.hand.hand.x + this.hand.hand.width * 0.5, this.hand.hand.y); this.hand.grabHand(); this.explosions.alpha = 1; this.explosions.scale.set(0, 0); if(this.explosionAnimation) this.explosionAnimation.kill(); this.explosionAnimation = new TimelineMax({onComplete : this.hand.idleHand.bind(this.hand)}); this.explosionAnimation.to(this.explosions.scale, 0.2, {x : 1, y : 1, ease : Power0.easeNone}); this.explosionAnimation.to(this.explosions, 0.5, {alpha : 0, ease : Power0.easeNone}, "-=0.2"); }; EHDI.scene.GameScene.prototype.updateDistance = function(dt) { if(EHDI.GAME.pauseButton.isPaused || EHDI.GAME.pauseButton.isEndGame) return; if(!this.isMoving) return; if(this.reverse) { if(this.hand.hand.x < EHDI.GAME.sceneManager.getStageWidth() * 0.75) { this.obstacleSpawner.moveSpeed = 8; this.hand.moveHandPosition(this.obstacleSpawner.moveSpeed * dt / 1000 * 96); } else if(this.ignoreInput) { this.ignoreInput = false; EHDI.GAME.updateManager.addFrameListener(this.hand.updateFunction); } var moveSpeed = this.obstacleSpawner.updateStars (dt); this.bg.moveBackground(-moveSpeed * dt / 1000); } else { var moveSpeed = this.obstacleSpawner.updateDistance(dt); this.bg.moveBackground(moveSpeed * dt / 1000); } this.updateLayerOrder(); }; EHDI.scene.GameScene.prototype.gameOver = function() { this.isMoving = false; EHDI.GAME.soundManager.playVO("negative"); EHDI.GAME.soundManager.playSFX("obstacle_hit"); this.isDown = false; this.hand.goTo(this.hand.hand.y); this.bg.interactive = false; EHDI.GAME.pauseButton.hidePauseButton(); this.animateMalfunction(); this.hand.destroyHand(); }; EHDI.scene.GameScene.prototype.victory = function() { this.isMoving = false; this.bg.interactive = false; this.hand.revertHand(); } EHDI.scene.GameScene.prototype.getSheep = function(number) { this.hand.hideHint(); this.reverse = true; this.ignoreInput = true; this.hand.getSheep(number); }; EHDI.scene.GameScene.prototype.endGame = function() { var score = EHDI.GAME.scoreManager.getScore(), star = 0; if(score > 0) star++; if(score > 200) star++; if(score >= 500) star++; 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); }; EHDI.scene.GameScene.prototype.screenDidDisappear = function() { if(this.smokeAnimation) this.smokeAnimation.kill(); EHDI.GAME.pauseButton.endGame(); EHDI.GAME.updateManager.removeFrameListener(this.updateFunction); EHDI.GAME.updateManager.removeFrameListener(this.hand.updateFunction); EHDI.GAME.updateManager.removeFrameListener(this.animateAnimations); dragonBones.WorldClock.clock.remove(this.sparkArmature); dragonBones.WorldClock.clock.remove(this.smokeArmature); dragonBones.WorldClock.clock.remove(this.cogsArmature); } EHDI.scene.GameScene.prototype.pointerDown = function(pointerData) { if(!this.isMoving) return; var yPos = pointerData.data.global.y / EHDI.GAME.scale; /*if(yPos < 90 + (this.hand.hand.height * this.hand.hand.anchor.y) || yPos > EHDI.GAME.sceneManager.getStageHeight() - (this.hand.handCollider.height) - 67.5) return;*/ this.isDown = true; this.hand.goTo(yPos); }; EHDI.scene.GameScene.prototype.pointerUp = function(pointerData) { if(!this.isMoving) return; this.isDown = false; this.hand.goTo(this.hand.hand.y); }; EHDI.scene.GameScene.prototype.move = function(pointerData) { if(!this.isMoving) return; var yPos = pointerData.data.global.y / EHDI.GAME.scale; /*if(this.isDown && yPos > 90 + (this.hand.hand.height * this.hand.hand.anchor.y) && yPos < EHDI.GAME.sceneManager.getStageHeight() - (this.hand.handCollider.height) - 67.5)*/ if(this.isDown) this.hand.goTo(yPos); }; EHDI.scene.GameScene.prototype.popUpWillAppear = function() { if(this.smokeAnimation) this.smokeAnimation.pause(); this.hand.pause(); this.obstacleSpawner.pauseObstacles(); EHDI.GAME.soundManager.pauseBGM(); }; EHDI.scene.GameScene.prototype.popUpDidDisappear = function() { if(EHDI.GAME.pauseButton.isEndGame) return; this.isDown = false; this.hand.resume(); if(!EHDI.GAME.pauseButton.isEndGame && this.smokeAnimation) this.smokeAnimation.play(); this.obstacleSpawner.resumeObstacles(); EHDI.GAME.soundManager.resumeBGM(); } EHDI.scene.GameScene.prototype.updateLayerOrder = function() { this.gameLayer.children.sort(function(a,b) { a.zIndex = a.zIndex || 0; b.zIndex = b.zIndex || 0; return a.zIndex - b.zIndex; }); };