expanding map

This commit is contained in:
biglyderv 2024-11-25 14:14:20 -05:00
parent 4602947942
commit e584361465
5 changed files with 27 additions and 10 deletions

View file

@ -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++) {

View file

@ -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() {

View file

@ -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;

View file

@ -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;

View file

@ -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]);