/
EnemyDestructionMonitorScript.cs
242 lines (214 loc) · 7.96 KB
/
EnemyDestructionMonitorScript.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
namespace Assets.Scripts.Levels.Common.Attack
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jundroo.SimplePlanes.ModTools;
using Jundroo.SimplePlanes.ModTools.PrefabProxies;
using Jundroo.SimplePlanes.ModTools.AI;
using SimplePlanesReflection.Assets.Scripts.Levels.Enemies;
using SimplePlanesReflection.Assets.Scripts.Parts.Modifiers.AircraftAi;
using UnityEngine;
/// <summary>
/// A script that monitors multiple enemies, setting a flag when they are all destroyed.
/// </summary>
/// <seealso cref="UnityEngine.MonoBehaviour" />
public class EnemyDestructionMonitorScript : MonoBehaviour
{
/// <summary>
/// The ground vehicles to monitor.
/// </summary>
private List<SimpleGroundVehicleScript> _groundVehicles;
/// <summary>
/// The ships that must be defeated in order to complete the level.
/// </summary>
[SerializeField]
private ShipProxy[] _objectiveShips = null;
/// <summary>
/// The tanks that must be defeated in order to complete the level.
/// </summary>
[SerializeField]
private AntiAircraftTankProxy[] _objectiveTanks = null;
/// <summary>
/// The turrets that must be defeated in order to complete the level.
/// </summary>
[SerializeField]
private RotatingWeaponProxy[] _objectiveTurrets = null;
[SerializeField]
private AIAircraftSpawner[] _objectiveAircraft = null;
/// <summary>
/// Gets a value indicating whether all objectives have been destroyed.
/// </summary>
/// <value>
/// <c>true</c> if all objectives have been destroyed; otherwise, <c>false</c>.
/// </value>
public bool AllObjectivesDestroyed { get; private set; }
/// <summary>
/// Gets the total number of objective targets.
/// </summary>
/// <value>
/// The total number of objective targets.
/// </value>
public int ObjectivesTotalCount
{
get
{
return
this._objectiveShips.Length +
this._objectiveTanks.Length +
this._objectiveTurrets.Length +
this._objectiveAircraft.Length +
(this._groundVehicles == null ? 0 : this._groundVehicles.Count);
}
}
public int store = 0;
/// <summary>
/// Adds a ship objective to monitor.
/// </summary>
/// <param name="ship">The ship objective to be added.</param>
public void AddObjective(ShipProxy ship)
{
if (this._objectiveShips == null)
{
this._objectiveShips = new ShipProxy[1] { ship };
return;
}
Array.Resize(ref this._objectiveShips, this._objectiveShips.Length + 1);
this._objectiveShips[this._objectiveShips.Length - 1] = ship;
}
/// <summary>
/// Adds a tank objective to monitor.
/// </summary>
/// <param name="tank">The tank objective to be added.</param>
public void AddObjective(AntiAircraftTankProxy tank)
{
if (this._objectiveTanks == null)
{
this._objectiveTanks = new AntiAircraftTankProxy[1] { tank };
return;
}
Array.Resize(ref this._objectiveTanks, this._objectiveTanks.Length + 1);
this._objectiveTanks[this._objectiveTanks.Length - 1] = tank;
}
/// <summary>
/// Adds the ground vehicle objective to monitor.
/// </summary>
/// <param name="vehicle">The vehicle objective to be added.</param>
public void AddObjective(SimpleGroundVehicleScript vehicle)
{
if (this._groundVehicles == null)
{
this._groundVehicles = new List<SimpleGroundVehicleScript>();
}
this._groundVehicles.Add(vehicle);
}
/// <summary>
/// Adds a turret objective to monitor.
/// </summary>
/// <param name="turret">The turret objective to be added.</param>
public void AddObjective(RotatingWeaponProxy turret)
{
if (this._objectiveTurrets == null)
{
this._objectiveTurrets = new RotatingWeaponProxy[1] { turret };
return;
}
Array.Resize(ref this._objectiveTurrets, this._objectiveTurrets.Length + 1);
this._objectiveTurrets[this._objectiveTurrets.Length - 1] = turret;
}
public void AddObjective(AIAircraftSpawner aircraft)
{
if (this._objectiveAircraft == null)
{
this._objectiveAircraft = new AIAircraftSpawner[1] { aircraft };
return;
}
Array.Resize(ref this._objectiveAircraft, this._objectiveAircraft.Length + 1);
this._objectiveAircraft[this._objectiveAircraft.Length - 1] = aircraft;
}
/// <summary>
/// Start is called on the frame when a script is enabled just before any of the Update methods is called the first time.
/// </summary>
protected virtual void Start()
{
this.StartCoroutine(this.MonitorLevelObjectivesCoroutine());
}
/// <summary>
/// The coroutine that monitors the level objectives.
/// </summary>
/// <returns>The enumerator for the coroutine.</returns>
private IEnumerator MonitorLevelObjectivesCoroutine()
{
while (true)
{
for (int i = 0; i < 5; i++)
{
yield return null;
}
int allDestroyed = 0;
if (this._objectiveTanks != null)
{
for (int i = 0; i < this._objectiveTanks.Length; i++)
{
if (this._objectiveTanks[i] != null && this._objectiveTanks[i].IsDead)
{
allDestroyed++;
}
}
}
if (this._objectiveShips != null)
{
for (int i = 0; i < this._objectiveShips.Length; i++)
{
if (this._objectiveShips[i] != null && this._objectiveShips[i].IsCriticallyDamaged)
{
allDestroyed++;
}
}
}
if (this._objectiveTurrets != null)
{
for (int i = 0; i < this._objectiveTurrets.Length; i++)
{
if (this._objectiveTurrets[i] != null && this._objectiveTurrets[i].IsDisabled)
{
allDestroyed++;
}
}
}
if (this._groundVehicles != null)
{
for (int i = 0; i < this._groundVehicles.Count; i++)
{
if (this._groundVehicles[i] != null && this._groundVehicles[i].IsDestroyed)
{
allDestroyed++;
}
}
}
if (this._objectiveAircraft != null)
{
for (int i = 0; i < this._objectiveAircraft.Length; i++)
{
if (this._objectiveAircraft[i] != null && this._objectiveAircraft[i].AircraftScript.CriticallyDamaged)
{
allDestroyed++;
}
}
}
if (allDestroyed != store)
{
store = allDestroyed;
ServiceProvider.Instance.GameWorld.ShowStatusMessage(allDestroyed + "/" + ObjectivesTotalCount + " targets destroyed", 5);
}
if (allDestroyed == ObjectivesTotalCount)
{
this.AllObjectivesDestroyed = true;
////break;
}
}
}
}
}