diff --git a/common/game_basic.js b/common/game_basic.js index 935d949..65fd2b0 100644 --- a/common/game_basic.js +++ b/common/game_basic.js @@ -2,7 +2,7 @@ import Player from "./player.js"; class GameBasic { constructor() { - let player = new Player(false,true); + let player = new Player(false, true); let entities = [player]; /*for (let i = 0; i < 50; i++) { diff --git a/common/player.js b/common/player.js index 1722f4f..6db06ce 100644 --- a/common/player.js +++ b/common/player.js @@ -1,8 +1,8 @@ import { distF, uuidv4 } from './util.js' class Player { - constructor(you, isPlayer) { - let pos = { x: Math.random() * 1000 - 50, y: Math.random() * 1000 - 50 }; + constructor(you, isPlayer, game = false) { + let pos = { x: (Math.random() - 0.5) * (game ? game.width : 1000), y:(Math.random() - 0.5) * (game ? game.height : 1000) }; let camera = { x: -pos.x, y: -pos.y }; let vel = { x: 0, y: 0 }; @@ -30,11 +30,11 @@ class Player { this.isYou = false; this.serverProps = [ - 'type', 'camera','pos','vel','rot','dir','ticks','health','you','isPlayer','headCount','isMenu','r','isYou' + 'type', 'camera', 'pos', 'vel', 'rot', 'dir', 'ticks', 'health', 'you', 'isPlayer', 'headCount', 'isMenu', 'r', 'isYou' ]; this.legalProps = [ - 'vel','dir','camera','ticks','isMenu','r' + 'vel', 'dir', 'camera', 'ticks', 'isMenu', 'r' ]; } bump() { diff --git a/game.js b/game.js index 296545a..8db4553 100644 --- a/game.js +++ b/game.js @@ -7,6 +7,13 @@ class Game extends GameBasic { this.ws = []; } sync(full = false) { + let onScreen = this.entities.filter(x => x && x.health > 0).length; + + onScreen += 4; + + this.width = (this.width * 0.99) + (Math.sqrt(onScreen) * 16); + this.height = (this.height * 0.99) + (Math.sqrt(onScreen) * 16); + let { entities } = this; let entList = []; for (let entity of entities) { @@ -40,6 +47,8 @@ class Game extends GameBasic { return props.map(prop => x[prop]); }); + filtered.push([this.width,this.height]) + client.send(JSON.stringify(filtered)); wsEnt.isYou = false; diff --git a/index.js b/index.js index c90a58e..a2292be 100644 --- a/index.js +++ b/index.js @@ -17,7 +17,7 @@ app.use(express.static('./static')); app.ws('/', function (ws, req) { game.ws.push(ws); - let player = new Player(); + let player = new Player(false, true, game); let playerI = game.entities.length; game.entities[playerI] = player; diff --git a/static/js/index.js b/static/js/index.js index f900d9e..fddd04b 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -147,11 +147,17 @@ class Game extends GameBasic { let that = this; let entList = JSON.parse(data); - entList = entList.map(y => { + entList = entList.map((y, i) => { + if (i == entList.length - 1) { + that.width = y[0]; + that.height = y[1]; + return undefined; + } + let type = (Object.keys(legalTypes).indexOf(y[0]) == -1) ? Player : legalTypes[y[0]]; let props = new type().serverProps; - + let x = {}; for (let i in props) { @@ -176,11 +182,13 @@ class Game extends GameBasic { return x; }) - let matchingPlayer = entList.filter(x => x.you == you) + entList = entList.filter(x => x); + + let matchingPlayer = entList.filter(x => x && x.you == you) if (matchingPlayer.length == 0) { - matchingPlayer = entList.filter(x => x.isYou); + matchingPlayer = entList.filter(x => x && x.isYou); } this.player = Object.assign(this.player || new Player(false, false), matchingPlayer[0]);