Skip to content

Commit c793b21

Browse files
committed
Code cleanup and documentation changes
1 parent f7f5337 commit c793b21

File tree

5 files changed

+38
-77
lines changed

5 files changed

+38
-77
lines changed

Assets/Scripts/Asteroid.cs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
/// </summary>
66
[RequireComponent(typeof(SpriteRenderer))]
77
[RequireComponent(typeof(Rigidbody2D))]
8-
[RequireComponent(typeof(BoxCollider2D))]
98
public class Asteroid : MonoBehaviour
109
{
1110
/// <summary>
@@ -48,64 +47,57 @@ public class Asteroid : MonoBehaviour
4847
/// <summary>
4948
/// The sprite renderer component attached to the asteroid.
5049
/// </summary>
51-
private SpriteRenderer _spriteRenderer;
50+
public SpriteRenderer spriteRenderer { get; private set; }
5251

5352
/// <summary>
5453
/// The rigidbody component attached to the asteroid.
5554
/// </summary>
56-
private Rigidbody2D _rigidbody;
55+
public new Rigidbody2D rigidbody { get; private set; }
5756

5857
private void Awake()
5958
{
60-
// Store references to the asteroid's components
61-
_rigidbody = GetComponent<Rigidbody2D>();
62-
_spriteRenderer = GetComponent<SpriteRenderer>();
59+
this.spriteRenderer = GetComponent<SpriteRenderer>();
60+
this.rigidbody = GetComponent<Rigidbody2D>();
6361
}
6462

6563
private void Start()
6664
{
67-
// Assign a random sprite to the asteroid
68-
_spriteRenderer.sprite = this.sprites[Random.Range(0, this.sprites.Length)];
69-
70-
// Assign a random rotation to the asteroid
65+
// Assign random properties to make each asteroid feel unique
66+
this.spriteRenderer.sprite = this.sprites[Random.Range(0, this.sprites.Length)];
7167
this.transform.eulerAngles = new Vector3(0.0f, 0.0f, Random.value * 360.0f);
7268

73-
// Set the scale and mass of the asteroid based on the assigned size
69+
// Set the scale and mass of the asteroid based on the assigned size so
70+
// the physics is more realistic
7471
this.transform.localScale = Vector3.one * this.size;
75-
_rigidbody.mass = this.size;
72+
this.rigidbody.mass = this.size;
7673

7774
// Destroy the asteroid after it reaches its max lifetime
7875
Destroy(this.gameObject, this.maxLifetime);
7976
}
8077

8178
public void SetTrajectory(Vector2 direction)
8279
{
83-
// Move the asteroid along the trajectory factoring in its speed
84-
_rigidbody.AddForce(direction * this.movementSpeed);
80+
// The asteroid only needs a force to be added once since they have no
81+
// drag to make them stop moving
82+
this.rigidbody.AddForce(direction * this.movementSpeed);
8583
}
8684

8785
private void OnCollisionEnter2D(Collision2D collision)
8886
{
89-
// Check if the asteroid was hit by a bullet
9087
if (collision.gameObject.tag == "Bullet")
9188
{
9289
// Check if the asteroid is large enough to split in half (both
9390
// parts must be greater than the minimum size)
9491
if ((this.size * 0.5f) >= this.minSize)
9592
{
96-
// Split the asteroid into two parts by creating two new
97-
// asteroids
9893
CreateSplit();
9994
CreateSplit();
10095
}
10196

102-
// Inform the game manager the asteroid was destroyed so score can
103-
// be calculated
10497
FindObjectOfType<GameManager>().AsteroidDestroyed(this);
10598

10699
// Destroy the current asteroid since it is either replaced by two
107-
// new asteroid objects or small enough to be destroyed by the
108-
// bullet
100+
// new asteroids or small enough to be destroyed by the bullet
109101
Destroy(this.gameObject);
110102
}
111103
}

Assets/Scripts/AsteroidSpawner.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public class AsteroidSpawner : MonoBehaviour
3939

4040
private void Start()
4141
{
42-
// Start spawning an asteroid at a fixed rate
4342
InvokeRepeating(nameof(Spawn), this.spawnRate, this.spawnRate);
4443
}
4544

Assets/Scripts/Bullet.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
/// Handles the physics/movement of a bullet projectile.
55
/// </summary>
66
[RequireComponent(typeof(Rigidbody2D))]
7-
[RequireComponent(typeof(BoxCollider2D))]
87
public class Bullet : MonoBehaviour
98
{
109
/// <summary>
@@ -23,27 +22,26 @@ public class Bullet : MonoBehaviour
2322
/// <summary>
2423
/// The rigidbody component attached to the bullet.
2524
/// </summary>
26-
private Rigidbody2D _rigidbody;
25+
public new Rigidbody2D rigidbody { get; private set; }
2726

2827
private void Awake()
2928
{
30-
// Store references to the bullet's components
31-
_rigidbody = GetComponent<Rigidbody2D>();
29+
this.rigidbody = GetComponent<Rigidbody2D>();
3230
}
3331

3432
public void Project(Vector2 direction)
3533
{
36-
// Move the bullet in the desired direction while factoring in the speed
37-
// of the bullet
38-
_rigidbody.AddForce(direction * this.speed);
34+
// The bullet only needs a force to be added once since they have no
35+
// drag to make them stop moving
36+
this.rigidbody.AddForce(direction * this.speed);
3937

40-
// Destroy the bullet after it reaches its max lifetime
38+
// Destroy the bullet after it reaches it max lifetime
4139
Destroy(this.gameObject, this.maxLifetime);
4240
}
4341

4442
private void OnCollisionEnter2D(Collision2D collision)
4543
{
46-
// Destroy the bullet if it collides with anything
44+
// Destroy the bullet as soon as it collides with anything
4745
Destroy(this.gameObject);
4846
}
4947

Assets/Scripts/GameManager.cs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
using UnityEngine.UI;
33

44
/// <summary>
5-
/// Manages the state of the game, such as scoring, dying, and starting a new
6-
/// game.
5+
/// Manages the state of the game such as scoring, dying, and game over.
76
/// </summary>
87
public class GameManager : MonoBehaviour
98
{
@@ -55,45 +54,39 @@ private void Start()
5554

5655
private void Update()
5756
{
58-
// Start a new game once the player presses 'Return'
5957
if (this.lives <= 0 && Input.GetKeyDown(KeyCode.Return)) {
6058
NewGame();
6159
}
6260
}
6361

6462
public void NewGame()
6563
{
66-
// Clear the scene of asteroids so we can start fresh
6764
Asteroid[] asteroids = FindObjectsOfType<Asteroid>();
65+
66+
// Clear the scene of asteroids so we start fresh
6867
for (int i = 0; i < asteroids.Length; i++) {
6968
Destroy(asteroids[i].gameObject);
7069
}
7170

72-
// Hide the game over UI
7371
this.gameOverUI.SetActive(false);
7472

75-
// Reset score and lives
7673
SetScore(0);
7774
SetLives(3);
78-
79-
// Spawn the player
8075
Respawn();
8176
}
8277

8378
public void Respawn()
8479
{
85-
// Move the player back to the center and reactivate their controls
8680
this.player.transform.position = Vector3.zero;
8781
this.player.gameObject.SetActive(true);
8882
}
8983

9084
public void AsteroidDestroyed(Asteroid asteroid)
9185
{
92-
// Play the explosion effect at the location of the asteroid
9386
this.explosionEffect.transform.position = asteroid.transform.position;
9487
this.explosionEffect.Play();
9588

96-
// Increase the score based on the size of the asteroid
89+
// Score is increased based on the size of the asteroid
9790
if (asteroid.size < 0.7f) {
9891
SetScore(this.score + 100); // small asteroid
9992
} else if (asteroid.size < 1.4f) {
@@ -105,38 +98,31 @@ public void AsteroidDestroyed(Asteroid asteroid)
10598

10699
public void PlayerDeath(Player player)
107100
{
108-
// Play the explosion effect at the location of the player
109101
this.explosionEffect.transform.position = player.transform.position;
110102
this.explosionEffect.Play();
111103

112-
// Decrement lives by 1
113104
SetLives(this.lives - 1);
114105

115-
// Check if a game over state has been reached (no lives)
116106
if (this.lives <= 0) {
117107
GameOver();
118108
} else {
119-
// Respawn the player if they have more lives
120109
Invoke(nameof(Respawn), player.respawnDelay);
121110
}
122111
}
123112

124113
public void GameOver()
125114
{
126-
// Show the game over UI
127115
this.gameOverUI.SetActive(true);
128116
}
129117

130118
private void SetScore(int score)
131119
{
132-
// Set the score and update the UI text
133120
this.score = score;
134121
this.scoreText.text = score.ToString();
135122
}
136123

137124
private void SetLives(int lives)
138125
{
139-
// Set the lives and update the UI text
140126
this.lives = lives;
141127
this.livesText.text = lives.ToString();
142128
}

Assets/Scripts/Player.cs

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
/// Handles the movement and shooting of the player ship.
55
/// </summary>
66
[RequireComponent(typeof(Rigidbody2D))]
7-
[RequireComponent(typeof(BoxCollider2D))]
87
public class Player : MonoBehaviour
98
{
109
/// <summary>
@@ -42,22 +41,21 @@ public class Player : MonoBehaviour
4241
/// <summary>
4342
/// The current direction the player is turning. 1=left, -1=right, 0=none
4443
/// </summary>
45-
private float _turnDirection = 0.0f;
44+
public float turnDirection { get; private set; } = 0.0f;
4645

4746
/// <summary>
4847
/// Whether the ship's thrusts are activated causing it to move forward.
4948
/// </summary>
50-
private bool _thrusting = false;
49+
public bool thrusting { get; private set; }
5150

5251
/// <summary>
5352
/// The rigidbody component attached to the player.
5453
/// </summary>
55-
private Rigidbody2D _rigidbody;
54+
public new Rigidbody2D rigidbody { get; private set; }
5655

5756
private void Awake()
5857
{
59-
// Store references to the player's components
60-
_rigidbody = GetComponent<Rigidbody2D>();
58+
this.rigidbody = GetComponent<Rigidbody2D>();
6159
}
6260

6361
private void OnEnable()
@@ -71,21 +69,16 @@ private void OnEnable()
7169

7270
private void Update()
7371
{
74-
// Activate thrust when pressing the 'w' key or 'up arrow' key
75-
_thrusting = Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow);
72+
this.thrusting = Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow);
7673

77-
// Set the turn direction of the ship based on which input key is being
78-
// held
7974
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) {
80-
_turnDirection = 1.0f;
75+
this.turnDirection = 1.0f;
8176
} else if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) {
82-
_turnDirection = -1.0f;
77+
this.turnDirection = -1.0f;
8378
} else {
84-
_turnDirection = 0.0f;
79+
this.turnDirection = 0.0f;
8580
}
8681

87-
// Shoot a bullet each time the 'space' key is pressed or when the mouse
88-
// left button is clicked
8982
if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) {
9083
Shoot();
9184
}
@@ -94,19 +87,18 @@ private void Update()
9487
private void FixedUpdate()
9588
{
9689
// Add force to move the ship forward
97-
if (_thrusting) {
98-
_rigidbody.AddForce(this.transform.up * this.thrustSpeed);
90+
if (this.thrusting) {
91+
this.rigidbody.AddForce(this.transform.up * this.thrustSpeed);
9992
}
10093

10194
// Add torque to rotate the ship
102-
if (_turnDirection != 0.0f) {
103-
_rigidbody.AddTorque(this.rotationSpeed * _turnDirection);
95+
if (this.turnDirection != 0.0f) {
96+
this.rigidbody.AddTorque(this.rotationSpeed * this.turnDirection);
10497
}
10598
}
10699

107100
private void Shoot()
108101
{
109-
// Spawn a bullet and project it the direction the player is aiming
110102
Bullet bullet = Instantiate(this.bulletPrefab, this.transform.position, this.transform.rotation);
111103
bullet.Project(this.transform.up);
112104
}
@@ -118,18 +110,12 @@ private void TurnOnCollisions()
118110

119111
private void OnCollisionEnter2D(Collision2D collision)
120112
{
121-
// Check if the player crashed into an asteroid
122113
if (collision.gameObject.tag == "Asteroid")
123114
{
124-
// Stop all movement of the ship
125-
_rigidbody.velocity = Vector3.zero;
126-
_rigidbody.angularVelocity = 0.0f;
127-
128-
// Stop all player controls and rendering of the ship
115+
this.rigidbody.velocity = Vector3.zero;
116+
this.rigidbody.angularVelocity = 0.0f;
129117
this.gameObject.SetActive(false);
130118

131-
// Inform the game manager the player has died so the lives can be
132-
// updated along with any other state changes
133119
FindObjectOfType<GameManager>().PlayerDeath(this);
134120
}
135121
}

0 commit comments

Comments
 (0)