-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameManager.cs
More file actions
160 lines (127 loc) · 3.15 KB
/
GameManager.cs
File metadata and controls
160 lines (127 loc) · 3.15 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
using UnityEngine;
using TMPro;
using System.Collections;
public class GameManager : MonoBehaviour
{
public static GameManager Instance;
[Header("계단")]
[Space(10)]
public GameObject[] Stairs;
public bool[] isTurn;
private enum State {Start,Left,Right };
private State state;
private Vector3 oldPosition;
[Header("UI")]
[Space(10)]
public GameObject UI_GameOver;
public TextMeshProUGUI textMaxScore;
public TextMeshProUGUI textNowScore;
public TextMeshProUGUI textShowScore;
private int maxScore = 0;
private int nowScore = 0;
[Header("Audio")]
[Space(10)]
private AudioSource sound;
public AudioClip bgmSound;
public AudioClip dieSound;
void Start()
{
Instance = this;
sound = GetComponent<AudioSource>();
Init();
InitStairs();
}
public void Init()
{
state = State.Start;
oldPosition = Vector3.zero;
isTurn = new bool[Stairs.Length];
for (int i = 0; i < Stairs.Length; i++)
{
Stairs[i].transform.position = Vector3.zero;
isTurn[i] = false;
}
nowScore = 0;
textShowScore.text = nowScore.ToString();
UI_GameOver.SetActive(false);
sound.clip = bgmSound;
sound.Play();
sound.loop = true;
sound.volume = 0.4f;
}
public void InitStairs()
{
for(int i = 0; i < Stairs.Length; i++)
{
switch(state)
{
case State.Start:
Stairs[i].transform.position = new Vector3(0.75f, -0.1f, 0);
state = State.Right;
break;
case State.Left:
Stairs[i].transform.position = oldPosition + new Vector3(-0.75f, 0.5f, 0);
isTurn[i] = true;
break;
case State.Right:
Stairs[i].transform.position = oldPosition + new Vector3(0.75f, 0.5f, 0);
isTurn[i] = false;
break;
}
oldPosition = Stairs[i].transform.position;
if(i != 0)
{
int ran = Random.Range(0,5);
if(ran < 2 && i < Stairs.Length - 1)
{
state = state == State.Left ? State.Right : State.Left;
}
}
}
}
public void SpawnStair(int cnt)
{
int ran = Random.Range(0,5);
if(ran < 2)
{
state = state == State.Left ? State.Right : State.Left;
}
switch(state)
{
case State.Left:
Stairs[cnt].transform.position = oldPosition + new Vector3(-0.75f, 0.5f, 0);
isTurn[cnt] = true;
break;
case State.Right:
Stairs[cnt].transform.position = oldPosition + new Vector3(0.75f, 0.5f, 0);
isTurn[cnt] = false;
break;
}
oldPosition = Stairs[cnt].transform.position;
}
public void GameOver()
{
sound.loop = false;
sound.Stop();
sound.clip = dieSound;
sound.Play();
sound.volume = 1;
StartCoroutine(ShowGameOver());
}
IEnumerator ShowGameOver()
{
yield return new WaitForSeconds(1f);
UI_GameOver.SetActive(true);
if(nowScore > maxScore)
{
maxScore = nowScore;
}
textMaxScore.text = maxScore.ToString();
textNowScore.text = nowScore.ToString();
}
public void AddScore()
{
nowScore++;
textShowScore.text = nowScore.ToString();
}
}