Message

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

const dist = (a, b) => Math.hypot(b.y - a.y, b.x - a.

x);
const angle = (a, b) => Math.atan2(b.y - a.y, b.x - a.x);
const nearestEntityFrom = (list, location = Process.self) =>
Object.values(list)
.filter(e => e.x && e.y && e?.id !== location?.id)
.sort((a, b) => dist(a, location) - dist(b, location))[0];
const toRad = angle => (Math.PI / 180) * angle;
const toDeg = angle => (180 / Math.PI) * angle;

class Process {
static get self() {
return Player.list[socket.id];
}

static send(inputId, state) {


socket.emit('keyPress', { inputId, state });
}

static sendAngle(deg) {
Process.send('angle', deg);
window.mouseAngle = deg;
}

static speed(num) {
Process.send('mouseDistance', num || 0);
}

static hit(bool) {
Process.send('leftButton', bool);
}

static boost(bool) {
Process.send('rightButton', bool);
}

static chat(msg) {
Process.send('chatMessage', msg);
}

static pause() {
Process.send('pause', true);
}

static vel(entity = Process.self) {


let x = entity.newX - entity.x;
let y = entity.newY - entity.y;

return { x, y };
}

static predictPos(entity) {
let vel = Process.vel(entity);

return { x: entity.newX + vel.x, y: entity.newY + vel.y };


}
}

// ----------Testing---------- //
setInterval(e => {
let mob = nearestEntityFrom(window.Mob.list);
let food = nearestEntityFrom(window.Food.list);
let enemy = nearestEntityFrom(window.Player.list);

// Welcome to IF / ELSE hell :--)

if (enemy && dist(enemy, Process.self) < 500) {


Process.sendAngle(toDeg(angle(enemy, Process.self)));
Process.boost(true);

return;
}

if (dist(food, Process.self) > 300) {


Process.sendAngle(toDeg(angle(Process.self, mob)));

if (dist(mob, Process.self) < 200) {


Process.speed();
inputAttack(true);
} else {
inputAttack(false);
Process.speed(1);
Process.boost(false);
}
} else {
Process.sendAngle(toDeg(angle(Process.self, food)));

inputAttack(false);
Process.speed(1);
}
});

You might also like