0% found this document useful (0 votes)
29 views2 pages

Message

The document defines functions for calculating distance and angle between entities, finding the nearest entity from a list, and converting between radians and degrees. It also defines a Process class with methods for sending input to the server like mouse movement and clicking. The Process class has a method to get the player entity and methods to predict future entity positions based on velocity. The code sample at the end uses these methods to control a player entity based on proximity to enemies, food, and other mobs.

Uploaded by

ferlux
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views2 pages

Message

The document defines functions for calculating distance and angle between entities, finding the nearest entity from a list, and converting between radians and degrees. It also defines a Process class with methods for sending input to the server like mouse movement and clicking. The Process class has a method to get the player entity and methods to predict future entity positions based on velocity. The code sample at the end uses these methods to control a player entity based on proximity to enemies, food, and other mobs.

Uploaded by

ferlux
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 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