private void Moving(int deltaTime) { PathAgent agent = owner.pathAgent; agent.Move(deltaSpeed); long distance = FixVector3.SqrDistance(targetPosition, owner.position); if (distance <= FixVector3.one.magnitude) { state = State.DashEnd; waitingChangeState = false; LogicUnit target = owner.target; if (target != null && target.Alive() && target.id == owner.target.id) { AttributeEffect ae = GenerateAttributeEffect(attributeEffects[0]); ae.Attach(owner, target); } DebugUtils.Log(DebugUtils.Type.AI_Skill, string.Format("YueGuangChongCi: arrive target position, change state to {0}", state)); } else { DebugUtils.Log(DebugUtils.Type.AI_Skill, string.Format("YueGuangChongCi: distance = {0}, step = {1}", distance, stepLength)); } }
private bool OwnerAttackSameTarget(LogicUnit target) { DebugUtils.Assert(ownerSkills.ContainsKey(UnitSkillTriggerType.OnAttackSameTarget), "UnitSkillHandler didn't register skill trigger OnAttackSameTarget, but still trigger it"); DebugUtils.LogWarning(DebugUtils.Type.AI_SkillTrigger, string.Format("Current attack: last target {0}, target {1}", lastTarget == null ? "null" : lastTarget.id.ToString(), target == null ? "null" : target.id.ToString())); bool result = false; List <SkillHandler> skills = ownerSkills[UnitSkillTriggerType.OnAttackSameTarget]; for (int i = 0; i < skills.Count; i++) { SkillHandler skillHandler = skills[i]; if (lastTarget != null && lastTarget.Alive() && target != null && target.Alive()) { if (target.id == lastTarget.id) { result = ExecuteSkill(skillHandler); restoreTargetTimer = 0; } else { lastTarget = null; } } } lastTarget = target; return(result); }
public override void Update(int deltaTime) { LogicUnit target = owner.target; // Check target if (target != null) { if (!target.Alive()) { owner.target = null; owner.targetId = 0; target = null; } else { // Target Alive } } if (target != null) { owner.Attack(target); } else { owner.FindOpponent(); } }
public virtual void Fight() { if (target != null && target.Alive()) { DebugUtils.Log(DebugUtils.Type.AI_Npc, "the " + npcType + " npc " + id + " begins to attack target " + target.id); damage = physicasAttack; target.Hurt(damage, hurtType, false, this); } }
public override void LogicUpdate(int deltaTime) { if (state == ProjectileState.Flying) { if (target != null && target.Alive()) { targetPosition = target.position; } else { // If target has been death // Projectile will fly to the target's body position if (!changeDataWhenTargetDeath) { DebugUtils.Log(DebugUtils.Type.AI_Projectile, "the projectile " + id + "'s target already death, now set Maxdistance = " + (targetDistance + flyingDistance)); SetMaxDistance(targetDistance + flyingDistance); changeDataWhenTargetDeath = !changeDataWhenTargetDeath; } } // modify coordinates for each frame position += speed * deltaTime * GameConstants.LOGIC_FIXPOINT_PRECISION_FACTOR * GameConstants.LOGIC_FIXPOINT_PRECISION_FACTOR; transform.position = position.vector3; // recode total mileage of flight flyingDistance = FixVector3.SqrDistance(position, startPosition); RenderMessage rm = new RenderMessage(); rm.type = RenderMessage.Type.SyncPosition; rm.ownerId = id; rm.arguments.Add("type", (int)type); rm.position = (position + heightOffset).vector3; rm.direction = speed.vector3; PostRenderMessage(rm); //To be decide projectile should it be over targetDistance = FixVector3.SqrDistance(targetPosition, position); FlyingOperation(deltaTime); } else if (state == ProjectileState.Hit) { DebugUtils.Log(DebugUtils.Type.AI_Projectile, "the projectile " + id + " hit the target " + target.id + " targetDistance " + targetDistance); Hit(); } }
public override void LogicUpdate(int deltaTime) { base.LogicUpdate(deltaTime); if (state == State.InstallBattery) { if (timer == 0) { RenderMessage rm = new RenderMessage(); rm.ownerId = owner.id; rm.type = RenderMessage.Type.SoldierUseBattery; rm.arguments.Add("state", (int)state); PostRenderMessage(rm); } else if (timer >= GameConstants.UNIT_INSTALLBATTRY_DURATION) { state = State.UsingBattery; timer = 0; } timer += deltaTime; } else if (state == State.UsingBattery) { if (target != null && target.Alive() && targetId == target.id) { // focus and fight with enemy BattryFight(deltaTime); } else { // search enemy... LogicUnit enemy = owner.FindOpponentUnit(); if (enemy != null) { BattryAttack(enemy); } } } else if (state == State.PackUpBattery) { PackUpBattery(); ReleaseEnd(); } }
public override void UnitFinishedMove() { LogicUnit target = owner.target; if (target != null && target.Alive()) { if (TargetWithInAttackArea()) { state = State.DashStart; } else { owner.FindChasePath(target.position); } } else { ReleaseEnd(); } }
private void SprintState(int deltaTime) { LogicUnit target = owner.target; FixVector3 position = owner.position; PathAgent agent = owner.pathAgent; if (target != null && target.Alive() && target.id == owner.target.id) { if ((target.position - owner.targetPosition).sqrMagnitude > GameConstants.BEGINCHASE_DISTANCE) // 5f is a testing distance { // After find path, Refresh target position owner.targetPosition = target.position; owner.FindChasePath(owner.target.position); return; } if (TargetWithInAttackArea()) { state = State.Attack; } else { if (!owner.CurrentPathAlreadyFinished()) { owner.WaypointHandler(); FixVector3 d = owner.speed * deltaTime; agent.Move(d); } else { // wait for chase path } } } else { // Skill will be shut down when target death. Stop(); owner.Idle(); } }
private void GuardPosture(int deltaTime) { if (state == BuildingState.IDLE) { FindOpponent(); } else if (state == BuildingState.ATTACK) { // is searching need to ignore the cloaking if (target.Alive() && target.id == targetId) { long distance = FixVector3.SqrDistance(target.position, position); if (distance > realAttakDistance) { state = BuildingState.IDLE; } else { fightTimer += deltaTime; if (fightTimer > attackInterval) { fightTimer = 0; Fire(); } } } else { Idle(); } } else { DebugUtils.LogError(DebugUtils.Type.AI_Tower, "the tower " + id + " is in strange state " + state); } //HealthAutoRecovery( deltaTime ); Designer don't want deploy type building have auto recovery.Locked this. }
public void Fight() { if (target != null && target.Alive() && target.id == targetId) { damage = physicasAttack; DebugUtils.Log(DebugUtils.Type.AI_Npc, "the " + npcType + " npc " + id + " begins to fire to target " + target.id); Projectile projectile = GenerateProjectile(this, projectileId, position, target); projectile.RegisterRenderMessageHandler(PostRenderMessage); projectile.RegisterDestroyHandler(PostDestroy); RenderMessage rm = new RenderMessage(); rm.type = RenderMessage.Type.SpawnProjectile; rm.ownerId = projectile.id; rm.position = projectile.position.vector3; rm.direction = projectile.speed.vector3; rm.arguments.Add("mark", projectile.mark); rm.arguments.Add("metaId", projectile.metaId); rm.arguments.Add("holderType", (int)projectile.owner.type); rm.arguments.Add("holderId", projectile.owner.id); PostRenderMessage(rm); } }
public override void Update(int deltaTime) { base.Update(deltaTime); LogicUnit target = owner.target; if (target != null && target.Alive()) { long distance = FixVector3.SqrDistance(target.position, owner.position); if (distance < owner.attackRadius) { if (inFightInterval) { fightIntervalTimer += deltaTime; if (fightIntervalTimer >= fightInterval) { fightState = FightState.StartSwingPoint; fightIntervalTimer = 0; inFightInterval = false; } } else { fightState = GetCurrentState(fightDurationTimer); fightDurationTimer += deltaTime; if (fightState == FightState.StartSwingPoint) { owner.direction = target.position - owner.position; RenderMessage rm = new RenderMessage(); rm.ownerId = owner.id; rm.direction = owner.direction.vector3; rm.type = RenderMessage.Type.SummonedUnitAttack; owner.PostRenderMessage(rm); } else if (fightState == FightState.HitPoint) { owner.damage = Formula.GetAttackFloatingValue(owner.physicalAttack, owner.GetRandomNumber(), owner.GetRandomNumber(owner.physicalAttackVar)); List <LogicUnit> targets = owner.FindOpponent(); for (int i = 0; i < targets.Count; i++) { LogicUnit t = targets[i]; // Check target state and still in the attack range if (t != null && t.Alive()) { t.Hurt(owner.damage, AttackPropertyType.PhysicalAttack, false, owner.ownerSoldier); } } } else if (fightState == FightState.FightEnd) { inFightInterval = true; fightDurationTimer = 0; } } } else { owner.Idle(); } } else { owner.Idle(); } }
public override void Update(int deltaTime) { LogicUnit target = owner.target; FixVector3 position = owner.position; if (target != null && target.Alive() && owner.target.id == owner.targetId) { if (inFightInterval) { // Timing interval fightIntervalTimer += deltaTime; if (fightIntervalTimer >= fightInterval) { // Enter fight fightState = FightState.StartSwingPoint; fightIntervalTimer = 0; fightInterval = 0; inFightInterval = false; } } else { fightState = GetCurrentState(fightDurationTimer); fightDurationTimer += deltaTime; if (fightState == FightState.StartSwingPoint) { DebugUtils.Assert(owner.target.id == owner.targetId, string.Format("Fight Status : the soldier {0}'s targetId = {1}, but its target's id = {2}!", owner.id, owner.targetId, owner.target.id)); if (owner.stateListener.PostFightEvent()) { // Trigger skill, will interrupt fight. return; } else { // Save the crit attack performance code isCrit = Formula.TriggerCritical(owner.GetRandomNumber(), owner.GetCriticalChance()); //if ( isCrit ) //{ // hitTime = owner.critHitTime; // fightDuration = owner.critDuration; //} //else { hitTime = owner.attackHitTime; fightDuration = owner.attackDuration; } // Reset direction at attack begin. FixVector3 direction = target.position - position; owner.direction = direction; if (owner.standardAttackType == AttackDistanceType.Melee) { RenderMessage rm = new RenderMessage(); rm.ownerId = owner.id; rm.direction = direction.vector3; rm.type = RenderMessage.Type.SoldierAttack; rm.arguments.Add("isCrit", false); rm.arguments.Add("intervalRate", 1); owner.PostRenderMessage(rm); } else { RenderMessage rm = new RenderMessage(); rm.type = RenderMessage.Type.SoldierSpawnProjectile; rm.ownerId = owner.id; rm.direction = direction.vector3; rm.arguments.Add("projectileMetaId", owner.projectileId); rm.arguments.Add("intervalRate", 1); owner.PostRenderMessage(rm); } } } else if (fightState == FightState.HitPoint) { owner.Fight(isCrit); owner.stateListener.PostUnitFightAfter(); } else if (fightState == FightState.BackSwing) { if (fightDurationTimer >= fightDuration) { fightDurationTimer = 0; fightInterval = owner.GetAttackInterval() - fightDuration; inFightInterval = true; } } } long distance = FixVector3.SqrDistance(target.position, owner.position); long attackDistance = owner.GetAttackArea(); if (distance >= attackDistance) { if ((target.position - owner.targetPosition).magnitude > attackDistance) { owner.Chase(target); } else { // continue fight } } } else { owner.Idle(); } }
public override void Update(int deltaTime) { int state = owner.state; DebugUtils.Assert(state == SoldierState.CHASING, string.Format("Soldier {0} is in state {1} when updating in FsmChase!", owner.id, state)); LogicUnit target = owner.target; FixVector3 position = owner.position; PathAgent agent = owner.pathAgent; if (target != null && target.Alive() && target.id == owner.targetId) { long mag = FixVector3.SqrDistance(target.position, owner.targetPosition); //Debug.LogWarning( string.Format( "current position:{0} record position:{1} length:{2}", target.position, owner.targetPosition, mag ) ); if (mag > GameConstants.BEGINCHASE_DISTANCE) // 5f is a testing distance { //Debug.Log( string.Format( "current position:{0} record position:{1} length:{2}", target.position, owner.targetPosition, mag ) ); owner.Chase(owner.target); return; } owner.WaypointHandler(); FixVector3 d = owner.speed * deltaTime; FixVector3 newPosition = position + d * FixVector3.PrecisionFactor * FixVector3.PrecisionFactor * FixVector3.PrecisionFactor; FixVector3 avoidance = owner.CalculateAvoidance(owner, newPosition); if (avoidance != FixVector3.zero && subState != CHASE2AVOIDANCE) { DebugUtils.LogWarning(DebugUtils.Type.AI_Soldier, string.Format("soldier {0} enters CHASE2AVOIDANCE.", owner.id)); subState = CHASE2AVOIDANCE; DebugUtils.Log(DebugUtils.Type.Avoidance, string.Format("CHASE : soldier {0} has received an avoidance ({1}, {2}, {3}), he's speed is ({4}, {5}, {6})!", owner.id, avoidance.x, avoidance.y, avoidance.z, owner.speed.x, owner.speed.y, owner.speed.z)); avoidSpeed = d + avoidance * deltaTime; FixVector3 pos = position + avoidSpeed * FixVector3.PrecisionFactor * FixVector3.PrecisionFactor; bool pass = agent.DirectPassToPosition(pos, NavMesh.AllAreas); if (pass) { agent.Move(avoidSpeed); DebugUtils.Log(DebugUtils.Type.Avoidance, string.Format("CHASE : soldier {0} has received an avoidance and moved to ({1}, {2}, {3})!", owner.id, owner.position.x, owner.position.y, owner.position.z)); } else { //still here. DebugUtils.Log(DebugUtils.Type.Avoidance, string.Format("CHASE : soldier {0} has stopped when avoiding others!", owner.id)); } } else { if (subState == CHASE2AVOIDANCE) { if (avoidance != FixVector3.zero) { avoidSpeed = d + avoidance * deltaTime; //Vector3 pos = position + avoidSpeed * FixVector3.PrecisionFactor * FixVector3.PrecisionFactor; //bool pass = agent.DirectPassToPosition( pos, NavMesh.AllAreas ); if (true) //pass ) { agent.ToughMove(avoidSpeed); DebugUtils.Log(DebugUtils.Type.Avoidance, string.Format("CHASE : soldier {0} has received an avoidance and moved to ({1}, {2}, {3})!", owner.id, owner.position.x, owner.position.y, owner.position.z)); } else { //still here. DebugUtils.Log(DebugUtils.Type.Avoidance, string.Format("CHASE : soldier {0} has stopped when avoiding others!", owner.id)); } } else { DebugUtils.LogWarning(DebugUtils.Type.AI_Soldier, string.Format("soldier {0} enters AVOIDANCE2CHASE.", owner.id)); subState = AVOIDANCE2CHASE; agent.Move(d); } } else { agent.Move(d); } } position = owner.position; FixVector3 v = target.position; long distance = FixVector3.SqrDistance(target.position, position); long attackDistance = owner.GetAttackArea(); if (distance < attackDistance) { owner.Attack(owner.target); } /* * else if( distance > chaseArea * chaseArea * 1.5f ) * { * DebugUtils.LogWarning( DebugUtils.Type.AI_Soldier, string.Format( "soldier {0}'s target {1} has escaped! distance = {2}, chaseArea * chaseArea * 1.5f = {3}", id, target.id, distance, chaseArea * chaseArea * 1.5f ) ); * //Idle(); * Walk( destination ); * } */ if (owner.stateListener.PostMoveDistanceChanged(d.magnitude)) { return; } owner.stateListener.PostChasingStateChanged(distance); } else { owner.Idle(); //Walk( destination ); } }
private void SprintState(int deltaTime) { LogicUnit target = owner.target; FixVector3 position = owner.position; PathAgent agent = owner.pathAgent; if (target != null && target.Alive() && target.id == owner.target.id) { if (FixVector3.SqrDistance(target.position, owner.targetPosition) > GameConstants.BEGINCHASE_DISTANCE) // 5f is a testing distance { // if target leave the position too far, need to refresh chase path owner.targetPosition = target.position; owner.FindChasePath(owner.target.position); return; } if (TargetWithInAttackArea()) { if (target != null && target.Alive() && target.id == owner.target.id) { if (playerOwner) { FixVector3 direction = (target.position - position).normalized; FixVector3 destination = owner.position + direction * 5f;// Temp distance. FixVector3 hitPosition = FixVector3.zero; FixVector3 simpleOnMapPoint = FixVector3.zero; // mapping destination on navmesh bool sampleResult = agent.SamplePosition(destination, out simpleOnMapPoint); if (sampleResult) { destination = simpleOnMapPoint; } bool result = agent.Raycast(destination, out hitPosition); if (result) { destination = hitPosition; } DataManager clientData = DataManager.GetInstance(); UpdateC2S message = new UpdateC2S(); message.timestamp = clientData.GetFrame(); Operation op = new Operation(); op.playerId = clientData.GetPlayerId(); op.unitId = id; op.targetId = owner.id; // test op.unitMetaId = metaId; // test op.opType = OperationType.SyncSkillTargetPosition; // wrong type op.x = destination.vector3.x; op.y = destination.vector3.y; op.z = destination.vector3.z; message.operation = op; PostBattleMessage(MsgCode.UpdateMessage, message); DebugUtils.Log(DebugUtils.Type.AI_Skill, string.Format("YueGuangChongCi sync destination : {0}", destination)); } } else { Stop(); } } else { if (!owner.CurrentPathAlreadyFinished()) { owner.WaypointHandler(); FixVector3 d = owner.speed * deltaTime; agent.Move(d); } else { // wait for chase path } } } else { // Skill will be shut down when target death. Stop(); owner.Idle(); } }