unibutton/static/js/index.js

130 lines
3.4 KiB
JavaScript
Raw Normal View History

2024-11-25 14:14:20 -05:00
import Player from "./player.js";
import GameBasic from "./game_basic.js";
2024-11-25 14:14:20 -05:00
const cs = 1024;
const assets = [
'assets/player.svg',
'assets/head.svg',
];
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:20 -05:00
render() {
let { ctx, assetsIn, entities, player } = 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: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
for (let ent of entities) {
2024-11-25 14:14:20 -05:00
if (ent.health <= 0) continue;
ctx.save();
2024-11-25 14:14:20 -05:00
ctx.translate(ent.pos.x, ent.pos.y);
ctx.rotate(ent.rot);
ctx.drawImage(assetsIn[1], -64 / 2, -128, 64, 128);
2024-11-25 14:14:20 -05:00
ctx.restore();
2024-11-25 14:14:20 -05:00
ctx.drawImage(assetsIn[0], ent.pos.x - 64 / 2, ent.pos.y - 64 / 2, 64, 64);
2024-11-25 14:14:20 -05:00
ctx.fillStyle = ent.you.split('-')[0];
ctx.strokeStyle = "rgb(255,255,255)";
ctx.lineWidth = "8";
ctx.textAlign = "center";
ctx.textBaseline = "bottom";
ctx.font = "bold 16px sans-serif";
2024-11-25 14:14:20 -05: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-11-25 14:14:20 -05:00
ctx.strokeText(...args);
ctx.fillText(...args);
}
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
if (player.health <= 0) {
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";
ctx.fillText('You died! Click to respawn', cs / 2, cs / 2);
}
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
this.ws = new WebSocket(window.location.href);
2024-11-25 14:14:20 -05:00
this.player = new Player(false, true);
this.entities.push(this.player);
} else {
player.bump();
}
}
2024-11-25 14:14:20 -05:00
sync() {
let { player } = this;
let { vel, dir, you } = player;
this.ws.send(JSON.stringify({ vel, dir, you }));
}
recv({data}) {
let {player} = this;
let you = player.you;
let entList = JSON.parse(data);
entList = entList.map(x => {
x.handleTick = Player.prototype.handleTick;
return x;
})
let matchingPlayer = entList.filter(x => x.you == you)
this.player = Object.assign(this.player || new Player(false,false),matchingPlayer[0]);
this.entities = entList;
if (this.entities.length == 0) this.entities = [this.player];
}
2024-11-25 14:14:20 -05:00
init() {
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(window.location.href);
this.ws.addEventListener('message',function(e) { that.recv(e) });
2024-11-25 14:14:20 -05:00
setInterval(function () { that.render() }, 1000 / 60);
2024-11-25 14:14:20 -05:00
setInterval(function () { that.sync() }, 1000 / 20);
2024-11-25 14:14:20 -05:00
game.canvas.onclick = () => that.click();
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();