finally, now multiplayer
This commit is contained in:
parent
25db22ab7c
commit
88d395c0e5
6 changed files with 93 additions and 17 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
import Player from "./player.js";
|
||||||
|
|
||||||
class GameBasic {
|
class GameBasic {
|
||||||
constructor() {
|
constructor() {
|
||||||
let player = new Player(false,true);
|
let player = new Player(false,true);
|
||||||
|
@ -21,4 +23,6 @@ class GameBasic {
|
||||||
let that = this;
|
let that = this;
|
||||||
setInterval(function () { that.main() }, 1000 / 60);
|
setInterval(function () { that.main() }, 1000 / 60);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default GameBasic;
|
|
@ -9,7 +9,7 @@ function uuidv4() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function Player(you, isPlayer) {
|
function Player(you, isPlayer) {
|
||||||
let pos = { x: Math.random() * 5000 - 50, y: Math.random() * 5000 - 50 };
|
let pos = { x: Math.random() * 1000 - 50, y: Math.random() * 1000 - 50 };
|
||||||
let camera = { x: -pos.x, y: -pos.y };
|
let camera = { x: -pos.x, y: -pos.y };
|
||||||
let vel = { x: 0, y: 0 };
|
let vel = { x: 0, y: 0 };
|
||||||
|
|
||||||
|
@ -84,4 +84,6 @@ Player.prototype.handleTick = function (game) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default Player;
|
21
game.js
21
game.js
|
@ -3,19 +3,30 @@ import GameBasic from "./common/game_basic.js";
|
||||||
class Game extends GameBasic {
|
class Game extends GameBasic {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
}
|
this.ws = [];
|
||||||
createPlayer(user) {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
sync() {
|
sync() {
|
||||||
|
let { entities } = this;
|
||||||
|
let entList = [];
|
||||||
|
for (let entity of entities) {
|
||||||
|
let { pos, vel, rot, dir, health, headCount, you, camera } = entity;
|
||||||
|
entList.push({ pos, vel, rot, dir, health, headCount, you, camera });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (entList.length == 0) return;
|
||||||
|
|
||||||
|
for (let client of this.ws) {
|
||||||
|
if (!client.active) continue;
|
||||||
|
client.send(JSON.stringify(entList));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
init() {
|
init() {
|
||||||
super.init();
|
super.init();
|
||||||
|
|
||||||
let that = this;
|
let that = this;
|
||||||
|
that.entities = [];
|
||||||
|
|
||||||
setInterval(function () { that.sync() }, 1000 / 10);
|
setInterval(function () { that.sync() }, 1000 / 20);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
35
index.js
35
index.js
|
@ -1,17 +1,44 @@
|
||||||
import express from "express";
|
import express from "express";
|
||||||
import expressW from "express-ws";
|
import expressW from "express-ws";
|
||||||
|
import Game from "./game.js";
|
||||||
|
import Player from "./common/player.js";
|
||||||
|
|
||||||
var app = express();
|
var app = express();
|
||||||
expressW(app);
|
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.use(express.static('./static'));
|
||||||
|
|
||||||
app.ws('/', function (ws, req) {
|
app.ws('/', function (ws, req) {
|
||||||
ws.on('message', function (msg) {
|
game.ws.push(ws);
|
||||||
console.log(msg);
|
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);
|
console.log('socket', req.testing);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -17,9 +17,7 @@
|
||||||
<canvas id='canvas'></canvas>
|
<canvas id='canvas'></canvas>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
<script src='js/player.js'></script>
|
<script src='js/index.js' type="module"></script>
|
||||||
<script src='js/game_basic.js'></script>
|
|
||||||
<script src='js/index.js'></script>
|
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
|
@ -1,3 +1,6 @@
|
||||||
|
import Player from "./player.js";
|
||||||
|
import GameBasic from "./game_basic.js";
|
||||||
|
|
||||||
const cs = 1024;
|
const cs = 1024;
|
||||||
const assets = [
|
const assets = [
|
||||||
'assets/player.svg',
|
'assets/player.svg',
|
||||||
|
@ -52,7 +55,7 @@ class Game extends GameBasic {
|
||||||
ctx.textBaseline = "bottom";
|
ctx.textBaseline = "bottom";
|
||||||
ctx.font = "bold 16px sans-serif";
|
ctx.font = "bold 16px sans-serif";
|
||||||
|
|
||||||
let args = [`HP: ${ent.health} KO: ${ent.headCount}`, ent.pos.x, ent.pos.y - 64 / 2];
|
let args = [`HP: ${ent.health} KO: ${ent.headCount} XY: ${Math.round(ent.pos.x)}, ${Math.round(ent.pos.y)}`, ent.pos.x, ent.pos.y - 64 / 2];
|
||||||
|
|
||||||
ctx.strokeText(...args);
|
ctx.strokeText(...args);
|
||||||
ctx.fillText(...args);
|
ctx.fillText(...args);
|
||||||
|
@ -74,18 +77,49 @@ class Game extends GameBasic {
|
||||||
let { player } = this;
|
let { player } = this;
|
||||||
|
|
||||||
if (player.health <= 0) {
|
if (player.health <= 0) {
|
||||||
|
this.ws = new WebSocket(window.location.href);
|
||||||
this.player = new Player(false, true);
|
this.player = new Player(false, true);
|
||||||
this.entities.push(this.player);
|
this.entities.push(this.player);
|
||||||
} else {
|
} else {
|
||||||
player.bump();
|
player.bump();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
sync() {
|
||||||
|
let { player } = this;
|
||||||
|
|
||||||
|
let { vel, dir, you } = player;
|
||||||
|
|
||||||
|
this.ws.send(JSON.stringify({ vel, dir, you }));
|
||||||
|
}
|
||||||
|
recv({data}) {
|
||||||
|
let {player} = this;
|
||||||
|
let you = player.you;
|
||||||
|
|
||||||
|
let entList = JSON.parse(data);
|
||||||
|
entList = entList.map(x => {
|
||||||
|
x.handleTick = Player.prototype.handleTick;
|
||||||
|
return x;
|
||||||
|
})
|
||||||
|
|
||||||
|
let matchingPlayer = entList.filter(x => x.you == you)
|
||||||
|
|
||||||
|
this.player = Object.assign(this.player || new Player(false,false),matchingPlayer[0]);
|
||||||
|
|
||||||
|
this.entities = entList;
|
||||||
|
|
||||||
|
if (this.entities.length == 0) this.entities = [this.player];
|
||||||
|
}
|
||||||
init() {
|
init() {
|
||||||
super.init();
|
super.init();
|
||||||
|
|
||||||
let that = this;
|
let that = this;
|
||||||
|
|
||||||
|
this.ws = new WebSocket(window.location.href);
|
||||||
|
|
||||||
|
this.ws.addEventListener('message',function(e) { that.recv(e) });
|
||||||
|
|
||||||
setInterval(function () { that.render() }, 1000 / 60);
|
setInterval(function () { that.render() }, 1000 / 60);
|
||||||
|
setInterval(function () { that.sync() }, 1000 / 20);
|
||||||
|
|
||||||
game.canvas.onclick = () => that.click();
|
game.canvas.onclick = () => that.click();
|
||||||
}
|
}
|
||||||
|
@ -93,4 +127,4 @@ class Game extends GameBasic {
|
||||||
|
|
||||||
var game = new Game();
|
var game = new Game();
|
||||||
|
|
||||||
game.init();
|
game.init();
|
Loading…
Reference in a new issue