add emotes and npcs. this really needs a cleanup

This commit is contained in:
biglyderv 2024-11-25 14:14:20 -05:00
parent da4b0f5df8
commit f8b014507f
11 changed files with 267 additions and 85 deletions

30
common/npc.js Normal file
View file

@ -0,0 +1,30 @@
import { distF, uuidv4 } from './util.js'
class NPC {
constructor(you) {
let pos = { x: Math.random() * 1000 - 50, y: Math.random() * 1000 - 50 };
let vel = { x: 0, y: 0 };
this.pos = pos;
this.vel = vel;
this.you = you || uuidv4();
this.type = 'NPC';
this.immortal = true;
}
handleTick(game) {
let { entities } = game;
let rEntity = entities[Math.floor(Math.random() * entities.length)];
this.vel.x += (rEntity.pos.x - this.pos.x) * 0.0001;
this.vel.y += (rEntity.pos.y - this.pos.y) * 0.0001;
this.pos.x += this.vel.x;
this.pos.y += this.vel.y;
}
}
export default NPC;

View file

@ -1,93 +1,94 @@
import crypto from "../crypto.js";
import { distF, uuidv4 } from './util.js'
function distF(ent, target) {
return ((ent.pos.x - target.pos.x) ** 2) + ((ent.pos.y - target.pos.y) ** 2)
}
class Player {
constructor(you, isPlayer) {
let pos = { x: Math.random() * 1000 - 50, y: Math.random() * 1000 - 50 };
let camera = { x: -pos.x, y: -pos.y };
let vel = { x: 0, y: 0 };
function uuidv4() {
return "#000000-000000".replace(/[018]/g, c =>
(+c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> +c / 4).toString(16)
);
}
this.camera = camera;
this.pos = pos;
this.vel = vel;
this.rot = 0;
this.dir = 1;
this.ticks = 0;
function Player(you, isPlayer) {
let pos = { x: Math.random() * 1000 - 50, y: Math.random() * 1000 - 50 };
let camera = { x: -pos.x, y: -pos.y };
let vel = { x: 0, y: 0 };
this.health = 100;
this.camera = camera;
this.pos = pos;
this.vel = vel;
this.rot = 0;
this.dir = 1;
this.ticks = 0;
this.you = you || uuidv4();
this.health = 100;
this.isPlayer = isPlayer;
this.you = you || uuidv4();
this.headCount = 0;
this.isPlayer = isPlayer;
this.type = 'Player';
this.headCount = 0;
}
this.isMenu = false;
Player.prototype.bump = function () {
let player = this;
if (player.ticks < 7) {
player.dir *= -1;
this.r = 1;
}
bump() {
let player = this;
console.log(player.ticks)
if (player.ticks < 7) {
player.dir *= -1;
}
player.vel.x *= 0.3;
player.vel.y *= 0.3;
console.log(player.ticks)
player.vel.x += Math.sin(player.rot) * 12;
player.vel.y -= Math.cos(player.rot) * 12;
player.vel.x *= 0.3;
player.vel.y *= 0.3;
player.ticks = 0;
}
player.vel.x += Math.sin(player.rot) * 12;
player.vel.y -= Math.cos(player.rot) * 12;
Player.prototype.handleTick = function (game) {
let { entities, width, height } = game;
player.ticks = 0;
}
handleTick(game) {
let { entities, width, height } = game;
let ent = this;
let ent = this;
if (ent.health <= 0) return;
if (ent.health <= 0) return;
ent.pos.x += ent.vel.x;
ent.pos.y += ent.vel.y;
ent.pos.x += ent.vel.x;
ent.pos.y += ent.vel.y;
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);
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);
ent.vel.x *= 0.9;
ent.vel.y *= 0.9;
ent.vel.x *= 0.9;
ent.vel.y *= 0.9;
ent.rot += 0.03 * ent.dir;
ent.rot = ent.rot % (Math.PI * 10);
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.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++;
ent.ticks++;
for (let target of entities) {
if (target.you == ent.you) continue;
for (let target of entities) {
if (target.you == ent.you) continue;
let dist = distF(ent, target);
let dist = distF(ent, target);
let dp = (Math.sin(ent.rot) * (ent.pos.x - target.pos.x))
- (Math.cos(ent.rot) * (ent.pos.y - target.pos.y));
let dp = (Math.sin(ent.rot) * (ent.pos.x - target.pos.x))
- (Math.cos(ent.rot) * (ent.pos.y - target.pos.y));
dp /= Math.sqrt(dist + 0.1);
dp /= Math.sqrt(dist + 0.1);
if (Math.sqrt(dist) < 128 && 1 / dp < -0.2) {
target.health--;
if (Math.sqrt(dist) < 128 && 1 / dp < -0.2) {
if (target.type == 'NPC') {
ent.isMenu = true;
}
if (target.immortal) continue;
if (target.health == 0) {
ent.headCount++;
target.health--;
if (target.health == 0) {
ent.headCount++;
}
}
}
}

16
common/util.js Normal file
View file

@ -0,0 +1,16 @@
import crypto from "../crypto.js";
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)
);
}
export {
distF,
uuidv4
}