finally, now multiplayer
This commit is contained in:
parent
873102a927
commit
b1501d7251
6 changed files with 93 additions and 17 deletions
35
index.js
35
index.js
|
@ -1,17 +1,44 @@
|
|||
import express from "express";
|
||||
import expressW from "express-ws";
|
||||
import Game from "./game.js";
|
||||
import Player from "./common/player.js";
|
||||
|
||||
var app = express();
|
||||
expressW(app);
|
||||
|
||||
var game = new Game();
|
||||
|
||||
app.use('/js',express.static('./common'));
|
||||
game.init();
|
||||
|
||||
app.use('/js', express.static('./common'));
|
||||
app.use(express.static('./static'));
|
||||
|
||||
app.ws('/', function (ws, req) {
|
||||
ws.on('message', function (msg) {
|
||||
console.log(msg);
|
||||
});
|
||||
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;
|
||||
})
|
||||
|
||||
console.log('socket', req.testing);
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue