var EHDI = EHDI || Object.create(null); EHDI.GAME = EHDI.GAME || Object.create(null); EHDI.GAME.components = EHDI.GAME.components || Object.create(null); EHDI.GAME.components.Sleeping = function(guard) { this.guard = guard; this.val = false; this.time = 5000; this.activeVision = false; this.actionSprite = new EHDI.aka.Container(); this.sleepSprites = []; for(var i = 0; i < 3; i++) { this.sleepSprites.push(new EHDI.aka.Sprite(EHDI.Assets.images["grd_fx_z"])); this.sleepSprites[i].position.y += 140; this.sleepSprites[i].position.x += 20; this.sleepSprites[i].toRemove = false; this.actionSprite.addChild(this.sleepSprites[i]); } this.sleepSprites[0].scale.set(0.5, 0.5); this.sleepSprites[1].position.y -= (this.sleepSprites[0].height + 20); this.sleepSprites[1].position.x -= 20; this.sleepSprites[1].scale.set(0.75, 0.75); this.sleepSprites[2].position.y -= (this.sleepSprites[1].height + 40); this.sleepSprites[2].position.x += 25; this.defaultPositions = []; for(var i = 0; i < this.sleepSprites.length; i++) { this.defaultPositions.push({ x: this.sleepSprites[i].x, y: this.sleepSprites[i].y }); } this.actionSprite.visible = false; this.actionSprite.pivot.set(this.actionSprite.width/2, this.actionSprite.height/2); guard.actionSpriteContainer.addChild(this.actionSprite); this.timeline = new TimelineMax({delay: 0.3, repeat:-1, yoyo:true}); this.setBaseAnimFunc = function() { this.guard.setAnimation(this.anim, 0); this.guard.gArmature.removeEventListener(dragonBones.AnimationEvent.COMPLETE); } } EHDI.GAME.components.Sleeping.prototype.setAnim = function(playTrans) { this.transitionAnim = this.guard.currentFace.anim.idle_to_sleeping; this.anim = this.guard.currentFace.anim.sleeping; if(playTrans) { this.guard.setAnimation(this.transitionAnim, 1); this.guard.gArmature.addEventListener(dragonBones.AnimationEvent.COMPLETE, this.setBaseAnimFunc, this); } else this.guard.setAnimation(this.anim, 0); } EHDI.GAME.components.Sleeping.prototype.reset = function() { this.stop(); this.val = false; this.timeline = new TimelineMax({delay: 0.3, repeat:-1, yoyo:true});; } EHDI.GAME.components.Sleeping.prototype.update = function() { var timeLeft = this.guard.stateChangeTimer.getTimeLeft(); if(timeLeft <= 2000) { var i = Math.floor(timeLeft/(2001/this.sleepSprites.length)); if(i >= 0) { if(!this.sleepSprites[i].toRemove) // TweenLite.to(this.sleepSprites[i], 0.6, {alpha: 0}); TweenMax.to(this.sleepSprites[i], 0.6, {alpha: 0}); this.sleepSprites[i].toRemove = true; } } } EHDI.GAME.components.Sleeping.prototype.start = function() { if(this.guard.stateChangeTimer.getTimeLeft() > 0) { if(this.timeline) this.timeline.play(); this.guard.stateChangeTimer.start(); } else { this.setVisibleSleepSprites(); this.guard.stateChangeTimer = new EHDI.GAME.Timer(this.guard.setNextBehaviour.bind(this.guard), this.time); } } EHDI.GAME.components.Sleeping.prototype.stop = function() { this.guard.stateChangeTimer.pause(); if(this.timeline) this.timeline.pause(); } EHDI.GAME.components.Sleeping.prototype.setTime = function(time) { this.time = EHDI.GAME.utils.randomInt(time-800, time+800); } EHDI.GAME.components.Sleeping.prototype.showActionSprite = function(val) { this.actionSprite.visible = val; } EHDI.GAME.components.Sleeping.prototype.setVisibleSleepSprites = function() { this.timeline.pause(0, true); this.timeline.remove(); for(var i = 0; i < this.sleepSprites.length; i++) { var posX = this.defaultPositions[i].x; this.sleepSprites[i].x = this.defaultPositions[i].x; this.sleepSprites[i].y = this.defaultPositions[i].y; // TweenLite.to(this.sleepSprites[i], 0.3, {alpha: 1}, "appearsleep"); TweenMax.to(this.sleepSprites[i], 0.3, {alpha: 1}, "appearsleep"); this.timeline.to(this.sleepSprites[i].position, 0.2, {x: posX+10}); this.sleepSprites[i].toRemove = false; } this.timeline.play(); } EHDI.GAME.components.Sleeping.prototype.destroy = function(){ if(this.timeline) this.timeline.kill(); }