//Augment Module var DSP = (function(dsp){ "use strict"; //aliases var displays, protot, _super; dsp.displays = dsp.displays || Object.create(null); displays = dsp.displays; var _dirAngles = []; var _tileDist = 100; //private statics - initialize on constructor function var _tipDist = -8; var _maxTimeStep = 2; var _blurIntensity = 1; displays.David = function(){ this.super.call(this); var _this = this; _super = _this.super; //members this.defaultPos = {x: 0, y: 0} this.currentAngle = _dirAngles[3]; this.revolution = 0; this.moveIndex = 0; this.revIndex = 0; this.isMoveArms = false; this.revUp = false; this.moveTimeline = null; this.isOnAttack = false; this.moveUpdateCallback = null; //display // this.armature = (createjs.BrowserDetect.isIOS) ? DSP.DBoneFactory.createArmature("david_idle") : DSP.DBoneFactory.createArmature("david"); this.armature = DSP.DBoneFactory.createArmature("david_idle"); // var _hitFXkeys = Object.keys(EHDI.Assets.fetch("hit_fx").textures); // this.asHit = new EHDI.aka.AnimatedSprite.fromImages(_hitFXkeys); var _hitFX = []; for(var i = 0; i < 6; i++){ _hitFX.push(EHDI.Assets.images["hit_fx000"+i]) } this.asHit = new EHDI.aka.AnimatedSprite(_hitFX); this.asHit.animationSpeed = 0.4; this.asHit.loop = false; this.sprAttk = new EHDI.aka.Sprite(EHDI.Assets.images["david_attack_fx"]); //behaviors this.movePattern = ["david_idle_east", "david_trans_east_to_southeast", "david_idle_southeast", "david_trans_southeast_to_southwest", "david_idle_southwest", "david_trans_southwest_to_west", "david_idle_west", "david_trans_west_to_northwest", "david_idle_northwest", "david_trans_northwest_to_northeast", "david_idle_northeast", "david_trans_northeast_to_east" ]; this.jumpPattern = ["david_attack_jump_east","david_attack_jump_west"]; this.attackPattern = ["david_attack_hit_east","david_attack_hit_west"]; //misc var isBlur = false; if (!EHDI.BrowserInfo.isMobileDevice){ var blurFilter = new EHDI.aka.PixiFilters.BlurFilter(); this.armature.display.filters = [blurFilter]; } Object.defineProperty(this, "enableBlur", { set: function(val){ isBlur = val; if (EHDI.BrowserInfo.isMobileDevice) return; if(!val){ blurFilter.blur = 0; }else blurFilter.blurX = _blurIntensity; }, get: function(){ return isBlur; } }); var armatureLoopCompleteHandler = function(e){ //e.armature //current target armature //e.exMovementID //preceding MovementData id, when you use gotoAndPlay again //e.movementID //the current movementID } var armatureCompleteHandler = function(e){ var timestep = 0; if(e.movementID === _this.movePattern[_this.moveIndex] && !!_this.isMoveArms){ _this.moveIndex++; if(_this.moveIndex >= _this.movePattern.length) _this.moveIndex = 0; if(_this.moveIndex === _this.revIndex) _this.revUp = true; timestep = ((_this.moveIndex & 1) === 1) ? 1 : _maxTimeStep; _this.armature.animation.gotoAndPlay(_this.movePattern[_this.moveIndex], -1,-1, timestep); } } var armatureMoveChange = function(e){ if(_this.sprAttk.visible) _this.sprAttk.visible = false; if(_this.asHit.visible) _this.asHit.visible = false; switch(e.movementID){ case _this.movePattern[1]: case _this.movePattern[2]: _this.currentAngle = _dirAngles[4]; break; case _this.movePattern[3]: case _this.movePattern[4]: _this.currentAngle = _dirAngles[5]; break; case _this.movePattern[5]: case _this.movePattern[6]: _this.currentAngle = _dirAngles[0]; break; case _this.movePattern[7]: case _this.movePattern[8]: _this.currentAngle = _dirAngles[1]; break; case _this.movePattern[9]: case _this.movePattern[10]: _this.currentAngle = _dirAngles[2]; break; case _this.movePattern[11]: case _this.movePattern[0]: _this.currentAngle = _dirAngles[3]; break; case _this.attackPattern[0]: _this.sprAttk.visible = true; _this.asHit.visible = true; _this.asHit.gotoAndPlay(0); _this.asHit.anchor.set(0,1); _this.sprAttk.anchor.set(0.4,1.25); _this.asHit.scale.x = 1; _this.sprAttk.scale.x = 1; break; case _this.attackPattern[1]: _this.asHit.visible = true; _this.sprAttk.visible = true; _this.asHit.gotoAndPlay(0); _this.asHit.anchor.set(0,1); _this.sprAttk.anchor.set(0.05,1.25); _this.asHit.scale.x = -1; _this.sprAttk.scale.x = -1; break; } if(typeof _this.moveUpdateCallback === "function" && _this.attackPattern.indexOf(e.movementID) < 0 && _this.jumpPattern.indexOf(e.movementID) < 0){ _this.moveUpdateCallback(); } } this.shadow = new EHDI.aka.Sprite(EHDI.Assets.images["david_shadow"]); this.shadow.anchor.set(0.5,0.45); this.addChild(this.shadow); this.addChild(this.sprAttk); this.addChild(this.armature.display); this.addChild(this.asHit); this.sprAttk.visible = false; this.enableBlur = false; this.armature.addEventListener(dragonBones.AnimationEvent.LOOP_COMPLETE, armatureLoopCompleteHandler); this.armature.addEventListener(dragonBones.AnimationEvent.COMPLETE, armatureCompleteHandler); this.armature.addEventListener(dragonBones.AnimationEvent.START, armatureMoveChange); // this.armature.addEventListener(dragonBones.AnimationEvent.MOVEMENT_CHANGE, armatureChange); this.disposeArmature = function(){ this.armature.removeEventListener(dragonBones.AnimationEvent.LOOP_COMPLETE, armatureLoopCompleteHandler); this.armature.removeEventListener(dragonBones.AnimationEvent.COMPLETE, armatureCompleteHandler); this.armature.removeEventListener(dragonBones.AnimationEvent.START, armatureMoveChange); // this.armature.removeEventListener(dragonBones.AnimationEvent.MOVEMENT_CHANGE, armatureChange); dsp.DBoneFactory.destroyArmature(this.armature); } } /** * @static */ displays.David.setTileDist = function(num){ _tileDist = num; } /** * @static */ displays.David.setDirArray = function(arr){ _dirAngles = arr; } protot = displays.David.prototype = Object.create(EHDI.aka.Container.prototype) protot.constructor = displays.David; protot.super = EHDI.aka.Container; protot.revCheck = function(){ if(!!this.revUp){ this.revolution++; this.revUp = false; return true; } return false; } protot.createPath = function(){ var jmp = _tileDist * 2.5; var deg = this.currentAngle; var rad = EHDI.NumberUtil.degreeToRadian(deg); var tx = _tipDist * Math.cos(rad), ty = _tipDist * Math.sin(rad), rad = Math.atan2(-ty, -tx), pointx = this.x - jmp * Math.cos(rad), pointy = this.y - jmp * Math.sin(rad) return { x: pointx, y: pointy } } protot.playSFX = function(miss){ if(!!miss){ DSP.SoundMgr.playSFX("david_whiff"); }else{ DSP.SoundMgr.playSFX("david_atk"); } } /** * @param path - the directional path * @param spd - the dur of tween * @param callback - callbackfunction * @param if the move will miss. */ protot.move = function(obj){ var path = obj.path || this.createPath(); if(typeof obj.callback !== "function") obj.callback = this.resetPos.bind(this); this.isMoveArms = false; var dur = obj.spd || 0.75, height_adjust = obj.height || 100, onDone = function(){ if(this.moveTimeline){ this.moveTimeline.kill(); this.moveTimeline = null; } if(!obj.miss){ this.playSFX(); } if(typeof obj.callback === "function") obj.callback.call(null); }.bind(this) this.updateStance("jump"); this.enableBlur = true; if(!obj.miss){ if(!this.moveTimeline){ this.moveTimeline = new TimelineMax({onComplete: onDone}); this.moveTimeline.to(this, dur, {x: path.x, ease: Linear.easeNone}) .to(this, dur * 0.5, {y: this.y - height_adjust, ease: Power1.easeInOut}, dur*0.1) .to(this, dur * 0.2, {y: path.y, ease: Power1.easeOut}, dur*0.8) } }else{ if(!this.moveTimeline)TweenMax.to(this, dur, {x: path.x, y: path.y, ease: Power1.easeOut, onComplete: onDone}); this.playSFX(true); } } protot.attack = function(onComplete){ this.enableBlur = false; this.updateStance("attk"); // this.asHit.onComplete = onComplete; this.isOnAttack = true; this.asHit.onComplete = function(){ if(typeof onComplete === "function") onComplete(); this.isOnAttack = false; onComplete = null; }.bind(this); } /** * @param (STRING) stance - display stances */ protot.updateStance = function(stance){ var dir = 0; if(this.currentAngle === _dirAngles[5] || this.currentAngle === _dirAngles[0] || this.currentAngle === _dirAngles[1]){ dir = 1; } var ac; switch(stance){ case "attk": if(this.armature.animation.movementID === this.attackPattern[dir]) return; ac = this.attackPattern[dir]; break; case "jump": default: if(this.armature.animation.movementID === this.jumpPattern[dir]) return; ac = this.jumpPattern[dir]; break; } this.armature.animation.gotoAndPlay(ac, -1, -1, 1); } protot.setInitialPos = function(x,y){ this.x = this.defaultPos.x = x; this.y = this.defaultPos.y = y; } protot.start = function(moveToNext){ var time = _maxTimeStep; this.revolution = 0; this.isMoveArms = true; this.enableBlur = false; if((this.moveIndex & 1) === 1) this.moveIndex++; if(this.moveIndex >= this.movePattern.length) this.moveIndex = 0; this.revIndex = this.moveIndex; //move on next state if(moveToNext){ // this.moveIndex+=2; this.moveIndex++; time = 1; } // if(this.moveIndex >= this.movePattern.length) this.moveIndex = 0; // this.revIndex = this.moveIndex; this.armature.animation.gotoAndPlay(this.movePattern[this.moveIndex], -1, -1, time); } protot.stop = function(){ this.isMoveArms = false; this.armature.animation.stop(); } protot.pause = function(){ if(this.isOnAttack){ this.asHit.stop(); } } protot.resume = function(){ if(this.isOnAttack){ this.asHit.gotoAndPlay(this.asHit.currentFrame); } } protot.resetPos = function(animate, onComplete){ var complete = function(){ if(typeof onComplete === 'function') onComplete(); this.start(true); }.bind(this) var update = function(){ this.updateStance(); }.bind(this) if(!!animate){ TweenMax.to(this, 0.1, {x: this.defaultPos.x, y: this.defaultPos.y, ease: Power4.easeOut, onComplete: complete, onUpdate: update}); }else{ this.position.set(this.defaultPos.x, this.defaultPos.y); // this.start(); complete(); } } protot.destroy = function(){ this.disposeArmature(); if(this.moveTimeline) this.moveTimeline.kill(); _super.prototype.destroy.apply(this, arguments); } return dsp; }(DSP || Object.create(null)));