Skip to content

Commit dd86663

Browse files
author
ויויאן אומנסקי
committed
final game
1 parent e7e3f38 commit dd86663

File tree

3 files changed

+254
-29
lines changed

3 files changed

+254
-29
lines changed

Assets/LevelManager.cs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,39 @@
44

55
public class LevelManager : MonoBehaviour
66
{
7-
public float levelTime = 30f; // Time allocated per level
8-
public TextMeshProUGUI timerText; // Timer display
7+
public float levelTime = 30f; // Total time for the level
8+
public TextMeshProUGUI timerText; // Timer display text
9+
910
private float remainingTime;
10-
private bool isLevelCompleted = false; // Prevents double triggers
11+
private bool isLevelCompleted = false; // Prevents timer from running after completion
1112

1213
private void Start()
1314
{
14-
remainingTime = levelTime; // Initialize timer
15+
remainingTime = levelTime; // Initialize timer
16+
UpdateTimerUI(); // Show initial time
1517
}
1618

1719
private void Update()
20+
{
21+
if (!isLevelCompleted && remainingTime > 0)
22+
{
23+
remainingTime -= Time.deltaTime;
24+
UpdateTimerUI();
25+
}
26+
else if (!isLevelCompleted && remainingTime <= 0)
27+
{
28+
Debug.Log("Time's up! Restarting level...");
29+
ReloadCurrentLevel(); // Restart if time runs out
30+
}
31+
}
32+
33+
// **ADD THIS METHOD** to stop the timer when the level is completed
34+
public void CompleteLevel()
1835
{
1936
if (!isLevelCompleted)
2037
{
21-
if (remainingTime > 0)
22-
{
23-
remainingTime -= Time.deltaTime;
24-
UpdateTimerUI();
25-
}
26-
else
27-
{
28-
Debug.Log("Time's up! Restarting level...");
29-
ReloadCurrentLevel(); // If time runs out
30-
}
38+
Debug.Log("Level Completed!");
39+
isLevelCompleted = true; // Stop the timer
3140
}
3241
}
3342

Assets/Scenes/Level3.unity

Lines changed: 216 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/MagneticTarget.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33

44
public class MagneticTarget : MonoBehaviour
55
{
6-
public GameObject hitEffect; // Particle effect
7-
public TextMeshProUGUI gameCompleteText; // "Game Complete" text
8-
public LevelManager levelManager; // Reference to LevelManager
9-
public float delayBeforeEnd = 2f; // Delay before ending the game
6+
public GameObject hitEffect; // Particle effect
7+
public GameObject gameCompletePanel; // Game complete screen
8+
public TextMeshProUGUI gameCompleteText; // Completion message
9+
public LevelManager levelManager; // Reference to the LevelManager
10+
public float delayBeforeMessage = 2f; // Delay before showing the message
1011

11-
private bool isGameCompleted = false; // Prevent multiple triggers
12+
private bool isGameCompleted = false; // Prevent multiple triggers
1213

1314
private void OnTriggerEnter2D(Collider2D collision)
1415
{
@@ -17,20 +18,21 @@ private void OnTriggerEnter2D(Collider2D collision)
1718
Debug.Log("Magnet Ball Hit the Final Target!");
1819
isGameCompleted = true;
1920

21+
// Stop the timer in LevelManager
22+
levelManager.CompleteLevel();
23+
2024
// Trigger the particle effect
2125
Instantiate(hitEffect, transform.position, Quaternion.identity);
2226

23-
// Show "Game Complete!" text
24-
gameCompleteText.gameObject.SetActive(true);
25-
26-
// End the game after a delay
27-
Invoke(nameof(EndGame), delayBeforeEnd);
27+
// Display "Game Complete!" message after delay
28+
Invoke(nameof(ShowEndScreen), delayBeforeMessage);
2829
}
2930
}
3031

31-
private void EndGame()
32+
private void ShowEndScreen()
3233
{
33-
Debug.Log("Game Completed! Exiting...");
34-
Application.Quit(); // Works only in builds
34+
Debug.Log("Congratulations! You Completed the Game!");
35+
gameCompletePanel.SetActive(true);
36+
gameCompleteText.text = "Congratulations! You Completed the Game!";
3537
}
3638
}

0 commit comments

Comments
 (0)