public override Vector2 Decide(Flock_Agent agent, List <Transform> context, Flock flock)
    {
        if (weights.Length != behaviours.Length)
        {
            Debug.LogError("weights to behaviour length mismatch");
            return(Vector2.zero); //do nothing
        }

        Vector2 move = Vector2.zero;

        for (int i = 0; i < behaviours.Length; i++)
        {
            Vector2 partialMove = behaviours[i].Decide(agent, context, flock) * weights[i];

            if (partialMove != Vector2.zero)
            {
                if (partialMove.sqrMagnitude > weights[i] * weights[i])
                {
                    partialMove.Normalize(); //range the values
                    partialMove *= weights[i];
                }

                move += partialMove;
            }
        }
        return(move);
    }
    public override Vector2 Decide(Flock_Agent agent, List <Transform> context, Flock flock)
    {
        if (context.Count == 0)
        {
            return(Vector2.zero);
        }

        Vector2 AvoidanceMove = Vector2.zero;
        int     inRange       = 0;

        foreach (Transform item in context)
        {
            if (Vector2.SqrMagnitude(item.position - agent.transform.position) < flock.SquareAvoidanceRadius)
            {
                inRange++;

                AvoidanceMove += (Vector2)(agent.transform.position - item.position);
            }
        }

        if (inRange > 0)
        {
            AvoidanceMove /= inRange; //average movement
        }
        return(AvoidanceMove);
    }
    public override Vector2 Decide(Flock_Agent agent, List <Transform> context, Flock flock)
    {
        Vector2 centerOffset = center - (Vector2)agent.transform.position;
        float   value        = centerOffset.magnitude / radius;

        if (value < 0.9f)
        {
            return(Vector2.zero);
        }

        return(centerOffset * value * value);
    }
Beispiel #4
0
    List <Transform> GetNearbyObjects(Flock_Agent bird)
    {
        List <Transform> context = new List <Transform>();

        Collider2D[] contextCollider = Physics2D.OverlapCircleAll(bird.transform.position, neighbour_radius);
        foreach (Collider2D c in contextCollider)
        {
            if (c != bird.AgentCollider)
            {
                context.Add(c.transform);
            }
        }
        return(context);
    }
Beispiel #5
0
    void Start()
    {
        squaremaxspeed        = flock_speed_offset * flock_speed_offset;
        squareNeighbourRadius = neighbour_radius * neighbour_radius;
        squareNeighbourRadius = avoidance_radius_multiplier * avoidance_radius_multiplier;

        //populate
        for (int i = 0; i < initialCount; i++)
        {
            Flock_Agent bird = Instantiate(
                flock_prefab,
                UnityEngine.Random.insideUnitCircle * initialCount * FlockDensity,
                Quaternion.Euler(Vector3.forward * UnityEngine.Random.Range(0f, 350f)),
                transform
                );
            bird.name = "bird" + i;
            flocks.Add(bird);
        }
    }
Beispiel #6
0
    //facing same direction
    public override Vector2 Decide(Flock_Agent agent, List <Transform> context, Flock flock)
    {
        //if no neighbours just keep moving
        if (context.Count == 0)
        {
            return(agent.transform.up);
        }

        Vector2 alignmentMove = Vector2.zero;

        foreach (Transform item in context)
        {
            alignmentMove += (Vector2)item.transform.up;
        }
        alignmentMove /= context.Count; //average movement


        return(alignmentMove);
    }
Beispiel #7
0
    public override Vector2 Decide(Flock_Agent agent, List <Transform> context, Flock flock)
    {
        if (context.Count == 0)
        {
            return(Vector2.zero);
        }

        Vector2 cohesionMove = Vector2.zero;

        foreach (Transform item in context)
        {
            cohesionMove += (Vector2)item.position;
        }
        cohesionMove /= context.Count; //average movement

        //create offset
        cohesionMove -= (Vector2)agent.transform.position;
        return(cohesionMove);
    }
    public override Vector2 Decide(Flock_Agent agent, List <Transform> context, Flock flock)
    {
        if (context.Count == 0)
        {
            return(Vector2.zero);
        }

        Vector2 cohesionMove = Vector2.zero;

        foreach (Transform item in context)
        {
            cohesionMove += (Vector2)item.position;
        }
        cohesionMove /= context.Count; //average movement

        //create offset
        cohesionMove -= (Vector2)agent.transform.position;
        cohesionMove  = Vector2.SmoothDamp(agent.transform.up, cohesionMove, ref currentVelocity, agentSmoothTime);
        //direction, whereto, velocity, timetakento move to "where to"
        return(cohesionMove);
    }
Beispiel #9
0
 //avoidance or cohesion applied here
 public abstract Vector2 Decide(Flock_Agent agent, List <Transform> context, Flock flock);