Skip to content

Commit ea30cfd

Browse files
committed
Code cleanup and documentation changes
1 parent 3c82f56 commit ea30cfd

File tree

7 files changed

+62
-109
lines changed

7 files changed

+62
-109
lines changed

Assets/Scripts/Bunker.cs

+15-16
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,23 @@ public class Bunker : MonoBehaviour
1717
/// <summary>
1818
/// The sprite renderer component of the bunker.
1919
/// </summary>
20-
private SpriteRenderer _spriteRenderer;
20+
public SpriteRenderer spriteRenderer { get; private set; }
2121

2222
/// <summary>
2323
/// The box collider component of the bunker.
2424
/// </summary>
25-
private BoxCollider2D _collider;
25+
public new BoxCollider2D collider { get; private set; }
2626

2727
/// <summary>
2828
/// The original texture of the sprite so we can clone it.
2929
/// </summary>
30-
private Texture2D _originalTexture;
30+
public Texture2D originalTexture { get; private set; }
3131

3232
private void Awake()
3333
{
34-
_spriteRenderer = GetComponent<SpriteRenderer>();
35-
_collider = GetComponent<BoxCollider2D>();
36-
_originalTexture = _spriteRenderer.sprite.texture;
34+
this.spriteRenderer = GetComponent<SpriteRenderer>();
35+
this.collider = GetComponent<BoxCollider2D>();
36+
this.originalTexture = this.spriteRenderer.sprite.texture;
3737

3838
ResetBunker();
3939
}
@@ -42,7 +42,7 @@ public void ResetBunker()
4242
{
4343
// Each bunker needs a unique instance of the sprite texture since we
4444
// will be modifying it at the source
45-
CopyTexture(_originalTexture);
45+
CopyTexture(this.originalTexture);
4646

4747
this.gameObject.SetActive(true);
4848
}
@@ -58,8 +58,8 @@ private void CopyTexture(Texture2D source)
5858
copy.Apply();
5959

6060
// Create a new sprite using the copied texture
61-
Sprite sprite = Sprite.Create(copy, _spriteRenderer.sprite.rect, new Vector2(0.5f, 0.5f), _spriteRenderer.sprite.pixelsPerUnit);
62-
_spriteRenderer.sprite = sprite;
61+
Sprite sprite = Sprite.Create(copy, this.spriteRenderer.sprite.rect, new Vector2(0.5f, 0.5f), this.spriteRenderer.sprite.pixelsPerUnit);
62+
this.spriteRenderer.sprite = sprite;
6363
}
6464

6565
public bool CheckPoint(Vector3 hitPoint, out int px, out int py)
@@ -69,14 +69,14 @@ public bool CheckPoint(Vector3 hitPoint, out int px, out int py)
6969

7070
// Offset the point to the corner of the object instead of the center so
7171
// we can transform to uv coordinates
72-
localPoint.x += _collider.size.x / 2;
73-
localPoint.y += _collider.size.y / 2;
72+
localPoint.x += this.collider.size.x / 2;
73+
localPoint.y += this.collider.size.y / 2;
7474

75-
Texture2D texture = _spriteRenderer.sprite.texture;
75+
Texture2D texture = this.spriteRenderer.sprite.texture;
7676

7777
// Transform the point from local space to uv coordinates
78-
px = (int)((localPoint.x / _collider.size.x) * texture.width);
79-
py = (int)((localPoint.y / _collider.size.y) * texture.height);
78+
px = (int)((localPoint.x / this.collider.size.x) * texture.width);
79+
py = (int)((localPoint.y / this.collider.size.y) * texture.height);
8080

8181
// Return true if the pixel is not empty (not transparent)
8282
return texture.GetPixel(px, py).a != 0.0f;
@@ -92,7 +92,7 @@ public bool Splat(Vector3 hitPoint)
9292
return false;
9393
}
9494

95-
Texture2D texture = _spriteRenderer.sprite.texture;
95+
Texture2D texture = this.spriteRenderer.sprite.texture;
9696

9797
// Offset the point by half the size of the splat texture so the splat
9898
// is centered around the hit point
@@ -121,7 +121,6 @@ public bool Splat(Vector3 hitPoint)
121121
py++;
122122
}
123123

124-
// Apply any changes made to the texture
125124
texture.Apply();
126125

127126
return true;

Assets/Scripts/GameManager.cs

-23
Original file line numberDiff line numberDiff line change
@@ -65,93 +65,73 @@ private void Awake()
6565

6666
private void Start()
6767
{
68-
// Register callbacks for game state
6968
this.player.killed += OnPlayerKilled;
7069
this.mysteryShip.killed += OnMysteryShipKilled;
7170
this.invaders.killed += OnInvaderKilled;
7271

73-
// Start a new game
7472
NewGame();
7573
}
7674

7775
private void Update()
7876
{
79-
// Start a new game once the player presses 'Return'
8077
if (this.lives <= 0 && Input.GetKeyDown(KeyCode.Return)) {
8178
NewGame();
8279
}
8380
}
8481

8582
private void NewGame()
8683
{
87-
// Hide the game over UI
8884
this.gameOverUI.SetActive(false);
8985

90-
// Reset score and lives
9186
SetScore(0);
9287
SetLives(3);
93-
94-
// Start the first round
9588
NewRound();
9689
}
9790

9891
private void NewRound()
9992
{
100-
// Reset all of the invaders
10193
this.invaders.ResetInvaders();
10294
this.invaders.gameObject.SetActive(true);
10395

104-
// Reset all of the bunkers
10596
for (int i = 0; i < this.bunkers.Length; i++) {
10697
this.bunkers[i].ResetBunker();
10798
}
10899

109-
// Spawn the player
110100
Respawn();
111101
}
112102

113103
private void Respawn()
114104
{
115-
// Reset the position of the player
116105
Vector3 position = this.player.transform.position;
117106
position.x = 0.0f;
118107
this.player.transform.position = position;
119-
120-
// Re-enable the player game object
121108
this.player.gameObject.SetActive(true);
122109
}
123110

124111
private void GameOver()
125112
{
126-
// Show the game over UI and hide the invaders
127113
this.gameOverUI.SetActive(true);
128114
this.invaders.gameObject.SetActive(false);
129115
}
130116

131117
private void SetScore(int score)
132118
{
133-
// Set score and update UI text
134119
this.score = score;
135120
this.scoreText.text = this.score.ToString().PadLeft(4, '0');
136121
}
137122

138123
private void SetLives(int lives)
139124
{
140-
// Set lives and update UI text
141125
this.lives = Mathf.Max(lives, 0);
142126
this.livesText.text = this.lives.ToString();
143127
}
144128

145129
private void OnPlayerKilled()
146130
{
147-
// Decrement lives by 1
148131
SetLives(this.lives - 1);
149132

150-
// Temporarily disable the player game object
151133
this.player.gameObject.SetActive(false);
152134

153-
// Start the round over again after 1 second or trigger the game over
154-
// state if out of lives
155135
if (this.lives > 0) {
156136
Invoke(nameof(NewRound), 1.0f);
157137
} else {
@@ -161,18 +141,15 @@ private void OnPlayerKilled()
161141

162142
private void OnInvaderKilled(Invader invader)
163143
{
164-
// Increment score by how much the invader is worth
165144
SetScore(this.score + invader.score);
166145

167-
// Start a new round after all invaders have been killed
168146
if (this.invaders.AmountKilled == this.invaders.TotalAmount) {
169147
NewRound();
170148
}
171149
}
172150

173151
private void OnMysteryShipKilled(MysteryShip mysteryShip)
174152
{
175-
// Increment score by how much the mystery ship is worth
176153
SetScore(this.score + mysteryShip.score);
177154
}
178155

Assets/Scripts/Invader.cs

+8-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class Invader : MonoBehaviour
99
/// <summary>
1010
/// The sprite renderer component of the invader.
1111
/// </summary>
12-
private SpriteRenderer _spriteRenderer;
12+
public SpriteRenderer spriteRenderer { get; private set; }
1313

1414
/// <summary>
1515
/// The sprites that are animated on the invader.
@@ -26,7 +26,7 @@ public class Invader : MonoBehaviour
2626
/// <summary>
2727
/// The index of the current sprite being rendered.
2828
/// </summary>
29-
private int _animationFrame;
29+
public int animationFrame { get; private set; }
3030

3131
/// <summary>
3232
/// The amount of points the invader is worth when killed.
@@ -41,34 +41,29 @@ public class Invader : MonoBehaviour
4141

4242
private void Awake()
4343
{
44-
// Get a reference to the sprite renderer so we can animate the sprite
45-
_spriteRenderer = GetComponent<SpriteRenderer>();
46-
_spriteRenderer.sprite = this.animationSprites[0];
44+
this.spriteRenderer = GetComponent<SpriteRenderer>();
45+
this.spriteRenderer.sprite = this.animationSprites[0];
4746
}
4847

4948
private void Start()
5049
{
51-
// Start an animation loop to cycle between sprites
5250
InvokeRepeating(nameof(AnimateSprite), this.animationTime, this.animationTime);
5351
}
5452

5553
private void AnimateSprite()
5654
{
57-
// Move to the next animation frame
58-
_animationFrame++;
55+
this.animationFrame++;
5956

6057
// Loop back to the start if the animation frame exceeds the length
61-
if (_animationFrame >= this.animationSprites.Length) {
62-
_animationFrame = 0;
58+
if (this.animationFrame >= this.animationSprites.Length) {
59+
this.animationFrame = 0;
6360
}
6461

65-
// Set the sprite based on the current animation frame
66-
_spriteRenderer.sprite = this.animationSprites[_animationFrame];
62+
this.spriteRenderer.sprite = this.animationSprites[this.animationFrame];
6763
}
6864

6965
private void OnTriggerEnter2D(Collider2D other)
7066
{
71-
// The invader dies when hit by the laser
7267
if (other.gameObject.layer == LayerMask.NameToLayer("Laser")) {
7368
this.killed?.Invoke(this);
7469
}

Assets/Scripts/Invaders.cs

+3-16
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class Invaders : MonoBehaviour
2828
/// <summary>
2929
/// The initial position of the invaders so they can be reset.
3030
/// </summary>
31-
private Vector3 _initialPosition;
31+
public Vector3 initialPosition { get; private set; }
3232

3333
/// <summary>
3434
/// The callback invoked when an invader is killed.
@@ -83,8 +83,7 @@ public class Invaders : MonoBehaviour
8383

8484
private void Awake()
8585
{
86-
// Store the initial position so we can reset after each round
87-
_initialPosition = this.transform.position;
86+
this.initialPosition = this.transform.position;
8887

8988
// Form the grid of invaders
9089
for (int i = 0; i < this.rows; i++)
@@ -111,7 +110,6 @@ private void Awake()
111110

112111
private void Start()
113112
{
114-
// Invoke a missile attack every given amount of seconds
115113
InvokeRepeating(nameof(MissileAttack), this.missileSpawnRate, this.missileSpawnRate);
116114
}
117115

@@ -145,8 +143,6 @@ private void Update()
145143
{
146144
// Evaluate the speed of the invaders based on how many have been killed
147145
float speed = this.speed.Evaluate(this.PercentKilled);
148-
149-
// Move all of the invaders in the current direction
150146
this.transform.position += this.direction * speed * Time.deltaTime;
151147

152148
// Transform the viewport to world coordinates so we can check when the
@@ -190,27 +186,18 @@ private void AdvanceRow()
190186

191187
private void OnInvaderKilled(Invader invader)
192188
{
193-
// Disable the invader that was killed
194189
invader.gameObject.SetActive(false);
195190

196-
// Increment the amount of invaders killed so the game manager can check
197-
// if they are all dead
198191
this.AmountKilled++;
199-
200-
// Invoke the kill callback
201192
this.killed(invader);
202193
}
203194

204195
public void ResetInvaders()
205196
{
206-
// Reset state
207197
this.AmountKilled = 0;
208198
this.direction = Vector3.right;
199+
this.transform.position = this.initialPosition;
209200

210-
// Reset the position of the invaders back to the top
211-
this.transform.position = _initialPosition;
212-
213-
// Re-enable all of the invaders that were killed
214201
foreach (Transform invader in this.transform) {
215202
invader.gameObject.SetActive(true);
216203
}

0 commit comments

Comments
 (0)