npetrovski / l2js-client

JavaScript client for Lineage 2
MIT License
105 stars 34 forks source link

Moveto #26

Closed ghost closed 2 years ago

ghost commented 3 years ago

I have an array with several game coordinates (x, y, z)

My doubt is How to check if the char arrived at the coordinate sent for me to send the next array list coordinate

Currently mine is already going straight to the last coordinate of the list is making the char get stuck in some obstacle.

l2.moveTo (x1, y1, z1) // got here?
l2.Me.MovingVector // ??
l2.moveTo (x2, y2, z2) // go to the next one in the list. 
npetrovski commented 2 years ago

I would suggest a custom moveTo function similar to this:

const moveTo = (x: number, y: number, z: number, timeoutSec = 20) => {
  return new Promise((resolve, reject) => {
    l2.moveTo(x, y, z);
    const t = setInterval(() => {
      if (Math.abs(l2.Me.X - x) <= 50 && Math.abs(l2.Me.Y - y) <= 50) {
        resolve(true);
        clearInterval(t);
      }
      timeoutSec--;
      if (timeoutSec <= 0) {
        reject("Timeout on moving to location.");
        clearInterval(t);
      }
    }, 1000);
  });
};