refactors, fix username check
This commit is contained in:
parent
60e579c349
commit
0a9fd2faf9
4 changed files with 196 additions and 166 deletions
|
@ -51,6 +51,51 @@ class Game extends GameBasic {
|
|||
this.ctx = canvas.getContext("2d");
|
||||
this.assetsIn = assetsIn;
|
||||
}
|
||||
renderEnt(ent, textArgs) {
|
||||
let { ctx, assetsIn } = this;
|
||||
|
||||
if (ent.health <= 0) return;
|
||||
|
||||
if (ent.type == 'Player') {
|
||||
ctx.save();
|
||||
|
||||
ctx.translate(ent.pos.x, ent.pos.y);
|
||||
ctx.rotate(ent.rot);
|
||||
ctx.drawImage(assetsIn[1], -64 / 2, -128, 64, 128);
|
||||
|
||||
ctx.restore();
|
||||
|
||||
if (ent.playing) {
|
||||
ctx.strokeStyle = 'white';
|
||||
ctx.lineWidth = "20";
|
||||
ctx.beginPath();
|
||||
ctx.arc(ent.pos.x, ent.pos.y, 32, 0, 2 * Math.PI);
|
||||
ctx.stroke();
|
||||
}
|
||||
|
||||
ctx.fillStyle = ent.you.split('-')[0];
|
||||
|
||||
ctx.beginPath();
|
||||
ctx.arc(ent.pos.x, ent.pos.y, 32, 0, 2 * Math.PI);
|
||||
ctx.fill();
|
||||
}
|
||||
|
||||
let a = assetsIn[bodies[ent.type]]
|
||||
ctx.drawImage(a, ent.pos.x - 64 / 2, ent.pos.y - 64 / 2, 64, 64);
|
||||
|
||||
if (ent.type != 'Player') return;
|
||||
|
||||
Object.assign(textArgs, [`HP: ${ent.health} PT: ${ent.headCount}`, ent.pos.x, ent.pos.y - 64 / 2]);
|
||||
|
||||
ctx.strokeText(...textArgs);
|
||||
ctx.fillText(...textArgs);
|
||||
|
||||
ctx.textBaseline = "top";
|
||||
Object.assign(textArgs, [`${ent.username || 'Guest'}`, ent.pos.x, ent.pos.y + 64 / 2]);
|
||||
|
||||
ctx.strokeText(...textArgs);
|
||||
ctx.fillText(...textArgs);
|
||||
}
|
||||
render() {
|
||||
let { ctx, assetsIn, entities, player, width, height } = this;
|
||||
|
||||
|
@ -63,7 +108,7 @@ class Game extends GameBasic {
|
|||
ctx.lineCap = "round";
|
||||
ctx.lineJoin = "round";
|
||||
ctx.font = "bold 16px sans-serif";
|
||||
|
||||
|
||||
ctx.save();
|
||||
|
||||
ctx.translate(player.camera.x + cs / 2, player.camera.y + cs / 2);
|
||||
|
@ -73,47 +118,7 @@ class Game extends GameBasic {
|
|||
let textArgs = [];
|
||||
|
||||
for (let ent of entities) {
|
||||
if (ent.health <= 0) continue;
|
||||
|
||||
if (ent.type == 'Player') {
|
||||
ctx.save();
|
||||
|
||||
ctx.translate(ent.pos.x, ent.pos.y);
|
||||
ctx.rotate(ent.rot);
|
||||
ctx.drawImage(assetsIn[1], -64 / 2, -128, 64, 128);
|
||||
|
||||
ctx.restore();
|
||||
|
||||
if (ent.playing) {
|
||||
ctx.strokeStyle = 'white';
|
||||
ctx.lineWidth = "20";
|
||||
ctx.beginPath();
|
||||
ctx.arc(ent.pos.x, ent.pos.y, 32, 0, 2 * Math.PI);
|
||||
ctx.stroke();
|
||||
}
|
||||
|
||||
ctx.fillStyle = ent.you.split('-')[0];
|
||||
|
||||
ctx.beginPath();
|
||||
ctx.arc(ent.pos.x, ent.pos.y, 32, 0, 2 * Math.PI);
|
||||
ctx.fill();
|
||||
}
|
||||
|
||||
let a = assetsIn[bodies[ent.type]]
|
||||
ctx.drawImage(a, ent.pos.x - 64 / 2, ent.pos.y - 64 / 2, 64, 64);
|
||||
|
||||
if (ent.type != 'Player') continue;
|
||||
|
||||
textArgs = [`HP: ${ent.health} PT: ${ent.headCount}`, ent.pos.x, ent.pos.y - 64 / 2];
|
||||
|
||||
ctx.strokeText(...textArgs);
|
||||
ctx.fillText(...textArgs);
|
||||
|
||||
ctx.textBaseline = "top";
|
||||
textArgs = [`${ent.username || 'Guest'}`, ent.pos.x, ent.pos.y + 64 / 2];
|
||||
|
||||
ctx.strokeText(...textArgs);
|
||||
ctx.fillText(...textArgs);
|
||||
this.renderEnt(ent, textArgs);
|
||||
}
|
||||
|
||||
ctx.restore();
|
||||
|
@ -122,7 +127,7 @@ class Game extends GameBasic {
|
|||
ctx.textBaseline = "top";
|
||||
ctx.font = "bold 32px sans-serif";
|
||||
|
||||
textArgs = [`XY: ${-Math.round(player.camera.x)}, ${-Math.round(player.camera.y)}`,25,25];
|
||||
textArgs = [`XY: ${-Math.round(player.camera.x)}, ${-Math.round(player.camera.y)}`, 25, 25];
|
||||
ctx.strokeText(...textArgs);
|
||||
ctx.fillText(...textArgs);
|
||||
|
||||
|
@ -179,6 +184,51 @@ class Game extends GameBasic {
|
|||
|
||||
this.ws.send(JSON.stringify(p));
|
||||
}
|
||||
entMapper(j, i, entList) {
|
||||
let that = this;
|
||||
|
||||
let { player } = this;
|
||||
let you = player.you;
|
||||
|
||||
if (i == entList.length - 1) {
|
||||
that.width = j[0];
|
||||
that.height = j[1];
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (!j) return undefined;
|
||||
|
||||
let y = j; //(typeof j == 'string') ? JSON.parse(j) : j;
|
||||
|
||||
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) {
|
||||
x[props[i]] = y[i];
|
||||
}
|
||||
|
||||
let { handleTick, bounce, handleInt } = type.prototype;
|
||||
Object.assign(x, { handleTick, bounce, handleInt });
|
||||
x.bounce = type.prototype.bounce;
|
||||
if (x.r != 1 && type == Player) {
|
||||
let a = new Audio(`sfx/${emojis[x.r]}.wav`);
|
||||
a.addEventListener('ended', () => {
|
||||
if (x.you == you) {
|
||||
that.player.playing = false;
|
||||
that.sync();
|
||||
}
|
||||
a.remove();
|
||||
})
|
||||
if (you != x.you) {
|
||||
x.r = 1;
|
||||
}
|
||||
a.play();
|
||||
}
|
||||
return x;
|
||||
}
|
||||
recv({ data }) {
|
||||
let { player } = this;
|
||||
let you = player.you;
|
||||
|
@ -186,45 +236,7 @@ class Game extends GameBasic {
|
|||
let that = this;
|
||||
|
||||
let entList = JSON.parse(data);
|
||||
entList = entList.map((j, i) => {
|
||||
if (i == entList.length - 1) {
|
||||
that.width = j[0];
|
||||
that.height = j[1];
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (!j) return undefined;
|
||||
|
||||
let y = j; //(typeof j == 'string') ? JSON.parse(j) : j;
|
||||
|
||||
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) {
|
||||
x[props[i]] = y[i];
|
||||
}
|
||||
|
||||
x.handleTick = type.prototype.handleTick;
|
||||
x.bounce = type.prototype.bounce;
|
||||
if (x.r != 1 && type == Player) {
|
||||
let a = new Audio(`sfx/${emojis[x.r]}.wav`);
|
||||
a.addEventListener('ended', () => {
|
||||
if (x.you == you) {
|
||||
that.player.playing = false;
|
||||
that.sync();
|
||||
}
|
||||
a.remove();
|
||||
})
|
||||
if (you != x.you) {
|
||||
x.r = 1;
|
||||
}
|
||||
a.play();
|
||||
}
|
||||
return x;
|
||||
})
|
||||
entList = entList.map((j, i) => that.entMapper(j, i, entList))
|
||||
|
||||
entList = entList.filter(x => x);
|
||||
|
||||
|
@ -246,11 +258,11 @@ class Game extends GameBasic {
|
|||
|
||||
if (this.entities.length == 0) this.entities = [this.player];
|
||||
}
|
||||
async init() {
|
||||
init() {
|
||||
let u = new URL(window.location);
|
||||
let tok = u.searchParams.get('token');
|
||||
if (!tok) tok = window.localStorage.getItem('tok');
|
||||
window.localStorage.setItem('tok',tok);
|
||||
window.localStorage.setItem('tok', tok);
|
||||
window.history.replaceState(null, '', window.location.pathname);
|
||||
|
||||
super.init();
|
||||
|
@ -266,7 +278,7 @@ class Game extends GameBasic {
|
|||
}
|
||||
setInterval(function () { that.sync() }, 1000 / 5);
|
||||
|
||||
that.ws.addEventListener('message', function (e) { that.recv(e) });
|
||||
that.ws.addEventListener('message', function (e) { that.recv(e) });
|
||||
};
|
||||
|
||||
this.ws.addEventListener('open', () => this.opener(that))
|
||||
|
@ -275,7 +287,11 @@ class Game extends GameBasic {
|
|||
|
||||
game.canvas.onclick = () => that.click();
|
||||
|
||||
let jason = await (fetch('/leaderboard').then(x => x.json()))
|
||||
this.handleScores();
|
||||
}
|
||||
|
||||
async handleScores() {
|
||||
let jason = await(fetch('/leaderboard').then(x => x.json()))
|
||||
|
||||
let scores = {};
|
||||
|
||||
|
@ -287,7 +303,7 @@ class Game extends GameBasic {
|
|||
for (let score in scores) {
|
||||
let s = scores[score];
|
||||
scores[score] = Math.round(Math.sqrt(s));
|
||||
}
|
||||
}
|
||||
|
||||
scores = Object.entries(scores).sort((a, b) => b[1] - a[1]);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue