Example #1
0
    /// <summary>
    /// Run the specified decider and returns its value
    /// </summary>
    /// <param name="d">Decider to run</param>
    /// <param name="tank">Tank being controlled</param>
    /// <returns>True if decider wants to run</returns>
    public static bool Decide(this DeciderType d, AITankControl tank)
    {
        switch (d)
        {
        case DeciderType.Always:
            return(true);

        case DeciderType.TooManyBullets:
            return(Physics2D.OverlapCircleAll(tank.transform.position, BulletSearchRadius, (int)Layers.Projectiles).Length
                   > MaxBullets);

        case DeciderType.LineOfSightToPlayer:
            return(!BehaviorTreeNode.WallBetween(tank.transform.position, BehaviorTreeNode.Player.position));

        case DeciderType.CanFire:
            float angle = Vector2.Angle(BehaviorTreeNode.Player.position - tank.transform.position,
                                        tank.transform.up);
            return(angle < MaxAngularDifference &&
                   Vector2.Distance(tank.transform.position, BehaviorTreeNode.Player.position)
                   < DistanceThreshold);

        default:
            throw new ArgumentException("Unknown Decider: " + d);
        }
    }
Example #2
0
    /// <summary>
    /// Compute the Neighbors list
    /// </summary>
    internal void Start()
    {
        var position = transform.position;

        if (AllWaypoints == null)
        {
            AllWaypoints = FindObjectsOfType <Waypoint>();
        }

        foreach (var wp in AllWaypoints)
        {
            if (wp != this && !BehaviorTreeNode.WallBetween(position, wp.transform.position))
            {
                Neighbors.Add(wp);
            }
        }
    }
Example #3
0
    /// <summary>
    /// Nearest waypoint to specified location that is reachable by a straight-line path.
    /// </summary>
    /// <param name="position"></param>
    /// <returns></returns>
    public static Waypoint NearestWaypointTo(Vector2 position)
    {
        Waypoint nearest = null;
        var      minDist = float.PositiveInfinity;

        for (int i = 0; i < AllWaypoints.Length; i++)
        {
            var wp = AllWaypoints[i];
            var p  = wp.transform.position;
            var d  = Vector2.Distance(position, p);
            if (d < minDist && !BehaviorTreeNode.WallBetween(p, position))
            {
                nearest = wp;
                minDist = d;
            }
        }
        return(nearest);
    }