-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontroller.js
162 lines (143 loc) · 4.26 KB
/
controller.js
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
var state = {}
var g_time = 0.0
function CreateInitialDirectorsFromWorld(world) {
// Note: directors are created even for disabled actors
// TODO: optimize which directors are in memory. we probably don't need them for disabled actors.
let directors = world.actors.map( actor => {
return {
director_type: actor.director,
id: actor.id
}
})
return directors
}
function StartGame() {
let resource_list = [
"resources/world.yaml",
"resources/model.yaml",
"resources/spritesheet.json",
"images/rivercity-school.gif",
"images/alex.png",
"images/ryan.png",
"images/yellow_rect.png",
"images/red_rect.png",
"images/black_rect.png",
"images/game-over.png",
]
LoadResources(resource_list).then( (resources) => {
let world = resources["resources/world.yaml"]
let directors = CreateInitialDirectorsFromWorld(world)
state = {
time: 0.0,
frame_num: 0,
resources: resources,
world: world,
directors: directors,
requested_directions: [],
system_directions: [],
signals: ["initialized"], // A signal on the first frame
user_input: {},
triggers_fired: [],
scripts_fired: [],
future_signals: [],
camera: { position: {x: 0, y: 0}, target: {x: 0, y: 0}, size: { width: 320, height: 240 } }
}
Tick()
}, () => {
console.log("Error loading resources")
})
}
function Tick() {
if (!g_paused) {
let user_input = {...GetKeyState()}
let frame_time = 1.0 / g_framerate
g_time = g_time + frame_time
state = f_State(state, user_input, g_time)
//console.log("Tick. " + state.frame_num)
//console.log(state)
let sprites = f_SpritesFromState(state)
let preprocessed_sprites = PreprocessedSprites(sprites, state.resources, state.camera)
let hud_sprites = HudSprites(state)
preprocessed_sprites = preprocessed_sprites.concat(hud_sprites)
RenderSprites(g_canvas, preprocessed_sprites)
// Play audio
let signal_ids = state.signals.map(signal => signal.id)
if (signal_ids.includes("sfx_oof")) {
console.log("oof")
PlayNote()
}
}
// Set up next tick call
const time_delta = 1000.0 / g_framerate
setTimeout(Tick, time_delta)
}
function PlayNote() {
let envelope = {
attack_time: 0.05,
attack_gain: 1.0,
decay_time: 0.1,
sustain_gain: 0.8,
release_time: 0.20,
}
let noise = {
oscillator: kNoiseWave,
envelope: envelope,
}
let square = {
oscillator: kSquareWave,
envelope: envelope,
}
let synth_commands = [
{
action_type: kSynthesizerAction_PlayTone,
channel: 0,
instrument: noise,
freq: 220,
time: g_audio_time + 0.05
},
{
action_type: kSynthesizerAction_ReleaseTone,
channel: 0,
time: g_audio_time + 0.15
},
{
action_type: kSynthesizerAction_PlayTone,
channel: 1,
instrument: square,
freq: 180,
time: g_audio_time
},
{
action_type: kSynthesizerAction_ReleaseTone,
channel: 1,
time: g_audio_time + 0.1
},
{
action_type: kSynthesizerAction_PlayTone,
channel: 2,
instrument: square,
freq: 60,
time: g_audio_time + 0.1
},
{
action_type: kSynthesizerAction_ReleaseTone,
channel: 2,
time: g_audio_time + 0.4
},
{
action_type: kSynthesizerAction_PlayTone,
channel: 3,
instrument: square,
freq: 120,
time: g_audio_time + 0.2
},
{
action_type: kSynthesizerAction_ReleaseTone,
channel: 3,
time: g_audio_time + 0.3
},
]
if (g_synthesizer != null) {
g_synthesizer = f_next_synthesizer_state(g_synthesizer, synth_commands)
}
}