151 lines
No EOL
3.9 KiB
JavaScript
151 lines
No EOL
3.9 KiB
JavaScript
import { distF, uuidv4 } from './util.js'
|
|
import Entity from './entity.js';
|
|
|
|
class Player extends Entity {
|
|
constructor(you, isPlayer, game = false) {
|
|
super(game);
|
|
|
|
this.camera = { x: -this.pos.x, y: -this.pos.y };
|
|
|
|
this.rot = 0;
|
|
this.dir = 2;
|
|
this.ticks = 100;
|
|
|
|
this.health = 100;
|
|
|
|
this.you = you || uuidv4();
|
|
|
|
this.isPlayer = isPlayer;
|
|
|
|
this.headCount = 0;
|
|
|
|
this.type = 'Player';
|
|
|
|
this.isMenu = false;
|
|
|
|
this.r = 1;
|
|
|
|
this.isYou = false;
|
|
|
|
this.serverProps = [
|
|
'type', 'camera', 'pos', 'vel', 'rot', 'dir', 'ticks', 'health', 'you', 'isPlayer', 'headCount', 'isMenu', 'r', 'isYou', 'username'
|
|
];
|
|
|
|
this.legalProps = [
|
|
'vel', 'dir', 'camera', 'ticks', 'isMenu', 'r', 'ref'
|
|
];
|
|
}
|
|
bump() {
|
|
let player = this;
|
|
|
|
let f = 1;
|
|
|
|
if (player.ticks < 300) {
|
|
player.dir = -3 * Math.sign(player.dir);
|
|
} else {
|
|
f = 3;
|
|
player.dir = -0.005 * Math.sign(player.dir);
|
|
}
|
|
|
|
player.vel.x *= 0.9;
|
|
player.vel.y *= 0.9;
|
|
|
|
player.vel.x += Math.sin(player.rot) * 15 * f;
|
|
player.vel.y -= Math.cos(player.rot) * 15 * f;
|
|
|
|
player.ticks = 0;
|
|
}
|
|
handleInt(target) {
|
|
if (target.you == this.you) return;
|
|
|
|
let dist = distF(this, target);
|
|
|
|
let dp = (Math.sin(this.rot) * (this.pos.x - target.pos.x))
|
|
- (Math.cos(this.rot) * (this.pos.y - target.pos.y));
|
|
|
|
dp /= Math.sqrt(dist + 0.1);
|
|
|
|
if (!(Math.sqrt(dist) < 128)) return;
|
|
|
|
if (target.type == 'Shooter') {
|
|
let h = Math.floor((-dp -1.001));
|
|
if (h < 0) {
|
|
this.health += h;
|
|
}
|
|
target.dir = (Math.PI / 2) + this.rot;
|
|
}
|
|
|
|
if (!(1 / dp < -0.2)) return;
|
|
|
|
let velness = Math.sqrt(this.vel.x ** 2 + this.vel.y ** 2);
|
|
|
|
if (target.type == 'NPC' && velness > 0.2 && this.ticks >= 15) {
|
|
this.isMenu = true;
|
|
let that = this;
|
|
setTimeout(() => that.isMenu = false, 8000);
|
|
this.rot = 1.2;
|
|
}
|
|
|
|
let dmg = Math.floor((dp - 0.001) * 10);
|
|
|
|
if (target.immortal && target.type != 'Shooter') return;
|
|
target.vel.x += (target.pos.x - this.pos.x) * 0.1;
|
|
target.vel.y += (target.pos.y - this.pos.y) * 0.1;
|
|
if (target.immortal) return;
|
|
|
|
let oldHealth = target.health
|
|
|
|
|
|
target.health += dmg;
|
|
|
|
target.vel.x += (target.pos.x - this.pos.x) * 0.1;
|
|
target.vel.y += (target.pos.y - this.pos.y) * 0.1;
|
|
|
|
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;
|
|
|
|
this.headCount += 1 / 40;
|
|
|
|
this.headCount = Math.round(this.headCount * 1000) / 1000;
|
|
|
|
if (this.health <= 0) return;
|
|
|
|
this.pos.x += this.vel.x;
|
|
this.pos.y += this.vel.y;
|
|
|
|
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);
|
|
|
|
this.bounce(game);
|
|
|
|
this.vel.x *= 0.85;
|
|
this.vel.y *= 0.85;
|
|
|
|
this.rot += 0.03 * this.dir;
|
|
this.rot = this.rot % (Math.PI * 10);
|
|
|
|
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;
|
|
|
|
this.ticks++;
|
|
|
|
for (let target of entities) {
|
|
this.handleInt(target);
|
|
}
|
|
|
|
if (this.dir == 0) {
|
|
this.dir = 0.01;
|
|
}
|
|
|
|
this.dir = ((1.5 - Math.abs(this.dir)) * 0.005 + Math.abs(this.dir)) * Math.sign(this.dir);
|
|
}
|
|
}
|
|
|
|
export default Player; |