SHOW:
|
|
- or go back to the newest paste.
1 | using System.Collections.Generic; | |
2 | using UnityEngine; | |
3 | using UnityEngine.SceneManagement; | |
4 | using TMPro; | |
5 | ||
6 | public class GameManager : MonoBehaviour | |
7 | { | |
8 | public static GameManager instance; | |
9 | public List<GameObject> bricks = new List<GameObject>(); | |
10 | public ArcanoidBall ball; | |
11 | public bool gameRun = false; | |
12 | ||
13 | public GameObject EndGameText; | |
14 | public TMP_Text bricksCounter; | |
15 | public TMP_Text livesCounter; | |
16 | public TMP_Text levelCounter; | |
17 | public TMP_Text winText; | |
18 | ||
19 | int currentLevel; | |
20 | public int maxLevel = 3; | |
21 | public BricksGenerator generator; | |
22 | public int lives; | |
23 | ||
24 | ||
25 | void Awake() | |
26 | { | |
27 | currentLevel = 1; | |
28 | lives = 3; | |
29 | ||
30 | EndGameText.SetActive(false); | |
31 | ||
32 | if (instance == null) | |
33 | { | |
34 | instance = this; | |
35 | } | |
36 | ||
37 | //bricks.AddRange(GameObject.FindGameObjectsWithTag("Brick")); | |
38 | ||
39 | } | |
40 | ||
41 | void Update() | |
42 | { | |
43 | //Rozpoczęcie gry | |
44 | if (Input.GetKeyDown(KeyCode.Space) && !gameRun) | |
45 | { | |
46 | ball.RunBall(); | |
47 | gameRun = true; | |
48 | } | |
49 | ||
50 | ||
51 | //Koniec gry przy stracie całego życia | |
52 | if (gameRun && lives <= 0) | |
53 | { | |
54 | EndGame(false); | |
55 | } | |
56 | ||
57 | ||
58 | //Koniec gry w przypadku zbicia wszystkich bloczków na ostatnim poziomie | |
59 | if (gameRun && bricks.Count == 0 && currentLevel == maxLevel) | |
60 | { | |
61 | EndGame(true); | |
62 | } | |
63 | ||
64 | //// | |
65 | if ((gameRun && bricks.Count == 0 && currentLevel < maxLevel)) | |
66 | { | |
67 | currentLevel++; | |
68 | ball.StopBall(); | |
69 | generator.StartLevel(currentLevel); | |
70 | } | |
71 | ||
72 | ||
73 | ||
74 | if (!gameRun) | |
75 | { | |
76 | if(Input.GetKeyDown(KeyCode.R)) | |
77 | { | |
78 | SceneManager.LoadScene(0); | |
79 | } | |
80 | } | |
81 | } | |
82 | ||
83 | ||
84 | //// | |
85 | public void EndGame(bool win) | |
86 | { | |
87 | EndGameText.SetActive(true); | |
88 | gameRun = false; | |
89 | winText.text = "You: " +(win ? "Win!" : "Lose!"); | |
90 | ball.StopBall(); | |
91 | } | |
92 | ||
93 | // | |
94 | public void UpdateUI() | |
95 | { | |
96 | bricksCounter.text = "Bricks left: " + bricks.Count; | |
97 | livesCounter.text = "Lives: " + lives.ToString(); | |
98 | levelCounter.text = "Level: " + currentLevel.ToString(); | |
99 | } | |
100 | ||
101 | } | |
102 |