summaryrefslogtreecommitdiff
path: root/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'index.js')
-rw-r--r--index.js42
1 files changed, 42 insertions, 0 deletions
diff --git a/index.js b/index.js
index 7b9af6d..c53b521 100644
--- a/index.js
+++ b/index.js
@@ -1,11 +1,53 @@
const express = require('express');
const path = require('path');
+const fs = require('fs');
+
+const MAP_LAYERS=2;
const app = express();
app.route('/').get((req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
+app.route('/levels').get((req, res) => {
+ fs.readFile(path.join(__dirname, 'level-all.bbiy'), 'utf8', (err, data) => {
+ if (err) {
+ console.log(err);
+ return;
+ }
+
+ const levels = [];
+ const lines = data.split('\r\n');
+ do {
+ const levelName = lines.shift();
+ if (!levelName) {
+ break;
+ }
+ const [xDim, yDim] = lines.shift().split(' x ').map((x) => parseInt(x));
+
+ let level = Array(yDim).fill(null).map(() => Array(xDim).fill(null).map(() => []));
+ for (let i = 0; i < MAP_LAYERS; i++) {
+ for (let y = 0; y < yDim; y++) {
+ const line = lines.shift().split('');
+ for (let x = 0; x < xDim; x++) {
+ if (line[x] !== ' ') {
+ level[y][x].push(line[x]);
+ }
+ }
+ }
+ }
+ levels.push({
+ levelName,
+ gridSize: {xDim, yDim},
+ level,
+ });
+ } while (lines.length);
+
+ // Send the array of objects
+ res.send(levels);
+ });
+});
+
app.use(express.static('.'));
app.listen(3000, () => {