-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlevels.js
68 lines (51 loc) · 1008 Bytes
/
levels.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
let fs = require('fs');
let nbLevels = 5;
function readLevel(level) {
fs.readFile(`./assets/level${level}.json`, 'utf8', parseLevel(level));
}
function writeFile(path, contents) {
fs.writeFile(path, contents, (err) => {
if (err) {
throw err;
}
console.log('done');
});
}
let levels = [];
function writeLevels() {
let template = `// Auto generated by levels.js\n`;
levels.forEach((level, i) => {
template += `const level${i} = [${level}];\n`;
});
template += `
export default [
level1,
level2,
level3,
level4,
level5
];
`;
writeFile('src/play/levels.js', template);
}
function parseLevel(level) {
return (err, contents) => {
if (err) {
throw err;
}
let data = JSON.parse(contents);
levels[level] = data.layers[0].data;
checkWrite();
};
}
function checkWrite() {
for (let i = 1; i <= nbLevels; i++) {
if (!levels[i]) {
return;
}
}
writeLevels();
};
for (let i = 1; i <= nbLevels; i++) {
readLevel(i);
}