Skip to content

Commit 138829b

Browse files
committed
Reset bunkers after starting a new round
1 parent 73502a7 commit 138829b

File tree

4 files changed

+72
-42
lines changed

4 files changed

+72
-42
lines changed

Assets/Scenes/Space Invaders.unity

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,6 @@ MonoBehaviour:
245245
m_Script: {fileID: 11500000, guid: 459f605e13eced949a32904be2fa0a25, type: 3}
246246
m_Name:
247247
m_EditorClassIdentifier:
248-
player: {fileID: 1639391302}
249-
invaders: {fileID: 962212320}
250-
mysteryShip: {fileID: 1154390492}
251248
scoreText: {fileID: 1984502043}
252249
livesText: {fileID: 188796889}
253250
gameOverUI: {fileID: 797358150}

Assets/Scripts/Bunker.cs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,41 @@ public class Bunker : MonoBehaviour
2424
/// </summary>
2525
private BoxCollider2D _collider;
2626

27+
/// <summary>
28+
/// The original texture of the sprite so we can clone it.
29+
/// </summary>
30+
private Texture2D _originalTexture;
31+
2732
private void Awake()
2833
{
2934
_spriteRenderer = GetComponent<SpriteRenderer>();
3035
_collider = GetComponent<BoxCollider2D>();
36+
_originalTexture = _spriteRenderer.sprite.texture;
3137

32-
// Each bunker needs a unique instance of the sprite since we will be
33-
// changing the source texture
34-
CreateSpriteInstance();
38+
ResetBunker();
3539
}
3640

37-
private void CreateSpriteInstance()
41+
public void ResetBunker()
3842
{
39-
Sprite sprite = _spriteRenderer.sprite;
43+
// Each bunker needs a unique instance of the sprite texture since we
44+
// will be modifying it at the source
45+
CopyTexture(_originalTexture);
46+
}
4047

48+
private void CopyTexture(Texture2D source)
49+
{
4150
// Create a copy of the source texture with the same properties
42-
Texture2D texture = new Texture2D(sprite.texture.width, sprite.texture.height, sprite.texture.format, false);
43-
texture.filterMode = sprite.texture.filterMode;
44-
texture.alphaIsTransparency = sprite.texture.alphaIsTransparency;
45-
texture.anisoLevel = sprite.texture.anisoLevel;
46-
texture.wrapMode = sprite.texture.wrapMode;
47-
texture.SetPixels(sprite.texture.GetPixels());
48-
texture.Apply();
49-
50-
// Create a new sprite using the cloned texture
51-
Sprite instance = Sprite.Create(texture, sprite.rect, new Vector2(0.5f, 0.5f), sprite.pixelsPerUnit);
52-
_spriteRenderer.sprite = instance;
51+
Texture2D copy = new Texture2D(source.width, source.height, source.format, false);
52+
copy.filterMode = source.filterMode;
53+
copy.alphaIsTransparency = source.alphaIsTransparency;
54+
copy.anisoLevel = source.anisoLevel;
55+
copy.wrapMode = source.wrapMode;
56+
copy.SetPixels(source.GetPixels());
57+
copy.Apply();
58+
59+
// Create a new sprite using the copied texture
60+
Sprite sprite = Sprite.Create(copy, _spriteRenderer.sprite.rect, new Vector2(0.5f, 0.5f), _spriteRenderer.sprite.pixelsPerUnit);
61+
_spriteRenderer.sprite = sprite;
5362
}
5463

5564
public bool CheckPoint(Vector3 hitPoint, out int px, out int py)

Assets/Scripts/GameManager.cs

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,22 @@ public sealed class GameManager : MonoBehaviour
1010
/// <summary>
1111
/// A reference to the player game object.
1212
/// </summary>
13-
[Tooltip("A reference to the player game object.")]
14-
public Player player;
13+
private Player player;
1514

1615
/// <summary>
1716
/// A reference to the invaders game object.
1817
/// </summary>
19-
[Tooltip("A reference to the invaders game object.")]
20-
public Invaders invaders;
18+
private Invaders invaders;
2119

2220
/// <summary>
2321
/// A reference to the MysteryShip game object.
2422
/// </summary>
25-
[Tooltip("A reference to the MysteryShip game object.")]
26-
public MysteryShip mysteryShip;
23+
private MysteryShip mysteryShip;
24+
25+
/// <summary>
26+
/// A reference to all of the bunker game objects.
27+
/// </summary>
28+
private Bunker[] bunkers;
2729

2830
/// <summary>
2931
/// The UI text that displays the player's score.
@@ -53,6 +55,14 @@ public sealed class GameManager : MonoBehaviour
5355
/// </summary>
5456
public int lives { get; private set; }
5557

58+
private void Awake()
59+
{
60+
this.player = FindObjectOfType<Player>();
61+
this.invaders = FindObjectOfType<Invaders>();
62+
this.mysteryShip = FindObjectOfType<MysteryShip>();
63+
this.bunkers = FindObjectsOfType<Bunker>();
64+
}
65+
5666
private void Start()
5767
{
5868
// Register callbacks for game state
@@ -74,28 +84,32 @@ private void Update()
7484

7585
private void NewGame()
7686
{
87+
// Hide the game over UI
88+
this.gameOverUI.SetActive(false);
89+
7790
// Reset score and lives
7891
SetScore(0);
7992
SetLives(3);
8093

94+
// Start the first round
95+
NewRound();
96+
}
97+
98+
private void NewRound()
99+
{
81100
// Reset all of the invaders
82101
this.invaders.ResetInvaders();
83102
this.invaders.gameObject.SetActive(true);
84103

85-
// Hide the game over UI
86-
this.gameOverUI.SetActive(false);
104+
// Reset all of the bunkers
105+
for (int i = 0; i < this.bunkers.Length; i++) {
106+
this.bunkers[i].ResetBunker();
107+
}
87108

88109
// Spawn the player
89110
Respawn();
90111
}
91112

92-
private void GameOver()
93-
{
94-
// Show the game over UI and hide the invaders
95-
this.gameOverUI.SetActive(true);
96-
this.invaders.gameObject.SetActive(false);
97-
}
98-
99113
private void Respawn()
100114
{
101115
// Reset the position of the player
@@ -107,6 +121,13 @@ private void Respawn()
107121
this.player.gameObject.SetActive(true);
108122
}
109123

124+
private void GameOver()
125+
{
126+
// Show the game over UI and hide the invaders
127+
this.gameOverUI.SetActive(true);
128+
this.invaders.gameObject.SetActive(false);
129+
}
130+
110131
private void SetScore(int score)
111132
{
112133
// Set score and update UI text
@@ -142,6 +163,11 @@ private void OnInvaderKilled(Invader invader)
142163
{
143164
// Increment score by how much the invader is worth
144165
SetScore(this.score + invader.score);
166+
167+
// Start a new round after all invaders have been killed
168+
if (this.invaders.amountKilled == this.invaders.totalAmount) {
169+
NewRound();
170+
}
145171
}
146172

147173
private void OnMysteryShipKilled(MysteryShip mysteryShip)

Assets/Scripts/Invaders.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,12 @@ private void Update()
168168
}
169169

170170
// Check the left edge or right edge based on the current direction
171-
if (_direction.x > 0.0f && invader.position.x >= (rightEdge.x - 1.0f))
171+
if (_direction == Vector3.right && invader.position.x >= (rightEdge.x - 1.0f))
172172
{
173173
AdvanceRow();
174174
break;
175175
}
176-
else if (invader.position.x <= (leftEdge.x + 1.0f))
176+
else if (_direction == Vector3.left && invader.position.x <= (leftEdge.x + 1.0f))
177177
{
178178
AdvanceRow();
179179
break;
@@ -200,14 +200,12 @@ private void OnInvaderKilled(Invader invader)
200200
// Disable the invader that was killed
201201
invader.gameObject.SetActive(false);
202202

203+
// Increment the amount of invaders killed so the game manager can check
204+
// if they are all dead
205+
this.amountKilled++;
206+
203207
// Invoke the kill callback
204208
this.killed(invader);
205-
206-
// Increment the amount of invaders that have been killed and reset all
207-
// of them after they have all been killed
208-
if (++this.amountKilled >= this.totalAmount) {
209-
ResetInvaders();
210-
}
211209
}
212210

213211
public void ResetInvaders()

0 commit comments

Comments
 (0)