UNPKG

@rpgjs/physic

Version:

A deterministic 2D top-down physics library for RPG, sandbox and MMO games

129 lines 4.46 kB
import { PhysicsEngine } from '../api/PhysicsEngine'; import { Entity } from '../physics/Entity'; import { MovementBody, MovementStrategy } from './MovementStrategy'; /** * Resolves an entity from an identifier. * * When provided to the movement manager, the resolver enables ergonomic calls * that pass a string identifier instead of the `Entity` instance. * * @example * ```typescript * const manager = new MovementManager((id) => engine.getEntityByUUID(id)); * manager.add(player.uuid, new Dash(6, { x: 1, y: 0 }, 0.2)); * ``` */ export type EntityResolver = (id: string) => MovementBody | undefined; /** * Manages movement strategies assigned to entities. * * The manager executes strategies before each physics step, removes completed * behaviours and exposes utilities to inspect and maintain assignments. * * @example * ```typescript * const manager = new MovementManager((id) => engine.getEntityByUUID(id)); * manager.add(playerEntity, new LinearMove({ x: 2, y: 0 })); * * // Game loop * manager.update(deltaSeconds); * engine.step(); * ``` */ export declare class MovementManager { private readonly resolveEntity?; private readonly entries; private readonly entityWrappers; constructor(resolveEntity?: EntityResolver | undefined); /** * Convenience factory that binds the manager to a physics engine. * * @param engine - Physics engine whose entities will be controlled * @returns A movement manager configured with an entity resolver */ static forEngine(engine: PhysicsEngine): MovementManager; /** * Adds a movement strategy to an entity. * * @param target - Entity instance or entity UUID when a resolver is configured * @param strategy - Strategy to execute */ add(target: Entity | MovementBody | string, strategy: MovementStrategy): void; /** * Removes a specific strategy from an entity. * * @param target - Entity instance or identifier * @param strategy - Strategy instance to remove * @returns True when the strategy has been removed */ remove(target: Entity | MovementBody | string, strategy: MovementStrategy): boolean; /** * Removes all strategies from an entity. * * @param target - Entity or identifier */ clear(target: Entity | MovementBody | string): void; /** * Stops all movement for an entity immediately * * This method completely stops an entity's movement by: * - Removing all active movement strategies (dash, linear moves, etc.) * - Stopping the entity's velocity and angular velocity * - Clearing accumulated forces * - Waking up the entity if it was sleeping * * This is useful when changing maps, teleporting, or when you need * to halt an entity's movement completely without making it static. * * @param target - Entity, MovementBody, or identifier * * @example * ```ts * // Stop movement when changing maps * if (mapChanged) { * movement.stopMovement(playerEntity); * } * * // Stop movement after teleporting * entity.position.set(100, 200); * movement.stopMovement(entity); * * // Stop movement when player dies * if (player.isDead()) { * movement.stopMovement(playerEntity); * } * ``` */ stopMovement(target: Entity | MovementBody | string): void; /** * Checks if an entity has active strategies. * * @param target - Entity or identifier * @returns True when strategies are registered */ hasActiveStrategies(target: Entity | MovementBody | string): boolean; /** * Returns a snapshot of the strategies assigned to an entity. * * @param target - Entity or identifier * @returns Copy of the strategies array (empty array when none) */ getStrategies(target: Entity | MovementBody | string): MovementStrategy[]; /** * Updates all registered strategies. * * Call this method once per frame before `PhysicsEngine.step()` so that the * physics simulation integrates the velocities that strategies configure. * * @param dt - Time delta in seconds */ update(dt: number): void; /** * Removes all strategies from all entities. */ clearAll(): void; private resolveTarget; private wrapEntity; private isMovementBody; } //# sourceMappingURL=MovementManager.d.ts.map