Ejemplo n.º 1
0
    public override Vector2 CalculateMove(Agent agent, List <Transform> context, FishFSM fish)
    {
        // No neighbours
        if (context.Count == 0)
        {
            return(Vector2.zero);
        }

        // Calcuate average between all neighbours
        Vector2 cohesionMove = Vector2.zero;
        // If filter is not null, use filtered context instead
        List <Transform> filteredContext = (filter == null) ? context : filter.Filter(agent, context);

        foreach (Transform item in filteredContext)
        {
            cohesionMove += (Vector2)item.position;
        }

        // Average position
        cohesionMove /= context.Count;

        // Create offset from agent position
        cohesionMove -= (Vector2)agent.transform.position;

        // Smooth movement
        cohesionMove = Vector2.SmoothDamp(agent.transform.up, cohesionMove, ref currentVelocity, agentSmoothTime);

        return(cohesionMove);
    }
Ejemplo n.º 2
0
    public override Vector2 CalculateMove(Agent agent, List <Transform> context, FishFSM fish)
    {
        // Check size of behaviours and weights is equal
        if (weights.Length != behaviours.Length)
        {
            Debug.LogError("Data mismatch in " + name, this);
            return(Vector2.zero);
        }

        // Composite move
        Vector2 move = Vector2.zero;

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

            if (partialMove != Vector2.zero)
            {
                // Limit magnitudde of partial move to extent of weight
                if (partialMove.sqrMagnitude > weights[i] * weights[i])
                {
                    partialMove.Normalize();
                    partialMove *= weights[i];
                }

                move += partialMove;
            }
        }

        return(move);
    }
Ejemplo n.º 3
0
    public override Vector2 CalculateMove(Agent agent, List <Transform> context, FishFSM fish)
    {
        // No neighbours
        if (context.Count == 0)
        {
            return(Vector2.zero);
        }

        // Calcuate average between all neighbours
        Vector2 avoidanceMove = Vector2.zero;
        // Number of agents to avoid
        int nAvoid = 0;
        // If filter is not null, use filtered context instead
        List <Transform> filteredContext = (filter == null) ? context : filter.Filter(agent, context);

        foreach (Transform item in filteredContext)
        {
            if (Vector2.SqrMagnitude(item.position - agent.transform.position) < fish.SquareAvoidanceRadius)
            {
                nAvoid++;
                avoidanceMove += (Vector2)(agent.transform.position - item.position);
            }
        }

        // Average position
        if (nAvoid > 0)
        {
            avoidanceMove /= nAvoid;
        }

        return(avoidanceMove);
    }
Ejemplo n.º 4
0
    public override Vector2 CalculateMove(Agent agent, List <Transform> context, FishFSM fish)
    {
        // No neighbours
        if (context.Count == 0)
        {
            return(agent.transform.up);
        }

        // Calcuate average between all neighbours
        Vector2 alignmentMove = Vector2.zero;
        // If filter is not null, use filtered context instead
        List <Transform> filteredContext = (filter == null) ? context : filter.Filter(agent, context);

        foreach (Transform item in filteredContext)
        {
            alignmentMove += (Vector2)item.transform.up;
        }

        // Average position
        alignmentMove /= context.Count;

        return(alignmentMove);
    }
Ejemplo n.º 5
0
 void Start()
 {
     fishFsm = GetComponent <FishFSM>();
 }
Ejemplo n.º 6
0
 public void Initialize(FishFSM fish)
 {
     agentFish = fish;
 }
 void Start()
 {
     fishFsm    = GetComponent <FishFSM>();
     fishWander = GetComponent <FishWander>();
     _selfTrans = transform;
 }
Ejemplo n.º 8
0
 public abstract Vector2 CalculateMove(Agent agent, List <Transform> context, FishFSM fish);