All files / src util.js

100% Statements 30/30
100% Branches 0/0
100% Functions 2/2
100% Lines 30/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 311x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 3x 4x 4x  
/**
 * Util.
 */
export const Util = {
  /**
   * Get the distance between a and b,
   * assuming a: { x, y, width, height } and b: { x, y, width, height }
   *
   * @param {} a
   * @param {} b
   */
  dist(a, b) {
    const ax = a.x + (a.width || 0) / 2; // Get the centers
    const ay = a.y + (a.height || 0) / 2;
    const bx = b.x + (b.width || 0) / 2;
    const by = b.y + (b.height || 0) / 2;
    return Math.sqrt((ax - bx) ** 2 + (ay - by) ** 2);
  },
 
  /**
   * isWithinArea.
   *
   * @param {}
   */
  isWithinArea({ area, actor }) {
    const diffx = actor.x - area.x;
    const diffy = actor.y - area.y;
    return diffx >= 0 && diffx <= area.width && diffy >= 0 && diffy <= area.height;
  },
};