//Augment Module var ITW = (function(itw){ "use strict"; //aliases var components, protot, _this, _super; itw.components = itw.components || Object.create(null); components = itw.components; //CONST var ACCEL_SPEED; var MAX_SPEED; var ROTATE_SPEED = 0.75; //privates - initialize on constructor function var _startPos; var _armature; var _texMain, _sprMain, _collider; var _speed, _moveDown, _botBorder; var _onSurface; //added var _fishState; components.Player = function(){ this.super.call(this); _this = this; _super = _this.super; var _init = function(){ // init stuff here ACCEL_SPEED = ( ITW.SceneMgr.getStageHeight() * 0.75 ) / 1.5; MAX_SPEED = ( ITW.SceneMgr.getStageHeight() * 0.75 ) / 3; _startPos = { x: 0, y: 0 }; _speed = 0; _moveDown = false; _onSurface = true; _botBorder = ITW.SceneMgr.getStageHeight() * 0.9; _this.currentState = "surface"; var _nearCollision = false; _this.isDead = false; _this.tlDeath = null; _this.hadBubbles = false; _this.hadSplash = true; _this.playBubbles; _this.playSplash; _this.tlScale = new TimelineMax({repeat: -1, yoyo: true}); Object.defineProperty(_this, "nearCollision",{ set: function(v){ if(typeof v !== "boolean") return; _nearCollision = v; if(_nearCollision){ this.updateState("surface"); } }, get: function(){ return _nearCollision; } }) var textures = []; for(var i = 0; i < 5; i++){ textures[i] = EHDI.Assets.images["hit_fx000"+i]; } _this.asHit = new EHDI.aka.AnimatedSprite(textures); _this.asHit.animationSpeed = 0.4; _this.asHit.loop = false; _this.asHit.visible = false; _armature = ITW.DBoneFactory.createArmature( "BigFish" ); _this.asHit.y = _armature.display.height * -0.55; _fishState = _armature.animation.gotoAndPlay( "swim3", -1, -1, 0 ); _this.addChild( _armature.display ); _this.addChild( _this.asHit); _this.asHit.onComplete = function(){ _this.asHit.visible = false; } _this.scale.set(0.98,0.98); _this.tlScale.to(_this.scale, 0.5, {x: 1, y: 1, ease: Power1.easeInOut}); _this.armLoopCompleteHandler = function(e){ if(e.movementID === "swim1" && !this.nearCollision){ if(Math.random() < 0.2) this.updateState("blink"); } }.bind(_this); _this.armCompleteHandler = function(e){ if(e.movementID === "swim3_blink" && !this.nearCollision){ if(Math.random() < 0.1){ this.updateState("normal"); this.updateState("blink"); }else{ this.updateState("normal"); } } }.bind(_this); _armature.addEventListener(dragonBones.AnimationEvent.LOOP_COMPLETE, _this.armLoopCompleteHandler); _armature.addEventListener(dragonBones.AnimationEvent.COMPLETE, _this.armCompleteHandler); if( ITW.GameMgr.getDebug() === true ){ var pixiCircle = new EHDI.aka.Graphics(); pixiCircle.beginFill( 0xFF00FF, 0.5 ); pixiCircle.drawCircle( ( _this.width * 0.1 ), ( -( _this.height * 0.02 ) ), ( _this.width * 0.125 ) ); //(x,y,radius) pixiCircle.endFill(); _this.addChild( pixiCircle ); } } _init(); } protot = components.Player.prototype = Object.create(EHDI.aka.Container.prototype); protot.constructor = components.Player; protot.super = EHDI.aka.Container; protot.loop = function( dt ){ if(!!this.isDead) return; _this.adjustSpeed( dt ); _this.movePlayer( dt ); _this.rotatePlayer( dt ); if( _this.y === _startPos.y && _onSurface === false ){ _onSurface = true; if(!_this.hadSplash){ if(typeof _this.playSplash === "function") _this.playSplash(); ITW.SoundMgr.playSFX("whale_emerge"); _this.hadSplash = true; } }else if( _this.y > _startPos.y && _onSurface === true ) _onSurface = false; } /** * @param (STRING) state - the animation state of the whale * [value: surface, normal, danger, death] */ protot.updateState = function( state ){ if(state === this.currentState) return; var currentTime = _fishState.currentTime; this.currentState = state; if(!this._onSurface) this.tlScale.repeat(2); switch(state){ case "surface": _fishState = _armature.animation.gotoAndPlay( "swim3", -1, -1, 0 ); break; case "danger": _fishState = _armature.animation.gotoAndPlay( "swim2", -1, -1, 0 ); break; case "death": _fishState = _armature.animation.gotoAndPlay("death", -1, -1, 1); break; case "blink": _fishState = _armature.animation.gotoAndPlay("swim3_blink", -1, -1, 1); break; case "normal": default: _fishState = _armature.animation.gotoAndPlay( "swim1", -1, -1, 0 ); break; } if(state !== "death"){ _armature.advanceTime(currentTime); if(this._onSurface && !this.tlScale.isActive()){ this.tlScale.repeat(-1); this.tlScale.restart(); } } } /** * @param (BOOL) isCollide - if death is from collision * @param (FUNCTION) callback - callback to be called when animation is done * @param (OBJECT) param - parameters for the callback */ protot.animateDeath = function(isCollide, callback, param){ if(!!_this.isDead) return; var onDone = function(){ if(typeof callback === "function") callback.call(null, param); } _this.isDead = true; this.updateState("death"); this.tlDeath = new TimelineMax(); if(!!isCollide){ this.tlDeath.to(this, 1, {x: this.x - 50, rotation: 0, ease: Back.easeOut.config(2)}); this.asHit.visible = true; this.asHit.gotoAndPlay(0); this.shakeScreen(); ITW.SoundMgr.playSFX("obstacle_hit"); }else{ if(_this.y > (_startPos.y + 90)){ this.tlDeath.to(this, 1, {y: this.y - 90, rotation: 0, ease: Power2.easeOut}) }else this.tlDeath.to(this, 1, {y: this.y + 10, rotation: 0, ease: Power2.easeOut}) ITW.SoundMgr.playSFX("whale_drown"); } this.tlDeath.call(onDone, null, null, "+=0.05"); } protot.shakeScreen = function(){ //shake var gamescene = ITW.SceneMgr.getStage().getChildAt(0).getChildAt(0); var bg = gamescene.getChildAt(0); this.shake1 = TweenMax.to(bg, 0.05, {x: "+=10", yoyo:true, repeat:5}); this.shake2 = TweenMax.to(bg, 0.05, {x: "-=10", yoyo:true, repeat:5}); } protot.adjustSpeed = function( dt ){ if( _moveDown === false ){ _speed -= dt * ACCEL_SPEED; if( _this.y === _startPos.y ){ _speed = 0; } if( _speed < -MAX_SPEED ) _speed = -MAX_SPEED; } else{ _speed += dt * ( ACCEL_SPEED * 0.7 ); if( _speed > MAX_SPEED ) _speed = MAX_SPEED; } } protot.pause = function(){ // this.fishTime = _fishState.currentTime; _fishState.stop(); if(this.tlScale instanceof TimelineMax) { if(!this.tlScale.paused()) this.tlScale.pause(); } if(this.tlDeath instanceof TimelineMax) { if(!this.tlDeath.paused()) this.tlDeath.pause(); } if(this.shake1 instanceof TweenMax){ if(!this.shake1.paused()) this.shake1.pause(); if(!this.shake2.paused()) this.shake2.pause(); } } protot.resume = function(){ _fishState.play(); // _armature.advanceTime(this.fishTime); if(this.tlScale instanceof TimelineMax){ if(this.tlScale.paused()) this.tlScale.resume(); } if(this.tlDeath instanceof TimelineMax){ if(this.tlDeath.paused()) this.tlDeath.resume(); } if(this.shake1 instanceof TweenMax){ if(this.shake1.paused()) this.shake1.resume(); if(this.shake2.paused()) this.shake2.resume(); } } protot.movePlayer = function( dt ){ _this.y += dt * _speed; if( _this.y < _startPos.y ){ _this.y = _startPos.y; _speed = 0; } else if( _this.y > _botBorder ){ _this.y = _botBorder; _speed = 0; } } protot.rotatePlayer = function( dt ){ if( _speed === 0 ){ if( _this.rotation < -0.05 ) _this.rotation += ( ROTATE_SPEED * 1.5 ) * dt; else if( _this.rotation > 0.05 ) _this.rotation -= ( ROTATE_SPEED * 1.5 ) * dt; else _this.rotation = 0; } else{ if( _moveDown === true ){ _this.rotation += dt * ( ROTATE_SPEED * 0.75 ); if( _this.rotation > 0.5 ) _this.rotation = 0.5; if( _this.rotation >= 0.16 && !_this.hadBubbles && typeof _this.playBubbles == "function"){ _this.hadBubbles = true; _this.playBubbles(); if(_this.y < _startPos.y + 20) ITW.SoundMgr.playSFX("whale_dive"); } } else{ _this.rotation -= dt * ROTATE_SPEED; _this.hadBubbles = false; if(_this.y > _startPos.y + _this.height * 0.5)_this.hadSplash = false; if( _this.rotation < -0.3 ) _this.rotation = -0.3; } } } protot.getSurfaceStatus = function(){ return _onSurface; } protot.setStartPos = function( x, y ){ _this.x = _startPos.x = x; _this.y = _startPos.y = y; } protot.setMoveDown = function( status ){ _moveDown = status; } protot.destroy = function(){ if(this.tlDeath) this.tlDeath.kill(); if(this.tlScale) this.tlScale.kill(); if(this.shakeTwn){ this.shake1.kill(); this.shake2.kill(); } this.playBubbles = null; this.playSplash = null; this.jonah = null; _armature.removeEventListener(dragonBones.AnimationEvent.LOOP_COMPLETE, _this.armLoopCompleteHandler); _armature.removeEventListener(dragonBones.AnimationEvent.COMPLETE, _this.armCompleteHandler); ITW.DBoneFactory.destroyArmature( _armature ); _super.prototype.destroy.call(this, {children: true}); } return itw; }(ITW || Object.create(null)));