unibutton/static/js/index.js

318 lines
8.6 KiB
JavaScript
Raw Permalink Normal View History

2024-11-25 14:14:20 -05:00
import Player from "./player.js";
import GameBasic from "./game_basic.js";
import NPC from "./npc.js";
2024-11-25 14:14:21 -05:00
import Shooter from "./shooter.js";
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:20 -05:00
const cs = 1024;
const assets = [
'assets/player.svg',
'assets/head.svg',
'assets/map.svg',
2024-11-25 14:14:21 -05:00
'assets/npc.svg',
'assets/shooter.svg'
2024-11-25 14:14:20 -05:00
];
const legalTypes = {
Player,
2024-11-25 14:14:21 -05:00
NPC,
Shooter
}
2024-11-25 14:14:21 -05:00
const bodies = {
'Player': 0,
'NPC': 3,
'Shooter': 4
};
2024-11-25 14:14:20 -05:00
const emojis = [
'Troll',
'Exit',
'Elec Piano Loop',
'No',
'Yes'
]
2024-11-25 14:14:21 -05:00
const origin = (window.location.href.indexOf('localhost') != -1) ? window.location.href : 'https://ub.dervland.net/'
2024-11-25 14:14:20 -05:00
class Game extends GameBasic {
constructor() {
super();
2024-11-25 14:14:20 -05:00
let assetsIn = {};
2024-11-25 14:14:20 -05:00
for (let asset in assets) {
assetsIn[asset] = new Image();
assetsIn[asset].src = assets[asset];
}
2024-11-25 14:14:20 -05:00
let canvas = document.querySelector("#canvas");
2024-11-25 14:14:20 -05:00
canvas.width = canvas.height = cs;
2024-11-25 14:14:20 -05:00
this.canvas = canvas;
this.ctx = canvas.getContext("2d");
this.assetsIn = assetsIn;
2024-11-25 14:14:20 -05:00
}
2024-11-25 14:14:21 -05:00
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;
2024-11-25 14:14:21 -05:00
Object.assign(textArgs, [`HP: ${ent.health}`, ent.pos.x, ent.pos.y - 64 / 2]);
2024-11-25 14:14:21 -05:00
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);
}
2024-11-25 14:14:20 -05:00
render() {
2024-11-25 14:14:20 -05:00
let { ctx, assetsIn, entities, player, width, height } = this;
2024-11-25 14:14:20 -05:00
ctx.clearRect(0, 0, cs, cs);
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:21 -05:00
ctx.strokeStyle = "rgb(255,255,255)";
ctx.lineWidth = "8";
ctx.textAlign = "center";
ctx.textBaseline = "bottom";
ctx.lineCap = "round";
ctx.lineJoin = "round";
2024-11-25 14:14:21 -05:00
ctx.font = "bold 32px sans-serif";
2024-11-25 14:14:21 -05:00
2024-11-25 14:14:20 -05:00
ctx.save();
2024-11-25 14:14:20 -05:00
ctx.translate(player.camera.x + cs / 2, player.camera.y + cs / 2);
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:20 -05:00
ctx.drawImage(assetsIn[2], -width / 2, -height / 2, width, height);
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:21 -05:00
let textArgs = [];
for (let ent of entities) {
2024-11-25 14:14:21 -05:00
this.renderEnt(ent, textArgs);
}
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:20 -05:00
ctx.restore();
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:21 -05:00
ctx.textAlign = "start";
ctx.textBaseline = "top";
2024-11-25 14:14:21 -05:00
ctx.font = "bold 48px sans-serif";
ctx.fillStyle = "rgb(0,0,0)";
2024-11-25 14:14:21 -05:00
2024-11-25 14:14:21 -05:00
textArgs = [`XY: ${-Math.round(player.camera.x)}, ${-Math.round(player.camera.y)}`, 25, 25];
2024-11-25 14:14:21 -05:00
ctx.strokeText(...textArgs);
ctx.fillText(...textArgs);
2024-11-25 14:14:21 -05:00
textArgs = [`PT: ${Math.round(player.headCount)}`, 25, 75];
ctx.strokeText(...textArgs);
ctx.fillText(...textArgs);
2024-11-25 14:14:21 -05:00
textArgs = [`CH: ${Math.round(player.ticks * 100 / 300)}%`, 25, 125];
ctx.strokeText(...textArgs);
ctx.fillText(...textArgs);
2024-11-25 14:14:21 -05:00
if (player.health <= 0 || player.isMenu) {
2024-11-25 14:14:20 -05:00
this.doMenu();
}
}
doMenu() {
let { ctx, player } = this;
ctx.fillStyle = 'rgba(0,0,0,0.5)';
ctx.fillRect(0, 0, cs, cs);
ctx.fillStyle = 'rgb(255,255,255)';
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = "bold 48px sans-serif";
2024-11-25 14:14:21 -05:00
if (player.health <= 0) {
ctx.fillText(`You died! Click to respawn\n`, cs / 2, cs / 2);
return;
}
2024-11-25 14:14:20 -05:00
if (player.isMenu) {
let r = Math.floor(Math.abs(player.rot / 1.2) % emojis.length);
ctx.fillText(`Click to react ${emojis[r]}`, cs / 2, cs / 2)
2024-11-25 14:14:21 -05:00
ctx.font = "bold 32px sans-serif";
2024-11-25 14:14:20 -05:00
ctx.fillText(`Wait for emojis ${emojis.join(', ')}`, cs / 2, cs / 2 + 50)
}
2024-11-25 14:14:20 -05:00
}
click() {
let { player } = this;
2024-11-25 14:14:20 -05:00
if (player.health <= 0) {
2024-11-25 14:14:20 -05:00
let that = this;
2024-11-25 14:14:20 -05:00
this.ws.close();
2024-11-25 14:14:20 -05:00
this.ws = new WebSocket(origin);
2024-11-25 14:14:21 -05:00
this.ws.addEventListener('open', () => this.opener(that))
} else if (player.isMenu) {
2024-11-25 14:14:20 -05:00
player.r = Math.floor(Math.abs(player.rot / 1.2) % emojis.length);
player.isMenu = false;
this.sync();
} else {
player.bump();
2024-11-25 14:14:20 -05:00
this.sync();
}
}
2024-11-25 14:14:20 -05:00
sync(first = false) {
2024-11-25 14:14:20 -05:00
let { player } = this;
2024-11-25 14:14:20 -05:00
if (first) {
player.ref = new URL(window.location).searchParams.get('ref') || 'nobody';
} else {
player.ref = false;
}
2024-11-25 14:14:21 -05:00
let p = player.legalProps.map(prop => player[prop]);
2024-11-25 14:14:21 -05:00
p.splice(0, 0, 'SYNC');
2024-11-25 14:14:21 -05:00
this.ws.send(JSON.stringify(p));
2024-11-25 14:14:20 -05:00
}
2024-11-25 14:14:21 -05:00
entMapper(j, i, entList) {
let that = this;
2024-11-25 14:14:20 -05:00
let { player } = this;
2024-11-25 14:14:20 -05:00
let you = player.you;
2024-11-25 14:14:21 -05:00
if (i == entList.length - 1) {
that.width = j[0];
that.height = j[1];
return undefined;
}
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:21 -05:00
if (!j) return undefined;
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:21 -05:00
let y = j; //(typeof j == 'string') ? JSON.parse(j) : j;
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:21 -05:00
let type = (Object.keys(legalTypes).indexOf(y[0]) == -1) ? Player : legalTypes[y[0]];
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:21 -05:00
let props = new type().serverProps;
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:21 -05:00
let x = {};
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:21 -05:00
for (let i in props) {
x[props[i]] = y[i];
}
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:21 -05:00
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();
}
2024-11-25 14:14:21 -05:00
a.remove();
})
if (you != x.you) {
x.r = 1;
}
2024-11-25 14:14:21 -05:00
a.play();
}
return x;
}
recv({ data }) {
let { player } = this;
let you = player.you;
let that = this;
let entList = JSON.parse(data);
entList = entList.map((j, i) => that.entMapper(j, i, entList))
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:20 -05:00
entList = entList.filter(x => x);
let matchingPlayer = entList.filter(x => x && x.you == you)
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:20 -05:00
if (matchingPlayer.length == 0) {
2024-11-25 14:14:20 -05:00
matchingPlayer = entList.filter(x => x && x.isYou);
2024-11-25 14:14:20 -05:00
}
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:20 -05:00
this.player = Object.assign(this.player || new Player(false, false), matchingPlayer[0]);
2024-11-25 14:14:20 -05:00
if (this.player.r != 1) {
this.player.playing = true;
this.player.r = 1;
that.sync();
}
2024-11-25 14:14:20 -05:00
this.entities = entList;
if (this.entities.length == 0) this.entities = [this.player];
}
2024-11-25 14:14:21 -05:00
init() {
2024-11-25 14:14:21 -05:00
let u = new URL(window.location);
let tok = u.searchParams.get('token');
if (!tok) tok = window.localStorage.getItem('tok');
2024-11-25 14:14:21 -05:00
window.localStorage.setItem('tok', tok);
2024-11-25 14:14:21 -05:00
window.history.replaceState(null, '', window.location.pathname);
super.init();
2024-11-25 14:14:20 -05:00
let that = this;
2024-11-25 14:14:20 -05:00
this.ws = new WebSocket(origin);
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:21 -05:00
this.opener = function (that) {
2024-11-25 14:14:20 -05:00
that.sync(true);
2024-11-25 14:14:21 -05:00
if (tok) {
2024-11-25 14:14:21 -05:00
that.ws.send(JSON.stringify(["AUTH", tok]));
}
2024-11-25 14:14:20 -05:00
setInterval(function () { that.sync() }, 1000 / 5);
2024-11-25 14:14:21 -05:00
2024-11-25 14:14:21 -05:00
that.ws.addEventListener('message', function (e) { that.recv(e) });
2024-11-25 14:14:21 -05:00
};
this.ws.addEventListener('open', () => this.opener(that))
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:20 -05:00
setInterval(function () { that.render() }, 1000 / 60);
game.canvas.onclick = () => that.click();
2024-11-25 14:14:21 -05:00
2024-11-25 14:14:21 -05:00
this.handleScores();
}
async handleScores() {
2024-11-25 14:14:21 -05:00
let jason = await (fetch('/leaderboard').then(x => x.text()))
2024-11-25 14:14:21 -05:00
2024-11-25 14:14:21 -05:00
document.querySelector('.lb').textContent = jason;
2024-11-25 14:14:21 -05:00
let sant = document.querySelector('.lb').innerHTML;
2024-11-25 14:14:21 -05:00
sant = sant.replaceAll(/\[([^\[]+)\]/g, '<span class="score">[<span class="score-content">$1</span>]</span>');
2024-11-25 14:14:21 -05:00
document.querySelector('.lb').innerHTML = sant;
2024-11-25 14:14:20 -05:00
}
2024-11-25 14:14:20 -05:00
}
var game = new Game();
2024-11-25 14:14:20 -05:00
game.init();