This module provides utility functions for geometric calculations in a grid-based game. It includes functions to calculate the Manhattan distance and Euclidean distance between two points, as well as a function to determine if a player is moving towards a specific parcel based on their direction. The functions operate on coordinate objects with x and y properties, and the direction is represented as a string. The module is useful for pathfinding, collision detection, and movement logic in grid-based games.
Example
// Example usage:
const pointA = { x: 1, y: 2 };
const pointB = { x: 4, y: 6 }; 
const manhattanDistance = distance(pointA, pointB); // 7
const euclideanDistance = euclidean_distance(pointA, pointB); // 5
const player = { x: 3, y: 4, direction: "up" };
const parcel = { x: 3, y: 5 };
const isMovingTowards = goingTowardsParcel(player, parcel); // true

Methods

(static) distance(a, b) → {number}

Calculates the Manhattan distance between two points. The Manhattan distance is the sum of the absolute differences of their Cartesian coordinates. It is often used in grid-based pathfinding algorithms.
Parameters:
NameTypeDescription
aObject
bObject
Returns:
- The Manhattan distance between points a and b.
Type: 
number

(static) euclidean_distance(a, b) → {number}

Calculates the Euclidean distance between two points. The Euclidean distance is the straight-line distance between two points in Euclidean space. It is calculated using the Pythagorean theorem.
Parameters:
NameTypeDescription
aObject
bObject
Returns:
- The Euclidean distance between points a and b.
Type: 
number

(static) goingTowardsParcel(a, p) → {boolean}

Checks if a player is moving towards a specific parcel based on their direction. The function evaluates the player's direction and compares it with the position of the parcel. If the player is moving in the direction of the parcel, it returns true; otherwise, it returns false.
Parameters:
NameTypeDescription
aObject
pObject
Returns:
- True if the player is moving towards the parcel, false otherwise.
Type: 
boolean