2024-11-25 14:14:20 -05:00
|
|
|
import crypto from "../crypto.js";
|
2024-11-25 14:14:20 -05:00
|
|
|
|
2024-11-25 14:14:20 -05:00
|
|
|
function distF(ent, target) {
|
|
|
|
return ((ent.pos.x - target.pos.x) ** 2) + ((ent.pos.y - target.pos.y) ** 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
function uuidv4() {
|
|
|
|
return "#000000-000000".replace(/[018]/g, c =>
|
|
|
|
(+c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> +c / 4).toString(16)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function Player(you, isPlayer) {
|
2024-11-25 14:14:20 -05:00
|
|
|
let pos = { x: Math.random() * 1000 - 50, y: Math.random() * 1000 - 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
|
|
|
|
2024-11-25 14:14:20 -05:00
|
|
|
this.you = you || uuidv4();
|
|
|
|
|
|
|
|
this.isPlayer = isPlayer;
|
|
|
|
|
|
|
|
this.headCount = 0;
|
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
|
|
|
}
|
|
|
|
|
2024-11-25 14:14:20 -05:00
|
|
|
Player.prototype.handleTick = function (game) {
|
|
|
|
let { entities } = game;
|
2024-11-25 14:14:20 -05:00
|
|
|
|
|
|
|
let ent = this;
|
|
|
|
|
2024-11-25 14:14:20 -05:00
|
|
|
if (ent.health <= 0) return;
|
|
|
|
|
2024-11-25 14:14:20 -05:00
|
|
|
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++;
|
|
|
|
|
2024-11-25 14:14:20 -05:00
|
|
|
for (let target of entities) {
|
|
|
|
if (target.you == ent.you) continue;
|
2024-11-25 14:14:20 -05:00
|
|
|
|
2024-11-25 14:14:20 -05:00
|
|
|
let dist = distF(ent, target);
|
2024-11-25 14:14:20 -05:00
|
|
|
|
2024-11-25 14:14:20 -05:00
|
|
|
let dp = (Math.sin(ent.rot) * (ent.pos.x - target.pos.x))
|
|
|
|
- (Math.cos(ent.rot) * (ent.pos.y - target.pos.y));
|
2024-11-25 14:14:20 -05:00
|
|
|
|
2024-11-25 14:14:20 -05:00
|
|
|
dp /= Math.sqrt(dist + 0.1);
|
2024-11-25 14:14:20 -05:00
|
|
|
|
2024-11-25 14:14:20 -05:00
|
|
|
if (Math.sqrt(dist) < 128 && 1 / dp < -0.2) {
|
|
|
|
target.health--;
|
2024-11-25 14:14:20 -05:00
|
|
|
|
2024-11-25 14:14:20 -05:00
|
|
|
if (target.health == 0) {
|
|
|
|
ent.headCount++;
|
|
|
|
}
|
|
|
|
}
|
2024-11-25 14:14:20 -05:00
|
|
|
}
|
2024-11-25 14:14:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Player;
|