Exemple #1
0
        public override Vector2 CalculateMove(FlockAgent agent, List <Transform> context, Flock flock)
        {
            // If no neighbors, maintain current alignment
            if (context.Count == 0)
            {
                return(agent.transform.up);
            }

            // Add all alignments of neighbors and average
            Vector2          move            = Vector2.zero;
            List <Transform> filteredContext = (filter == null) ? context : filter.Filter(agent, context);

            foreach (Transform neighbor in filteredContext)
            {
                move += (Vector2)neighbor.transform.up;
            }

            return(move / context.Count); // average
        }
Exemple #2
0
        public override Vector2 CalculateMove(FlockAgent agent, List <Transform> context, Flock flock)
        {
            // If no neighbors, then nothing to move towards
            if (context.Count == 0)
            {
                return(Vector2.zero);
            }

            // Add all points together and average
            Vector2          move            = Vector2.zero;
            List <Transform> filteredContext = (filter == null) ? context : filter.Filter(agent, context);

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

            move /= context.Count;

            // Create offset from agent's current position
            return(move - (Vector2)agent.transform.position);
        }
Exemple #3
0
        public override Vector2 CalculateMove(FlockAgent agent, List <Transform> context, Flock flock)
        {
            // If no neighbors, nothing to steal
            if (context.Count == 0)
            {
                return(Vector2.zero);
            }

            // Instead of moving, steal!
            List <Transform> filteredContext = (filter == null) ? context : filter.Filter(agent, context);

            foreach (Transform neighbor in filteredContext)
            {
                FlockAgent neighborAgent = neighbor.GetComponent <FlockAgent>();
                if (neighborAgent != null && Random.Range(0f, 1f) < stealChance)
                {
                    flock.StealAgentFromFlock(neighborAgent, neighborAgent.AgentFlock);
                }
            }

            return(Vector2.zero); // nothing to move
        }
Exemple #4
0
        public override Vector2 CalculateMove(FlockAgent agent, List <Transform> context, Flock flock)
        {
            Vector2 move = Vector2.zero;

            foreach (Behavior b in behaviors)
            {
                Vector2 partialMove = b.behavior.CalculateMove(agent, context, flock) * b.weight;

                if (partialMove != Vector2.zero)
                {
                    // If partial move is greater than our weight, then normalize it so it's exactly the weight
                    if (partialMove.sqrMagnitude > b.weight + b.weight)
                    {
                        partialMove.Normalize();
                        partialMove *= b.weight;
                    }

                    move += partialMove;
                }
            }

            return(move);
        }
Exemple #5
0
 public bool IsAgentStolen(FlockAgent agent)
 {
     return(!agents.Contains(agent));
 }
Exemple #6
0
 public void AddAgent(FlockAgent agent)
 {
     agents.Add(agent);
 }
Exemple #7
0
 public void RemoveAgent(FlockAgent agent)
 {
     agents.Remove(agent);
 }
Exemple #8
0
 public abstract List <Transform> Filter(FlockAgent agent, List <Transform> original);
Exemple #9
0
 public abstract Vector2 CalculateMove(FlockAgent agent, List <Transform> context, Flock flock);