2024-09-27 10:32:50 -04:00
|
|
|
const cs = 1024;
|
|
|
|
const assets = [
|
|
|
|
'assets/player.svg',
|
|
|
|
'assets/head.svg',
|
|
|
|
];
|
|
|
|
|
|
|
|
function Game() {
|
2024-09-27 21:42:21 -04:00
|
|
|
let assetsIn = {};
|
2024-09-27 10:32:50 -04:00
|
|
|
|
2024-09-27 21:42:21 -04:00
|
|
|
for (let asset in assets) {
|
|
|
|
assetsIn[asset] = new Image();
|
|
|
|
assetsIn[asset].src = assets[asset];
|
|
|
|
}
|
2024-09-27 10:32:50 -04:00
|
|
|
|
2024-09-27 21:42:21 -04:00
|
|
|
let canvas = document.querySelector("#canvas");
|
|
|
|
let player = new Player();
|
|
|
|
let entities = [player];
|
|
|
|
|
2024-09-27 22:05:46 -04:00
|
|
|
for (let i = 0; i < 50; i++) {
|
2024-09-27 21:42:21 -04:00
|
|
|
entities.push(new Player())
|
|
|
|
}
|
2024-09-27 10:32:50 -04:00
|
|
|
|
|
|
|
canvas.width = canvas.height = cs;
|
|
|
|
|
|
|
|
this.canvas = canvas;
|
2024-09-27 21:42:21 -04:00
|
|
|
this.ctx = canvas.getContext("2d");
|
|
|
|
this.assetsIn = assetsIn;
|
|
|
|
this.player = player;
|
|
|
|
this.entities = entities;
|
2024-09-27 10:32:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Game.prototype.main = function () {
|
2024-09-27 22:21:01 -04:00
|
|
|
|
2024-09-27 22:05:46 -04:00
|
|
|
let { entities, player } = this;
|
2024-09-27 10:32:50 -04:00
|
|
|
|
2024-09-27 22:21:01 -04:00
|
|
|
if (player.health <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-27 22:05:46 -04:00
|
|
|
for (let ent of entities) {
|
|
|
|
ent.pos.x += ent.vel.x;
|
|
|
|
ent.pos.y += ent.vel.y;
|
|
|
|
|
2024-09-27 22:21:01 -04:00
|
|
|
ent.vel.x *= 0.9;
|
|
|
|
ent.vel.y *= 0.9;
|
2024-09-27 22:05:46 -04:00
|
|
|
|
|
|
|
ent.rot += 0.03 * ent.dir;
|
|
|
|
ent.rot = ent.rot % (Math.PI * 10);
|
|
|
|
|
|
|
|
ent.camera.x = -ent.pos.x * 0.1 + ent.camera.x * 0.9;
|
|
|
|
ent.camera.y = -ent.pos.y * 0.1 + ent.camera.y * 0.9;
|
|
|
|
|
|
|
|
ent.ticks++;
|
2024-09-27 10:32:50 -04:00
|
|
|
|
2024-09-27 22:21:01 -04:00
|
|
|
let dist = ((ent.pos.x - player.pos.x) ** 2) + ((ent.pos.y - player.pos.y) ** 2);
|
|
|
|
|
2024-09-27 22:05:46 -04:00
|
|
|
let dp = (Math.sin(ent.rot) * (ent.pos.x - player.pos.x))
|
|
|
|
- (Math.cos(ent.rot) * (ent.pos.y - player.pos.y));
|
2024-09-27 10:32:50 -04:00
|
|
|
|
2024-09-27 22:21:01 -04:00
|
|
|
dp /= dist;
|
|
|
|
|
|
|
|
dp *= 10;
|
2024-09-27 17:24:30 -04:00
|
|
|
|
2024-09-27 22:21:01 -04:00
|
|
|
if (ent == player) continue;
|
2024-09-27 21:42:21 -04:00
|
|
|
|
2024-09-27 22:21:01 -04:00
|
|
|
if (Math.random() < -dp && Math.random() > 3 / (ent.ticks+2)) {
|
2024-09-27 22:05:46 -04:00
|
|
|
ent.bump();
|
|
|
|
}
|
2024-09-27 22:21:01 -04:00
|
|
|
|
|
|
|
if (Math.sqrt(dist) < 96 / 2) {
|
|
|
|
player.health --;
|
|
|
|
}
|
2024-09-27 21:42:21 -04:00
|
|
|
}
|
2024-09-27 10:32:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// todo: move into its own file
|
|
|
|
Game.prototype.render = function () {
|
2024-09-27 21:42:21 -04:00
|
|
|
let { ctx, assetsIn, entities, player } = this;
|
2024-09-27 10:32:50 -04:00
|
|
|
|
|
|
|
ctx.clearRect(0, 0, cs, cs);
|
|
|
|
|
|
|
|
ctx.save();
|
|
|
|
|
2024-09-27 21:42:21 -04:00
|
|
|
ctx.translate(player.camera.x + cs / 2, player.camera.y + cs / 2);
|
2024-09-27 10:32:50 -04:00
|
|
|
|
2024-09-27 21:42:21 -04:00
|
|
|
for (let ent of entities) {
|
|
|
|
ctx.save();
|
2024-09-27 10:32:50 -04:00
|
|
|
|
2024-09-27 21:42:21 -04:00
|
|
|
ctx.translate(ent.pos.x, ent.pos.y);
|
|
|
|
ctx.rotate(ent.rot);
|
|
|
|
ctx.drawImage(assetsIn[1], -64 / 2, -128, 64, 128);
|
2024-09-27 10:32:50 -04:00
|
|
|
|
2024-09-27 21:42:21 -04:00
|
|
|
ctx.restore();
|
2024-09-27 10:32:50 -04:00
|
|
|
|
2024-09-27 21:42:21 -04:00
|
|
|
ctx.drawImage(assetsIn[0], ent.pos.x - 64 / 2, ent.pos.y - 64 / 2, 64, 64);
|
|
|
|
}
|
2024-09-27 10:32:50 -04:00
|
|
|
|
|
|
|
ctx.restore();
|
2024-09-28 20:06:48 -04: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-09-27 10:32:50 -04:00
|
|
|
}
|
|
|
|
|
2024-09-27 22:21:01 -04:00
|
|
|
Game.prototype.ui = function() {
|
|
|
|
document.querySelector('.ui-text').textContent = `HP: ${this.player.health}`
|
|
|
|
}
|
|
|
|
|
2024-09-27 10:32:50 -04:00
|
|
|
Game.prototype.click = function () {
|
2024-09-27 21:42:21 -04:00
|
|
|
let { player } = this;
|
2024-09-27 10:32:50 -04:00
|
|
|
|
2024-09-28 20:06:48 -04:00
|
|
|
if (player.health <= 0) {
|
|
|
|
game = new Game();
|
|
|
|
} else {
|
|
|
|
player.bump(game);
|
|
|
|
}
|
2024-09-27 10:32:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var game = new Game();
|
|
|
|
|
|
|
|
setInterval(function () { game.main() }, 1000 / 60);
|
|
|
|
setInterval(function () { game.render() }, 1000 / 60);
|
2024-09-27 22:21:01 -04:00
|
|
|
setInterval(function () { game.ui() }, 1000 / 10);
|
2024-09-27 10:32:50 -04:00
|
|
|
|
|
|
|
game.canvas.onclick = () => game.click();
|