@dill-pixel/plugin-crunch-physics
Version:
Crunch Physics
246 lines • 7.23 kB
TypeScript
import { PointLike, SizeLike } from 'dill-pixel';
import { Container } from 'pixi.js';
import { Actor } from './Actor';
import { Entity } from './Entity';
import { Group } from './Group';
import { Sensor } from './Sensor';
import { Solid } from './Solid';
export interface Vector2 {
x: number;
y: number;
}
export interface Rectangle {
x: number;
y: number;
width: number;
height: number;
}
export type CollisionShape = 'rectangle';
export type EntityData = {
[key: string]: any;
};
export type PhysicsEntityClass = new (config?: PhysicsEntityConfig) => Actor | Solid | Sensor;
/**
* Collision layers using bitwise flags
* Each entity can belong to multiple layers (using bitwise OR)
* and can collide with multiple layers (using bitwise AND)
*/
export declare enum CollisionLayer {
NONE = 0,
DEFAULT = 1,
PLAYER = 2,
ENEMY = 4,
PROJECTILE = 8,
PLATFORM = 16,
TRIGGER = 32,
ITEM = 64,
WALL = 128,
FX = 256,
ALL = 4294967295
}
/**
* Interface for a registered collision layer
*/
export interface RegisteredCollisionLayer {
/** Name of the collision layer */
name: string;
/** Numeric value of the collision layer (bitwise) */
value: number;
/** Description of the collision layer (optional) */
description?: string;
}
/**
* Registry for tracking custom collision layers
*/
export declare class CollisionLayerRegistry {
private static _instance;
private _layers;
private _usedIndices;
/**
* Get the singleton instance of the registry
*/
static get instance(): CollisionLayerRegistry;
/**
* Register a new collision layer
*
* @param name Name of the collision layer
* @param index Index from 0-15 representing which user bit to use (gets shifted to bits 16-31)
* @param description Optional description of the layer
* @returns The registered collision layer
*/
register(name: string, index: number, description?: string): RegisteredCollisionLayer;
/**
* Get a registered collision layer by name
*
* @param name Name of the collision layer
* @returns The registered collision layer or undefined if not found
*/
get(name: string): RegisteredCollisionLayer | undefined;
/**
* Get all registered collision layers
*
* @returns Array of all registered collision layers
*/
getAll(): RegisteredCollisionLayer[];
/**
* Check if a collision layer with the given name exists
*
* @param name Name of the collision layer
* @returns True if the layer exists
*/
has(name: string): boolean;
/**
* Remove a registered collision layer
*
* @param name Name of the collision layer to remove
* @returns True if the layer was removed, false if it didn't exist
*/
remove(name: string): boolean;
/**
* Clear all registered collision layers
*/
clear(): void;
/**
* Get the next available index for a custom collision layer
*
* @returns The next available index or -1 if all indices are used
*/
getNextAvailableIndex(): number;
}
/**
* Utility functions for working with collision layers
*/
export declare const CollisionLayers: {
/**
* Creates a custom collision layer using bits 16-31 (user space)
*
* @param index Index from 0-15 representing which user bit to use (gets shifted to bits 16-31)
* @returns A unique collision layer value
*
* @example
* ```typescript
* // Create custom collision layers
* const WATER_LAYER = CollisionLayers.createLayer(0); // 1 << 16
* const LAVA_LAYER = CollisionLayers.createLayer(1); // 1 << 17
* const CLOUD_LAYER = CollisionLayers.createLayer(2); // 1 << 18
*
* // Use in entity creation
* const waterEntity = physics.createActor({
* type: 'Water',
* position: [100, 400],
* size: [800, 100],
* collisionLayer: WATER_LAYER,
* collisionMask: CollisionLayer.PLAYER | CollisionLayer.ENEMY
* });
* ```
*/
createLayer(index: number): number;
/**
* Creates a collision mask from multiple layers
*
* @param layers Array of collision layers to combine
* @returns A combined collision mask
*
* @example
* ```typescript
* // Create a mask that collides with players, enemies and projectiles
* const mask = CollisionLayers.createMask([
* CollisionLayer.PLAYER,
* CollisionLayer.ENEMY,
* CollisionLayer.PROJECTILE
* ]);
* ```
*/
createMask(layers: number[]): number;
/**
* Checks if a layer is included in a mask
*
* @param layer The layer to check
* @param mask The mask to check against
* @returns True if the layer is included in the mask
*
* @example
* ```typescript
* // Check if player layer is in the mask
* if (CollisionLayers.isLayerInMask(CollisionLayer.PLAYER, entity.collisionMask)) {
* console.log('Entity can collide with players');
* }
* ```
*/
isLayerInMask(layer: number, mask: number): boolean;
/**
* Get the registry for custom collision layers
*
* @returns The collision layer registry
*/
getRegistry(): CollisionLayerRegistry;
};
export interface PhysicsEntityConfig<D extends EntityData = EntityData> {
id?: string;
class?: PhysicsEntityClass;
type?: PhysicsEntityType;
position?: PointLike;
size?: SizeLike;
x?: number;
y?: number;
width?: number;
height?: number;
restitution?: number;
view?: PhysicsEntityView;
data?: Partial<D>;
group?: Group;
groupOffset?: PointLike;
follows?: Entity;
followOffset?: PointLike;
/** Collision layer this entity belongs to (bitwise) */
collisionLayer?: number;
/** Collision mask defining which layers this entity collides with (bitwise) */
collisionMask?: number;
/** Whether to disable actor-to-actor collisions for this entity */
disableActorCollisions?: boolean;
}
export interface CollisionResult {
collided: boolean;
normal?: Vector2;
penetration?: number;
solid: Solid;
}
export interface SensorOverlap {
type: `${PhysicsEntityType}|${PhysicsEntityType}`;
actor: Actor;
sensor: Sensor;
}
export interface CollisionResult {
collided: boolean;
normal?: Vector2;
penetration?: number;
solid: Solid;
pushingSolid?: Solid;
}
/**
* Result of an actor-to-actor collision
*/
export interface ActorCollisionResult {
collided: boolean;
normal?: Vector2;
penetration?: number;
actor: Actor;
}
export interface Collision {
type: `${PhysicsEntityType}|${PhysicsEntityType}`;
entity1: Actor | Sensor;
entity2: Actor | Solid;
result: CollisionResult;
}
/**
* Represents a collision between two actors
*/
export interface ActorCollision {
type: `${PhysicsEntityType}|${PhysicsEntityType}`;
actor1: Actor;
actor2: Actor;
result: ActorCollisionResult;
}
export type PhysicsEntityView = Container;
export type PhysicsEntityType = 'Actor' | 'Solid' | 'Sensor' | 'Group' | string;
//# sourceMappingURL=types.d.ts.map