Demo entry 6770681

ja

   

Submitted by anonymous on Nov 14, 2018 at 11:09
Language: TypeScript. Code size: 33.1 kB.

class PlayerController extends OtherRoleController {
	public constructor(scene:MapScene, player:Player) {
		super(scene, player);
		var self = this;
		//
		self._player = player;
        self._navAgent = new MapNavAgent(player);
		self._noticeServer = !scene.mapMode.isPve;
		self.InitSkill();
		self.InitSacredBeastSkill();
		self._opRange = GameDataManager.instance.getData<GameFloatConst>("GameFloatConst","constName","OpRange").constValue;

		self.SetTargetMode(MapManager.instance.scene.mapMode.enterPlayTargetMode);
		self.gotoState(self.st_idle);
	}
	
	private _player:Player;
	private _noticeServer:boolean;
	private _navAgent:MapNavAgent;
	private _autoSelNormalSkillCount:number;
	private _normalSkillId:number;
	private _chgSkillId:number;
	private _chgSkillDist:number;
	private _skillIds:number[];
	private _sacredBeastSkillIds:number[];
	private _autoSelSkillIdxList:number[];
	private _autoSelSkillIdx:number = 0;
	private _selNormalRemainCount:number = 0;
	private _sacredBeastSKillManager:SkillManager;
	private _sacredBeastSKillCd:number;
	private _sacredBeastSKillCdTimer:number;

	private _modeSpecialSKillManager:SkillManager;

	private _jointAttackSKillManager:SkillManager;

	private _opRange:number;
	
	public InitSkill():void {
		var self = this;
		self._normalSkillId = -1;
		self._chgSkillId = self._player.ROLE_CHG_SKILL_ID;
		self._chgSkillDist = self._player.ROLE_CHG_AVAILIBLE_DIS;
		self._skillIds = [];
		let allSillls = self._unit.skillManager.skillIds;
		for(let sid of allSillls) {
			let sconfig = GameDataManager.instance.getData<GameSkillConfig>("GameSkillConfig","skillId",sid.toString());
			if(sconfig.skillType == SkillType.Normal) {
				self._normalSkillId = sid;
			}
			else if(sconfig.skillType == SkillType.Skill && sid != self._chgSkillId) {
				self._skillIds.push(sid);
			}
		}
		//
		let autoSelConfig = GameDataManager.instance.getData<RoleAutoSkillSelConfig>("RoleAutoSkillSelConfig","unlockSkillCount",self._skillIds.length.toString());
		self._autoSelNormalSkillCount = autoSelConfig ? autoSelConfig.normalAttackCount : 1;
	}

	public InitSacredBeastSkill():void {
		var self = this;
		if(!MapManager.instance.scene.mapMode) return;
		
		self._sacredBeastSkillIds = [];
		let sacredBeastSkillCombatDataMap = {};
		if(self._player.sacredBeastList) {
			for(let beast of self._player.sacredBeastList) {
				self._sacredBeastSKillCdTimer = 0;
				if(!MapManager.instance.scene.mapMode.isPve && self._player.id == App.GameManager.Role.roleId)
					self._sacredBeastSKillCdTimer = App.GameManager.Role.SacredBeastManager.skillCommonCdTime - egret.getTimer() / 1000;
				if(beast && beast.isActive()) {
					let skillAddDmgPercent = App.GameManager.Role.CeletialManager.GetGradeImpactValue(self._player.roleBattleInfo.celetialInfo, CeletialActivateType.SACREDBEAST_ADD_DAMAGE_PERCENT,beast.id)+Math.floor(beast.skill.damageCof*100);
					let skillReduceCd = App.GameManager.Role.CeletialManager.GetGradeImpactValue(self._player.roleBattleInfo.celetialInfo, CeletialActivateType.SACREDBEAST_SKILLCD_TIME);
					self._sacredBeastSkillIds.push(beast.config.skillId);
					let skillCombatData = new SkillCombatData();
					sacredBeastSkillCombatDataMap[beast.config.skillId] = skillCombatData;
					skillCombatData.addDmg = beast.totalExtDamage;
					skillCombatData.addDmgCof = skillAddDmgPercent / 100;
					skillCombatData.cd = beast.config.skillCd - skillReduceCd;
				}
			}
		}
		//
		self._sacredBeastSKillCd =  GameDataManager.instance.getData<GameFloatConst>("GameFloatConst","constName","SacredBeastCommonCd").constValue;
		if(self._sacredBeastSkillIds.length > 0) {
			if(!self._sacredBeastSKillManager)
				self._sacredBeastSKillManager = new SkillManager();
			self._sacredBeastSKillManager.init(self._unit, self._sacredBeastSkillIds, sacredBeastSkillCombatDataMap);
		}
		else if(self._sacredBeastSKillManager) {
			self._sacredBeastSKillManager.Refresh(null, null);
		}
	}

	public UpdateSacredBeastSkillCombatData():void {
		var self = this;
		if(!self._player || !self._sacredBeastSKillManager || !MapManager.instance.scene.mapMode)
			return;
		if(self._player.sacredBeastList) {
			for(let beast of self._player.sacredBeastList) {
				self._sacredBeastSKillCdTimer = 0;
				if(!MapManager.instance.scene.mapMode.isPve && self._player.id == App.GameManager.Role.roleId)
					self._sacredBeastSKillCdTimer = App.GameManager.Role.SacredBeastManager.skillCommonCdTime - egret.getTimer() / 1000;
				if(beast && beast.isActive()) {
					let skillAddDmgPercent = App.GameManager.Role.CeletialManager.GetGradeImpactValue(self._player.roleBattleInfo.celetialInfo, CeletialActivateType.SACREDBEAST_ADD_DAMAGE_PERCENT,beast.id)+Math.floor(beast.skill.damageCof*100);
					let skillReduceCd = App.GameManager.Role.CeletialManager.GetGradeImpactValue(self._player.roleBattleInfo.celetialInfo, CeletialActivateType.SACREDBEAST_SKILLCD_TIME);
					let skillCombatData = new SkillCombatData();
					skillCombatData.addDmg = beast.totalExtDamage;
					skillCombatData.addDmgCof = skillAddDmgPercent / 100;
					skillCombatData.cd = beast.config.skillCd - skillReduceCd;
					self._sacredBeastSKillManager.UpdateSkillCombatData(beast.skill.skillId, skillCombatData);
				}
			}
		}
	}

	public InitModeSpecialSkillInfo(skillIds:number[]):void {
		var self = this;
		if(self._player != MapManager.instance.scene.mc)
			return;
		if(!self._modeSpecialSKillManager) {
			self._modeSpecialSKillManager = new SkillManager();
		}
		self._modeSpecialSKillManager.init(self._player, skillIds, null);
	}

	public InitJointAttackSkillInfo():void {
		var self = this;
		if(self._player != MapManager.instance.scene.mc)
			return;
		if(!self._jointAttackSKillManager) {
			self._jointAttackSKillManager = new SkillManager();
		}
		self._jointAttackSKillManager.init(self._player, [1101], null);
	}

	public NotifyDoSacredBeastSkill(skillId:number, target:GameUnit):void {
		this._sacredBeastSKillManager.notifyServerDoSkill(skillId, SkillDmgType.SB, target);
	}

	public DoSacredBeastSkill(skillId:number, target:GameUnit, effectList:SkillEffectMacro[]):void {
		this._sacredBeastSKillManager.doSkill(skillId, target, effectList);
	}

	public DoSacredBeastSkillFailed(skillId:number):void {
		this._sacredBeastSKillCdTimer = 0;
		this._sacredBeastSKillManager.doSkillFailed(skillId);
	}

	public DoModeSpecialSkill(skillId:number, target:GameUnit, effectList:SkillEffectMacro[]):void {
		if(this._modeSpecialSKillManager) {
			this._modeSpecialSKillManager.doSkill(skillId, target, effectList);
		}
	}
	
	public DoModeSpecialSkillFailed(skillId:number):void {
		if(this._modeSpecialSKillManager)
			this._modeSpecialSKillManager.doSkillFailed(skillId);
	}

	//join attack.
	private requestDoJoinAttackSkill(skillId: number, target: GameUnit):void {
		let msg: any = {};
		msg.key = "CgPlayJointAttackSkill";
		let body: CgPlayJointAttackSkill = <CgPlayJointAttackSkill>{}
		msg.body = body;
		body.targetId = target.id;
		body.targetType = target.unitType;
		
		App.Socket.send(msg);
	}
	public DoJoinAttackSkill(skillId:number, target:GameUnit, effectList:SkillEffectMacro[]):void {
		this._jointAttackSKillManager.doSkill(skillId, target, effectList);
	}

	public DoJoinAttackSkillFailed(skillId:number):void {
		this._jointAttackSKillManager.doSkillFailed(skillId);
	}
	//
	
	private _willDoIsJoinAttack:boolean;
    private _lastDoSpecialSkillTime: number = 0;
	private _willDoSpecialSkill:number = 0;
	private _willDoSpecialSkillTargetType: number = 0;
	private _willDoSpecialSkillTargetId: number = 0;
	private _doSpecialSkillCallBack:Function;
	private _doSpecialSkillCaller:any;
    public TryDoSpecialSkill(skillId: number, targetType: unitType, targetId: number, isJoinAttack?:boolean, callBack?:Function, caller?:any): void {
        var self = this;
        //
        let skillData = GameDataManager.instance.getData<GameSkillConfig>("GameSkillConfig", "skillId", skillId.toString());
        if (!skillData)
            return;
        if (skillData.affectTargetType == SkillAffectTargetType.Self) {
            targetType = unitType.player;
            targetId = App.GameManager.Role.roleId;
        }
        else if (targetType <= 0 || targetId <= 0) {
			if(skillData.selTargetType == SkillSelTargetType.All) {
				targetType = unitType.player;
            	targetId = App.GameManager.Role.roleId;
			}
			else {
				UIEffectManager.Instance.ShowOpTipsEffect(Localization.Get("MODE_PVPTOWER_SKILL_ERROR_NOT_SEL_TARGET"));
				return;
			}
        }
        if (skillData.selTargetType != SkillSelTargetType.All) {
            if (skillData.selTargetType == SkillSelTargetType.Monster && targetType != unitType.monster) {
                UIEffectManager.Instance.ShowOpTipsEffect(Localization.Get("MODE_PVPTOWER_SKILL_ERROR_NOT_MONSTER"));
                return;
            }
            if (skillData.selTargetType == SkillSelTargetType.Player && targetType != unitType.player) {
                UIEffectManager.Instance.ShowOpTipsEffect(Localization.Get("MODE_PVPTOWER_SKILL_ERROR_NOT_PLAYER"));
                return;
            }
        }
        
        if(skillData.affectTargetType != SkillAffectTargetType.Self /*&& skillData.selTargetType != SkillSelTargetType.All*/ && targetId > 0) {
			let target = self._scene.getUnit(targetType, targetId);
			if(!self._unit.isEnemy(target)) return;
			if(!self._unit.seledTarget || self._unit.unitType != targetType || self._unit.seledTarget.id != targetId) {
				if(!target) return;
				self._unit.setSeledTarget(target);
			}
            if(!self._unit.seledTargetInAttackRange()) {
				UIEffectManager.Instance.ShowOpTipsEffect(Localization.Get("SPECIAL_SKILL_NOT_RANGE"));
				if(self._willDoSpecialSkill == skillId && self._willDoSpecialSkillTargetType == targetType && self._willDoSpecialSkillTargetId == targetId) {
					return;
				}
                self.moveTo(self._unit.seledTarget.x, self._unit.seledTarget.y);
				self._willDoSpecialSkillTargetType = targetType;
				self._willDoSpecialSkillTargetId = targetId;
				self._willDoSpecialSkill = skillId;
				self._willDoIsJoinAttack = isJoinAttack;
				self._doSpecialSkillCallBack = callBack;
				self._doSpecialSkillCaller = caller;
				return;
            }
        }
		//
		self._willDoSpecialSkill = 0;
		if(isJoinAttack) {
			let target = self._scene.getUnit(targetType, targetId);
			let option = {notifyServerCall:self.requestDoJoinAttackSkill, notifyServerCaller:self, preEffectType:SkillPreEffectType.JoinAttack};
			if(self._jointAttackSKillManager.notifyServerDoSkill(1101, SkillDmgType.JOINT, target, option)) {
				if(callBack && caller) {
					callBack.call(caller);
				}
			}
		}
		else {
			if(self.requestDoSpecialSkill(skillId, targetType, targetId)) {
				if(callBack && caller) {
					callBack.call(caller);
				}
			}
		}
    }
	private checkWillDoSpecialSkill():boolean {
		var self = this;
		let isDo:boolean = false;
		if(self._willDoSpecialSkill > 0) {
			if(self._unit.seledTarget && !self._unit.seledTarget.isDead() && self._unit.seledTarget.isInScene()) {
				if(self._unit.seledTarget.unitType == self._willDoSpecialSkillTargetType && self._unit.seledTarget.id == self._willDoSpecialSkillTargetId) {
					isDo = true;
					if(self._willDoIsJoinAttack) {
						let option = {notifyServerCall:self.requestDoJoinAttackSkill, notifyServerCaller:self, preEffectType:SkillPreEffectType.JoinAttack};
						if(self._jointAttackSKillManager.notifyServerDoSkill(1101, SkillDmgType.JOINT, self._unit.seledTarget, option)) {
							if(self._doSpecialSkillCallBack && self._doSpecialSkillCaller) {
								let callback = self._doSpecialSkillCallBack;
								let caller = self._doSpecialSkillCaller;
								self._doSpecialSkillCallBack = null;
								self._doSpecialSkillCaller = null;
								callback.call(caller);
							}
						}
					}
					else {
						if(self.requestDoSpecialSkill(self._willDoSpecialSkill, self._unit.seledTarget.unitType, self._unit.seledTarget.id)) {
							if(self._doSpecialSkillCallBack && self._doSpecialSkillCaller) {
								let callback = self._doSpecialSkillCallBack;
								let caller = self._doSpecialSkillCaller;
								self._doSpecialSkillCallBack = null;
								self._doSpecialSkillCaller = null;
								callback.call(caller);
							}
						}
					}
				}
			}
			self._willDoSpecialSkill = 0;
		}
		return isDo;
	}
	private cancelDoSpecialSkill():void {
		if(this._willDoSpecialSkill > 0) {
			UIEffectManager.Instance.ShowOpTipsEffect(Localization.Get("SPECIAL_SKILL_CANCEL"));
			this._willDoSpecialSkill = 0;
		}
	}
	private requestDoSpecialSkill(skillId: number, targetType: number, targetId: number):void {
		var self = this;

        let now = egret.getTimer() / 1000;
        if (self._lastDoSpecialSkillTime > now)
            return;
		
		let msg: any = {};
		if(MapManager.instance.scene.mapMode && MapManager.instance.scene.mapMode instanceof MapModePvp) {
			MapManager.instance.scene.mapMode.RequestDoSpecialSkill(skillId, targetType, targetId);
		}
	}
	
	private autoSelSkillId():number {
		var self = this;

		let remainSkillCount = 0;
		if(self._autoSelSkillIdxList) {
			remainSkillCount = self._autoSelSkillIdxList.length - self._autoSelSkillIdx;
			if(remainSkillCount < 0)
				remainSkillCount = 0;
		}

		let totalCount = remainSkillCount + self._selNormalRemainCount;
		if(totalCount <= 0) {
			self._autoSelSkillIdxList = GeoTools.GetRandoms(self._skillIds.length, self._skillIds.length);
			self._autoSelSkillIdx = 0;
			self._selNormalRemainCount = self._autoSelNormalSkillCount;
			remainSkillCount = self._autoSelSkillIdxList.length;
		}
		let seledId = -1;
		if(remainSkillCount > 0) {
			if(self._selNormalRemainCount <= 0 || Math.random() < remainSkillCount / totalCount) {
				seledId = self._skillIds[self._autoSelSkillIdxList[self._autoSelSkillIdx]];
				self._autoSelSkillIdx++;
			}
			else {
				seledId = self._normalSkillId;
				if(self._selNormalRemainCount > 0)
					self._selNormalRemainCount--;
			}
		}
		else if(self._selNormalRemainCount > 0) {
			seledId = self._normalSkillId;
			self._selNormalRemainCount--;
		}
		
		return seledId;
	}

	private autoSelSacredBeastSkillId():number {
		var self = this;
		if(!self._sacredBeastSKillManager) {
			return -1;
		}
		for(let skill of self._sacredBeastSkillIds) {
			if(!self._sacredBeastSKillManager.isInSkillCd(skill)) {
				return skill;
			}
		}
		return -1;
	}

	public notifyHitBack(caster:GameUnit):void {
		var self = this;
		if(!(self._opProtectTimer <= 0 || self.isMoveStop()))
			return;
		if (self._willDoAction_move)
			return;
		if(self._targetMode == PlayTargetMode.Pk || !self._unit.isEnemy(caster)) {
			return;
		}
		if(self._unit.seledTarget && !self._unit.seledTarget.isDead() && self._unit.seledTarget.isInScene()) {
			let needReChoice:boolean = false;
			if(!(self._unit.seledTarget instanceof PlayerNpc) && caster instanceof PlayerNpc) {
				needReChoice = true;
			}
			//
			if(!needReChoice)
				return;
		}
		self._player.choiceTarget(caster);
	}
	
    private _opProtectTimer: number = 0;
    private refreshOpProtectTimer(): void {
        this._opProtectTimer = 2;
    }
	
	private _willDoAction_move: boolean = false;
    private _willMove_x: number;
    private _willMove_y: number;
    protected UpdateWillDoAction() {
        var self = this;

        if (self._willDoAction_move) {
            self._unit.setSeledTarget(null);
			if(self._player)
				self._player.selMapObj(null);
            self._willDoAction_move = false;
            self.SetTargetMode(PlayTargetMode.Move);
            self.moveTo(self._willMove_x, self._willMove_y);
            if(self._player && self._player.comMount && self._player.comMount.hasMount && !self._player.comMount.isRide) {
                self._player.comMount.UpMount();
            }
            self.refreshOpProtectTimer();
        }
    }

	private _moveTargetTimer:number = 0;
	public moveTo(x:number, y:number):void {
        var self = this;
		
		if(self._unit.IsDizz() || !self._unit.CanMove())
			return;

		if(self._unit.isPlayingSkill()) {
			self._willDoAction_move = true;
			self._willMove_x = x;
			self._willMove_y = y;
			return;
		}
		
		if(self._navAgent.setTarget(self._unit.x, self._unit.y, x, y))
			self.gotoState(self.st_move);
		else 
			self.gotoState(self.st_idle);
		self._moveTargetTimer = 1;
		self.cancelDoSpecialSkill();
    }

	public stopMove(nosyncServer?:boolean):void {
		var self = this;
		if(!self._navAgent.isMoveStop()) {
			self._navAgent.stopMove();
			//nodtify remote sever stop move.
			if(!nosyncServer && self._noticeServer)
				self.SendMoveStop();
		}
		
		self.gotoState(self.st_idle);
	}

	public doDizz():void {
		var self = this;
		self.stopMove();
	}
	
	public isMoveStop():boolean { return this._navAgent.isMoveStop()}

	public ChangeMoveTarget(x:number, y:number):void {
		var self = this;
		if (self._willDoAction_move) {
            self._willDoAction_move = false;
        }
		//nodtify remote sever change move dir.
		if(self._noticeServer)
			self.SendMoveChangeDir(x, y);
	}

	public MoveTarget():egret.Point {
		var self = this;
		if(self._navAgent && !self._navAgent.isMoveStop()) return self._navAgent.moveTarget;

		return null;
	}

	public update(dt:number):void {
		var self = this;

        if(self._unit == null)
            return;
        if(self._sacredBeastSKillCdTimer > 0) {
			self._sacredBeastSKillCdTimer -= dt;
		}
		if(self._sacredBeastSKillManager)
			self._sacredBeastSKillManager.update(dt);
		if(self._modeSpecialSKillManager)
			self._modeSpecialSKillManager.update(dt);
		if(self._jointAttackSKillManager)
			self._jointAttackSKillManager.update(dt);
		
		let autoAttack = self._isRun && !self._unit.isDead() && !MapManager.instance.scene.isAiPause;
        if(!self._navAgent.isMoveStop()) {
            self._navAgent.update(dt);
			if(!autoAttack && self._navAgent.isMoveStop())
				self.stopMove();
        }
		//
		//autoAttack = false;
		if(autoAttack)
			self.runStateMachine(dt);
	}

	private _lastSeledTargetX:number;
	private _lastSeledTargetY:number;
    private seledTargetInFindRange(range?:number):boolean {
        var self = this;
		
		if(!range) range = self._opRange;
        let dx = self._lastSeledTargetX - self._unit.x;
        let dy = self._lastSeledTargetY - self._unit.y;
        self._unit.seledTargetDist = Math.floor(Math.sqrt(dx*dx + dy*dy));
        self._unit.dir2SeledTarget.setTo(dx / self._unit.seledTargetDist, dy / self._unit.seledTargetDist);
        
		return self._unit.seledTargetDist <= range;
    }

	private _targetMode:PlayTargetMode = PlayTargetMode.Wait;
	public get curTargetMode() {return this._targetMode;}
	public SetTargetMode(mode:PlayTargetMode):void {
		var self = this;
		switch(mode) {
			case PlayTargetMode.Wait:
				break;
			case PlayTargetMode.Move:
				break;
			case PlayTargetMode.LockMonster:
				break;
			case PlayTargetMode.Task:
				break;
			case PlayTargetMode.Find:
				break;
			case PlayTargetMode.Pk:
				break;
			case PlayTargetMode.OpenTreasure:
				break;
			case PlayTargetMode.RepairBuild:
				break;
		}

        self.refreshOpProtectTimer();
		self._willDoAction_move = false;
		self._targetMode = mode;
	}

	private SelTarget():void {
		var self = this;
		if(self._willDoAction_move) {
			self.UpdateWillDoAction();
			return;
		}
		//
		self.cancelDoSpecialSkill();
		switch(self._targetMode) {
			case PlayTargetMode.Wait:
				self._unit.setSeledTarget(null);
				break;
			case PlayTargetMode.Move:
				self.SetTargetMode(MapManager.instance.scene.mapMode.defaltPlayTargetMode);
				break;
			case PlayTargetMode.LockMonster:
				if(MapManager.instance.scene.mapMode.needLockMonster)
					self._unit.setSeledTarget(self._unit.selMonsterTarget(self._player.choiceMonsterTmpName));
				else {
					self.SetTargetMode(MapManager.instance.scene.mapMode.defaltPlayTargetMode);
					self._unit.setSeledTarget(null);
				}
				break;
			case PlayTargetMode.Task:
				self._unit.setSeledTarget(MapManager.instance.scene.mapMode.playerAutoSelTarget(self._unit));
				break;
			case PlayTargetMode.Find:
				if(self._unit.seledTarget && !self._unit.seledTarget.isDead() && self._unit.seledTarget.isInScene())
					self.SetTargetMode(PlayTargetMode.Wait);
				else 
					self.SetTargetMode(MapManager.instance.scene.mapMode.defaltPlayTargetMode);
				self._unit.setSeledTarget(null);
				break;
			case PlayTargetMode.Pk:
				self.SetTargetMode(MapManager.instance.scene.mapMode.defaltPlayTargetMode);
				self._unit.setSeledTarget(null);
				break;
			case PlayTargetMode.OpenTreasure:
				if(self._player && self._player.isOpeningTreasure && self._player.seledOpMapObj) {
				}
				else if(!self._player.seledOpMapObj) {
					self.SetTargetMode(MapManager.instance.scene.mapMode.defaltPlayTargetMode);
				}
				break;
			case PlayTargetMode.RepairBuild:
				if(self._player && self._player.isRepairingBuild && self._player.seledOpMapObj) {
				}
				else if(!self._player.seledOpMapObj) {
					self.SetTargetMode(MapManager.instance.scene.mapMode.defaltPlayTargetMode);
				}
				break;
		}
	}
	
	private CheckTarget():void {
		var self = this;
        if (!self._unit.isPlayingSkill()) {
			if(self._willDoAction_move) {
				self.UpdateWillDoAction();
				return;
			}
		}
		//
		switch(self._targetMode) {
			case PlayTargetMode.Wait:
			case PlayTargetMode.LockMonster:
			case PlayTargetMode.Task:
			case PlayTargetMode.Pk:
				if(self.CheckGodShieldSkill()) break;
				if(self.CheckChg()) break;
				if(self._unit.seledTarget != null) {
					if(!self._unit.seledTargetInAttackRange()) {
						self.moveTo(self._unit.seledTarget.x, self._unit.seledTarget.y);
					}
					else if(self._stTimer <= 0) {
						self.ProcessTarget();
					}
				}
				break;
			case PlayTargetMode.Find:
				if(self.CheckGodShieldSkill()) break;
				if(self.CheckChg()) break;
				if(!self.seledTargetInFindRange(200)) {
					self.moveTo(self._lastSeledTargetX, self._lastSeledTargetY);
				}
				else if(self._stTimer <= 0) {
					self.ProcessTarget();
				}
				break;
			case PlayTargetMode.OpenTreasure:
				if(self._player && self._player.isOpeningTreasure && self._player.seledOpMapObj) {
				}
				else {
					if(!self.seledTargetInFindRange()) {
						self.moveTo(self._lastSeledTargetX, self._lastSeledTargetY);
					}
					else if(self._stTimer <= 0) {
						self.ProcessTarget();
					}
				}
				break;
			case PlayTargetMode.RepairBuild:
				if(self._player && self._player.isRepairingBuild && self._player.seledOpMapObj) {
				}
				else {
					if(!self.seledTargetInFindRange()) {
						self.moveTo(self._lastSeledTargetX, self._lastSeledTargetY);
					}
					else if(self._stTimer <= 0) {
						self.ProcessTarget();
					}
				}
				break;
		}
	}

	private CheckMoveTarget():void {
		var self = this;
		switch(self._targetMode) {
			case PlayTargetMode.Wait:
			case PlayTargetMode.Move:
				if(self._navAgent.isMoveStop())
					self.stopMove();
				break;
			case PlayTargetMode.LockMonster:
			case PlayTargetMode.Pk:
			case PlayTargetMode.Task:
				if(self.CheckGodShieldSkill()) break;
				if(self.CheckChg()) break;
				let inAttackRange = self._unit.seledTargetInAttackRange();
				if(inAttackRange) {
					self.stopMove();
					self.ProcessTarget();
				}
				else if(self._navAgent.isMoveStop() || self._moveTargetTimer <= 0) {
					if(self._unit.seledTarget == null || self._unit.seledTarget.isDead() || !self._unit.seledTarget.isInScene()) {
						self.stopMove();
					}
					else {
						let dx = self._navAgent.moveTarget.x - self._unit.seledTarget.x;
						let dy = self._navAgent.moveTarget.y - self._unit.seledTarget.y;
						let distsqr = dx*dx+dy*dy;
						if(distsqr >= 50 * 50) 
						{
							/*if(!self._navAgent.isMoveStop()) {
								self._navAgent.stopMove();
								//nodtify remote sever stop move.
								if(self._noticeServer)
									self.SendMoveStop();
							}*/
							self.moveTo(self._unit.seledTarget.x, self._unit.seledTarget.y);
						}
					}
				}
				break;
			case PlayTargetMode.Find:
				if(self.CheckGodShieldSkill()) break;
				if(self.CheckChg()) break;
				if(self.seledTargetInFindRange()) {
					self.stopMove();
					self.ProcessTarget();
				}
				else if(self._navAgent.isMoveStop() || self._moveTargetTimer <= 0) {
					/*if(!self._navAgent.isMoveStop()) {
						self._navAgent.stopMove();
						//nodtify remote sever stop move.
						if(self._noticeServer)
							self.SendMoveStop();
					}*/
					self.moveTo(self._lastSeledTargetX, self._lastSeledTargetY);
				}
				break;
			case PlayTargetMode.OpenTreasure:
				if(self._player && self._player.isOpeningTreasure && self._player.seledOpMapObj) {
				}
				else {
					if(self.seledTargetInFindRange()) {
						self.stopMove();
						self.ProcessTarget();
					}
					else if(self._navAgent.isMoveStop() || self._moveTargetTimer <= 0) {
						/*if(!self._navAgent.isMoveStop()) {
							self._navAgent.stopMove();
							//nodtify remote sever stop move.
							if(self._noticeServer)
								self.SendMoveStop();
						}*/
						self.moveTo(self._lastSeledTargetX, self._lastSeledTargetY);
					}
				}
				break;
			case PlayTargetMode.RepairBuild:
				if(self._player && self._player.isRepairingBuild && self._player.seledOpMapObj) {
				}
				else {
					if(self.seledTargetInFindRange()) {
						self.stopMove();
						self.ProcessTarget();
					}
					else if(self._navAgent.isMoveStop() || self._moveTargetTimer <= 0) {
						self.moveTo(self._lastSeledTargetX, self._lastSeledTargetY);
					}
				}
				break;
		}
	}

	private ProcessTarget():void {
		var self = this;
		if(!MapManager.instance.scene.mapMode || MapManager.instance.scene.mapMode.isHideMode) {
			if(self._unit.seledTarget) self._unit.seledTarget.setSeledTarget(null);
			return;
		}
		if(self._unit.seledTarget && !self._unit.seledTarget.canSelOnMap()) {
			self._unit.seledTarget.setSeledTarget(null);
			return;
		}

		switch(self._targetMode) {
			case PlayTargetMode.Wait:
			case PlayTargetMode.Move:
			case PlayTargetMode.LockMonster:
			case PlayTargetMode.Pk:
			case PlayTargetMode.Task:
				if(self._unit.seledTarget) {
					if(self._sacredBeastSKillManager) {
						if(self._sacredBeastSKillCdTimer <= 0 && self._sacredBeastSKillManager.canDoSkill()) {
							let sid = self.autoSelSacredBeastSkillId();
							if(sid > 0) {
								self._sacredBeastSKillCdTimer = self._sacredBeastSKillCd;
								self._sacredBeastSKillManager.notifyServerDoSkill(sid, SkillDmgType.SB, self._unit.seledTarget);
							}
						}
					}
				}
				if(self._unit.seledTarget && self._unit.skillManager.canDoSkill()) {
					self.gotoState(self.st_attack);
				}
				break;
			case PlayTargetMode.Find:
				break;
			case PlayTargetMode.OpenTreasure:
				if(self._player && self._player.seledOpMapObj) {
					if(self._player.seledOpMapObj instanceof MapCollect)
						self._player.seledOpMapObj.tryOpen(self._player);
				}
				break;
			case PlayTargetMode.RepairBuild:
				if(self._player && self._player.seledOpMapObj && self._player.seledOpMapObj instanceof Monster && self._player.seledOpMapObj.isBuilding) {
					self._player.seledOpMapObj.tryRepair(self._player);
				}
				break;
		}
	}

	private st_idle:number = 1;
	private st_move:number = 2;
	private st_attack:number = 3;
	private _st:number;
	private _stTimer:number = 0;
	private gotoState(st:number):void {
		var self = this;
		if(self._comMount.isPlaying) {
			return;
		}
		//
		switch(st) {
			case self.st_idle:
				self._unit.playAnim("idle", "");
				break;
			case self.st_move:
				if(self._comMount && self._comMount.hasMount && !self._comMount.isRide) {
					self._comMount.UpMount();
				}
				break;
			case self.st_attack:
				if(self._comMount && self._comMount.hasMount) {
					self._comMount.ResetDownMountTime();
					if(self._comMount.isRide) {
						self._comMount.DownMount();
						return;
					}
				}
				if(!self.checkWillDoSpecialSkill()) {
					let r = self._unit.skillManager.notifyServerDoSkill(self.autoSelSkillId(), SkillDmgType.ROLE, self._unit.seledTarget);
				}
				break;
		}
		self._st = st;
	}

	private runStateMachine(dt:number):void {
		var self = this;
		if(self._unit.isDead())
			return;
		
        if (self._opProtectTimer > 0) {
            self._opProtectTimer -= dt;
		}
		if(self._stTimer > 0) {
			self._stTimer -= dt;
		}
		if(self._moveTargetTimer > 0) {
			self._moveTargetTimer -= dt;
		}
		
		if(self._targetMode == PlayTargetMode.OpenTreasure || self._targetMode == PlayTargetMode.RepairBuild) {
			if(self._player && self._player.seledOpMapObj) {
				self._lastSeledTargetX = self._player.seledOpMapObj.x;
				self._lastSeledTargetY = self._player.seledOpMapObj.y;
			}
		}
        else if(self._unit.seledTarget)
		{
			self._lastSeledTargetX = self._unit.seledTarget.x;
			self._lastSeledTargetY = self._unit.seledTarget.y;
		}
		switch(self._st) {
			case self.st_idle:
				if(self._unit.IsDizz() || !self._unit.CanMove())
					break;
				if(self._unit.seledTarget == null || self._unit.seledTarget.isDead() || !self._unit.seledTarget.isInScene()) {
					self.SelTarget();
				}
				self.CheckTarget();
				break;
			case self.st_move:
				self.CheckMoveTarget();
				break;
			case self.st_attack:
				if(!self._unit.isPlayingSkill()){
					if(self._unit.IsDizz() || !self._unit.CanMove()) {
						self.gotoState(self.st_idle);
					}
					else {
						if(self._unit.seledTarget == null || self._unit.seledTarget.isDead() || !self._unit.seledTarget.isInScene()) {
							self.SelTarget();
						}
						self.CheckTarget();
					}
				}
				break;
		}
	}

	private CheckChg():boolean {
		let self = this;
		if(self._unit.IsDizz() || !self._unit.CanMove()) return false;
		

		let isUnlocked = self._unit.property.GetValue(CombatPropertyType.CHGT) > 0;
		if(!isUnlocked || !self._unit.seledTarget || !self._unit.skillManager.hasSkill(self._chgSkillId) || self._unit.skillManager.isInSkillCd(self._chgSkillId)) return false;
		let inAttackRange = self._unit.seledTargetInAttackRange(self._chgSkillDist);
		if(inAttackRange) {
			self.stopMove();
			self._unit.skillManager.notifyServerDoSkill(self._chgSkillId, SkillDmgType.CHG, self._unit.seledTarget);
			self._st = self.st_attack;
			return true;
		}
		return false;
	}
	
	private _canDoGodShieldSkill:boolean = false;
	private _canDoGodShieldSkillOverTime:number = 0;
	public SetCanDoGodShieldSkill():void {
		this._canDoGodShieldSkill = true;
		this._canDoGodShieldSkillOverTime = egret.getTimer() + 1000;
	}
	private CheckGodShieldSkill():boolean {
		let self = this;
		if(!self._player) return false;
		if(!self._canDoGodShieldSkill) return false;
		if(egret.getTimer() > self._canDoGodShieldSkillOverTime) return false;
		//
		let godShieldSkillId = self._player.ROLE_GA_SKILL_ID;
		let isUnlocked = godShieldSkillId > 0;
		if(!isUnlocked) return false;
		let setTarget = self._player.lastDmgCaster;
		if(!setTarget) setTarget = self._player.seledTarget;
		if(setTarget && !setTarget.isEnemy(self._player)) setTarget = null;

		if(!setTarget || !self._player.skillManager.hasSkill(godShieldSkillId) || self._player.skillManager.isInSkillCd(godShieldSkillId)) return false;
		self.stopMove();
		self._unit.skillManager.notifyServerDoSkill(godShieldSkillId, SkillDmgType.SHIELD, setTarget);
		self._st = self.st_attack;
		self._canDoGodShieldSkill = false;
		return true;
	}

	//send message.
	private SendMoveStop() {
		var self = this;

		let msg: any = {};
		msg.key = "CgStopMove";
		let body:CgStopMove = <CgStopMove>{};
		msg.body = body;
		body.currentX = Math.floor(self._unit.x);
		body.currentY = Math.floor(self._unit.y);
		App.Socket.send(msg);
	}

	private SendMoveChangeDir(x:number, y:number) {
		var self = this;

		let msg: any = {};
		msg.key = "CgMove";
		let body:CgMove = <CgMove>{};
		msg.body = body;
		body.currentX = Math.floor(self._unit.x);
		body.currentY = Math.floor(self._unit.y);
		body.targetX = Math.floor(x);
		body.targetY = Math.floor(y);
		App.Socket.send(msg);
	}

	// private server notice.
	public forceStopMove(x:number, y:number):void {
        var self = this;
		
        self._navAgent.stopMove();
		self.gotoState(self.st_idle);
    }
}

This snippet took 0.05 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).