Skip to content

Commit 1ea7233

Browse files
committed
Rename and cleanup variables
1 parent 1abf03b commit 1ea7233

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

Assets/Scripts/GameManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ private void OnInvaderKilled(Invader invader)
165165
SetScore(this.score + invader.score);
166166

167167
// Start a new round after all invaders have been killed
168-
if (this.invaders.amountKilled == this.invaders.totalAmount) {
168+
if (this.invaders.AmountKilled == this.invaders.TotalAmount) {
169169
NewRound();
170170
}
171171
}

Assets/Scripts/Invaders.cs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class Invaders : MonoBehaviour
2323
/// <summary>
2424
/// The direction the invaders are moving.
2525
/// </summary>
26-
private Vector3 _direction = Vector3.right;
26+
public Vector3 direction { get; private set; } = Vector3.right;
2727

2828
/// <summary>
2929
/// The initial position of the invaders so they can be reset.
@@ -38,12 +38,22 @@ public class Invaders : MonoBehaviour
3838
/// <summary>
3939
/// The amount of invaders that have been killed.
4040
/// </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;
4247

4348
/// <summary>
4449
/// The total amount of invaders (dead or alive).
4550
/// </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;
4757

4858
/// <summary>
4959
/// The number of rows of invaders.
@@ -107,25 +117,23 @@ private void Start()
107117

108118
private void MissileAttack()
109119
{
110-
int aliveInvaders = this.totalAmount - this.amountKilled;
120+
int amountAlive = this.AmountAlive;
111121

112122
// No missiles should spawn when no invaders are alive
113-
if (aliveInvaders == 0) {
123+
if (amountAlive == 0) {
114124
return;
115125
}
116126

117-
for (int i = 0; i < this.transform.childCount; i++)
127+
foreach (Transform invader in this.transform)
118128
{
119-
Transform invader = this.transform.GetChild(i);
120-
121129
// Any invaders that are killed cannot shoot missiles
122130
if (!invader.gameObject.activeInHierarchy) {
123131
continue;
124132
}
125133

126134
// Random chance to spawn a missile based upon how many invaders are
127135
// 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))
129137
{
130138
Instantiate(this.missilePrefab, invader.position, Quaternion.identity);
131139
break;
@@ -136,11 +144,10 @@ private void MissileAttack()
136144
private void Update()
137145
{
138146
// 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);
141148

142149
// 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;
144151

145152
// Transform the viewport to world coordinates so we can check when the
146153
// invaders reach the edge of the screen
@@ -157,12 +164,12 @@ private void Update()
157164
}
158165

159166
// 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))
161168
{
162169
AdvanceRow();
163170
break;
164171
}
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))
166173
{
167174
AdvanceRow();
168175
break;
@@ -173,7 +180,7 @@ private void Update()
173180
private void AdvanceRow()
174181
{
175182
// Flip the direction the invaders are moving
176-
_direction.x *= -1.0f;
183+
this.direction = new Vector3(-this.direction.x, 0.0f, 0.0f);
177184

178185
// Move the entire grid of invaders down a row
179186
Vector3 position = this.transform.position;
@@ -188,7 +195,7 @@ private void OnInvaderKilled(Invader invader)
188195

189196
// Increment the amount of invaders killed so the game manager can check
190197
// if they are all dead
191-
this.amountKilled++;
198+
this.AmountKilled++;
192199

193200
// Invoke the kill callback
194201
this.killed(invader);
@@ -197,8 +204,8 @@ private void OnInvaderKilled(Invader invader)
197204
public void ResetInvaders()
198205
{
199206
// Reset state
200-
this.amountKilled = 0;
201-
_direction = Vector3.right;
207+
this.AmountKilled = 0;
208+
this.direction = Vector3.right;
202209

203210
// Reset the position of the invaders back to the top
204211
this.transform.position = _initialPosition;

0 commit comments

Comments
 (0)