@caperjs/plugin-crunch 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,377 @@
1
+ import { defaultFactoryMethods, IApplication, PointLike, SignalConnection, SignalConnections } from '@caperjs/core';
2
+ import { default as CrunchPhysicsPlugin } from './CrunchPhysicsPlugin';
3
+ import { Group } from './Group';
4
+ import { System } from './System';
5
+ import { EntityData, PhysicsEntityConfig, PhysicsEntityType, PhysicsEntityView, Rectangle } from './types';
6
+ /**
7
+ * Base class for all physics entities in the Crunch physics system.
8
+ * Provides common functionality for position, size, view management, and lifecycle.
9
+ *
10
+ * Entity is the foundation for:
11
+ * - Actors (dynamic objects)
12
+ * - Solids (static objects)
13
+ * - Sensors (trigger zones)
14
+ *
15
+ * It handles:
16
+ * - Position and size management
17
+ * - View (sprite) management and updates
18
+ * - Group membership and relative positioning
19
+ * - Culling and lifecycle states
20
+ * - Signal connections for event handling
21
+ *
22
+ * @typeParam A - Application type, defaults to base Application
23
+ * @typeParam D - Entity data type, defaults to base EntityData
24
+ *
25
+ * @example
26
+ * ```typescript
27
+ * // Create a custom entity
28
+ * class CustomEntity extends Entity {
29
+ * constructor() {
30
+ * super({
31
+ * type: 'Custom',
32
+ * position: [100, 100],
33
+ * size: [32, 32],
34
+ * view: sprite
35
+ * });
36
+ * }
37
+ *
38
+ * // Override update for custom behavior
39
+ * update(dt: number) {
40
+ * super.update(dt);
41
+ * // Custom update logic
42
+ * }
43
+ * }
44
+ * ```
45
+ */
46
+ export declare class Entity<D extends EntityData = EntityData> {
47
+ readonly entityType: PhysicsEntityType;
48
+ protected _id: string;
49
+ /** Unique type identifier for this entity */
50
+ type: string;
51
+ /** Color to use when rendering debug visuals */
52
+ debugColor: number;
53
+ /** Whether the entity should be removed when culled (out of bounds) */
54
+ shouldRemoveOnCull: boolean;
55
+ /** Entity width in pixels */
56
+ width: number;
57
+ /** Entity height in pixels */
58
+ height: number;
59
+ /** Visual representation (sprite/graphics) of this entity */
60
+ view: PhysicsEntityView;
61
+ /** Whether this entity is active and should be updated */
62
+ /** Collision layer this entity belongs to (bitwise) */
63
+ collisionLayer: number;
64
+ /** Collision mask defining which layers this entity collides with (bitwise) */
65
+ collisionMask: number;
66
+ protected _data: Partial<D>;
67
+ protected _group: Group | null;
68
+ protected _groupOffset: {
69
+ x: number;
70
+ y: number;
71
+ };
72
+ protected _isCulled: boolean;
73
+ protected _isDestroyed: boolean;
74
+ protected _isInitialized: boolean;
75
+ protected _xRemainder: number;
76
+ protected _yRemainder: number;
77
+ protected _x: number;
78
+ protected _y: number;
79
+ protected signalConnections: SignalConnections;
80
+ protected _following: Entity | null;
81
+ protected _followOffset: {
82
+ x: number;
83
+ y: number;
84
+ };
85
+ protected _config: PhysicsEntityConfig<D> | undefined;
86
+ protected _active: boolean;
87
+ updatedFollowPosition: boolean;
88
+ updatedGroupPosition: boolean;
89
+ set active(value: boolean);
90
+ get active(): boolean;
91
+ set id(value: string);
92
+ get id(): string;
93
+ get make(): typeof defaultFactoryMethods;
94
+ /**
95
+ * Custom data associated with this entity
96
+ */
97
+ set data(value: Partial<D>);
98
+ get data(): Partial<D>;
99
+ setFollowing(entityToFollow: Entity | null, offset?: PointLike): void;
100
+ get followOffset(): {
101
+ x: number;
102
+ y: number;
103
+ };
104
+ get following(): Entity | null;
105
+ get followers(): Entity[];
106
+ /**
107
+ * The group this entity belongs to, if any.
108
+ * Groups allow for collective movement and management of entities.
109
+ */
110
+ get group(): Group | null;
111
+ set groupOffset(value: {
112
+ x: number;
113
+ y: number;
114
+ });
115
+ get groupOffset(): {
116
+ x: number;
117
+ y: number;
118
+ };
119
+ setGroup(group: Group | null, offset?: PointLike): void;
120
+ set position(value: PointLike);
121
+ get position(): PointLike;
122
+ /**
123
+ * Entity's X position in world space.
124
+ * If the entity belongs to a group, returns position relative to group.
125
+ */
126
+ set x(value: number);
127
+ get x(): number;
128
+ /**
129
+ * Entity's Y position in world space.
130
+ * If the entity belongs to a group, returns position relative to group.
131
+ */
132
+ set y(value: number);
133
+ get y(): number;
134
+ /** Whether this entity is currently culled (out of bounds) */
135
+ get isCulled(): boolean;
136
+ /** Whether this entity has been destroyed */
137
+ get isDestroyed(): boolean;
138
+ /** Reference to the main application instance */
139
+ get app(): IApplication;
140
+ /** Reference to the physics plugin */
141
+ get physics(): CrunchPhysicsPlugin;
142
+ /**
143
+ * Creates a new Entity instance.
144
+ *
145
+ * @param config - Optional configuration for the entity
146
+ */
147
+ constructor(config?: PhysicsEntityConfig<D>);
148
+ /**
149
+ * Called after construction to perform additional initialization.
150
+ * Override this in subclasses to add custom initialization logic.
151
+ */
152
+ protected initialize(): void;
153
+ /**
154
+ * Called before update to prepare for the next frame.
155
+ * Override this in subclasses to add pre-update logic.
156
+ */
157
+ preUpdate(): void;
158
+ /**
159
+ * Called every frame to update the entity's state.
160
+ * Override this in subclasses to add update logic.
161
+ *
162
+ * @param dt - Delta time in seconds since last update
163
+ */
164
+ update(dt: number): void;
165
+ /**
166
+ * Called after update to finalize the frame.
167
+ * Override this in subclasses to add post-update logic.
168
+ */
169
+ postUpdate(): void;
170
+ /**
171
+ * Excludes collision types for this entity
172
+ * @deprecated Use setCollisionMask instead
173
+ */
174
+ excludeCollisionType(): void;
175
+ /**
176
+ * Includes collision types for this entity
177
+ * @deprecated Use setCollisionMask instead
178
+ */
179
+ includeCollisionType(): void;
180
+ /**
181
+ * Removes collision types for this entity
182
+ * @deprecated Use removeCollisionMask instead
183
+ */
184
+ removeCollisionType(): void;
185
+ /**
186
+ * Adds collision types for this entity
187
+ * @deprecated Use addCollisionMask instead
188
+ */
189
+ addCollisionType(): void;
190
+ /**
191
+ * Checks if this entity can collide with a specific type
192
+ */
193
+ canCollideWith(): boolean;
194
+ /**
195
+ * Adds the entity's view to the physics container and updates its position.
196
+ */
197
+ protected addView(): void;
198
+ /**
199
+ * Initializes or reinitializes the entity with new configuration.
200
+ * Used by object pools when recycling entities.
201
+ *
202
+ * @param config - New configuration to apply
203
+ */
204
+ init(config: PhysicsEntityConfig<D>): void;
205
+ /**
206
+ * Resets the entity to its initial state for reuse in object pools.
207
+ * Override this to handle custom reset logic.
208
+ */
209
+ reset(): void;
210
+ /** Reference to the physics system */
211
+ get system(): System;
212
+ /**
213
+ * Called when the entity is added to a group.
214
+ * Override this to handle custom group addition logic.
215
+ */
216
+ onAddedToGroup(): void;
217
+ /**
218
+ * Called when the entity is removed from a group.
219
+ * Override this to handle custom group removal logic.
220
+ */
221
+ onRemovedFromGroup(): void;
222
+ /**
223
+ * Updates the entity's position and view.
224
+ */
225
+ updatePosition(): void;
226
+ /**
227
+ * Called when the entity is culled (goes out of bounds).
228
+ * Override this to handle culling differently.
229
+ */
230
+ onCull(): void;
231
+ /**
232
+ * Called when the entity is brought back after being culled.
233
+ * Override this to handle unculling differently.
234
+ */
235
+ onUncull(): void;
236
+ /**
237
+ * Prepares the entity for removal/recycling.
238
+ * Override this to handle custom cleanup.
239
+ */
240
+ destroy(): void;
241
+ /**
242
+ * Called when the entity is removed from the physics system.
243
+ * Override this to handle custom removal logic.
244
+ */
245
+ onRemoved(): void;
246
+ /**
247
+ * Sets a new view for the entity and updates its position.
248
+ *
249
+ * @param view - The new view to use
250
+ */
251
+ setView(view: PhysicsEntityView): void;
252
+ /**
253
+ * Updates the view's position to match the entity's position.
254
+ */
255
+ updateView(): void;
256
+ /**
257
+ * Gets the entity's bounding rectangle.
258
+ *
259
+ * @returns Rectangle representing the entity's bounds
260
+ */
261
+ getBounds(): Rectangle;
262
+ /**
263
+ * Sets the entity's position, resetting any movement remainders.
264
+ *
265
+ * @param x - New X position
266
+ * @param y - New Y position
267
+ */
268
+ setPosition(x: number, y: number): void;
269
+ /**
270
+ * Alias for setPosition.
271
+ *
272
+ * @param x - New X position
273
+ * @param y - New Y position
274
+ */
275
+ moveTo(x: number, y: number): void;
276
+ /**
277
+ * Adds signal connections to the entity.
278
+ *
279
+ * @param args - Signal connections to add
280
+ */
281
+ addSignalConnection(...args: SignalConnection[]): void;
282
+ /**
283
+ * Alias for addSignalConnection.
284
+ *
285
+ * @param args - Signal connections to add
286
+ */
287
+ connectSignal(...args: SignalConnection[]): void;
288
+ /**
289
+ * Alias for addSignalConnection, specifically for action signals.
290
+ *
291
+ * @param args - Action signal connections to add
292
+ */
293
+ connectAction(...args: SignalConnection[]): void;
294
+ /**
295
+ * Checks if this entity can collide with another entity
296
+ */
297
+ canCollideWithEntity(entity: Entity): boolean;
298
+ /**
299
+ * Sets the collision layer for this entity
300
+ *
301
+ * @param layer The collision layer or layers (can be combined with bitwise OR)
302
+ */
303
+ setCollisionLayer(layer: number): void;
304
+ /**
305
+ * Adds the specified layers to this entity's collision layer
306
+ *
307
+ * @param layers The layers to add (can be combined with bitwise OR)
308
+ */
309
+ addCollisionLayer(layers: number): void;
310
+ /**
311
+ * Removes the specified layers from this entity's collision layer
312
+ *
313
+ * @param layers The layers to remove (can be combined with bitwise OR)
314
+ */
315
+ removeCollisionLayer(layers: number): void;
316
+ /**
317
+ * Sets the collision mask for this entity
318
+ *
319
+ * @param mask The collision mask (can be combined with bitwise OR)
320
+ */
321
+ setCollisionMask(...mask: number[]): void;
322
+ /**
323
+ * Adds the specified layers to this entity's collision mask
324
+ *
325
+ * @param layers The layers to add to the mask (can be combined with bitwise OR)
326
+ */
327
+ addCollisionMask(layers: number): void;
328
+ /**
329
+ * Removes the specified layers from this entity's collision mask
330
+ *
331
+ * @param layers The layers to remove from the mask (can be combined with bitwise OR)
332
+ */
333
+ removeCollisionMask(layers: number): void;
334
+ /**
335
+ * Checks if this entity belongs to a specific collision layer
336
+ *
337
+ * @param layer The layer to check
338
+ * @returns True if the entity belongs to the specified layer
339
+ *
340
+ * @example
341
+ * ```typescript
342
+ * // Check if entity is on the PLAYER layer
343
+ * if (entity.hasCollisionLayer(CollisionLayer.PLAYER)) {
344
+ * console.log('Entity is a player');
345
+ * }
346
+ *
347
+ * // Check if entity is on a custom layer
348
+ * const WATER_LAYER = CollisionLayers.createLayer(0);
349
+ * if (entity.hasCollisionLayer(WATER_LAYER)) {
350
+ * console.log('Entity is water');
351
+ * }
352
+ * ```
353
+ */
354
+ hasCollisionLayer(layer: number): boolean;
355
+ /**
356
+ * Checks if this entity can collide with a specific collision layer
357
+ *
358
+ * @param layer The layer to check
359
+ * @returns True if the entity can collide with the specified layer
360
+ *
361
+ * @example
362
+ * ```typescript
363
+ * // Check if entity can collide with players
364
+ * if (entity.canCollideWithLayer(CollisionLayer.PLAYER)) {
365
+ * console.log('Entity can collide with players');
366
+ * }
367
+ *
368
+ * // Check if entity can collide with a custom layer
369
+ * const WATER_LAYER = CollisionLayers.createLayer(0);
370
+ * if (entity.canCollideWithLayer(WATER_LAYER)) {
371
+ * console.log('Entity can collide with water');
372
+ * }
373
+ * ```
374
+ */
375
+ canCollideWithLayer(layer: number): boolean;
376
+ }
377
+ //# sourceMappingURL=Entity.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Entity.d.ts","sourceRoot":"","sources":["../src/Entity.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,qBAAqB,EACrB,YAAY,EACZ,SAAS,EAGT,gBAAgB,EAChB,iBAAiB,EAClB,MAAM,eAAe,CAAC;AACvB,OAAO,mBAAmB,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAEL,UAAU,EACV,mBAAmB,EACnB,iBAAiB,EACjB,iBAAiB,EACjB,SAAS,EACV,MAAM,SAAS,CAAC;AAGjB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,qBAAa,MAAM,CAAC,CAAC,SAAS,UAAU,GAAG,UAAU;IACnD,SAAgB,UAAU,EAAE,iBAAiB,CAAC;IAC9C,SAAS,CAAC,GAAG,EAAE,MAAM,CAAC;IACtB,6CAA6C;IACtC,IAAI,EAAG,MAAM,CAAC;IA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SAAS;IAS7B;;;;;OAKG;IACI,WAAW,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,IAAI;IAQ9C;;;;;OAKG;IACI,MAAM,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,IAAI;IAIzC;;;;OAIG;IACI,mBAAmB,CAAC,GAAG,IAAI,EAAE,gBAAgB,EAAE;IAMtD;;;;OAIG;IACI,aAAa,CAAC,GAAG,IAAI,EAAE,gBAAgB,EAAE;IAMhD;;;;OAIG;IACI,aAAa,CAAC,GAAG,IAAI,EAAE,gBAAgB,EAAE;IAMhD;;OAEG;IACI,oBAAoB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAMpD;;;;OAIG;IACI,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAI7C;;;;OAIG;IACI,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAI9C;;;;OAIG;IACI,oBAAoB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAIjD;;;;OAIG;IACI,gBAAgB,CAAC,GAAG,IAAI,EAAE,MAAM,EAAE,GAAG,IAAI;IAIhD;;;;OAIG;IACI,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAI7C;;;;OAIG;IACI,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAIhD;;;;;;;;;;;;;;;;;;;OAmBG;IACI,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;IAIhD;;;;;;;;;;;;;;;;;;;OAmBG;IACI,mBAAmB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;CAGnD"}
package/lib/Group.d.ts ADDED
@@ -0,0 +1,122 @@
1
+ import { Actor } from './Actor';
2
+ import { Entity } from './Entity';
3
+ import { Sensor } from './Sensor';
4
+ import { Solid } from './Solid';
5
+ import { EntityData, PhysicsEntityType } from './types';
6
+ /**
7
+ * A container for managing collections of physics entities that move together.
8
+ * Groups are useful for creating compound objects, moving platforms with riders,
9
+ * and other scenarios where multiple entities need to move as one unit.
10
+ *
11
+ * Features:
12
+ * - Maintains relative positioning of child entities
13
+ * - Supports actors, solids, and sensors as children
14
+ * - Preserves world positions when adding entities
15
+ * - Provides type-safe access to children by entity type
16
+ *
17
+ * @typeParam T - Application type, defaults to base Application
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * // Create a moving platform with obstacles
22
+ * const platformGroup = physics.createGroup({
23
+ * type: 'MovingPlatform',
24
+ * position: [100, 300]
25
+ * });
26
+ *
27
+ * // Add platform and spikes
28
+ * const platform = physics.createSolid({
29
+ * type: 'Platform',
30
+ * size: [200, 32]
31
+ * });
32
+ *
33
+ * const spikes = physics.createSensor({
34
+ * type: 'Spikes',
35
+ * position: [0, -32],
36
+ * size: [200, 32]
37
+ * });
38
+ *
39
+ * platformGroup.add(platform);
40
+ * platformGroup.add(spikes);
41
+ *
42
+ * // Move the entire group
43
+ * gsap.to(platformGroup, {
44
+ * x: 500,
45
+ * duration: 2,
46
+ * yoyo: true,
47
+ * repeat: -1
48
+ * });
49
+ * ```
50
+ */
51
+ export declare class Group<D extends EntityData = EntityData> extends Entity<D> {
52
+ protected _visible: boolean;
53
+ entityType: PhysicsEntityType;
54
+ /** Map of child entities to their relative offsets from the group's position */
55
+ private childOffsets;
56
+ /** Whether this group's position is static (not affected by physics) */
57
+ isStatic: boolean;
58
+ /**
59
+ * Gets the relative offset of a child entity from the group's position.
60
+ * @param entity - The child entity to get the offset for
61
+ * @returns The relative {x, y} offset of the entity
62
+ */
63
+ getChildOffset(entity: Entity): {
64
+ x: number;
65
+ y: number;
66
+ };
67
+ set visible(value: boolean);
68
+ get visible(): boolean;
69
+ /**
70
+ * Sets the group's X position, affecting all child entities.
71
+ */
72
+ set x(value: number);
73
+ get x(): number;
74
+ /**
75
+ * Sets the group's Y position, affecting all child entities.
76
+ */
77
+ set y(value: number);
78
+ get y(): number;
79
+ /**
80
+ * Add an entity to this container
81
+ * @param entity The entity to add
82
+ * @param preserveWorldPosition If true, the entity's world position will be preserved
83
+ */
84
+ add(entity: Entity, offset?: {
85
+ x: number;
86
+ y: number;
87
+ }): Group;
88
+ /**
89
+ * Remove an entity from this container
90
+ */
91
+ remove(entity: Entity): Group;
92
+ /**
93
+ * Move the container and all its children
94
+ */
95
+ move(x: number, y: number): void;
96
+ /**
97
+ * Force move the container and all its children to an absolute position
98
+ */
99
+ moveTo(x: number, y: number): void;
100
+ /**
101
+ * Get all children of a specific type
102
+ */
103
+ getChildrenByType<E extends Entity>(type: PhysicsEntityType): E[];
104
+ /**
105
+ * Get all actors in this container
106
+ */
107
+ getActors(): Actor[];
108
+ /**
109
+ * Get all solids in this container
110
+ */
111
+ getSolids(): Solid[];
112
+ /**
113
+ * Get all sensors in this container
114
+ */
115
+ getSensors(): Sensor[];
116
+ /**
117
+ * Get all groups in this container
118
+ */
119
+ getGroups(): Group[];
120
+ destroy(): void;
121
+ }
122
+ //# sourceMappingURL=Group.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Group.d.ts","sourceRoot":"","sources":["../src/Group.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAExD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,qBAAa,KAAK,CAAC,CAAC,SAAS,UAAU,GAAG,UAAU,CAAE,SAAQ,MAAM,CAAC,CAAC,CAAC;IACrE,SAAS,CAAC,QAAQ,EAAE,OAAO,CAAQ;IAE5B,UAAU,EAAE,iBAAiB,CAAW;IAC/C,gFAAgF;IAChF,OAAO,CAAC,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Data, PhysicsEntityConfig, PhysicsEntityType, SensorOverlap, Vector2 } from './types';
5
+ /**
6
+ * A trigger zone that can detect overlaps with actors.
7
+ * Sensors are typically used for collectibles, triggers, and detection zones.
8
+ *
9
+ * Features:
10
+ * - Overlap detection with specific actor types
11
+ * - Optional gravity and movement
12
+ * - Can be static or dynamic
13
+ * - Callbacks for enter/exit events
14
+ *
15
+ * @typeParam T - Application type, defaults to base Application
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * // Create a coin pickup sensor
20
+ * class Coin extends Sensor {
21
+ * constructor() {
22
+ * super({
23
+ * type: 'Coin',
24
+ * position: [100, 100],
25
+ * size: [32, 32],
26
+ * view: coinSprite
27
+ * });
28
+ *
29
+ * // Only detect overlaps with player
30
+ * this.collidableTypes = ['Player'];
31
+ * }
32
+ *
33
+ * // Called when a player enters the coin
34
+ * onActorEnter(actor: Actor) {
35
+ * if (actor.type === 'Player') {
36
+ * increaseScore(10);
37
+ * this.physics.removeSensor(this);
38
+ * }
39
+ * }
40
+ * }
41
+ *
42
+ * // Create a damage zone
43
+ * class Spikes extends Sensor {
44
+ * constructor() {
45
+ * super({
46
+ * type: 'Spikes',
47
+ * position: [300, 500],
48
+ * size: [100, 32],
49
+ * view: spikesSprite
50
+ * });
51
+ *
52
+ * this.collidableTypes = ['Player', 'Enemy'];
53
+ * this.isStatic = true; // Don't move or fall
54
+ * }
55
+ *
56
+ * onActorEnter(actor: Actor) {
57
+ * if (actor.type === 'Player') {
58
+ * actor.damage(10);
59
+ * }
60
+ * }
61
+ * }
62
+ * ```
63
+ */
64
+ export declare class Sensor<D extends EntityData = EntityData> extends Entity<D> {
65
+ readonly entityType: PhysicsEntityType;
66
+ /** Whether this sensor should be removed when culled */
67
+ shouldRemoveOnCull: boolean;
68
+ /** List of actor types this sensor can detect */
69
+ collidableTypes: string[];
70
+ /** Current velocity in pixels per second */
71
+ velocity: Vector2;
72
+ /** Whether this sensor should stay in place */
73
+ isStatic: boolean;
74
+ /** Set of actors currently overlapping this sensor */
75
+ private overlappingActors;
76
+ /** Cache for isRidingSolid check */
77
+ private _isRidingSolidCache;
78
+ private _currentSensorOverlaps;
79
+ private _currentOverlaps;
80
+ setPosition(x: number, y: number): void;
81
+ set x(value: number);
82
+ get x(): number;
83
+ set y(value: number);
84
+ get y(): number;
85
+ /**
86
+ * Initializes or reinitializes the sensor with new configuration.
87
+ *
88
+ * @param config - Configuration for the sensor
89
+ */
90
+ init(config: PhysicsEntityConfig<D>): void;
91
+ /**
92
+ * Checks if this sensor is riding the given solid.
93
+ * Takes into account gravity direction for proper riding detection.
94
+ *
95
+ * @param solid - The solid to check against
96
+ * @returns True if riding the solid
97
+ */
98
+ isRiding(solid: Solid): boolean;
99
+ /**
100
+ * Checks if this sensor is riding any solid.
101
+ * Uses caching to optimize multiple checks per frame.
102
+ *
103
+ * @returns True if riding any solid
104
+ */
105
+ isRidingSolid(): boolean;
106
+ /**
107
+ * Force moves the sensor to a new position, ignoring static state and collisions.
108
+ *
109
+ * @param x - New X position
110
+ * @param y - New Y position
111
+ */
112
+ moveStatic(x: number, y: number): void;
113
+ /**
114
+ * Moves the sensor horizontally, passing through solids.
115
+ *
116
+ * @param amount - Distance to move in pixels
117
+ */
118
+ moveX(amount: number): void;
119
+ /**
120
+ * Moves the sensor vertically, colliding with solids for riding.
121
+ *
122
+ * @param amount - Distance to move in pixels
123
+ */
124
+ moveY(amount: number): void;
125
+ /**
126
+ * Updates the sensor's position and checks for overlapping actors.
127
+ *
128
+ * @param deltaTime - Delta time in seconds
129
+ */
130
+ update(deltaTime: number): void;
131
+ /**
132
+ * Checks for overlapping actors and triggers callbacks.
133
+ *
134
+ * @returns Set of current overlaps
135
+ */
136
+ checkActorOverlaps(): Set<SensorOverlap>;
137
+ reset(): void;
138
+ /**
139
+ * Called when an actor starts overlapping with this sensor.
140
+ * Override this to handle overlap start events.
141
+ *
142
+ * @param actor - The actor that entered
143
+ */
144
+ onActorEnter<A extends Actor = Actor>(actor: A): void;
145
+ /**
146
+ * Called when an actor stops overlapping with this sensor.
147
+ * Override this to handle overlap end events.
148
+ *
149
+ * @param actor - The actor that exited
150
+ */
151
+ onActorExit<A extends Actor = Actor>(actor: A): void;
152
+ /**
153
+ * Gets all solids at the specified position.
154
+ *
155
+ * @param x - X position to check
156
+ * @param y - Y position to check
157
+ * @returns Array of solids at the position
158
+ */
159
+ protected getSolidsAt(x: number, y: number): Solid[];
160
+ }
161
+ //# sourceMappingURL=Sensor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Sensor.d.ts","sourceRoot":"","sources":["../src/Sensor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,UAAU,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAErG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0DG;AACH,qBAAa,MAAM,CAAC,CAAC,SAAS,UAAU,GAAG,UAAU,CAAE,SAAQ,MAAM,CAAC,CAAC,CAAC;IACtE,SAAgB,UAAU,EAAE,iBAAiB,CAAY;IACzD,wDAAwD;IACjD,kBAAkB,UAAS;IAElC,iDAAiD;IAC1C,eAAe,EAAE,MAAM,EAAE,CAAM;IAEtC,4CAA4C;IACrC,QAAQ,EAAE,OAAO,CAAkB;IAE1C,+CAA+C;IACxC,QAAQ,EAAE,OAAO,CAAS;IAEjC,sDAAsD;IACtD,OAAO,CAAC,iBAAiB,CAAyB;IAElD,oCAAoC;IACpC,OAAO,CAAC,mBAAmB,CAAwB;IAEnD,OAAO,CAAC,sBAAsB,CAA4B;IAC1D,OAAO,CAAC,gBAAgB,CAAoB;IAErC,WAAW,CAAC,CAAC,EAAE,MAAM,EAAE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