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