unibutton/index.js

45 lines
1 KiB
JavaScript
Raw Normal View History

2024-09-29 02:05:42 -04:00
import express from "express";
import expressW from "express-ws";
2024-09-29 03:55:22 -04:00
import Game from "./game.js";
import Player from "./common/player.js";
2024-09-29 02:05:42 -04:00
var app = express();
expressW(app);
2024-09-29 03:55:22 -04:00
var game = new Game();
2024-09-29 03:55:22 -04:00
game.init();
app.use('/js', express.static('./common'));
2024-09-29 02:05:42 -04:00
app.use(express.static('./static'));
app.ws('/', function (ws, req) {
2024-09-29 03:55:22 -04:00
game.ws.push(ws);
let player = new Player();
let playerI = game.entities.length;
game.entities[playerI] = player;
ws.active = true;
ws.on('message', function message(msg) {
let data = {};
try {
data = JSON.parse(msg);
} catch (err) {
console.log(err);
data = {};
}
let { vel, dir, you } = data;
let data2 = { vel, dir, you };
game.entities[playerI] = Object.assign(game.entities[playerI], data2);
})
ws.on('close', function () {
ws.active = false;
player.health = -1;
})
2024-09-29 02:05:42 -04:00
console.log('socket', req.testing);
});
app.listen(process.env.PORT || 3069);