示例#1
0
    // ignoreNonPreferrential
    // In a sphere with the radius of range, find all enemy units and pick one to target
    List <ITargetable> ScanForTargets(bool ignoreNonPreferred)
    {
        Collider[]         cols  = Physics.OverlapSphere(transform.position, range, gameRules.targetLayerMask);
        List <ITargetable> targs = new List <ITargetable>();

        for (int i = 0; i < cols.Length; i++)
        {
            ITargetable targ = GetITargetableFromCol(cols[i]);

            if (IsNull(targ))             // No targetable found
            {
                continue;
            }

            if (targs.Contains(targ))             // Ignore multiple colliders for one targetable
            {
                continue;
            }

            if (targ.GetTeam() == parentUnit.GetTeam())             // Can't target allies
            {
                continue;
            }

            if (!targ.GetVisibleTo(parentUnit.GetTeam()))             // Must be visible to this team
            {
                continue;
            }

            // Can ignore non-preferred targets altogether (used when searching for a better target)
            if (ignoreNonPreferred && targ.GetTargetType() != preferredTargetType)
            {
                continue;
            }

            // The list of colliders is created by intersection with a range-radius sphere, but the center of this unit can still actually be out of range, leading to a target which cannot be shot at
            if (!IsValid(targ))
            {
                continue;
            }

            targs.Add(targ);
        }

        // Sort by distance and target type
        targs.Sort(delegate(ITargetable a, ITargetable b)
        {
            return(ComparisonWeight(a)
                   .CompareTo(
                       ComparisonWeight(b)));
        });

        return(targs);
    }
示例#2
0
    void UpdateTargeting()
    {
        if (!suspended)
        {
            // Have target and it's valid
            if (!IsNull(target) && IsValid(target))
            {
                // If we are targeting a non-preferred target type, we want to constantly search for a better target
                if (!hasManualTarget && target.GetTargetType() != preferredTargetType)
                {
                    // Collect a list of all valid targets, ignoring non-preferred targets
                    List <ITargetable> autoTargets = ScanForTargets(true);

                    // Pick best one (if any were found)
                    if (autoTargets.Count > 0)
                    {
                        // This new target will ALWAYS be different from the previous target because it was filtered differently
                        target = autoTargets[0];

                        // Update resetRotFrame
                        UpdateTarget(false);
                    }
                }

                // Rotate towards the target
                CalcTargetLookRotation();
                Rotate();
                // Shoot if possible
                if (parentUnit.isServer)                 // Only server actually shoots, it's faked on clients
                {
                    AttemptStartShooting();
                }
            }
            else             // We haven't assigned one yet, it died, or it's become invalid
            {
                // Collect a list of all valid targets
                List <ITargetable> autoTargets = ScanForTargets(ignoreNonPreferrential);
                // Pick best one
                target = autoTargets.Count > 0 ? autoTargets[0] : null;

                // Found a valid target
                if (!IsNull(target))
                {
                    Debug.Log("my parent team is " + parentUnit.GetTeam() + " targets team is " + target.GetTeam());
                    // Update resetRotFrame
                    UpdateTarget(false);

                    // Rotate towards the target
                    CalcTargetLookRotation();
                    Rotate();
                    // Shoot if possible
                    if (parentUnit.isServer)                     // Only server actually shoots, it's faked on clients
                    {
                        AttemptStartShooting();
                    }
                }
                else                 // Failed to find a valid target
                {
                    // Rotate to standby position
                    direction    = transform.forward;
                    lookRotation = CalcLookRotation(direction);
                    Rotate();
                }
            }     // if have target and it's valid
        }         // if not suspended
        else
        {
            direction    = transform.forward;
            lookRotation = CalcLookRotation(direction);
            Rotate();
        }
    }