2024-10-01 04:03:55 -04:00
|
|
|
import { distF, uuidv4 } from './util.js'
|
2024-10-18 19:30:00 -04:00
|
|
|
import Entity from './entity.js';
|
2024-09-29 03:57:50 -04:00
|
|
|
|
2024-10-18 19:30:00 -04:00
|
|
|
class Player extends Entity {
|
2024-10-03 05:16:40 -04:00
|
|
|
constructor(you, isPlayer, game = false) {
|
2024-10-18 19:30:00 -04:00
|
|
|
super(game);
|
|
|
|
|
|
|
|
this.camera = { x: -this.pos.x, y: -this.pos.y };
|
2024-09-29 03:16:15 -04:00
|
|
|
|
2024-10-01 04:03:55 -04:00
|
|
|
this.rot = 0;
|
|
|
|
this.dir = 1;
|
|
|
|
this.ticks = 0;
|
2024-09-27 21:42:21 -04:00
|
|
|
|
2024-10-01 04:03:55 -04:00
|
|
|
this.health = 100;
|
2024-09-27 22:21:01 -04:00
|
|
|
|
2024-10-01 04:03:55 -04:00
|
|
|
this.you = you || uuidv4();
|
2024-09-29 01:58:56 -04:00
|
|
|
|
2024-10-01 04:03:55 -04:00
|
|
|
this.isPlayer = isPlayer;
|
2024-09-29 03:16:15 -04:00
|
|
|
|
2024-10-01 04:03:55 -04:00
|
|
|
this.headCount = 0;
|
2024-09-29 03:16:15 -04:00
|
|
|
|
2024-10-01 04:03:55 -04:00
|
|
|
this.type = 'Player';
|
2024-09-27 22:05:46 -04:00
|
|
|
|
2024-10-01 04:03:55 -04:00
|
|
|
this.isMenu = false;
|
2024-09-27 22:05:46 -04:00
|
|
|
|
2024-10-01 04:03:55 -04:00
|
|
|
this.r = 1;
|
2024-10-02 04:14:56 -04:00
|
|
|
|
2024-10-02 04:22:32 -04:00
|
|
|
this.isYou = false;
|
|
|
|
|
2024-10-02 04:14:56 -04:00
|
|
|
this.serverProps = [
|
2024-10-20 16:09:22 -04:00
|
|
|
'type', 'camera', 'pos', 'vel', 'rot', 'dir', 'ticks', 'health', 'you', 'isPlayer', 'headCount', 'isMenu', 'r', 'isYou', 'username'
|
2024-10-02 04:14:56 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
this.legalProps = [
|
2024-10-06 07:14:05 -04:00
|
|
|
'vel', 'dir', 'camera', 'ticks', 'isMenu', 'r', 'ref'
|
2024-10-02 04:14:56 -04:00
|
|
|
];
|
2024-09-27 22:05:46 -04:00
|
|
|
}
|
2024-10-01 04:03:55 -04:00
|
|
|
bump() {
|
|
|
|
let player = this;
|
2024-09-27 22:05:46 -04:00
|
|
|
|
2024-10-02 04:14:56 -04:00
|
|
|
if (player.ticks < 15) {
|
2024-10-01 04:03:55 -04:00
|
|
|
player.dir *= -1;
|
|
|
|
}
|
2024-09-29 22:41:58 -04:00
|
|
|
|
2024-10-01 04:03:55 -04:00
|
|
|
player.vel.x *= 0.3;
|
|
|
|
player.vel.y *= 0.3;
|
2024-09-27 22:05:46 -04:00
|
|
|
|
2024-10-01 04:03:55 -04:00
|
|
|
player.vel.x += Math.sin(player.rot) * 12;
|
|
|
|
player.vel.y -= Math.cos(player.rot) * 12;
|
|
|
|
|
|
|
|
player.ticks = 0;
|
|
|
|
}
|
2024-10-21 20:46:47 -04:00
|
|
|
handleInt(target) {
|
|
|
|
if (target.you == this.you) return;
|
2024-09-29 01:58:56 -04:00
|
|
|
|
2024-10-21 20:46:47 -04:00
|
|
|
let dist = distF(this, target);
|
2024-10-19 18:24:14 -04:00
|
|
|
|
2024-10-21 20:46:47 -04:00
|
|
|
let dp = (Math.sin(this.rot) * (this.pos.x - target.pos.x))
|
|
|
|
- (Math.cos(this.rot) * (this.pos.y - target.pos.y));
|
2024-09-29 01:58:56 -04:00
|
|
|
|
2024-10-21 20:46:47 -04:00
|
|
|
dp /= Math.sqrt(dist + 0.1);
|
2024-09-29 01:58:56 -04:00
|
|
|
|
2024-10-21 20:46:47 -04:00
|
|
|
if (!(Math.sqrt(dist) < 128 && 1 / dp < -0.2)) return;
|
2024-09-29 03:16:15 -04:00
|
|
|
|
2024-10-21 20:46:47 -04:00
|
|
|
let velness = Math.sqrt(this.vel.x ** 2 + this.vel.y ** 2);
|
2024-09-29 01:58:56 -04:00
|
|
|
|
2024-10-21 20:46:47 -04:00
|
|
|
if (target.type == 'NPC' && velness > 0.2 && this.ticks >= 15) {
|
|
|
|
this.isMenu = true;
|
|
|
|
this.rot = 1.2;
|
|
|
|
}
|
2024-10-04 10:56:17 -04:00
|
|
|
|
2024-10-21 20:46:47 -04:00
|
|
|
if (target.type == 'Shooter') {
|
|
|
|
this.health -= 1;
|
|
|
|
}
|
2024-09-29 21:20:56 -04:00
|
|
|
|
2024-10-21 20:46:47 -04:00
|
|
|
if (target.immortal) return;
|
2024-09-29 01:58:56 -04:00
|
|
|
|
2024-10-21 20:46:47 -04:00
|
|
|
let oldHealth = target.health
|
2024-09-29 01:58:56 -04:00
|
|
|
|
2024-10-21 20:46:47 -04:00
|
|
|
let dmg = Math.floor((dp - 0.001) * 10);
|
2024-09-29 01:58:56 -04:00
|
|
|
|
2024-10-21 20:46:47 -04:00
|
|
|
target.health += Math.floor((dp - 0.001) * 10);
|
2024-10-01 18:41:44 -04:00
|
|
|
|
2024-10-21 20:46:47 -04:00
|
|
|
target.vel.x += (target.pos.x - this.pos.x) * 0.1;
|
|
|
|
target.vel.y += (target.pos.y - this.pos.y) * 0.1;
|
2024-09-29 01:58:56 -04:00
|
|
|
|
2024-10-21 20:46:47 -04:00
|
|
|
if (target.health <= 0 && oldHealth > 0) {
|
|
|
|
console.log(`Player ${target.you} died to a player ${this.you}`);
|
|
|
|
this.health -= dmg;
|
|
|
|
if (this.health > 100) this.health = 100;
|
|
|
|
this.headCount += 500;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
handleTick(game) {
|
|
|
|
let { entities, width, height } = game;
|
2024-09-29 01:58:56 -04:00
|
|
|
|
2024-10-21 20:46:47 -04:00
|
|
|
this.headCount += 1 / 200;
|
2024-09-29 01:58:56 -04:00
|
|
|
|
2024-10-21 20:46:47 -04:00
|
|
|
this.headCount = Math.round(this.headCount * 1000) / 1000;
|
2024-09-29 01:58:56 -04:00
|
|
|
|
2024-10-21 20:46:47 -04:00
|
|
|
if (this.health <= 0) return;
|
2024-10-18 19:43:40 -04:00
|
|
|
|
2024-10-21 20:46:47 -04:00
|
|
|
this.pos.x += this.vel.x;
|
|
|
|
this.pos.y += this.vel.y;
|
2024-10-18 19:43:40 -04:00
|
|
|
|
2024-10-21 20:46:47 -04:00
|
|
|
this.pos.x = Math.max(Math.min(this.pos.x, width / 2), - width / 2);
|
|
|
|
this.pos.y = Math.max(Math.min(this.pos.y, height / 2), - height / 2);
|
2024-09-29 01:58:56 -04:00
|
|
|
|
2024-10-21 20:46:47 -04:00
|
|
|
this.bounce(game);
|
2024-10-04 12:40:28 -04:00
|
|
|
|
2024-10-21 20:46:47 -04:00
|
|
|
this.vel.x *= 0.9;
|
|
|
|
this.vel.y *= 0.9;
|
2024-10-08 20:48:38 -04:00
|
|
|
|
2024-10-21 20:46:47 -04:00
|
|
|
this.rot += 0.03 * this.dir;
|
|
|
|
this.rot = this.rot % (Math.PI * 10);
|
2024-10-04 10:56:17 -04:00
|
|
|
|
2024-10-21 20:46:47 -04:00
|
|
|
this.camera.x = -this.pos.x * 0.1 + this.camera.x * 0.9;
|
|
|
|
this.camera.y = -this.pos.y * 0.1 + this.camera.y * 0.9;
|
2024-09-29 01:58:56 -04:00
|
|
|
|
2024-10-21 20:46:47 -04:00
|
|
|
this.ticks++;
|
|
|
|
|
|
|
|
for (let target of entities) {
|
|
|
|
this.handleInt(target);
|
2024-09-29 03:16:15 -04:00
|
|
|
}
|
2024-09-29 01:58:56 -04:00
|
|
|
}
|
2024-09-29 03:55:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Player;
|