forked from boardtobits/flocking-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Flock.cs
84 lines (73 loc) · 2.52 KB
/
Flock.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Flock : MonoBehaviour
{
public FlockAgent agentPrefab;
List<FlockAgent> agents = new List<FlockAgent>();
public FlockBehavior behavior;
[Range(10, 500)]
public int startingCount = 250;
const float AgentDensity = 0.08f;
[Range(1f, 100f)]
public float driveFactor = 10f;
[Range(1f, 100f)]
public float maxSpeed = 5f;
[Range(1f, 10f)]
public float neighborRadius = 1.5f;
[Range(0f, 1f)]
public float avoidanceRadiusMultiplier = 0.5f;
float squareMaxSpeed;
float squareNeighborRadius;
float squareAvoidanceRadius;
public float SquareAvoidanceRadius { get { return squareAvoidanceRadius; } }
// Start is called before the first frame update
void Start()
{
squareMaxSpeed = maxSpeed * maxSpeed;
squareNeighborRadius = neighborRadius * neighborRadius;
squareAvoidanceRadius = squareNeighborRadius * avoidanceRadiusMultiplier * avoidanceRadiusMultiplier;
for (int i = 0; i < startingCount; i++)
{
FlockAgent newAgent = Instantiate(
agentPrefab,
Random.insideUnitCircle * startingCount * AgentDensity,
Quaternion.Euler(Vector3.forward * Random.Range(0f, 360f)),
transform
);
newAgent.name = "Agent " + i;
newAgent.Initialize(this);
agents.Add(newAgent);
}
}
// Update is called once per frame
void Update()
{
foreach (FlockAgent agent in agents)
{
List<Transform> context = GetNearbyObjects(agent);
//FOR DEMO ONLY
//agent.GetComponentInChildren<SpriteRenderer>().color = Color.Lerp(Color.white, Color.red, context.Count / 6f);
Vector2 move = behavior.CalculateMove(agent, context, this);
move *= driveFactor;
if (move.sqrMagnitude > squareMaxSpeed)
{
move = move.normalized * maxSpeed;
}
agent.Move(move);
}
}
List<Transform> GetNearbyObjects(FlockAgent agent)
{
List<Transform> context = new List<Transform>();
Collider2D[] contextColliders = Physics2D.OverlapCircleAll(agent.transform.position, neighborRadius);
foreach (Collider2D c in contextColliders)
{
if (c != agent.AgentCollider)
{
context.Add(c.transform);
}
}
return context;
}
}