Skip to content

Commit

Permalink
dev: save map
Browse files Browse the repository at this point in the history
  • Loading branch information
hashrock committed May 22, 2020
1 parent 1ed7064 commit 565c488
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
32 changes: 32 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ app.use(express.static("public"));

const users = [];
let tile = null;

let fs = require("fs");
const tileStr = fs.readFileSync("./public/tile.csv", "utf-8");
let map = tileStr.split("\n").map((i) => i.split(","));
console.log(map);

io.on("connection", function (socket) {
users.push(socket.id);
socket.on("tile", function (msg) {
Expand All @@ -15,6 +21,8 @@ io.on("connection", function (socket) {
});
socket.on("draw", function (msg) {
io.emit("draw", msg);
console.log(msg);
map[+msg.y][+msg.x] = msg.index;
});
socket.on("login", function (obj) {
io.emit("users", users);
Expand All @@ -39,10 +47,34 @@ function saveImage(data) {
});
}

// function initTile(){
// let out = [];
// for (let y = 0; y < 32; y++) {
// let row = [];
// for (let x = 0; x < 32; x++) {
// row.push("0");
// }
// out.push(row);
// }
// }

function saveTile() {
const o = map.map((i) => i.join(",")).join("\n");
var fs = require("fs");
fs.writeFile("public/tile.csv", o, function (err) {
if (err) {
console.log(err);
}
});
}

setInterval(() => {
if (tile) {
saveImage(tile);
}
}, 1000);
setInterval(() => {
saveTile();
}, 1000);

http.listen(process.env.PORT | 3000, function () {});
10 changes: 7 additions & 3 deletions public/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const game = new Phaser.Game(400, 300, Phaser.CANVAS, "phaser-example", {
// game.scale.setMinMax(400, 300, 800, 600);

function preload() {
game.load.tilemap("map", "init.csv", null, Phaser.Tilemap.CSV);
game.load.tilemap("map", "tile.csv", null, Phaser.Tilemap.CSV);
game.load.image("tiles", "tile.png");
}

Expand Down Expand Up @@ -48,7 +48,7 @@ let cursors;
// });
// });
socket.on("draw", (op) => {
putTile(op.index, op.x, op.y);
putTile(op.index, op.x * 16, op.y * 16);
});

function create() {
Expand Down Expand Up @@ -97,7 +97,11 @@ function update() {
result.push(getTile(x, y).index);
}
}
socket.emit("draw", { index: currentTile, x: marker.x, y: marker.y });
socket.emit("draw", {
index: currentTile,
x: marker.x / 16,
y: marker.y / 16,
});
// drawRef.push({ index: currentTile, x: marker.x, y: marker.y });
}
}
Expand Down
32 changes: 32 additions & 0 deletions public/tile.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
1,1,1,0,1,1,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,1,,,,,,,,,,,,,,,,1,,,,,,,,,,,,,,,,1,,,,,,,,,,,,,,,,1,,,,,,,,,,,,,,,,1
1,1,1,2,2,2,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0
4,4,4,2,2,2,4,4,4,10,10,4,4,4,4,4,4,4,2,1,0,0,0,0,0,0,0,0,0,0,0,0
4,4,4,2,2,2,2,1,10,10,2,2,2,2,0,2,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0
4,4,4,2,2,1,0,2,10,10,10,10,1,8,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
4,4,4,2,2,2,2,1,1,10,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
4,4,4,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
4,4,4,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
4,4,4,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
4,4,4,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
4,4,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0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