generated from robo-technical-group/javascript-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pausemenu.ts
280 lines (244 loc) · 7.43 KB
/
pausemenu.ts
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Refer to pxt-common-packages/libs/game/systemmenu.ts
namespace PauseMenu {
enum Items {
SaveGame = 0,
ShowActionQueue,
ManageGames,
VolumeDown,
VolumeUp,
BrightnessDown,
BrightnessUp,
Stats,
Console,
Sleep,
RunTests,
Close,
}
const MENU_TEXT: string[] = [
'Save game',
'Show action queue',
'Manage game saves',
'Volume down',
'Volume up',
'Brightness down',
'Brightness up',
'Show stats',
'Show console',
'Sleep',
'Run tests',
'Close',
]
const MENU_TEXT_ALTERNATE: string[] = [
'',
'',
'',
'',
'',
'',
'',
'Hide stats',
'Hide console',
'',
'Stop tests',
'Close',
]
const MENU_TITLE: string = 'Pause Menu'
const TESTS_PROMPT: string = 'Restart game to start/stop tests.'
const VOLUMES: number[] = [0, 32, 64, 96, 128, 160, 192, 224, 255]
const Z: number = 255
let isMenuRunning: boolean = false
let isShowingStats: boolean = false
let isShowingConsole: boolean = false
let menu: miniMenu.MenuSprite = null
let priorMode: GameMode = GameMode.NotReady
let volume: number = 4
function changeBrightness(delta: number): void {
if ((delta < 0 && screen.brightness() > 0) ||
(delta > 0 && screen.brightness() < 100)) {
screen.setBrightness(screen.brightness() + delta)
updateBrightness()
}
}
function changeVolume(delta: number): void {
if (
(delta < 0 && volume > 0) ||
(delta > 0 && volume < VOLUMES.length - 1)
) {
if (delta < 0) {
volume--
} else {
volume++
}
updateVolume()
playVolume()
}
}
export function disableEvents(): void {
if (isMenuRunning) {
menu.buttonEventsEnabled = false
}
}
export function enableEvents(): void {
if (isMenuRunning) {
menu.buttonEventsEnabled = true
}
}
export function hide(): void {
if (isMenuRunning) {
menu.setFlag(SpriteFlag.Invisible, true)
menu.buttonEventsEnabled = false
}
}
export function menuRunning(): boolean {
return isMenuRunning
}
export function menuVisible(): boolean {
return isMenuRunning &&
((menu.flags & SpriteFlag.Invisible) == 0)
}
function playVolume(): void {
music.playTone(440, 500)
}
function processSelection(selection: string, selectedIndex: number): void {
switch (selectedIndex) {
case Items.ShowActionQueue:
showActionQueue()
break
case Items.VolumeDown:
changeVolume(-1)
break
case Items.VolumeUp:
changeVolume(1)
break
case Items.Stats:
toggleStats()
break
case Items.Console:
toggleConsole()
break
case Items.Sleep:
sleep()
break
case Items.Close:
release()
break
case Items.SaveGame:
GameStateUI.save(priorMode)
break
case Items.BrightnessUp:
changeBrightness(5)
break
case Items.BrightnessDown:
changeBrightness(-5)
break
case Items.ManageGames:
GameStateUI.manage()
break
case Items.RunTests:
toggleTests()
break
}
}
export function release(): void {
menu.close()
isMenuRunning = false
g_state.Mode = priorMode
}
export function show(): void {
if (isMenuRunning) {
menu.setFlag(SpriteFlag.Invisible, false)
menu.buttonEventsEnabled = true
return
}
priorMode = g_state.Mode
g_state.Mode = GameMode.PauseMenu
let menuItems: miniMenu.MenuItem[] = []
for (let t of MENU_TEXT) {
menuItems.push(miniMenu.createMenuItem(t))
}
menu = miniMenu.createMenuFromArray(menuItems)
menu.setTitle(MENU_TITLE)
menu.setMenuStyleProperty(miniMenu.MenuStyleProperty.Width, 140)
menu.setMenuStyleProperty(miniMenu.MenuStyleProperty.Height, 100)
menu.setStyleProperty(miniMenu.StyleKind.Title,
miniMenu.StyleProperty.Foreground, Color.White)
menu.setStyleProperty(miniMenu.StyleKind.Title,
miniMenu.StyleProperty.Background, Color.Wine)
menu.top = 10
menu.left = 10
menu.z = Z
isShowingConsole = game.consoleOverlay.isVisible()
isShowingStats = game.stats
updateBrightness()
updateConsole()
updateStats()
updateVolume()
updateTests()
menu.onButtonPressed(controller.A, processSelection)
menu.onButtonPressed(controller.B, processSelection)
isMenuRunning = true
}
function showActionQueue(): void {
let message: string = ''
if (g_state == null) {
message = 'Game state has not yet been initialized.'
} else {
message = g_state.ActionQueueString
}
game.showLongText(message, DialogLayout.Full)
}
function sleep(): void {
power.deepSleep()
}
function toggleConsole(): void {
isShowingConsole = !isShowingConsole
game.consoleOverlay.setVisible(isShowingConsole)
updateConsole()
}
function toggleStats(): void {
isShowingStats = !isShowingStats
game.stats = isShowingStats
updateStats()
}
function toggleTests(): void {
if (settings.exists(Tests.TESTING_KEY)) {
settings.remove(Tests.TESTING_KEY)
} else {
settings.writeNumber(Tests.TESTING_KEY, 0)
}
game.splash(TESTS_PROMPT)
updateTests()
}
function updateBrightness(): void {
menu.items[Items.BrightnessDown].text = MENU_TEXT[Items.BrightnessDown] +
' (' + screen.brightness() + ')'
menu.items[Items.BrightnessUp].text = MENU_TEXT[Items.BrightnessUp] +
' (' + screen.brightness() + ')'
}
function updateConsole(): void {
menu.items[Items.Console].text = isShowingConsole ?
MENU_TEXT_ALTERNATE[Items.Console] :
MENU_TEXT[Items.Console]
}
function updateStats(): void {
menu.items[Items.Stats].text = isShowingStats ?
MENU_TEXT_ALTERNATE[Items.Stats] :
MENU_TEXT[Items.Stats]
if (!isShowingStats && control.EventContext.onStats) {
control.EventContext.onStats('');
}
}
function updateTests(): void {
menu.items[Items.RunTests].text = settings.exists(Tests.TESTING_KEY) ?
MENU_TEXT_ALTERNATE[Items.RunTests] :
MENU_TEXT[Items.RunTests]
}
function updateVolume(): void {
let v: number = VOLUMES[volume]
music.setVolume(v)
menu.items[Items.VolumeDown].text = MENU_TEXT[Items.VolumeDown] +
' (' + v + ')'
menu.items[Items.VolumeUp].text = MENU_TEXT[Items.VolumeUp] +
' (' + v + ')'
}
}