-
Notifications
You must be signed in to change notification settings - Fork 0
/
reduce.ts
63 lines (56 loc) · 2.08 KB
/
reduce.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
import * as effectful from '../ui/use-effectful-reducer';
import { produce } from '../util/produce';
import { Action } from './action';
import { Effect, SoundEffect } from './effect-types';
import { getLowAction, resolveLowAction } from './low-actions';
import { mkGameSceneState } from './mkGameState';
import { SceneState } from './scene-state';
export function keyCaptured(keyCode: string): boolean {
switch (keyCode) {
case 'C-S-i': return false;
case 'C-S-r': return false;
case 'C-r': return false;
case 'C-l': return false;
default: return true;
}
}
function effectOfSoundEffect(se: SoundEffect): Effect {
return { t: 'soundEffect', sound: se };
}
function extractEffects(state: SceneState): { cleanState: SceneState, effects: Effect[] } {
switch (state.t) {
case 'game': {
let ses = state.gameState.coreState.soundEffects;
if (ses.length == 0) {
return { cleanState: state, effects: [] };
}
if (ses.length > 1)
ses = [ses[0]];
const effects: Effect[] = ses.map(effectOfSoundEffect);
return { cleanState: { t: 'game', revision: 0, gameState: produce(state.gameState, s => { s.coreState.soundEffects = []; }) }, effects }
}
case 'menu': // fallthrough intentional
case 'instructions':
return { cleanState: state, effects: [] };
}
}
export function reduce(scState: SceneState, action: Action): effectful.Result<SceneState, Effect> {
switch (action.t) {
case 'resize': return { state: scState, effects: [] }; // XXX maybe stash viewdata this in state somewhere?
case 'newGame':
return {
state: mkGameSceneState(Date.now(), action.creative ?? false, Date.now()), effects: [],
};
case 'setSceneState':
return { state: action.state, effects: [] };
default:
if (scState.t == 'game') {
const state = resolveLowAction(scState, getLowAction(scState.gameState, action));
const { cleanState, effects } = extractEffects(state);
return { state: cleanState, effects };
}
else {
return { state: scState, effects: [] };
}
}
}