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