@dill-pixel/plugin-crunch-physics
Version:
Crunch Physics
265 lines • 9.18 kB
TypeScript
import { Container } from 'pixi.js';
import { Actor } from './Actor';
import { default as CrunchPhysicsPlugin } from './CrunchPhysicsPlugin';
import { Entity } from './Entity';
import { Group } from './Group';
import { AABBLike } from './interfaces';
import { Sensor } from './Sensor';
import { Solid } from './Solid';
import { ActorCollision, Collision, PhysicsEntityConfig, PhysicsEntityView, Rectangle, SensorOverlap } from './types';
/**
* Configuration options for the Crunch physics system.
* These options control the core behavior of the physics simulation.
*
* @example
* ```typescript
* const options: PhysicsSystemOptions = {
* gridSize: 32,
* gravity: 980,
* maxVelocity: 1000,
* debug: true,
* boundary: { x: 0, y: 0, width: 800, height: 600 },
* culling: true,
* collisionResolver: (collisions) => {
* for (const collision of collisions) {
* handleCollision(collision);
* }
* }
* };
* ```
*/
export interface PhysicsSystemOptions {
/** Reference to the parent Crunch physics plugin */
plugin: CrunchPhysicsPlugin;
/** Size of each grid cell for spatial partitioning (in pixels) */
gridSize: number;
/** Gravity strength in pixels per second squared */
gravity: number;
/** Maximum velocity for any entity in pixels per second */
maxVelocity: number;
/** Whether to render debug visualizations */
debug?: boolean;
/** World boundary for culling entities */
boundary?: Rectangle;
/** Whether to automatically cull out-of-bounds entities */
culling?: boolean;
/** Custom handler for resolving collisions */
collisionResolver?: (collisions: Collision[]) => void;
/** Custom handler for resolving sensor overlaps */
overlapResolver?: (overlaps: SensorOverlap[]) => void;
/** Custom handler for resolving actor-to-actor collisions */
actorCollisionResolver?: (collisions: ActorCollision[]) => void;
/** Whether to enable actor-to-actor collisions */
enableActorCollisions?: boolean;
/** Default collision layer for new entities */
defaultCollisionLayer?: number;
/** Default collision mask for new entities */
defaultCollisionMask?: number;
}
/**
* Core physics system that manages all physics entities and their interactions.
* Handles spatial partitioning, collision detection, and entity lifecycle.
*
* Features:
* - Grid-based spatial partitioning for efficient collision checks
* - Entity management (actors, solids, sensors, groups)
* - Collision and overlap detection
* - Debug visualization
* - Culling system for out-of-bounds entities
*
* @example
* ```typescript
* // Create the physics system
* const system = new System({
* gridSize: 32,
* gravity: 980,
* maxVelocity: 1000
* });
*
* // Add entities
* const player = system.createActor({
* type: 'Player',
* position: [100, 100]
* });
*
* const platform = system.createSolid({
* type: 'Platform',
* position: [0, 500],
* size: [800, 32]
* });
*
* // Update physics (in game loop)
* system.update(deltaTime);
* ```
*/
export declare class System {
private readonly options;
entities: Set<Entity>;
private _flaggedEntities;
actors: Set<Actor>;
solids: Set<Solid>;
sensors: Set<Sensor>;
groups: Set<Group>;
private actorsByType;
private solidsByType;
private sensorsByType;
private groupsByType;
private followers;
private groupWithEntities;
private grid;
private collisions;
private sensorOverlaps;
private actorCollisions;
private _checkedPairs;
private _collisionResultPool;
private _collisionResultPoolIndex;
private _actorCollisionPool;
private _actorCollisionPoolIndex;
private _debugContainer;
private _debugGfx;
private _debug;
private _movedActors;
private _activeGridCells;
private _potentialCollisions;
set debug(value: boolean);
set gridSize(value: number);
set gravity(value: number);
get gravity(): number;
set maxVelocity(value: number);
get maxVelocity(): number;
set boundary(value: Rectangle);
get boundary(): Rectangle;
get container(): Container;
addView(view: PhysicsEntityView): void;
constructor(options: PhysicsSystemOptions);
private _resetPositionFlags;
update(dt: number): void;
private updateSolids;
private updateActors;
private updateEntityPositions;
private processCollisionsAndOverlaps;
private cullOutOfBounds;
private updateSensor;
createEntity(config: PhysicsEntityConfig): Actor | Solid | Sensor | Group;
addEntity(entity: Entity | Actor | Solid | Sensor | Group): Actor | Solid | Sensor | Group;
removeEntity(entity: Entity | Actor | Solid | Sensor | Group): void;
createActor(config: PhysicsEntityConfig): Actor;
addActor(actor: Actor): Actor;
createSensor(config: PhysicsEntityConfig): Sensor;
addSensor(sensor: Sensor): Sensor;
createSolid(config: PhysicsEntityConfig): Solid;
addSolid(solid: Solid): Solid;
removeActor(actor: Actor, destroyView?: boolean): void;
removeSolid(solid: Solid, destroyView?: boolean): void;
removeSensor(sensor: Sensor, destroyView?: boolean): void;
moveSolid(solid: Solid, x: number, y: number): void;
getSolidsAt(x: number, y: number, entity: Actor | Sensor): Solid[];
addFollower(entity: Entity, follower: Entity): void;
removeFollower(entity: Entity): void;
getFollowersOf(entity: Entity): Entity[];
removeFollowersOf(entity: Entity): void;
addToGroup(group: Group, entity: Entity): Entity;
removeFromGroup(entity: Entity): Entity;
getEntitiesInGroup(group: Group): Entity[];
removeEntitiesOfGroup(group: Group): Entity[];
private overlaps;
private getCells;
private addSolidToGrid;
private removeSolidFromGrid;
debugRender(): void;
/**
* Get all entities of a specific type
* @param type The type to look for
* @returns Array of entities matching the type
*/
getByType(type: string): (Actor | Solid)[];
/**
* Get all actors of a specific type
* @param type The type to look for
* @returns Array of actors matching the type
*/
getActorsByType(type: string | string[]): Actor[];
/**
* Get all solids of a specific type
* @param type The type to look for
* @returns Array of solids matching the type
*/
getSolidsByType(type: string | string[]): Solid[];
/**
* Get all sensors of a specific type
* @param type The type to look for
* @returns Array of sensors matching the type
*/
getSensorsByType(type: string | string[]): Sensor[];
clearGrid(): void;
clearAll(destroy?: boolean): void;
destroy(): void;
/**
* Gets a collision result object from the pool or creates a new one if needed
*/
private getCollisionResult;
/**
* Gets an actor collision object from the pool or creates a new one if needed
*/
private getActorCollision;
/**
* Resets the collision result pools for the next frame
*/
private resetCollisionPools;
/**
* Updates an actor's position in the grid.
* This is called when an actor moves or when its size changes.
*
* @param actor - The actor to update in the grid
*/
updateActorInGrid(actor: Actor): void;
/**
* Removes an actor from its current grid cells.
*
* @param actor - The actor to remove from the grid
*/
removeActorFromGrid(actor: Actor): void;
/**
* Checks for collisions between all active actors.
* This is an O(n²) operation, so it can be expensive with many actors.
*/
private checkActorCollisions;
setCollisionResolver(resolver: (collisions: Collision[]) => void): void;
/**
* Sets a custom resolver for actor-to-actor collisions.
*
* @param resolver - Function to handle actor-to-actor collisions
*/
setActorCollisionResolver(resolver: (collisions: ActorCollision[]) => void): void;
/**
* Returns whether actor-to-actor collision detection is enabled.
*
* @returns True if actor collisions are enabled
*/
get enableActorCollisions(): boolean;
/**
* Enables or disables actor-to-actor collision detection.
* When enabled, it initializes the grid for all existing actors.
*
* @param enabled - Whether to enable actor-to-actor collisions
*/
setActorCollisionsEnabled(enabled: boolean): void;
createGroup(config: PhysicsEntityConfig): Group;
addGroup(group: Group): Group;
removeGroup(group: Group, destroyView?: boolean): void;
/**
* Get all groups of a specific type
* @param type The type to look for
* @returns Array of groups matching the type
*/
getGroupsByType(type: string | string[]): Group[];
/**
* Checks if two AABB rectangles overlap.
*
* @param a - The first AABB rectangle
* @param b - The second AABB rectangle
* @returns True if the rectangles overlap, false otherwise
*/
aabbOverlap(a: AABBLike, b: AABBLike): boolean;
}
//# sourceMappingURL=System.d.ts.map