-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathGameUI.tsx
More file actions
460 lines (429 loc) · 15.8 KB
/
GameUI.tsx
File metadata and controls
460 lines (429 loc) · 15.8 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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
import * as React from 'react';
import {fullscreen} from './styles/index';
import CinemaEffect from './game/CinemaEffect';
import TextBox from './game/TextBox';
import AskChoice from './game/AskChoice';
import TextInterjections from './game/TextInterjections';
import FoundObject from './game/FoundObject';
import Loader from './game/Loader';
import Video from './game/Video';
import Menu from './game/Menu';
import TeleportMenu from './game/TeleportMenu';
import Ribbon from './game/Ribbon';
import {KeyHelpIcon, KeyHelpScreen} from './game/KeyboardHelp';
import { getVideoPath } from '../resources';
import BehaviourMenu from './game/BehaviourMenu';
import Inventory from './game/Inventory';
import NoAudio from './game/NoAudio';
import UIState from './UIState';
import { SceneManager } from '../game/SceneManager';
import Renderer from '../renderer';
import Game from '../game/Game';
import { ControlsState } from '../game/ControlsState';
interface GameUIProps {
uiState: UIState;
game: Game;
renderer: Renderer;
sceneManager: SceneManager;
setUiState: (state: any, callback?: Function) => void;
sharedState?: any;
stateHandler?: any;
showMenu: (inGameMenu?: boolean, continueMenu?: boolean) => void;
hideMenu: (wasPaused?: boolean) => void;
}
interface GameUIState {
keyHelp: boolean;
}
export default class GameUI extends React.Component<GameUIProps, GameUIState> {
constructor(props) {
super(props);
this.listenerKeyDown = this.listenerKeyDown.bind(this);
this.listenerKeyUp = this.listenerKeyUp.bind(this);
this.openKeyHelp = this.openKeyHelp.bind(this);
this.closeKeyHelp = this.closeKeyHelp.bind(this);
this.startNewGameScene = this.startNewGameScene.bind(this);
this.onMenuItemChanged = this.onMenuItemChanged.bind(this);
this.closeInventory = this.closeInventory.bind(this);
this.textAnimEndedHandler = this.textAnimEndedHandler.bind(this);
this.noAudioClick = this.noAudioClick.bind(this);
this.onAskChoiceChanged = this.onAskChoiceChanged.bind(this);
this.gamepadListener = this.gamepadListener.bind(this);
this.state = {
keyHelp: false
};
}
componentWillMount() {
window.addEventListener('keydown', this.listenerKeyDown);
window.addEventListener('keyup', this.listenerKeyUp);
window.addEventListener('lbagamepadchanged', this.gamepadListener);
}
componentWillUnmount() {
window.removeEventListener('lbagamepadchanged', this.gamepadListener);
window.removeEventListener('keyup', this.listenerKeyUp);
window.removeEventListener('keydown', this.listenerKeyDown);
}
openKeyHelp() {
this.setState({keyHelp: true});
}
closeKeyHelp() {
this.setState({keyHelp: false});
}
closeInventory() {
this.props.setUiState({inventory: false});
this.props.game.resume(false);
}
isInventoryKey(key: string | number, controlsState: ControlsState) {
return key === 'ShiftLeft' || key === 'ShiftRight' ||
controlsState?.shift === 1;
}
isBehaviourKey(key: string | number, controlsState: ControlsState) {
const isMac = /^Mac/.test(navigator && navigator.platform);
if (isMac) {
return key === 'MetaLeft' || key === 'MetaRight' || controlsState?.control === 1;
}
return key === 'ControlLeft' || key === 'ControlRight' || controlsState?.control === 1;
}
showHideMenus(key: string | number, controlsState: ControlsState) {
const {
uiState,
game,
sceneManager,
stateHandler,
sharedState
} = this.props;
if (!uiState.video) {
if (key === 'Escape' || controlsState?.home === 1) {
if (sharedState && sharedState.objectToAdd) {
stateHandler.setAddingObject(null);
} else if (uiState.teleportMenu) {
this.props.setUiState({ teleportMenu: false });
} else if (!game.isPaused()) {
this.props.showMenu(true);
} else if (uiState.showMenu && uiState.inGameMenu) {
this.props.hideMenu();
}
}
const showBehaviourMenu =
!uiState.loading &&
uiState.ask.choices.length === 0 &&
uiState.text === null &&
uiState.foundObject === null &&
!(uiState.showMenu || uiState.inGameMenu) &&
!uiState.inventory &&
!uiState.cinema;
if (showBehaviourMenu && this.isBehaviourKey(key, controlsState)) {
this.props.setUiState({ behaviourMenu: true });
const scene = sceneManager.getScene();
if (!uiState.cinema && scene && scene.actors[0]) {
scene.actors[0].cancelAnims();
}
game.pause(false);
}
const showInventory =
!uiState.loading &&
uiState.ask.choices.length === 0 &&
uiState.text === null &&
uiState.foundObject === null &&
!(uiState.showMenu || uiState.inGameMenu) &&
!uiState.behaviourMenu &&
!uiState.cinema;
if (showInventory && this.isInventoryKey(key, controlsState)) {
this.props.setUiState({ inventory: !this.props.uiState.inventory });
if (game.isPaused()) {
game.resume(false);
} else {
game.pause(false);
}
}
}
}
hideBehaviourMenu(key: string | number, controlsState: ControlsState) {
const {
uiState,
game,
} = this.props;
if (!uiState.video) {
if (this.props.uiState.behaviourMenu && this.isBehaviourKey(key, controlsState)) {
this.props.setUiState({ behaviourMenu: false });
game.resume(false);
}
}
}
listenerKeyDown(event) {
this.showHideMenus(event.code, null);
}
listenerKeyUp(event) {
this.hideBehaviourMenu(event.code, null);
}
gamepadListener(event) {
const controlsState = event.detail as ControlsState;
this.showHideMenus(null, controlsState);
this.hideBehaviourMenu(null, controlsState);
}
startNewGameScene() {
const { game, sceneManager } = this.props;
game.resume();
game.resetState();
sceneManager.goto(0, true);
}
saveGame() {
const { game, sceneManager } = this.props;
const state = game.getState();
state.save(sceneManager.getScene().actors[0]);
state.hero.animState = null; // Reset hero anim state
state.scene.index = sceneManager.getScene().index;
localStorage.setItem('game_state', JSON.stringify(state));
game.setUiState({hasSaveGame: true});
}
async continueGameScene() {
const { game, sceneManager } = this.props;
game.resume();
game.continueState();
const scene = await sceneManager.goto(game.getState().scene.index, true, false, true);
game.getState().load(JSON.stringify(game.getState()), scene.actors[0]);
}
onMenuItemChanged(item) {
const { game } = this.props;
switch (item) {
case 70: { // Resume
this.props.hideMenu();
break;
}
case 73: { // Save Game
if (game.isPaused()) {
game.resume();
}
this.props.setUiState({showMenu: false, inGameMenu: true});
this.saveGame();
this.props.setUiState({hasSaveGame: true});
break;
}
case 69: { // Continue
this.props.hideMenu();
this.continueGameScene();
break;
}
case 71: { // New Game
this.props.hideMenu();
const onEnded = () => {
this.props.setUiState({video: null});
this.startNewGameScene();
game.controlsState.skipListener = null;
};
game.controlsState.skipListener = onEnded;
game.pause();
const videoPath = getVideoPath('INTRO');
if (videoPath !== undefined) {
this.props.setUiState({
video: {
path: videoPath,
onEnded
}
});
} else {
onEnded();
}
break;
}
case -1: { // Teleport
this.props.setUiState({teleportMenu: true});
break;
}
case -2: { // Editor Mode
const audio = game.getAudioManager();
audio.stopMusicTheme();
if ('exitPointerLock' in document) {
document.exitPointerLock();
}
if (window.location.hash) {
window.location.hash = `${window.location.hash}&editor=true`;
} else {
window.location.hash = 'editor=true';
}
break;
}
case -3: { // Exit editor
const audio = game.getAudioManager();
audio.stopMusicTheme();
if ('exitPointerLock' in document) {
document.exitPointerLock();
}
window.location.hash = '';
break;
}
case -4: { // Enable Iso 3d
if (window.location.hash) {
window.location.hash = `${window.location.hash}&iso3d=true`;
} else {
window.location.hash = 'iso3d=true';
}
location.reload();
break;
}
case -5: { // Disable Iso 3d
window.location.hash = window.location.hash.replace('iso3d=true', '');
location.reload();
break;
}
case -6: { // Switch to LBA1
window.location.hash = window.location.hash.replace('&game=lba2', '');
window.location.hash = window.location.hash.replace('#game=lba2', '');
if (window.location.hash) {
window.location.hash = `${window.location.hash}&game=lba1`;
} else {
window.location.hash = 'game=lba1';
}
location.reload();
break;
}
case -7: { // Switch to LBA2
window.location.hash = window.location.hash.replace('&game=lba1', '');
window.location.hash = window.location.hash.replace('#game=lba1', '');
if (window.location.hash) {
window.location.hash = `${window.location.hash}&game=lba2`;
} else {
window.location.hash = 'game=lba2';
}
location.reload();
break;
}
case -8: { // Enable 3d Audio
if (window.location.hash) {
window.location.hash = `${window.location.hash}&audio3d=true`;
} else {
window.location.hash = 'audio3d=true';
}
location.reload();
break;
}
case -9: { // Disable 3d Audio
window.location.hash = window.location.hash.replace('audio3d=true', '');
location.reload();
break;
}
}
}
onAskChoiceChanged(choice) {
this.props.setUiState({choice});
}
textAnimEndedHandler() {
this.props.setUiState({ skip: true });
}
async noAudioClick() {
const { uiState } = this.props;
const audio = this.props.game.getAudioManager();
audio.resumeContext();
this.props.setUiState({ noAudio: false }, () => {
if (uiState.showMenu) {
audio.playMusicTheme();
}
});
}
render() {
const {
game,
renderer,
sceneManager,
uiState
} = this.props;
const {
cinema,
interjections,
video,
behaviourMenu,
inventory,
showMenu,
teleportMenu,
inGameMenu,
hasSaveGame,
loading,
text,
skip,
foundObject,
ask,
noAudio
} = uiState;
const { keyHelp } = this.state;
const scene = sceneManager.getScene();
return <React.Fragment>
<CinemaEffect enabled={cinema} />
<TextInterjections
scene={scene}
renderer={renderer}
interjections={interjections}
/>
<Video video={video} renderer={renderer} />
{behaviourMenu ?
<BehaviourMenu
game={game}
scene={scene}
/>
: null }
{inventory ?
<Inventory
game={game}
scene={scene}
closeInventory={this.closeInventory}
/>
: null }
<Menu
showMenu={showMenu && !teleportMenu}
inGameMenu={inGameMenu}
hasSaveGame={hasSaveGame}
onItemChanged={this.onMenuItemChanged}
/>
{showMenu && !teleportMenu
&& <KeyHelpIcon open={this.openKeyHelp}/>}
<Ribbon mode={showMenu ? 'menu' : 'game'} />
{teleportMenu
&& <TeleportMenu
inGameMenu={inGameMenu}
game={game}
sceneManager={sceneManager}
exit={(e) => {
e.preventDefault();
e.stopPropagation();
this.props.setUiState({teleportMenu: false});
}}
/>}
<div id="stats" style={{position: 'absolute', top: 0, left: 0, width: '50%'}}/>
{loading ? <Loader/> : null}
{!showMenu ? <TextBox
text={text}
skip={skip}
textAnimEnded={this.textAnimEndedHandler}
/> : null}
{!showMenu ? <AskChoice
ask={ask}
onChoiceChanged={this.onAskChoiceChanged}
/> : null}
{foundObject !== null && !showMenu ? <FoundObject foundObject={foundObject} /> : null}
{this.renderNewObjectPickerOverlay()}
{keyHelp && <KeyHelpScreen close={this.closeKeyHelp}/>}
{noAudio && (
<NoAudio onClick={this.noAudioClick} />
)}
</React.Fragment>;
}
renderNewObjectPickerOverlay() {
const { sharedState } = this.props;
if (sharedState && sharedState.objectToAdd) {
const baseBannerStyle = {
...fullscreen,
height: 30,
lineHeight: '30px',
fontSize: 16,
background: 'rgba(0, 0, 128, 0.5)',
color: 'white'
};
const headerStyle = { ...baseBannerStyle, bottom: 'initial' };
const footerStyle = { ...baseBannerStyle, top: 'initial' };
return <React.Fragment>
<div style={headerStyle}>
Pick a location for the new {sharedState.objectToAdd.type}...
</div>
<div style={footerStyle}/>
</React.Fragment>;
}
}
}