GameControl.cs
1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using UnityEngine;
using System.Collections;
using UnityEngine.AI;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameControl : MonoBehaviour
{
private static GameControl instance; //A reference to our game control script so we can access it statically.
public Text scoreText; //A reference to the UI text component that displays the player's score.
public GameObject gameOvertext; //A reference to the object that displays the text which appears when the player dies.
public int score { get; private set; } //The player's score.
public bool gameOver = false; //Is the game over?
public float scrollSpeed = -1.5f;
public static GameControl Instance
{
get
{
if (instance != null)
{
return instance;
}
else
{
instance = FindObjectOfType<GameControl>();
if (instance == null)
{
instance = new GameObject().AddComponent<GameControl>();
}
}
return instance;
}
}
void Update()
{
//If the game is over and the player has pressed some input...
if (gameOver && Input.GetMouseButtonDown(0))
{
//...reload the current scene.
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
public void BirdScored()
{
//The bird can't score if the game is over.
if (gameOver)
return;
//If the game is not over, increase the score...
score++;
if (scoreText != null)
{
//...and adjust the score text.
scoreText.text = "Score: " + score.ToString();
}
}
public void BirdDied()
{
if (gameOvertext != null)
{
//Activate the game over text.
gameOvertext.SetActive(true);
}
//Set the game to be over.
gameOver = true;
}
public void Reset()
{
gameOver = false;
score = 0;
}
}