@@ -23,7 +23,7 @@ public class Invaders : MonoBehaviour
23
23
/// <summary>
24
24
/// The direction the invaders are moving.
25
25
/// </summary>
26
- private Vector3 _direction = Vector3 . right ;
26
+ public Vector3 direction { get ; private set ; } = Vector3 . right ;
27
27
28
28
/// <summary>
29
29
/// The initial position of the invaders so they can be reset.
@@ -38,12 +38,22 @@ public class Invaders : MonoBehaviour
38
38
/// <summary>
39
39
/// The amount of invaders that have been killed.
40
40
/// </summary>
41
- public int amountKilled { get ; private set ; }
41
+ public int AmountKilled { get ; private set ; }
42
+
43
+ /// <summary>
44
+ /// The amount of invaders that are still alive.
45
+ /// </summary>
46
+ public int AmountAlive => this . TotalAmount - this . AmountKilled ;
42
47
43
48
/// <summary>
44
49
/// The total amount of invaders (dead or alive).
45
50
/// </summary>
46
- public int totalAmount => this . rows * this . columns ;
51
+ public int TotalAmount => this . rows * this . columns ;
52
+
53
+ /// <summary>
54
+ /// The percentage of invaders that have been killed.
55
+ /// </summary>
56
+ public float PercentKilled => ( float ) this . AmountKilled / ( float ) this . TotalAmount ;
47
57
48
58
/// <summary>
49
59
/// The number of rows of invaders.
@@ -107,25 +117,23 @@ private void Start()
107
117
108
118
private void MissileAttack ( )
109
119
{
110
- int aliveInvaders = this . totalAmount - this . amountKilled ;
120
+ int amountAlive = this . AmountAlive ;
111
121
112
122
// No missiles should spawn when no invaders are alive
113
- if ( aliveInvaders == 0 ) {
123
+ if ( amountAlive == 0 ) {
114
124
return ;
115
125
}
116
126
117
- for ( int i = 0 ; i < this . transform . childCount ; i ++ )
127
+ foreach ( Transform invader in this . transform )
118
128
{
119
- Transform invader = this . transform . GetChild ( i ) ;
120
-
121
129
// Any invaders that are killed cannot shoot missiles
122
130
if ( ! invader . gameObject . activeInHierarchy ) {
123
131
continue ;
124
132
}
125
133
126
134
// Random chance to spawn a missile based upon how many invaders are
127
135
// alive (the more invaders alive the lower the chance)
128
- if ( Random . value < ( 1.0f / ( float ) aliveInvaders ) )
136
+ if ( Random . value < ( 1.0f / ( float ) amountAlive ) )
129
137
{
130
138
Instantiate ( this . missilePrefab , invader . position , Quaternion . identity ) ;
131
139
break ;
@@ -136,11 +144,10 @@ private void MissileAttack()
136
144
private void Update ( )
137
145
{
138
146
// Evaluate the speed of the invaders based on how many have been killed
139
- int totalInvaders = this . columns * this . rows ;
140
- float speed = this . speed . Evaluate ( ( float ) this . amountKilled / ( float ) this . totalAmount ) ;
147
+ float speed = this . speed . Evaluate ( this . PercentKilled ) ;
141
148
142
149
// Move all of the invaders in the current direction
143
- this . transform . position += _direction * speed * Time . deltaTime ;
150
+ this . transform . position += this . direction * speed * Time . deltaTime ;
144
151
145
152
// Transform the viewport to world coordinates so we can check when the
146
153
// invaders reach the edge of the screen
@@ -157,12 +164,12 @@ private void Update()
157
164
}
158
165
159
166
// Check the left edge or right edge based on the current direction
160
- if ( _direction == Vector3 . right && invader . position . x >= ( rightEdge . x - 1.0f ) )
167
+ if ( this . direction == Vector3 . right && invader . position . x >= ( rightEdge . x - 1.0f ) )
161
168
{
162
169
AdvanceRow ( ) ;
163
170
break ;
164
171
}
165
- else if ( _direction == Vector3 . left && invader . position . x <= ( leftEdge . x + 1.0f ) )
172
+ else if ( this . direction == Vector3 . left && invader . position . x <= ( leftEdge . x + 1.0f ) )
166
173
{
167
174
AdvanceRow ( ) ;
168
175
break ;
@@ -173,7 +180,7 @@ private void Update()
173
180
private void AdvanceRow ( )
174
181
{
175
182
// Flip the direction the invaders are moving
176
- _direction . x *= - 1.0f ;
183
+ this . direction = new Vector3 ( - this . direction . x , 0.0f , 0.0f ) ;
177
184
178
185
// Move the entire grid of invaders down a row
179
186
Vector3 position = this . transform . position ;
@@ -188,7 +195,7 @@ private void OnInvaderKilled(Invader invader)
188
195
189
196
// Increment the amount of invaders killed so the game manager can check
190
197
// if they are all dead
191
- this . amountKilled ++ ;
198
+ this . AmountKilled ++ ;
192
199
193
200
// Invoke the kill callback
194
201
this . killed ( invader ) ;
@@ -197,8 +204,8 @@ private void OnInvaderKilled(Invader invader)
197
204
public void ResetInvaders ( )
198
205
{
199
206
// Reset state
200
- this . amountKilled = 0 ;
201
- _direction = Vector3 . right ;
207
+ this . AmountKilled = 0 ;
208
+ this . direction = Vector3 . right ;
202
209
203
210
// Reset the position of the invaders back to the top
204
211
this . transform . position = _initialPosition ;
0 commit comments