unibutton/static/js/index.js

203 lines
5.7 KiB
JavaScript
Raw Normal View History

2024-09-29 03:55:22 -04:00
import Player from "./player.js";
import GameBasic from "./game_basic.js";
import NPC from "./npc.js";
2024-09-29 03:55:22 -04:00
2024-09-27 10:32:50 -04:00
const cs = 1024;
const assets = [
'assets/player.svg',
'assets/head.svg',
'assets/map.svg',
'assets/npc.svg'
2024-09-27 10:32:50 -04:00
];
const legalTypes = {
Player,
NPC
}
2024-10-01 18:41:44 -04:00
const emojis = [
'Troll',
'Exit',
'Elec Piano Loop',
'No',
'Yes'
]
const origin = (window.location.href.indexOf('localhost') != -1) ? window.location.href : 'https://ub.zenoverse.net/'
2024-09-27 10:32:50 -04:00
class Game extends GameBasic {
constructor() {
super();
2024-09-29 03:16:15 -04:00
let assetsIn = {};
2024-09-29 03:16:15 -04:00
for (let asset in assets) {
assetsIn[asset] = new Image();
assetsIn[asset].src = assets[asset];
}
2024-09-29 03:16:15 -04:00
let canvas = document.querySelector("#canvas");
2024-09-29 03:16:15 -04:00
canvas.width = canvas.height = cs;
2024-09-29 03:16:15 -04:00
this.canvas = canvas;
this.ctx = canvas.getContext("2d");
this.assetsIn = assetsIn;
2024-09-27 21:42:21 -04:00
}
2024-09-29 03:16:15 -04:00
render() {
2024-09-29 21:20:56 -04:00
let { ctx, assetsIn, entities, player, width, height } = this;
2024-09-29 03:16:15 -04:00
ctx.clearRect(0, 0, cs, cs);
2024-09-29 03:16:15 -04:00
2024-09-27 21:42:21 -04:00
ctx.save();
2024-09-29 03:16:15 -04:00
ctx.translate(player.camera.x + cs / 2, player.camera.y + cs / 2);
2024-09-29 03:16:15 -04:00
2024-09-29 22:41:58 -04:00
ctx.drawImage(assetsIn[2], -width / 2, -height / 2, width, height);
2024-09-29 21:20:56 -04:00
for (let ent of entities) {
2024-09-29 03:16:15 -04:00
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();
2024-09-29 03:16:15 -04:00
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();
}
2024-09-29 03:16:15 -04:00
ctx.fillStyle = ent.you.split('-')[0];
2024-09-29 03:16:15 -04:00
ctx.beginPath();
ctx.arc(ent.pos.x, ent.pos.y, 32, 0, 2 * Math.PI);
ctx.fill();
}
ctx.drawImage((ent.type == 'NPC') ? assetsIn[3] : assetsIn[0], ent.pos.x - 64 / 2, ent.pos.y - 64 / 2, 64, 64);
if (ent.type != 'Player') continue;
2024-09-29 22:41:58 -04:00
2024-09-29 03:16:15 -04:00
ctx.strokeStyle = "rgb(255,255,255)";
ctx.lineWidth = "8";
ctx.textAlign = "center";
ctx.textBaseline = "bottom";
ctx.font = "bold 16px sans-serif";
2024-09-29 03:55:22 -04:00
let args = [`HP: ${ent.health} KO: ${ent.headCount} XY: ${Math.round(ent.pos.x)}, ${Math.round(ent.pos.y)}`, ent.pos.x, ent.pos.y - 64 / 2];
2024-09-29 03:16:15 -04:00
ctx.strokeText(...args);
ctx.fillText(...args);
}
2024-09-29 03:16:15 -04:00
2024-09-27 21:42:21 -04:00
ctx.restore();
2024-09-29 03:16:15 -04:00
if (player.health <= 0 || player.isMenu) {
2024-10-01 18:41:44 -04: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";
if (player.health <= 0) ctx.fillText('You died! Click to respawn', cs / 2, cs / 2);
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)
ctx.font = "bold 16px sans-serif";
ctx.fillText(`Wait for emojis ${emojis.join(', ')}`, cs / 2, cs / 2 + 50)
}
2024-09-27 21:42:21 -04:00
}
click() {
let { player } = this;
2024-09-29 03:16:15 -04:00
if (player.health <= 0) {
2024-09-30 08:37:41 -04:00
this.ws = new WebSocket(origin);
2024-09-29 03:16:15 -04:00
this.player = new Player(false, true);
this.entities.push(this.player);
} else if (player.isMenu) {
2024-10-01 18:41:44 -04:00
player.r = Math.floor(Math.abs(player.rot / 1.2) % emojis.length);
player.isMenu = false;
this.sync();
} else {
player.bump();
2024-09-29 18:46:23 -04:00
this.sync();
}
}
2024-09-29 03:55:22 -04:00
sync() {
let { player } = this;
let { vel, dir, you, ticks, isMenu, r, playing } = player;
2024-09-29 03:55:22 -04:00
this.ws.send(JSON.stringify({ vel, dir, you, ticks, isMenu, r, playing }));
2024-09-29 03:55:22 -04:00
}
2024-09-29 22:41:58 -04:00
recv({ data }) {
let { player } = this;
2024-09-29 03:55:22 -04:00
let you = player.you;
let that = this;
2024-09-29 03:55:22 -04:00
let entList = JSON.parse(data);
entList = entList.map(x => {
let type = (Object.keys(legalTypes).indexOf(x.type) == -1) ? Player : legalTypes[x.type];
x.handleTick = type.prototype.handleTick;
if (x.r != 1) {
2024-10-01 18:41:44 -04:00
let a = new Audio(`assets/${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();
}
2024-09-29 03:55:22 -04:00
return x;
})
2024-09-29 22:41:58 -04:00
2024-09-29 03:55:22 -04:00
let matchingPlayer = entList.filter(x => x.you == you)
2024-09-29 22:41:58 -04:00
this.player = Object.assign(this.player || new Player(false, false), matchingPlayer[0]);
2024-09-29 03:55:22 -04:00
if (this.player.r != 1) {
this.player.playing = true;
this.player.r = 1;
that.sync();
}
2024-09-29 03:55:22 -04:00
this.entities = entList;
if (this.entities.length == 0) this.entities = [this.player];
}
2024-09-29 03:16:15 -04:00
init() {
super.init();
2024-09-29 03:16:15 -04:00
let that = this;
2024-09-30 08:37:56 -04:00
this.ws = new WebSocket(origin);
2024-09-29 03:55:22 -04:00
2024-09-29 22:41:58 -04:00
this.ws.addEventListener('message', function (e) { that.recv(e) });
2024-09-29 03:55:22 -04:00
2024-09-29 03:16:15 -04:00
setInterval(function () { that.render() }, 1000 / 60);
2024-09-29 17:07:22 -04:00
setInterval(function () { that.sync() }, 1000 / 5);
2024-09-29 03:16:15 -04:00
game.canvas.onclick = () => that.click();
2024-09-28 20:06:48 -04:00
}
2024-09-27 10:32:50 -04:00
}
var game = new Game();
2024-09-29 03:55:22 -04:00
game.init();