unibutton/common/player.js

71 lines
1.4 KiB
JavaScript
Raw Normal View History

2024-11-25 14:14:20 -05:00
function Player(you) {
2024-11-25 14:14:20 -05:00
let pos = { x: Math.random() * 5000 - 50, y: Math.random() * 5000 - 50 };
2024-11-25 14:14:20 -05:00
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;
2024-11-25 14:14:20 -05:00
this.ticks = 0;
2024-11-25 14:14:20 -05:00
this.health = 100;
2024-11-25 14:14:20 -05:00
this.you = you;
2024-11-25 14:14:20 -05:00
}
Player.prototype.bump = function () {
let player = this;
if (player.ticks < 10) {
player.dir *= -1;
}
2024-11-25 14:14:20 -05:00
player.vel.x *= 0.3;
player.vel.y *= 0.3;
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:20 -05:00
player.vel.x += Math.sin(player.rot) * 12;
player.vel.y -= Math.cos(player.rot) * 12;
2024-11-25 14:14:20 -05:00
player.ticks = 0;
2024-11-25 14:14:20 -05:00
}
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 --;
}
2024-11-25 14:14:20 -05:00
}