1 line
6.1 KiB
JavaScript
1 line
6.1 KiB
JavaScript
define(["entity","transition","timer"],function(e,t,n){var r=e.extend({init:function(e,n){var r=this;this._super(e,n),this.nextGridX=-1,this.nextGridY=-1,this.orientation=Types.Orientations.DOWN,this.atkSpeed=50,this.moveSpeed=120,this.walkSpeed=100,this.idleSpeed=450,this.setAttackRate(800),this.movement=new t,this.path=null,this.newDestination=null,this.adjacentTiles={},this.target=null,this.unconfirmedTarget=null,this.attackers={},this.hitPoints=0,this.maxHitPoints=0,this.isDead=!1,this.attackingMode=!1,this.followingMode=!1},clean:function(){this.forEachAttacker(function(e){e.disengage(),e.idle()})},setMaxHitPoints:function(e){this.maxHitPoints=e,this.hitPoints=e},setDefaultAnimation:function(){this.idle()},hasWeapon:function(){return!1},hasShadow:function(){return!0},animate:function(e,t,n,r){var i=["atk","walk","idle"];o=this.orientation;if(!this.currentAnimation||this.currentAnimation.name!=="death")this.flipSpriteX=!1,this.flipSpriteY=!1,_.indexOf(i,e)>=0&&(e+="_"+(o===Types.Orientations.LEFT?"right":Types.getOrientationAsString(o)),this.flipSpriteX=this.orientation===Types.Orientations.LEFT?!0:!1),this.setAnimation(e,t,n,r)},turnTo:function(e){this.orientation=e,this.idle()},setOrientation:function(e){e&&(this.orientation=e)},idle:function(e){this.setOrientation(e),this.animate("idle",this.idleSpeed)},hit:function(e){this.setOrientation(e),this.animate("atk",this.atkSpeed,1)},walk:function(e){this.setOrientation(e),this.animate("walk",this.walkSpeed)},moveTo_:function(e,t,n){this.destination={gridX:e,gridY:t},this.adjacentTiles={};if(this.isMoving())this.continueTo(e,t);else{var r=this.requestPathfindingTo(e,t);this.followPath(r)}},requestPathfindingTo:function(e,t){return this.request_path_callback?this.request_path_callback(e,t):(log.error(this.id+" couldn't request pathfinding to "+e+", "+t),[])},onRequestPath:function(e){this.request_path_callback=e},onStartPathing:function(e){this.start_pathing_callback=e},onStopPathing:function(e){this.stop_pathing_callback=e},followPath:function(e){e.length>1&&(this.path=e,this.step=0,this.followingMode&&e.pop(),this.start_pathing_callback&&this.start_pathing_callback(e),this.nextStep())},continueTo:function(e,t){this.newDestination={x:e,y:t}},updateMovement:function(){var e=this.path,t=this.step;e[t][0]<e[t-1][0]&&this.walk(Types.Orientations.LEFT),e[t][0]>e[t-1][0]&&this.walk(Types.Orientations.RIGHT),e[t][1]<e[t-1][1]&&this.walk(Types.Orientations.UP),e[t][1]>e[t-1][1]&&this.walk(Types.Orientations.DOWN)},updatePositionOnGrid:function(){this.setGridPosition(this.path[this.step][0],this.path[this.step][1])},nextStep:function(){var e=!1,t,n,r;this.isMoving()&&(this.before_step_callback&&this.before_step_callback(),this.updatePositionOnGrid(),this.checkAggro(),this.interrupted?(e=!0,this.interrupted=!1):(this.hasNextStep()&&(this.nextGridX=this.path[this.step+1][0],this.nextGridY=this.path[this.step+1][1]),this.step_callback&&this.step_callback(),this.hasChangedItsPath()?(t=this.newDestination.x,n=this.newDestination.y,r=this.requestPathfindingTo(t,n),this.newDestination=null,r.length<2?e=!0:this.followPath(r)):this.hasNextStep()?(this.step+=1,this.updateMovement()):e=!0),e&&(this.path=null,this.idle(),this.stop_pathing_callback&&this.stop_pathing_callback(this.gridX,this.gridY)))},onBeforeStep:function(e){this.before_step_callback=e},onStep:function(e){this.step_callback=e},isMoving:function(){return this.path!==null},hasNextStep:function(){return this.path.length-1>this.step},hasChangedItsPath:function(){return this.newDestination!==null},isNear:function(e,t){var n,r,i=!1;return n=Math.abs(this.gridX-e.gridX),r=Math.abs(this.gridY-e.gridY),n<=t&&r<=t&&(i=!0),i},onAggro:function(e){this.aggro_callback=e},onCheckAggro:function(e){this.checkaggro_callback=e},checkAggro:function(){this.checkaggro_callback&&this.checkaggro_callback()},aggro:function(e){this.aggro_callback&&this.aggro_callback(e)},onDeath:function(e){this.death_callback=e},lookAtTarget:function(){this.target&&this.turnTo(this.getOrientationTo(this.target))},go:function(e,t){this.isAttacking()?this.disengage():this.followingMode&&(this.followingMode=!1,this.target=null),this.moveTo_(e,t)},follow:function(e){e&&(this.followingMode=!0,this.moveTo_(e.gridX,e.gridY))},stop:function(){this.isMoving()&&(this.interrupted=!0)},engage:function(e){this.attackingMode=!0,this.setTarget(e),this.follow(e)},disengage:function(){this.attackingMode=!1,this.followingMode=!1,this.removeTarget()},isAttacking:function(){return this.attackingMode},getOrientationTo:function(e){return this.gridX<e.gridX?Types.Orientations.RIGHT:this.gridX>e.gridX?Types.Orientations.LEFT:this.gridY>e.gridY?Types.Orientations.UP:Types.Orientations.DOWN},isAttackedBy:function(e){return e.id in this.attackers},addAttacker:function(e){this.isAttackedBy(e)?log.error(this.id+" is already attacked by "+e.id):this.attackers[e.id]=e},removeAttacker:function(e){this.isAttackedBy(e)?delete this.attackers[e.id]:log.error(this.id+" is not attacked by "+e.id)},forEachAttacker:function(e){_.each(this.attackers,function(t){e(t)})},setTarget:function(e){this.target!==e?(this.hasTarget()&&this.removeTarget(),this.unconfirmedTarget=null,this.target=e):log.debug(e.id+" is already the target of "+this.id)},removeTarget:function(){var e=this;this.target&&(this.target instanceof r&&this.target.removeAttacker(this),this.target=null)},hasTarget:function(){return this.target!==null},waitToAttack:function(e){this.unconfirmedTarget=e},isWaitingToAttack:function(e){return this.unconfirmedTarget===e},canAttack:function(e){return this.canReachTarget()&&this.attackCooldown.isOver(e)?!0:!1},canReachTarget:function(){return this.hasTarget()&&this.isAdjacentNonDiagonal(this.target)?!0:!1},die:function(){this.removeTarget(),this.isDead=!0,this.death_callback&&this.death_callback()},onHasMoved:function(e){this.hasmoved_callback=e},hasMoved:function(){this.setDirty(),this.hasmoved_callback&&this.hasmoved_callback(this)},hurt:function(){var e=this;this.stopHurting(),this.sprite=this.hurtSprite,this.hurting=setTimeout(this.stopHurting.bind(this),75)},stopHurting:function(){this.sprite=this.normalSprite,clearTimeout(this.hurting)},setAttackRate:function(e){this.attackCooldown=new n(e)}});return r}); |