Skip to content

Commit 4db351b

Browse files
committed
Add variables for respawn delay and invulnerability
1 parent 31d158a commit 4db351b

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

Assets/Scripts/GameManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public void PlayerDeath(Player player)
119119
GameOver();
120120
} else {
121121
// Respawn the player if they have more lives
122-
Invoke(nameof(Respawn), 3.0f);
122+
Invoke(nameof(Respawn), player.respawnDelay);
123123
}
124124
}
125125

Assets/Scripts/Player.cs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
using UnityEngine;
22

33
/// <summary>
4-
/// Handles the movement and shooting of the
5-
/// player ship.
4+
/// Handles the movement and shooting of the player ship.
65
/// </summary>
76
[RequireComponent(typeof(Rigidbody2D))]
87
[RequireComponent(typeof(BoxCollider2D))]
@@ -20,21 +19,33 @@ public class Player : MonoBehaviour
2019
[Tooltip("How quickly the player is able to turn.")]
2120
public float rotationSpeed = 0.1f;
2221

22+
/// <summary>
23+
/// The amount of seconds it takes for the player to respawn after dying.
24+
/// </summary>
25+
[Tooltip("The amount of seconds it takes for the player to respawn after dying.")]
26+
public float respawnDelay = 3.0f;
27+
28+
/// <summary>
29+
/// The amount of seconds the player has invulnerability after respawning.
30+
/// This is to prevent the player from instantly dying if spawning into an
31+
/// asteroid.
32+
/// </summary>
33+
[Tooltip("The amount of seconds the player has invulnerability after respawning. This is to prevent the player from instantly dying if spawning into an asteroid.")]
34+
public float respawnInvulnerability = 3.0f;
35+
2336
/// <summary>
2437
/// The object that is cloned when creating a bullet.
2538
/// </summary>
2639
[Tooltip("The object that is cloned when creating a bullet.")]
2740
public Bullet bulletPrefab;
2841

2942
/// <summary>
30-
/// The current direction the player is turning.
31-
/// 1=left, -1=right, 0=none
43+
/// The current direction the player is turning. 1=left, -1=right, 0=none
3244
/// </summary>
3345
private float _turnDirection = 0.0f;
3446

3547
/// <summary>
36-
/// Whether the ship's thrusts are activated causing
37-
/// it to move forward.
48+
/// Whether the ship's thrusts are activated causing it to move forward.
3849
/// </summary>
3950
private bool _thrusting = false;
4051

@@ -51,21 +62,20 @@ private void Awake()
5162

5263
private void OnEnable()
5364
{
54-
// Turn off collisions for a few seconds after
55-
// spawning to ensure the player has enough
56-
// time to safely move away from asteroids
65+
// Turn off collisions for a few seconds after spawning to ensure the
66+
// player has enough time to safely move away from asteroids
5767
this.gameObject.layer = LayerMask.NameToLayer("Ignore Collisions");
5868

59-
Invoke(nameof(TurnOnCollisions), 3.0f);
69+
Invoke(nameof(TurnOnCollisions), this.respawnInvulnerability);
6070
}
6171

6272
private void Update()
6373
{
6474
// Activate thrust when pressing the 'w' key or 'up arrow' key
6575
_thrusting = Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow);
6676

67-
// Set the turn direction of the ship based on
68-
// which input key is being held
77+
// Set the turn direction of the ship based on which input key is being
78+
// held
6979
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) {
7080
_turnDirection = 1.0f;
7181
} else if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) {
@@ -74,8 +84,8 @@ private void Update()
7484
_turnDirection = 0.0f;
7585
}
7686

77-
// Shoot a bullet each time the 'space' key is pressed
78-
// or when the mouse left button is clicked
87+
// Shoot a bullet each time the 'space' key is pressed or when the mouse
88+
// left button is clicked
7989
if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) {
8090
Shoot();
8191
}
@@ -118,9 +128,8 @@ private void OnCollisionEnter2D(Collision2D collision)
118128
// Stop all player controls and rendering of the ship
119129
this.gameObject.SetActive(false);
120130

121-
// Inform the game manager the player has died
122-
// so the lives can be updated along with any
123-
// other state changes
131+
// Inform the game manager the player has died so the lives can be
132+
// updated along with any other state changes
124133
FindObjectOfType<GameManager>().PlayerDeath(this);
125134
}
126135
}

0 commit comments

Comments
 (0)