Slash Hero Game

16.592 lượt xem;
1 using System.Collections;
2 using
System.Collections.Generic;
3 using
UnityEngine;
4 using
UnityEngine.SceneManagement;
5 using
UnityEngine.UI;
6
7 public
class GameplayController : MonoBehaviour {
8     
public static GameplayController instance;
9     
10     
public bool gameInProgress, gameInPause;
11     
public GameObject pausePanel, gameOverPanel;
12     
public float scoreMultiplier;
13     
public Text scoreText, gameoverScoreText, highScoreText;
14
15     
[HideInInspector]
16     
public bool playerDied;
17
18     
[HideInInspector]
19     
public float score;
20
21     
void Awake(){
22         score =
0f;
23         CreateInstance ();
24         gameInProgress =
true;
25     }
26         
27
28     
// Use this for initialization
29     
void Start () {
30         
if (GameController.instance.isMusicOn) {
31             
if (MusicController.instance.audioSource.isPlaying) {
32                 MusicController.instance.StopBgMusic ();
33             }
34             MusicController.instance.PlayGameplayMusic ();
35         }
else {
36             MusicController.instance.StopBgMusic ();
37         }
38
39
40     }
41     
42     
// Update is called once per frame
43     
void Update () {
44         UpdateGameplay ();
45     }
46
47     
void CreateInstance(){
48         
if(this != null){
49             instance =
this;
50         }
51     }
52
53     
public void PauseGame(){
54         
if(gameInProgress){
55             
if(!gameInPause){
56                 Time.timeScale =
0;
57                 gameInPause =
true;
58                 gameInProgress =
false;
59                 pausePanel.SetActive (
true);
60             }
61         }
62     }
63
64     
public void ResumeGame(){
65         
if(!gameInProgress){
66             
if (gameInPause) {
67                 Time.timeScale =
1;
68                 pausePanel.SetActive (
false);
69                 gameInPause =
false;
70                 gameInProgress =
true;
71             }
72         }
73     }
74
75     
public void RestartGame(){
76         Time.timeScale =
1;
77         SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex);
78     }
79
80     
public void ExitGame(){
81         Time.timeScale =
1;
82         SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex -
1);
83     }
84
85     
public void GameOver(){
86         gameOverPanel.SetActive (
true);
87         gameInProgress =
false;
88         playerDied =
true;
89         
if(GameController.instance.isMusicOn){
90             
if(MusicController.instance.audioSource.isPlaying){
91                 MusicController.instance.StopBgMusic ();
92             }
93         }
94         AudioSource.PlayClipAtPoint (MusicController.instance.audioClips [
4], Camera.main.transform.position);
95         gameoverScoreText.text =
"" + GameController.instance.currentScore;
96         
if(GameController.instance.currentScore > GameController.instance.highScore){
97             GameController.instance.highScore = GameController.instance.currentScore;
98             GameController.instance.Save ();
99             highScoreText.gameObject.SetActive (
true);
100         }
101     }
102
103     
void UpdateGameplay(){
104         
if(gameInProgress){
105             score += scoreMultiplier * Time.deltaTime;
106             scoreText.text =
"" + (int)score;
107             GameController.instance.currentScore = (
int)score;
108         }
109     }
110 }


Gõ tìm kiếm nhanh...