unibutton/js/player.js
2024-09-29 01:58:56 -04:00

71 lines
No EOL
1.4 KiB
JavaScript

function Player(you) {
let pos = { x: Math.random() * 5000 - 50, y: Math.random() * 5000 - 50 };
let camera = { x: -pos.x, y: -pos.y };
let vel = { x: 0, y: 0 };
this.camera = camera;
this.pos = pos;
this.vel = vel;
this.rot = 0;
this.dir = 1;
this.ticks = 0;
this.health = 100;
this.you = you;
}
Player.prototype.bump = function () {
let player = this;
if (player.ticks < 10) {
player.dir *= -1;
}
player.vel.x *= 0.3;
player.vel.y *= 0.3;
player.vel.x += Math.sin(player.rot) * 12;
player.vel.y -= Math.cos(player.rot) * 12;
player.ticks = 0;
}
Player.prototype.handleTick = function(game) {
let { player } = game;
let ent = this;
ent.pos.x += ent.vel.x;
ent.pos.y += ent.vel.y;
ent.vel.x *= 0.9;
ent.vel.y *= 0.9;
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++;
let dist = ((ent.pos.x - player.pos.x) ** 2) + ((ent.pos.y - player.pos.y) ** 2);
let dp = (Math.sin(ent.rot) * (ent.pos.x - player.pos.x))
- (Math.cos(ent.rot) * (ent.pos.y - player.pos.y));
dp /= dist;
dp *= 10;
if (ent.you) return;
if (Math.random() < -dp && Math.random() > 3 / (ent.ticks+2)) {
ent.bump();
}
if (Math.sqrt(dist) < 96 / 2) {
player.health --;
}
}