Skip to content

Commit 981aba5

Browse files
committed
Allow invaders to destroy bunkers and the player
1 parent 2a1cc8b commit 981aba5

File tree

6 files changed

+42
-26
lines changed

6 files changed

+42
-26
lines changed

Assets/Prefabs/Invader_Base.prefab

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ GameObject:
1010
m_Component:
1111
- component: {fileID: 8922502034929116583}
1212
- component: {fileID: 8922502034929116582}
13+
- component: {fileID: -4447578080803963419}
1314
- component: {fileID: 8922502034929116585}
1415
- component: {fileID: 1551307908973724939}
1516
m_Layer: 7
@@ -84,6 +85,27 @@ SpriteRenderer:
8485
m_WasSpriteAssigned: 0
8586
m_MaskInteraction: 0
8687
m_SpriteSortPoint: 0
88+
--- !u!50 &-4447578080803963419
89+
Rigidbody2D:
90+
serializedVersion: 4
91+
m_ObjectHideFlags: 0
92+
m_CorrespondingSourceObject: {fileID: 0}
93+
m_PrefabInstance: {fileID: 0}
94+
m_PrefabAsset: {fileID: 0}
95+
m_GameObject: {fileID: 8922502034929116581}
96+
m_BodyType: 1
97+
m_Simulated: 1
98+
m_UseFullKinematicContacts: 0
99+
m_UseAutoMass: 0
100+
m_Mass: 1
101+
m_LinearDrag: 0
102+
m_AngularDrag: 0.05
103+
m_GravityScale: 1
104+
m_Material: {fileID: 0}
105+
m_Interpolate: 0
106+
m_SleepingMode: 1
107+
m_CollisionDetection: 0
108+
m_Constraints: 0
87109
--- !u!61 &8922502034929116585
88110
BoxCollider2D:
89111
m_ObjectHideFlags: 0

Assets/Scenes/Space Invaders.unity

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,6 @@ MonoBehaviour:
724724
m_RotationOrder: 4
725725
rows: 5
726726
columns: 11
727-
advances: 10
728727
missilePrefab: {fileID: 6161936928069685931, guid: 3c328e673b90a7f418f1f1e66f83522b, type: 3}
729728
missileSpawnRate: 1
730729
--- !u!1 &1001845995

Assets/Scripts/Bunker.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public void ResetBunker()
4343
// Each bunker needs a unique instance of the sprite texture since we
4444
// will be modifying it at the source
4545
CopyTexture(_originalTexture);
46+
47+
this.gameObject.SetActive(true);
4648
}
4749

4850
private void CopyTexture(Texture2D source)
@@ -139,4 +141,11 @@ public bool CheckCollision(BoxCollider2D other, Vector3 hitPoint)
139141
Splat(hitPoint + (Vector3.right * offset.x));
140142
}
141143

144+
private void OnTriggerEnter2D(Collider2D other)
145+
{
146+
if (other.gameObject.layer == LayerMask.NameToLayer("Invader")) {
147+
this.gameObject.SetActive(false);
148+
}
149+
}
150+
142151
}

Assets/Scripts/GameManager.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private void Start()
7777
private void Update()
7878
{
7979
// Start a new game once the player presses 'Return'
80-
if (this.lives == 0 && Input.GetKeyDown(KeyCode.Return)) {
80+
if (this.lives <= 0 && Input.GetKeyDown(KeyCode.Return)) {
8181
NewGame();
8282
}
8383
}
@@ -138,7 +138,7 @@ private void SetScore(int score)
138138
private void SetLives(int lives)
139139
{
140140
// Set lives and update UI text
141-
this.lives = lives;
141+
this.lives = Mathf.Max(lives, 0);
142142
this.livesText.text = this.lives.ToString();
143143
}
144144

@@ -150,10 +150,10 @@ private void OnPlayerKilled()
150150
// Temporarily disable the player game object
151151
this.player.gameObject.SetActive(false);
152152

153-
// Respawn the player after 1 second or trigger the game over state
154-
// after running out of lives
153+
// Start the round over again after 1 second or trigger the game over
154+
// state if out of lives
155155
if (this.lives > 0) {
156-
Invoke(nameof(Respawn), 1.0f);
156+
Invoke(nameof(NewRound), 1.0f);
157157
} else {
158158
GameOver();
159159
}

Assets/Scripts/Invaders.cs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,6 @@ public class Invaders : MonoBehaviour
5858
[Tooltip("The number of columns of invaders.")]
5959
public int columns = 11;
6060

61-
/// <summary>
62-
/// The number of times the invaders can advance rows.
63-
/// </summary>
64-
[Tooltip("The number of times the invaders can advance rows.")]
65-
public int advances = 10;
66-
67-
/// <summary>
68-
/// The current row the invaders have advanced to.
69-
/// </summary>
70-
private int _row;
71-
7261
/// <summary>
7362
/// The prefab that is cloned when an invader shoots a missile.
7463
/// </summary>
@@ -187,12 +176,9 @@ private void AdvanceRow()
187176
_direction.x *= -1.0f;
188177

189178
// Move the entire grid of invaders down a row
190-
if (_row++ < this.advances)
191-
{
192-
Vector3 position = this.transform.position;
193-
position.y -= 1.0f;
194-
this.transform.position = position;
195-
}
179+
Vector3 position = this.transform.position;
180+
position.y -= 1.0f;
181+
this.transform.position = position;
196182
}
197183

198184
private void OnInvaderKilled(Invader invader)
@@ -213,7 +199,6 @@ public void ResetInvaders()
213199
// Reset state
214200
this.amountKilled = 0;
215201
_direction = Vector3.right;
216-
_row = 0;
217202

218203
// Reset the position of the invaders back to the top
219204
this.transform.position = _initialPosition;

Assets/Scripts/Player.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ private void OnLaserDestroyed(Projectile laser)
6666

6767
private void OnTriggerEnter2D(Collider2D other)
6868
{
69-
// The player is killed when hit by a missile
70-
if (other.gameObject.layer == LayerMask.NameToLayer("Missile")) {
69+
// The player is killed when hit by a missile or invader
70+
if (other.gameObject.layer == LayerMask.NameToLayer("Missile") ||
71+
other.gameObject.layer == LayerMask.NameToLayer("Invader")) {
7172
this.killed?.Invoke();
7273
}
7374
}

0 commit comments

Comments
 (0)