-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
296 lines (251 loc) · 6.15 KB
/
game.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
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
"use strict";
var blessed = require('blessed');
var contrib = require('blessed-contrib');
var player = require('play-sound')(opts = {});
function closest(arr, target) {
return Math.max.apply(Math, arr.filter(function(x) {
return x <= target;
}));
}
function isASCII(str) {
return /^[\x00-\x7F]*$/.test(str);
}
String.prototype.substring_wrap = function(begin, end) {
return this.substring(begin, end) + this.substring(0, end - this.length);
};
class Player {
constructor(number, keys, notes, grid, screen, bpms, bpm_changes) {
this.number = number;
this.notes = notes;
this.columns = {};
this.directions = [];
this.screen = screen;
this.bpms = bpms;
this.bpm_changes = bpm_changes;
var that = this;
for (var i = 0; i < keys.length; i++) {
screen.debug(this.number);
var key = keys[i];
this.directions.push(key.name);
var button = grid.set(1, i * 2 + (number - 1) * 16 + 1, 1, 1, blessed.box, {
content: key.content,
align: "center",
style: {
bold: true
}
});
button.setFront();
var col = [];
for (var j = 0; j < 12; j++) {
col.push(grid.set(j, i * 2 + (number - 1) * 16 + 1, 1, 1, blessed.box, {
content: key.content,
align: "center"
}));
}
col.forEach(function(box) {
box.hide();
});
this.columns[key.name] = {
"button": button,
"col": col,
"content": key.content,
"name": key.name
}
}
screen.key(this.directions, function(ch, key) {
that.columns[key.name]['button'].style.bg = 'green';
setTimeout(function() {
that.resetKey(that.columns[key.name]['button']);
}, 200);
screen.render();
});
var that = this;
this.notes.forEach(function(note) {
var steps = note.steps.split('');
var realsteps = [];
for (var i = 0; i < 4; i++) {
if (steps[i] != '0') {
realsteps.push(that.directions[i]);
}
}
setTimeout(function() {
realsteps.forEach(function(step) {
that.addNote(
step,
1000 / (that.bpms[closest(that.bpm_changes, note.measure * 4)] / 60)
);
});
}, note.offset);
});
}
addNote(direction, speed) {
var rownum = 11;
var col = this.columns[direction]['col'];
var dir = this.columns[direction]['button'];
var char = this.columns[direction]['content'];
var that = this;
var timer = setInterval(function() {
try {
col[rownum + 1].hide();
col[rownum].show();
that.screen.render();
} catch ( err ) {
that.screen.render();
}
if ((rownum == -1 || rownum === 0 || rownum == 1) && dir.style.bg == 'green') {
dir.setContent(char + "\n Good");
try {
col[rownum].hide();
} catch ( err ) {}
//setTimeout(function(){dir.setContent(char)},500);
clearInterval(timer);
return;
} else if (rownum == -1) {
dir.setContent(char + "\n Miss");
//setTimeout(function(){dir.setContent(char)},500);
clearInterval(timer);
}
rownum--;
}, speed);
col[rownum].show();
}
resetKey(key) {
key.style.bg = undefined;
this.screen.render();
}
}
function bigTextTicker(text, numchars, speed, screen) {
var bigbox = blessed.bigtext({
left: 'center',
content: text.substring(0, numchars),
shrink: true,
border: 'line',
fch: ' ',
ch: '\u2592',
style: {
fg: 'red',
bg: 'blue',
bold: false
}
});
screen.append(bigbox);
bigbox.abottom = '100%+' + bigbox.height;
var begin = 0;
var end = numchars;
setInterval(function() {
begin = (begin + 1) % text.length;
end = begin + numchars;
bigbox.setContent(text.substring_wrap(begin, end));
screen.render();
}, speed);
}
var play = function(data_file, audio_file, bg_file, mode) {
var song = require(data_file);
var notes_data = song.notes[mode];
var notes = notes_data.notes;
var title = isASCII(song.title) ? song.title : song.titleTranslit;
var artist = song.artist;
// Create a screen object.
var screen = blessed.screen({
smartCSR: true,
debug: true
});
var grid = new contrib.grid({
rows: 12,
cols: 25,
screen: screen
});
screen.title = 'Term Term Revolution!';
bigTextTicker("TERM TERM REVOLUTION! Song: " + title + ' By: ' + artist + ' ', 6, 200, screen);
screen.key(['escape', 'q', 'C-c'], function(ch, key) {
return process.exit(0);
});
var bpm_array = song.bpms;
var bpm_changes = [];
var bpms = {};
bpm_array.forEach(function(bpm) {
bpms[bpm[0]] = bpm[1]; bpm_changes.push(bpm[0]);
});
player.play(audio_file, function(err) {});
var p1 = new Player(1,
[
{
name: "a",
content: "A"
},
{
name: "s",
content: "S"
},
{
name: "w",
content: "W"
},
{
name: "d",
content: "D"
}
],
notes.map(function(note) {
note.steps = note.steps.slice(0, 4); return note
}),
grid,
screen,
bpms,
bpm_changes
);
if (notes_data.mode.indexOf('double') != -1) {
var p2 = new Player(2,
[
{
name: "left",
content: "◀"
},
{
name: "down",
content: "▼"
},
{
name: "up",
content: "▲"
},
{
name: "right",
content: "▶"
}
],
notes.map(function(note) {
note.steps = note.steps.slice(0, 4); return note
}),
grid,
screen,
bpms,
bpm_changes
);
}
if (bg_file.indexOf('.avi') != -1) {
var bg = blessed.video({
parent: screen,
left: 'center',
top: 'center',
file: bg_file,
width: '100%',
height: '100%'
});
}
else{
var bg = blessed.ansiimage({
parent: screen,
left: 'center',
top: 'center',
file: bg_file,
width: '100%',
height: '100%',
ascii: true
});
}
bg.setBack();
// Render the screen.
screen.render();
}
exports.play = play;