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 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(); } }
public override void Update(int deltaTime) { owner.WaypointHandler(); FixVector3 position = owner.position; PathAgent agent = owner.pathAgent; 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 != WALK2AVOIDANCE) { DebugUtils.LogWarning(DebugUtils.Type.AI_Soldier, string.Format("soldier {0} enters WALK2AVOIDANCE.", owner.id)); subState = WALK2AVOIDANCE; DebugUtils.Log(DebugUtils.Type.Avoidance, string.Format("WALK : 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); owner.stateListener.PostMoveDistanceChanged(avoidance.magnitude); DebugUtils.Log(DebugUtils.Type.Avoidance, string.Format("WALK : soldier {0} has received an avoidance and move 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("WALK : soldier {0} has stopped when avoiding others!", owner.id)); } } else { if (subState == WALK2AVOIDANCE) { if (avoidance != FixVector3.zero) { avoidSpeed = d + avoidance * deltaTime; FixVector3 pos = position + avoidSpeed * FixVector3.PrecisionFactor * FixVector3.PrecisionFactor; //bool pass = agent.DirectPassToPosition( pos, NavMesh.AllAreas ); if (true) //pass ) { //agent.Move( avoidSpeed ); agent.ToughMove(avoidSpeed); DebugUtils.Log(DebugUtils.Type.Avoidance, string.Format("WALK : soldier {0} has received an avoidance and wants to move to ({1}, {2}, {3}), then his position is ({4}, {5}, {6})!", owner.id, pos.x, pos.y, pos.z, owner.position.x, owner.position.y, owner.position.z)); } else { //still here. DebugUtils.Log(DebugUtils.Type.Avoidance, string.Format("WALK : soldier {0} has stopped when avoiding others!", owner.id)); } } else { DebugUtils.LogWarning(DebugUtils.Type.AI_Soldier, string.Format("soldier {0} enters AVOIDANCE2WALK.", owner.id)); subState = AVOIDANCE2WALK; agent.Move(d); owner.stateListener.PostMoveDistanceChanged(avoidance.magnitude); } } else { agent.Move(d); owner.stateListener.PostMoveDistanceChanged(avoidance.magnitude); } owner.stateListener.PostMoveDistanceChanged(d.magnitude); } }
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(); } }