Beispiel #1
0
        void BehaveWithTarget()
        {
            if (Target.IsActive == false || Target.SpawnVersion != targetVersion ||
                (this.TargetAllegiance & Agent.GetAllegiance(Target)) == 0)
            {
                //Target's lifecycle has ended
                StopEngage();
                BehaveWithNoTarget();
                return;
            }

            if (!IsWindingUp)
            {
                Vector2d targetDirection = Target.Body._position - cachedBody._position;
                long     fastMag         = targetDirection.FastMagnitude();

                //TODO: Optimize this instead of recalculating magnitude multiple times
                if (CheckRange())
                {
                    if (!inRange)
                    {
                        if (CanMove)
                        {
                            cachedMove.StopMove();
                        }
                        inRange = true;
                    }
                    Agent.SetState(EngagingAnimState);

                    long mag;
                    targetDirection.Normalize(out mag);
                    bool withinTurn = TrackAttackAngle == false ||
                                      (fastMag != 0 &&
                                       cachedBody.Forward.Dot(targetDirection.x, targetDirection.y) > 0 &&
                                       cachedBody.Forward.Cross(targetDirection.x, targetDirection.y).Abs() <= AttackAngle);
                    bool needTurn = mag != 0 && !withinTurn;
                    if (needTurn)
                    {
                        if (CanTurn)
                        {
                            cachedTurn.StartTurnDirection(targetDirection);
                        }
                    }
                    else
                    {
                        if (attackCount >= AttackInterval)
                        {
                            StartWindup();
                        }
                    }
                }
                else
                {
                    if (CanMove)
                    {
                        cachedMove.PauseAutoStop();
                        cachedMove.PauseCollisionStop();
                        if (cachedMove.IsMoving == false)
                        {
                            cachedMove.StartMove(Target.Body._position);
                            cachedBody.Priority = basePriority;
                        }
                        else
                        {
                            if (inRange)
                            {
                                cachedMove.Destination = Target.Body.Position;
                            }
                            else
                            {
                                if (repathTimer.AdvanceFrame())
                                {
                                    if (Target.Body.PositionChangedBuffer &&
                                        Target.Body.Position.FastDistance(cachedMove.Destination.x, cachedMove.Destination.y) >= (repathDistance * repathDistance))
                                    {
                                        cachedMove.StartMove(Target.Body._position);
                                        //So units don't sync up and path on the same frame
                                        repathTimer.AdvanceFrames(repathRandom);
                                    }
                                }
                            }
                        }
                    }

                    if (IsAttackMoving || isFocused == false)
                    {
                        searchCount -= 1;
                        if (searchCount <= 0)
                        {
                            searchCount = SearchRate;
                            if (ScanAndEngage())
                            {
                            }
                            else
                            {
                            }
                        }
                    }
                    if (inRange == true)
                    {
                        inRange = false;
                    }
                }
            }
            if (IsWindingUp)
            {
                //TODO: Do we need AgentConditional checks here?
                windupCount += LockstepManager.DeltaTime;
                if (CanTurn)
                {
                    Vector2d targetVector = Target.Body._position - cachedBody._position;
                    cachedTurn.StartTurnVector(targetVector);
                }
                if (windupCount >= Windup)
                {
                    windupCount = 0;
                    Fire();
                    while (this.attackCount >= AttackInterval)
                    {
                        //resetting back down after attack is fired
                        this.attackCount -= (this.AttackInterval);
                    }
                    this.attackCount += Windup;
                    IsWindingUp       = false;
                }
            }
            else
            {
                windupCount = 0;
            }
            if (inRange)
            {
                cachedMove.PauseAutoStop();
                cachedMove.PauseCollisionStop();
            }
        }