diff --git a/common/game_basic.js b/common/game_basic.js
index 7cee28c..754d17b 100644
--- a/common/game_basic.js
+++ b/common/game_basic.js
@@ -1,3 +1,5 @@
+import Player from "./player.js";
+
class GameBasic {
constructor() {
let player = new Player(false,true);
@@ -21,4 +23,6 @@ class GameBasic {
let that = this;
setInterval(function () { that.main() }, 1000 / 60);
}
-}
\ No newline at end of file
+}
+
+export default GameBasic;
\ No newline at end of file
diff --git a/common/player.js b/common/player.js
index 1f72ca1..cd7592e 100644
--- a/common/player.js
+++ b/common/player.js
@@ -9,7 +9,7 @@ function uuidv4() {
}
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 vel = { x: 0, y: 0 };
@@ -84,4 +84,6 @@ Player.prototype.handleTick = function (game) {
}
}
}
-}
\ No newline at end of file
+}
+
+export default Player;
\ No newline at end of file
diff --git a/game.js b/game.js
index 04202cf..a3d422c 100644
--- a/game.js
+++ b/game.js
@@ -3,19 +3,30 @@ import GameBasic from "./common/game_basic.js";
class Game extends GameBasic {
constructor() {
super();
- }
- createPlayer(user) {
-
+ this.ws = [];
}
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() {
super.init();
let that = this;
+ that.entities = [];
- setInterval(function () { that.sync() }, 1000 / 10);
+ setInterval(function () { that.sync() }, 1000 / 20);
}
}
diff --git a/index.js b/index.js
index f2342b2..b2e4546 100644
--- a/index.js
+++ b/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);
});
diff --git a/static/index.html b/static/index.html
index 3d05439..c919548 100644
--- a/static/index.html
+++ b/static/index.html
@@ -17,9 +17,7 @@
-
-
-
+