-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnitSquad.cs
222 lines (164 loc) · 5.71 KB
/
UnitSquad.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using UnityEditor;
using UnityEngine;
using static UnityEditor.PlayerSettings;
using static UnityEngine.UI.CanvasScaler;
public class UnitSquad : MonoBehaviour
{
#region Variables
public FormationRule formation = null;
[SerializeField] private GameObject virtualLeaderPrefab = null;
[SerializeField] private ScriptableTeam squadTeam;
public ScriptableTeam SquadTeam {get {return squadTeam;}}
public UnitLeader leader = null;
private List<Unit> units = new List<Unit>();
private Vector3[] unitPositions;
public float defendRange = 2f;
public float alertMaxDistance = 10f;
#endregion
#region Properties
public List<Unit> Units
{
get => units;
set
{
units = value;
foreach (Unit unit in units)
unit.SetSquad(this);
unitPositions = new Vector3[units.Count];
}
}
#endregion
#region MonoBehaviour
private void Start()
{
OnInitializeLeader();
}
private void OnDestroy()
{
if (leader && leader.TryGetComponent(out Movement leaderMovement))
leaderMovement.OnMoveChange.RemoveListener(UpdatePositions);
}
#endregion
#region Functions
private void OnInitializeLeader()
{
// If leader is null, set a virtual one
leader ??= CreateVirtuaLeader();
leader.SetSquad(this);
if(leader.agent) leader.agent.AgentTeam = squadTeam;
leader.GetComponent<Movement>().OnMoveChange.AddListener(UpdatePositions);
}
public bool AssignHealerTo(Unit target)
{
float lowestSqrDistance = float.MaxValue;
HealerUnit nearestHealer = null;
foreach (Unit unit in units)
{
if(unit == target) continue;
HealerUnit healer = unit as HealerUnit;
if (healer == null || healer.Target == leader) continue;
// If the target is not the leader (who is priorized)
if(target != leader)
{
// If the current healer already has a target, skip.
if (healer.Target != null && healer.TargetHealNeedFactor() > 0f) continue;
}
Vector3 healerPosition = healer.transform.position;
float sqrDistance = Vector3.SqrMagnitude(healerPosition - target.transform.position);
if(lowestSqrDistance > sqrDistance)
{
lowestSqrDistance = sqrDistance;
nearestHealer = healer;
}
}
if (nearestHealer)
{
nearestHealer.Target = target;
return true;
}
return false;
}
public bool AssignSupportTo(Unit target)
{
float lowestSqrDistance = float.MaxValue;
SupportUnit nearestSupport = null;
// Don't need support anymore
if (target.agent.Agressor == null) return true;
foreach (Unit unit in units)
{
if (unit == target) continue;
SupportUnit support = unit as SupportUnit;
if (support == null || support.Target == leader) continue;
Vector3 coverPosition = support.GetCoverPosition(target.transform.position, target.agent.Agressor.transform.position);
float sqrDistance = Vector3.SqrMagnitude(support.transform.position - coverPosition);
if (lowestSqrDistance > sqrDistance)
{
lowestSqrDistance = sqrDistance;
nearestSupport = support;
}
}
if (nearestSupport)
{
nearestSupport.Target = target;
return true;
}
return false;
}
public void ReceiveAlert(Unit fromUnit)
{
foreach (Unit unit in units)
{
if (unit.agent.Agressor) continue;
if (Vector3.SqrMagnitude(unit.transform.position - fromUnit.transform.position) < alertMaxDistance * alertMaxDistance) continue;
// One for all, all for one
unit.agent.Agressor = fromUnit.agent.Agressor;
}
}
public void UpdatePositions()
{
for (int i = 0; i < units.Count; i++)
{
Unit unit = units[i];
if (unit && (!unit.gameObject.activeInHierarchy || unit.HasDuty())) continue;
Vector3 pos = ComputeUnitPosition(i);
unit.movement.MoveTo(pos);
unitPositions[i] = pos;
}
}
public void SetTarget(GameObject target)
{
for (int i = 0; i < units.Count; i++)
{
AIAgent ai = units[i].agent as AIAgent;
if (ai != null)
{
ai.SetTarget(target);
}
}
}
Vector3 ComputeUnitPosition(int index)
{
if (formation)
{
if (leader.movement.PositionTarget is not null)
return formation.ComputePosition(leader.movement.PositionTarget.Value, leader.transform.rotation, index);
if (leader.movement.TransformTarget is not null)
{
Transform target = leader.movement.TransformTarget;
return formation.ComputePosition(target.position, target.rotation, index);
}
return formation.ComputePosition(leader.transform.position, leader.transform.rotation, index);
}
return leader.transform.position;
}
UnitLeader CreateVirtuaLeader()
{
GameObject leader = Instantiate(virtualLeaderPrefab, transform.position, transform.rotation);
leader.GetComponent<PatrolAction>().patrolPoints = GetComponentsInChildren<Transform>();
return leader.GetComponent<UnitLeader>();
}
#endregion
}