-
Notifications
You must be signed in to change notification settings - Fork 0
/
Flock.cs
88 lines (79 loc) · 2.89 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
85
86
87
88
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Flock : MonoBehaviour
{
public FlockAgent agentPrefab;
List<FlockAgent> agents = new List<FlockAgent>();
public FlockBehaviour behaviour;
[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, 100f)]
public float neighbourRadius = 1.5f;
[Range(0f, 1f)]
public float avoidanceRadiusMultiplier = 0.5f;
[Range(10f, 100f)]
public float detectRadius = 25f;
float squareMaxSpeed;
float squareNeighbourRadius;
float squareAvoidanceRadius;
public float SquareAvoidanceRadius { get { return squareAvoidanceRadius; } }
// Start is called before the first frame update
void Start()
{
squareMaxSpeed = maxSpeed * maxSpeed;
squareNeighbourRadius = neighbourRadius * neighbourRadius;
squareAvoidanceRadius = squareNeighbourRadius * avoidanceRadiusMultiplier * avoidanceRadiusMultiplier;
for (int i = 0; i < startingCount; i++)
{
FlockAgent newAgent = Instantiate(
agentPrefab,
Random.insideUnitCircle * startingCount * AgentDensity + (Vector2)transform.position,
Quaternion.Euler(Vector3.forward * Random.Range(0f, 360f)),
transform
);
newAgent.name = "Agent" + i;
newAgent.Initialize(this);
newAgent.GetComponent<ShipAttack>().enabled = true;
agents.Add(newAgent);
}
}
// Update is called once per frame
void Update()
{
foreach (FlockAgent agent in agents)
{
List<Transform> context = GetNearbyObjects(agent);
Vector2 move = behaviour.CalculateMove(agent, context, this);
move *= driveFactor;
if (move.sqrMagnitude > squareMaxSpeed) move = move.normalized * maxSpeed;
if (float.IsNaN(move.x) || float.IsNaN(move.y)) move = agent.transform.up;
agent.Move(move);
}
}
List<Transform> GetNearbyObjects(FlockAgent agent)
{
List<Transform> context = new List<Transform>();
Collider2D[] colliders = Physics2D.OverlapCircleAll(agent.transform.position, neighbourRadius);
foreach (Collider2D c in colliders)
{
if (c != agent.AgentCollider ) context.Add(c.transform);
}
return context;
}
public List<Transform> GetEnemyInRange(FlockAgent agent)
{
List<Transform> context = new List<Transform>();
Collider2D[] colliders = Physics2D.OverlapCircleAll(agent.transform.position, detectRadius);
foreach (Collider2D c in colliders)
{
if (c != agent.AgentCollider) context.Add(c.transform);
}
return context;
}
}