UNPKG

@dill-pixel/plugin-crunch-physics

Version:

Crunch Physics

161 lines 4.81 kB
import { Actor } from './Actor'; import { Entity } from './Entity'; import { Solid } from './Solid'; import { EntityData, PhysicsEntityConfig, PhysicsEntityType, SensorOverlap, Vector2 } from './types'; /** * A trigger zone that can detect overlaps with actors. * Sensors are typically used for collectibles, triggers, and detection zones. * * Features: * - Overlap detection with specific actor types * - Optional gravity and movement * - Can be static or dynamic * - Callbacks for enter/exit events * * @typeParam T - Application type, defaults to base Application * * @example * ```typescript * // Create a coin pickup sensor * class Coin extends Sensor { * constructor() { * super({ * type: 'Coin', * position: [100, 100], * size: [32, 32], * view: coinSprite * }); * * // Only detect overlaps with player * this.collidableTypes = ['Player']; * } * * // Called when a player enters the coin * onActorEnter(actor: Actor) { * if (actor.type === 'Player') { * increaseScore(10); * this.physics.removeSensor(this); * } * } * } * * // Create a damage zone * class Spikes extends Sensor { * constructor() { * super({ * type: 'Spikes', * position: [300, 500], * size: [100, 32], * view: spikesSprite * }); * * this.collidableTypes = ['Player', 'Enemy']; * this.isStatic = true; // Don't move or fall * } * * onActorEnter(actor: Actor) { * if (actor.type === 'Player') { * actor.damage(10); * } * } * } * ``` */ export declare class Sensor<D extends EntityData = EntityData> extends Entity<D> { readonly entityType: PhysicsEntityType; /** Whether this sensor should be removed when culled */ shouldRemoveOnCull: boolean; /** List of actor types this sensor can detect */ collidableTypes: string[]; /** Current velocity in pixels per second */ velocity: Vector2; /** Whether this sensor should stay in place */ isStatic: boolean; /** Set of actors currently overlapping this sensor */ private overlappingActors; /** Cache for isRidingSolid check */ private _isRidingSolidCache; private _currentSensorOverlaps; private _currentOverlaps; setPosition(x: number, y: number): void; set x(value: number); get x(): number; set y(value: number); get y(): number; /** * Initializes or reinitializes the sensor with new configuration. * * @param config - Configuration for the sensor */ init(config: PhysicsEntityConfig<D>): void; /** * Checks if this sensor is riding the given solid. * Takes into account gravity direction for proper riding detection. * * @param solid - The solid to check against * @returns True if riding the solid */ isRiding(solid: Solid): boolean; /** * Checks if this sensor is riding any solid. * Uses caching to optimize multiple checks per frame. * * @returns True if riding any solid */ isRidingSolid(): boolean; /** * Force moves the sensor to a new position, ignoring static state and collisions. * * @param x - New X position * @param y - New Y position */ moveStatic(x: number, y: number): void; /** * Moves the sensor horizontally, passing through solids. * * @param amount - Distance to move in pixels */ moveX(amount: number): void; /** * Moves the sensor vertically, colliding with solids for riding. * * @param amount - Distance to move in pixels */ moveY(amount: number): void; /** * Updates the sensor's position and checks for overlapping actors. * * @param deltaTime - Delta time in seconds */ update(deltaTime: number): void; /** * Checks for overlapping actors and triggers callbacks. * * @returns Set of current overlaps */ checkActorOverlaps(): Set<SensorOverlap>; reset(): void; /** * Called when an actor starts overlapping with this sensor. * Override this to handle overlap start events. * * @param actor - The actor that entered */ onActorEnter<A extends Actor = Actor>(actor: A): void; /** * Called when an actor stops overlapping with this sensor. * Override this to handle overlap end events. * * @param actor - The actor that exited */ onActorExit<A extends Actor = Actor>(actor: A): void; /** * Gets all solids at the specified position. * * @param x - X position to check * @param y - Y position to check * @returns Array of solids at the position */ protected getSolidsAt(x: number, y: number): Solid[]; } //# sourceMappingURL=Sensor.d.ts.map