-
Notifications
You must be signed in to change notification settings - Fork 0
/
game-design.html
232 lines (198 loc) · 5.27 KB
/
game-design.html
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
<html>
<head>
<title>NEJS 2016 - Game Design</title>
<style>
body {
background: #777;
display: flex;
align-items: center;
justify-content: center;
height: 100%;
font-size: 30px;
color: #333;
margin: 0;
overflow: hidden;
padding: 0;
width: 100%;
}
.character {
background: white;
height: 50px;
left: 0;
position: absolute;
top: 0;
transition: .3s;
width: 50px;
z-index: 2;
}
.target {
background: black;
height: 50px;
position: absolute;
right: 0;
top: 50%;
transform: translateY(-50px);
width: 50px;
z-index: 1;
}
</style>
</head>
<body>
<p>Move towards the target using the arrow keys</p>
<div data-character class="character"></div>
<div data-target class="target"></div>
<script>
// create web audio context
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
// DOM nodes
const ui = {
character: document.querySelector('[data-character]'),
target: document.querySelector('[data-target]'),
};
// store window dimensions
const dimensions = {
width: window.innerWidth,
height: window.innerHeight,
};
// target position
const targetPosition = {
left: dimensions.width - 50,
top: (dimensions.height / 2) - 25,
};
// create gain node
const gainNode = audioCtx.createGain();
gainNode.gain.value = .8;
// create biquad filter node
const biquadFilter = audioCtx.createBiquadFilter();
// set biquad filter values
biquadFilter.type = "lowpass";
biquadFilter.frequency.value = 0;
biquadFilter.Q.value = 10;
// create oscillators
const oscillators = {
low: audioCtx.createOscillator(),
mid: audioCtx.createOscillator(),
high: audioCtx.createOscillator(),
};
// arpeggio notes
const arpeggio = {
notes: [
[261.63, 523.26, 1046.52],
[293.66, 587.32, 1174.64],
[311.13, 622.26, 1244.52],
[392, 784, 1568],
],
currentNote: 0,
maxFilter: 1200,
speed: window.innerWidth,
};
// oscillator initial settings
oscillators.low.type = 'sine';
oscillators.low.frequency.value = arpeggio.notes[0][0];
oscillators.mid.type = 'sine';
oscillators.mid.frequency.value = arpeggio.notes[0][1];
oscillators.high.type = 'sine';
oscillators.high.frequency.value = arpeggio.notes[0][2];
// connect oscillators to gain
// connect gain to biquad filter
// connect filter to output
oscillators.low.connect(gainNode);
oscillators.mid.connect(gainNode);
oscillators.high.connect(gainNode);
gainNode.connect(biquadFilter);
biquadFilter.connect(audioCtx.destination);
// start the oscillators
oscillators.low.start(0);
oscillators.mid.start(0);
oscillators.high.start(0);
// map a value from one range to anoter
// e.g. 5 out of 10 expressed as a value between 20 - 40 = 30
function rangeMap(val, inRange, outRange) {
return (val - inRange.min) * (outRange.max - outRange.min) / (inRange.max - inRange.min) + outRange.min;
}
// play next note in arpeggio and recursively call the following
function arpeggioNote() {
oscillators.low.frequency.value = arpeggio.notes[arpeggio.currentNote][0];
oscillators.mid.frequency.value = arpeggio.notes[arpeggio.currentNote][1];
oscillators.high.frequency.value = arpeggio.notes[arpeggio.currentNote][2];
// start arpeggio again at the end
if (arpeggio.currentNote >= arpeggio.notes.length - 1) {
arpeggio.currentNote = 0;
} else {
arpeggio.currentNote++;
}
setTimeout(arpeggioNote, arpeggio.speed);
}
// kick the arpeggio off
arpeggioNote();
// move character on key down
document.addEventListener('keydown', e => {
// calculate current position
const coords = {
top: Number(ui.character.style.top.replace(/px/, '')),
left: Number(ui.character.style.left.replace(/px/, '')),
};
// move character based on arrow keys
switch(e.keyCode) {
// left
case 37:
if (coords.left >= 0) {
ui.character.style.left = `${coords.left - 20}px`;
}
break;
// up
case 38:
if (coords.top >= 0) {
ui.character.style.top = `${coords.top - 20}px`;
}
break;
// right
case 39:
if (coords.left <= targetPosition.left) {
ui.character.style.left = `${coords.left + 20}px`;
}
break;
// down
case 40:
if (coords.top <= dimensions.height) {
ui.character.style.top = `${coords.top + 20}px`;
}
break;
}
// update arpeggio speed
arpeggio.speed = targetPosition.left - coords.left;
let mappedvalue;
// set filter value for top half of screen
// based on distance from target
if (coords.top < targetPosition.top) {
mappedvalue = rangeMap(
coords.top,
{
min: 0,
max: targetPosition.top,
},
{
min: 0,
max: arpeggio.maxFilter,
}
);
// set filter value for bottom half of screen
// based on distance from target
} else {
mappedvalue = rangeMap(
coords.top,
{
min: targetPosition.top,
max: dimensions.height,
},
{
min: arpeggio.maxFilter,
max: 0,
}
);
}
biquadFilter.frequency.value = mappedvalue;
});
</script>
</body>
</html>