gameController.cs
5.6 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Vuforia;
using UnityEngine.SceneManagement;
public class gameController : DefaultTrackableEventHandler
{
public static gameController instance;
public int playerScore = 0;
public Text scoreCountText;
public Text livesCountText;
public int round = 1;
public Text roundTextTargetText;
public Text roundTextNumber;
public Text GameOverScoreText;
public int shotsPerRound = 3;
private int lives = 2;
public GameObject[] shells;
public GameObject GUIScoreText;
public GameObject GUILivesText;
public GameObject GUICenterTarget;
public GameObject GUIFireButton;
public GameObject GUIDog;
public GameObject GUIRoundText;
public GameObject GUIGameOverPanel;
public GameObject startPanel;
public GameObject Terrain;
public GameObject GUIGun;
AudioSource audio;
public AudioClip[] clips;
//Round Rules
private int roundTargetScore = 3;
public int roundScore = 0;
private int scoreIncrement = 2;
public bool playerStarted = false;
void Awake() {
if(instance == null) {
instance = this;
}
}
// Start is called before the first frame update
void Start()
{
playerScore = int.Parse(scoreCountText.text);
showStartPanel();
audio = GetComponent<AudioSource>();
livesCountText.text = lives.ToString();
}
private void playFX(int sound) {
audio.clip = clips[sound];
audio.Play();
}
void Update()
{
if(DefaultTrackableEventHandler.trueFalse == true) {
hideStartPanel();
showItems();
if(playerStarted == false) {
StartCoroutine(playRound());
}
playerStarted = true;
}else {
showStartPanel();
hideItems();
}
if(roundScore == roundTargetScore) {
playFX(0);
StartCoroutine(newRound());
roundScore = 0;
roundTargetScore = roundTargetScore+scoreIncrement;
}
if(shotsPerRound == 0) {
shells[0].SetActive(false);
StartCoroutine(loseLife());
shotsPerRound = 3;
}
scoreCountText.text = playerScore.ToString();
}
public void showItems() {
GUIScoreText.SetActive(true);
GUILivesText.SetActive(true);
GUICenterTarget.SetActive(true);
GUIFireButton.SetActive(true);
Terrain.SetActive(true);
GUIGun.SetActive(true);
showShells();
}
public void hideItems() {
GUIScoreText.SetActive(false);
GUILivesText.SetActive(false);
GUICenterTarget.SetActive(false);
GUIFireButton.SetActive(false);
Terrain.SetActive(false);
GUIGun.SetActive(false);
}
public IEnumerator playRound() {
yield return new WaitForSeconds(2f);
roundTextTargetText.text = "SHOOT " + roundTargetScore + " DUCKS";
GUIRoundText.SetActive(true);
playFX(0);
StartCoroutine(hideRoundText());
}
private IEnumerator newRound() {
yield return new WaitForSeconds(3);
round++;
GUIRoundText.SetActive(true);
roundTextTargetText.text = "SHOOT " + roundTargetScore + " DUCKS";
roundTextNumber.text = round.ToString();
StartCoroutine(hideRoundText());
}
private IEnumerator hideRoundText() {
yield return new WaitForSeconds(4);
GUIRoundText.SetActive(false);
}
public void showShells() {
if(shotsPerRound == 3) {
shells[0].SetActive(true);
shells[1].SetActive(true);
shells[2].SetActive(true);
}else if (shotsPerRound == 2) {
shells[0].SetActive(true);
shells[1].SetActive(true);
shells[2].SetActive(false);
} else if (shotsPerRound == 1) {
shells[0].SetActive(true);
shells[1].SetActive(false);
shells[2].SetActive(false);
}
}
private void showStartPanel() {
startPanel.SetActive(true);
}
private void hideStartPanel() {
startPanel.SetActive(false);
}
private IEnumerator loseLife() {
lives--;
if(lives == 0) {
GUIFireButton.SetActive(false);
playFX(1);
GUIGameOverPanel.SetActive(true);
GameOverScoreText.text = playerScore.ToString();
lives = 0;
}else {
GUIFireButton.SetActive(true);
playFX(2);
GUIDog.SetActive(true);
yield return new WaitForSeconds(3);
GUIDog.SetActive(false);
GUIFireButton.SetActive(true);
shotsPerRound = 3;
}
yield return new WaitForSeconds(0);
livesCountText.text = lives.ToString();
}
public void restart() {
hideItems();
lives = 2;
livesCountText.text = lives.ToString();
playerScore = 0;
scoreCountText.text = playerScore.ToString();
roundTargetScore = 3;
roundScore = 0;
GameOverScoreText.text = "0";
round = 1;
roundTextNumber.text = round.ToString();
GUIGameOverPanel.SetActive(false);
StartCoroutine(playRound());
}
public void quit() {
Application.Quit();
//SceneManager.LoadScene("intro");
}
}