2024-10-01 04:03:55 -04:00
|
|
|
import { distF, uuidv4 } from './util.js'
|
2024-09-29 03:57:50 -04:00
|
|
|
|
2024-10-01 04:03:55 -04:00
|
|
|
class Player {
|
2024-10-03 05:16:40 -04:00
|
|
|
constructor(you, isPlayer, game = false) {
|
|
|
|
let pos = { x: (Math.random() - 0.5) * (game ? game.width : 1000), y:(Math.random() - 0.5) * (game ? game.height : 1000) };
|
2024-10-01 04:03:55 -04:00
|
|
|
let camera = { x: -pos.x, y: -pos.y };
|
|
|
|
let vel = { x: 0, y: 0 };
|
2024-09-29 03:16:15 -04:00
|
|
|
|
2024-10-01 04:03:55 -04:00
|
|
|
this.camera = camera;
|
|
|
|
this.pos = pos;
|
|
|
|
this.vel = vel;
|
|
|
|
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-03 05:16:40 -04:00
|
|
|
'type', 'camera', 'pos', 'vel', 'rot', 'dir', 'ticks', 'health', 'you', 'isPlayer', 'headCount', 'isMenu', 'r', 'isYou'
|
2024-10-02 04:14:56 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
this.legalProps = [
|
2024-10-03 05:16:40 -04:00
|
|
|
'vel', 'dir', 'camera', 'ticks', 'isMenu', 'r'
|
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
|
|
|
console.log(player.ticks)
|
2024-09-27 22:05:46 -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;
|
|
|
|
}
|
|
|
|
handleTick(game) {
|
|
|
|
let { entities, width, height } = game;
|
2024-09-29 01:58:56 -04:00
|
|
|
|
2024-10-01 04:03:55 -04:00
|
|
|
let ent = this;
|
2024-09-29 01:58:56 -04:00
|
|
|
|
2024-10-01 04:03:55 -04:00
|
|
|
if (ent.health <= 0) return;
|
2024-09-29 01:58:56 -04:00
|
|
|
|
2024-10-01 04:03:55 -04:00
|
|
|
ent.pos.x += ent.vel.x;
|
|
|
|
ent.pos.y += ent.vel.y;
|
2024-09-29 03:16:15 -04:00
|
|
|
|
2024-10-01 04:03:55 -04:00
|
|
|
ent.pos.x = Math.max(Math.min(ent.pos.x, width / 2), - width / 2);
|
|
|
|
ent.pos.y = Math.max(Math.min(ent.pos.y, height / 2), - height / 2);
|
2024-09-29 01:58:56 -04:00
|
|
|
|
2024-10-04 10:56:17 -04:00
|
|
|
if (Math.abs(ent.pos.x) >= width / 2 ) {
|
|
|
|
ent.vel.x = (Math.abs(ent.vel.x) + 10) * -Math.sign(ent.pos.x);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Math.abs(ent.pos.y) >= height / 2) {
|
|
|
|
ent.vel.y = (Math.abs(ent.vel.y) + 10) * -Math.sign(ent.pos.y);
|
|
|
|
}
|
|
|
|
|
2024-10-01 04:03:55 -04:00
|
|
|
ent.vel.x *= 0.9;
|
|
|
|
ent.vel.y *= 0.9;
|
2024-09-29 21:20:56 -04:00
|
|
|
|
2024-10-01 04:03:55 -04:00
|
|
|
ent.rot += 0.03 * ent.dir;
|
|
|
|
ent.rot = ent.rot % (Math.PI * 10);
|
2024-09-29 01:58:56 -04:00
|
|
|
|
2024-10-01 04:03:55 -04:00
|
|
|
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;
|
2024-09-29 01:58:56 -04:00
|
|
|
|
2024-10-01 04:03:55 -04:00
|
|
|
ent.ticks++;
|
2024-09-29 01:58:56 -04:00
|
|
|
|
2024-10-01 18:41:44 -04:00
|
|
|
let velness = Math.sqrt(ent.vel.x ** 2 + ent.vel.y ** 2);
|
|
|
|
|
2024-10-01 04:03:55 -04:00
|
|
|
for (let target of entities) {
|
|
|
|
if (target.you == ent.you) continue;
|
2024-09-29 01:58:56 -04:00
|
|
|
|
2024-10-01 04:03:55 -04:00
|
|
|
let dist = distF(ent, target);
|
2024-09-29 01:58:56 -04:00
|
|
|
|
2024-10-01 04:03:55 -04:00
|
|
|
let dp = (Math.sin(ent.rot) * (ent.pos.x - target.pos.x))
|
|
|
|
- (Math.cos(ent.rot) * (ent.pos.y - target.pos.y));
|
2024-09-29 01:58:56 -04:00
|
|
|
|
2024-10-01 04:03:55 -04:00
|
|
|
dp /= Math.sqrt(dist + 0.1);
|
2024-09-29 01:58:56 -04:00
|
|
|
|
2024-10-01 04:03:55 -04:00
|
|
|
if (Math.sqrt(dist) < 128 && 1 / dp < -0.2) {
|
2024-10-01 18:41:44 -04:00
|
|
|
if (target.type == 'NPC' && velness > 0.2) {
|
2024-10-01 04:03:55 -04:00
|
|
|
ent.isMenu = true;
|
2024-10-01 18:54:04 -04:00
|
|
|
ent.rot = 1.2;
|
2024-10-01 04:03:55 -04:00
|
|
|
}
|
|
|
|
if (target.immortal) continue;
|
2024-09-29 01:58:56 -04:00
|
|
|
|
2024-10-04 12:40:28 -04:00
|
|
|
let oldHealth = target.health
|
|
|
|
|
2024-10-04 10:56:17 -04:00
|
|
|
target.health += Math.floor( (dp-0.001) * 10);
|
|
|
|
|
|
|
|
target.vel.x += (target.pos.x - ent.pos.x) * 0.1;
|
|
|
|
target.vel.y += (target.pos.y - ent.pos.y) * 0.1;
|
2024-09-29 01:58:56 -04:00
|
|
|
|
2024-10-04 12:40:28 -04:00
|
|
|
if (target.health <= 0 && oldHealth > 0) {
|
2024-10-01 04:03:55 -04:00
|
|
|
ent.headCount++;
|
|
|
|
}
|
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;
|