// Start is called before the first frame update void Start() { square_Max_Speed = Mathf.Pow(max_Speed, 2f); square_Neighbor_Rad = Mathf.Pow(neighbor_Radius, 2f); square_Avoidance_Rad = square_Neighbor_Rad * avoidance_Rad_Mult * avoidance_Rad_Mult; for (int i = 0; i < spawn_Count; i++) { Sheep_Controller sheeb = Instantiate(sheebPrefab, Random.insideUnitCircle * spawn_Count * sheeb_Density, Quaternion.Euler(Vector3.forward * Random.Range(0f, 360f)), transform); sheeb.name = "Sheeb " + i; sheebs.Add(sheeb); switch (sheeb.tag) { case "Red_Sheeb": red_Sheeb_Count++; break; case "Blue_Sheeb": blue_Sheeb_Count++; break; case "Green_Sheeb": green_Sheeb_Count++; break; } } }
public override Vector2 Calculate_Move(Sheep_Controller sheeb, List <Transform> context, Herd_Controller herd) { // If there are no neighbors, do not adjust if (context.Count == 0f) { return(Vector2.zero); } // Add points together and average Vector2 avoid_Move = Vector2.zero; int n_Avoid = 0; foreach (Transform thing in context) { if (Vector2.SqrMagnitude(thing.position - sheeb.transform.position) < herd.SquareAvoidRadius) { n_Avoid++; avoid_Move += (Vector2)(sheeb.transform.position - thing.position); } } if (n_Avoid > 0) { avoid_Move /= n_Avoid; } return(avoid_Move); }
List <Transform> GetNearbyObjects(Sheep_Controller sheeb) { List <Transform> s_Context = new List <Transform>(); Collider2D[] context_Cols = Physics2D.OverlapCircleAll(sheeb.transform.position, neighbor_Radius); foreach (Collider2D col in context_Cols) { if (col != sheeb.SheebCollider && col.gameObject.tag.Contains("Sheeb") && col.transform.parent == transform) { if (sheeb.gameObject.CompareTag("Red_Sheeb") && col.gameObject.tag.Contains("Red")) { s_Context.Add(col.transform); } else if (sheeb.gameObject.CompareTag("Blue_Sheeb") && col.gameObject.tag.Contains("Blue")) { s_Context.Add(col.transform); } else if (sheeb.gameObject.CompareTag("Green_Sheeb") && col.gameObject.tag.Contains("Green")) { s_Context.Add(col.transform); } } } return(s_Context); }
public override Vector2 Calculate_Move(Sheep_Controller sheeb, List <Transform> context, Herd_Controller herd) { // Handle mismatched data if (weights.Length != behaviors.Length) { Debug.LogError("Data mismatch"); return(Vector2.zero); } // Set up move Vector2 move = Vector2.zero; for (int i = 0; i < behaviors.Length; i++) { Vector2 partial_Move = behaviors[i].Calculate_Move(sheeb, context, herd) * weights[i]; if (partial_Move != Vector2.zero) { if (partial_Move.sqrMagnitude > weights[i] * weights[i]) { partial_Move.Normalize(); partial_Move *= weights[i]; } move += partial_Move; } } return(move); }
public override Vector2 Calculate_Move(Sheep_Controller sheeb, List <Transform> context, Herd_Controller herd) { // If there are no neighbors, do not adjust if (context.Count == 0f) { return(sheeb.transform.up); } // Add points together and average Vector2 align_Move = Vector2.zero; foreach (Transform thing in context) { align_Move += (Vector2)thing.transform.up; } align_Move /= context.Count; return(align_Move); }
public void Spawn_Sheeb(Vector2 position) { Sheep_Controller sheeb = Instantiate(sheebPrefab, position, Quaternion.Euler(Vector3.forward * Random.Range(0f, 360f)), transform); sheebs.Add(sheeb); // Randomly determine the sheeb's tag and color switch (Random.Range(0, 3)) { case 0: sheeb.tag = "Red_Sheeb"; sheeb.GetComponentInChildren <SpriteRenderer>().color = new Color32(255, 102, 102, 255); break; case 1: sheeb.tag = "Blue_Sheeb"; sheeb.GetComponentInChildren <SpriteRenderer>().color = new Color32(102, 214, 255, 255); break; case 2: sheeb.tag = "Green_Sheeb"; GetComponentInChildren <SpriteRenderer>().color = new Color32(102, 255, 110, 255); break; } switch (sheeb.tag) { case "Red_Sheeb": red_Sheeb_Count++; break; case "Blue_Sheeb": blue_Sheeb_Count++; break; case "Green_Sheeb": green_Sheeb_Count++; break; } }
public override Vector2 Calculate_Move(Sheep_Controller sheeb, List <Transform> context, Herd_Controller herd) { // If there are no neighbors, do not adjust if (context.Count == 0f) { return(Vector2.zero); } // Add points together and average Vector2 coh_Move = Vector2.zero; foreach (Transform thing in context) { coh_Move += (Vector2)thing.position; } coh_Move /= context.Count; // Offset coh_Move -= (Vector2)sheeb.transform.position; return(coh_Move); }
public abstract Vector2 Calculate_Move(Sheep_Controller sheeb, List <Transform> context, Herd_Controller herd);