Skip to content

Commit

Permalink
Added type definition
Browse files Browse the repository at this point in the history
  • Loading branch information
kotonefami committed Feb 28, 2024
1 parent 8166854 commit e9fd8cf
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 2 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"main": "dist/three-pathfinding.js",
"module": "dist/three-pathfinding.module.js",
"types": "dist/three-pathfinding.d.ts",
"source": "src/index.js",
"browserslist": [
"last 2 versions and > 1%",
Expand All @@ -22,8 +23,8 @@
"scripts": {
"build": "yarn dist && vite build --config demo/vite.config.js",
"dev": "concurrently \"yarn watch\" \"vite --config demo/vite.config.js --port 3000\"",
"watch": "microbundle watch --target web --globals three=THREE --external three",
"dist": "microbundle --target web --globals three=THREE --external three",
"watch": "microbundle watch --target web --globals three=THREE --external three --no-generateTypes && node scripts/copy-definition.mjs",
"dist": "microbundle --target web --globals three=THREE --external three --no-generateTypes && node scripts/copy-definition.mjs",
"version": "npm run dist && yarn test",
"postversion": "git push && git push --tags && npm publish",
"docs": "documentation build src/Pathfinding.js src/PathfindingHelper.js --shallow -f md | replace-between --target README.md --token API",
Expand Down
3 changes: 3 additions & 0 deletions scripts/copy-definition.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { copyFile } from 'fs/promises';

await copyFile('src/three-pathfinding.d.ts', 'dist/three-pathfinding.d.ts');
131 changes: 131 additions & 0 deletions src/three-pathfinding.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import type { BufferGeometry, Object3D, Vector3 } from "three";

declare module "three-pathfinding" {

/**
* Defines an instance of the pathfinding module, with one or more zones.
*/
export class Pathfinding {
/**
* Sets data for the given zone.
* @param zoneID
* @param zone
*/
public setZoneData(zoneID: string, zone: Zone): void;

/**
* Returns a random node within a given range of a given position.
* @param zoneID
* @param groupID
* @param nearPosition
* @param nearRange
*/
public getRandomNode(zoneID: string, groupID: number, nearPosition: Vector3, nearRange: number): Node;

/**
* Returns the closest node to the target position.
* @param position
* @param zoneID
* @param groupID
* @param checkPolygon (optional, default false)
*/
public getClosestNode(position: Vector3, zoneID: string, groupID: number, checkPolygon?: boolean): Node;

/**
* Returns a path between given start and end points. If a complete path cannot be found, will return the nearest endpoint available.
* @param startPosition Start position
* @param targetPosition Destination
* @param zoneID ID of current zone
* @param groupID Current group ID
* @returns Array of points defining the path
*/
public findPath(startPosition: Vector3, targetPosition: Vector3, zoneID: string, groupID: number): Vector3[];

/**
* Returns closest node group ID for given position.
* @param zoneID
* @param position
*/
public getGroup(zoneID: string, position: Vector3): number;

/**
* Clamps a step along the navmesh, given start and desired endpoint. May be used to constrain first-person / WASD controls.
* @param start
* @param end Desired endpoint.
* @param node
* @param zoneID
* @param groupID
* @param endTarget Updated endpoint.
* @returns Updated node.
*/
public clampStep(start: Vector3, end: Vector3, node: Node, zoneID: string, groupID: number, endTarget: Vector3): Node;

/**
* Builds a zone/node set from navigation mesh geometry.
* @param geometry
* @param tolerance Vertex welding tolerance. (optional, default 1e-4)
*/
public static createZone(geometry: BufferGeometry, tolerance?: number): Zone;
}

/**
* Helper for debugging pathfinding behavior.
*/
export class PathfindingHelper extends Object3D {
public setPath(path: Vector3[]): this;

public setPlayerPosition(position: Vector3): this;

public setTargetPosition(position: Vector3): this;

public setNodePosition(position: Vector3): this;

public setStepPosition(position: Vector3): this;

/**
* Hides all markers.
*/
public reset(): this;
}

/**
* Defines a zone of interconnected groups on a navigation mesh.
*/
type Zone = {
groups: Group[],
vertices: Vector3[]
};

/**
* Defines a group within a navigation mesh.
*/
type Group = {

};

/**
* Defines a node (or polygon) within a group.
*/
type Node = {
id: number,

/**
* IDs of neighboring nodes.
*/
neighbours: number[],

vertexIds: number[],

centroid: Vector3,

/**
* Array of portals, each defined by two vertex IDs.
*/
portals: number[][],

closed: boolean,

cost: number,
};

}

0 comments on commit e9fd8cf

Please sign in to comment.