@blorkfield/overlay-core 0.7.1 → 0.8.1

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.
package/dist/index.d.cts CHANGED
@@ -196,6 +196,31 @@ interface DynamicObject {
196
196
  angle: number;
197
197
  tags: string[];
198
198
  }
199
+ /**
200
+ * Extended object state for querying and manipulation.
201
+ * Includes velocity data not present in DynamicObject.
202
+ */
203
+ interface ObjectState {
204
+ id: string;
205
+ x: number;
206
+ y: number;
207
+ velocity: {
208
+ x: number;
209
+ y: number;
210
+ };
211
+ angle: number;
212
+ tags: string[];
213
+ }
214
+ /**
215
+ * Lifecycle events that can be subscribed to.
216
+ */
217
+ type LifecycleEvent = 'objectSpawned' | 'objectRemoved' | 'objectCollision';
218
+ /**
219
+ * Callback type for lifecycle events.
220
+ * - objectSpawned/objectRemoved: receives the affected object
221
+ * - objectCollision: receives both colliding objects
222
+ */
223
+ type LifecycleCallback<T extends LifecycleEvent> = T extends 'objectCollision' ? (a: ObjectState, b: ObjectState) => void : (object: ObjectState) => void;
199
224
  interface UpdateCallbackData {
200
225
  /** All dynamic objects (objects with 'falling' tag) */
201
226
  objects: DynamicObject[];
@@ -509,7 +534,6 @@ declare class OverlayScene {
509
534
  private objects;
510
535
  private boundaries;
511
536
  private updateCallbacks;
512
- private mouseX;
513
537
  private config;
514
538
  private animationFrameId;
515
539
  private mouse;
@@ -525,6 +549,8 @@ declare class OverlayScene {
525
549
  private floorSegmentPressure;
526
550
  private collapsedSegments;
527
551
  private backgroundManager;
552
+ private lifecycleCallbacks;
553
+ private followTargets;
528
554
  static createContainer(parent: HTMLElement, options?: ContainerOptions): {
529
555
  canvas: HTMLCanvasElement;
530
556
  bounds: Bounds;
@@ -544,6 +570,11 @@ declare class OverlayScene {
544
570
  * Draws transparency/frosted glass layer after physics objects.
545
571
  */
546
572
  private handleAfterRender;
573
+ /**
574
+ * Handler for Matter.js collision events.
575
+ * Emits objectCollision lifecycle events.
576
+ */
577
+ private handleCollisionStart;
547
578
  /** Get a display name for an obstacle (letter char or short ID) */
548
579
  private getObstacleDisplayName;
549
580
  /** Update pressure tracking - check which dynamic objects rest on static obstacles */
@@ -638,7 +669,111 @@ declare class OverlayScene {
638
669
  * Get all unique tags currently in use by objects in the scene.
639
670
  */
640
671
  getAllTags(): string[];
641
- setMousePosition(x: number, _y: number): void;
672
+ /**
673
+ * Set the mouse position for follow behavior.
674
+ * This overrides the browser mouse position for the 'follow' and 'follow-mouse' tags.
675
+ * @deprecated Use setFollowTarget('mouse', x, y) instead
676
+ */
677
+ setMousePosition(x: number, y: number): void;
678
+ /**
679
+ * Set a follow target position. Objects with 'follow-{key}' tag will
680
+ * automatically move toward this target each frame.
681
+ * @param key - The target key (e.g., 'absolute' for 'follow-absolute' tag)
682
+ * @param x - Target X position
683
+ * @param y - Target Y position
684
+ */
685
+ setFollowTarget(key: string, x: number, y: number): void;
686
+ /**
687
+ * Remove a follow target. Objects with the corresponding tag will stop following.
688
+ * @param key - The target key to remove
689
+ */
690
+ removeFollowTarget(key: string): void;
691
+ /**
692
+ * Get all registered follow target keys.
693
+ * @returns Array of follow target keys
694
+ */
695
+ getFollowTargetKeys(): string[];
696
+ /**
697
+ * Programmatically grab an object at the current mouse position.
698
+ * Uses the externally set mouse position (via setFollowTarget('mouse', x, y))
699
+ * or the native canvas mouse position if no external position is set.
700
+ * Only objects with the 'grabable' tag can be grabbed.
701
+ * @returns The ID of the grabbed object, or null if no grabable object at position
702
+ */
703
+ startGrab(): string | null;
704
+ /**
705
+ * Release any currently grabbed object.
706
+ */
707
+ endGrab(): void;
708
+ /**
709
+ * Get the ID of the currently grabbed object.
710
+ * @returns The ID of the grabbed object, or null if nothing is grabbed
711
+ */
712
+ getGrabbedObject(): string | null;
713
+ /**
714
+ * Apply a force to an object.
715
+ * @param objectId - The ID of the object
716
+ * @param force - The force vector to apply
717
+ */
718
+ applyForce(objectId: string, force: {
719
+ x: number;
720
+ y: number;
721
+ }): void;
722
+ /**
723
+ * Apply a force to all objects with a specific tag.
724
+ * @param tag - The tag to match
725
+ * @param force - The force vector to apply
726
+ */
727
+ applyForceToTag(tag: string, force: {
728
+ x: number;
729
+ y: number;
730
+ }): void;
731
+ /**
732
+ * Set the velocity of an object.
733
+ * @param objectId - The ID of the object
734
+ * @param velocity - The velocity vector to set
735
+ */
736
+ setVelocity(objectId: string, velocity: {
737
+ x: number;
738
+ y: number;
739
+ }): void;
740
+ /**
741
+ * Set the position of an object.
742
+ * @param objectId - The ID of the object
743
+ * @param position - The position to set
744
+ */
745
+ setPosition(objectId: string, position: {
746
+ x: number;
747
+ y: number;
748
+ }): void;
749
+ /**
750
+ * Get the current state of an object.
751
+ * @param id - The ID of the object
752
+ * @returns The object state, or null if not found
753
+ */
754
+ getObject(id: string): ObjectState | null;
755
+ /**
756
+ * Get the current state of all objects with a specific tag.
757
+ * @param tag - The tag to match
758
+ * @returns Array of object states
759
+ */
760
+ getObjectsByTag(tag: string): ObjectState[];
761
+ /**
762
+ * Subscribe to a lifecycle event.
763
+ * @param event - The event type to subscribe to
764
+ * @param callback - The callback to invoke when the event occurs
765
+ */
766
+ on<T extends LifecycleEvent>(event: T, callback: LifecycleCallback<T>): void;
767
+ /**
768
+ * Unsubscribe from a lifecycle event.
769
+ * @param event - The event type to unsubscribe from
770
+ * @param callback - The callback to remove
771
+ */
772
+ off<T extends LifecycleEvent>(event: T, callback: LifecycleCallback<T>): void;
773
+ /** Create ObjectState from an ObjectEntry */
774
+ private toObjectState;
775
+ /** Emit a lifecycle event to all registered callbacks */
776
+ private emitLifecycleEvent;
642
777
  /**
643
778
  * Get the current pressure (number of objects resting) on an obstacle.
644
779
  * @param obstacleId - The ID of the obstacle
@@ -967,4 +1102,4 @@ declare class BackgroundManager {
967
1102
  static clearCache(): void;
968
1103
  }
969
1104
 
970
- export { type BackgroundConfig, type BackgroundImageConfig, type BackgroundImageSizing, BackgroundManager, type BackgroundTransparencyConfig, type BaseEffectConfig, type Bounds, type BurstEffectConfig, type ClickToFallConfig, type ContainerOptions, type DespawnEffectConfig, type DynamicObject, type EffectConfig, type EffectObjectConfig, type EffectType, type FloorConfig, type FontInfo, type FontManifest, type GlyphData, type LoadedFont, type LogLevel, type ObjectConfig, OverlayScene, type OverlaySceneConfig, type PressureThresholdConfig, type RainEffectConfig, type ShadowConfig, type ShapeConfig, type ShapePreset, type StreamEffectConfig, type TTFTextObstacleConfig, type TextAlign, type TextBounds, type TextObstacleConfig, type TextObstacleResult, type UpdateCallback, type UpdateCallbackData, type WeightConfig, clearFontCache, getGlyphData, getKerning, getLogLevel, loadFont, logger, measureText, setLogLevel };
1105
+ export { type BackgroundConfig, type BackgroundImageConfig, type BackgroundImageSizing, BackgroundManager, type BackgroundTransparencyConfig, type BaseEffectConfig, type Bounds, type BurstEffectConfig, type ClickToFallConfig, type ContainerOptions, type DespawnEffectConfig, type DynamicObject, type EffectConfig, type EffectObjectConfig, type EffectType, type FloorConfig, type FontInfo, type FontManifest, type GlyphData, type LifecycleCallback, type LifecycleEvent, type LoadedFont, type LogLevel, type ObjectConfig, type ObjectState, OverlayScene, type OverlaySceneConfig, type PressureThresholdConfig, type RainEffectConfig, type ShadowConfig, type ShapeConfig, type ShapePreset, type StreamEffectConfig, type TTFTextObstacleConfig, type TextAlign, type TextBounds, type TextObstacleConfig, type TextObstacleResult, type UpdateCallback, type UpdateCallbackData, type WeightConfig, clearFontCache, getGlyphData, getKerning, getLogLevel, loadFont, logger, measureText, setLogLevel };
package/dist/index.d.ts CHANGED
@@ -196,6 +196,31 @@ interface DynamicObject {
196
196
  angle: number;
197
197
  tags: string[];
198
198
  }
199
+ /**
200
+ * Extended object state for querying and manipulation.
201
+ * Includes velocity data not present in DynamicObject.
202
+ */
203
+ interface ObjectState {
204
+ id: string;
205
+ x: number;
206
+ y: number;
207
+ velocity: {
208
+ x: number;
209
+ y: number;
210
+ };
211
+ angle: number;
212
+ tags: string[];
213
+ }
214
+ /**
215
+ * Lifecycle events that can be subscribed to.
216
+ */
217
+ type LifecycleEvent = 'objectSpawned' | 'objectRemoved' | 'objectCollision';
218
+ /**
219
+ * Callback type for lifecycle events.
220
+ * - objectSpawned/objectRemoved: receives the affected object
221
+ * - objectCollision: receives both colliding objects
222
+ */
223
+ type LifecycleCallback<T extends LifecycleEvent> = T extends 'objectCollision' ? (a: ObjectState, b: ObjectState) => void : (object: ObjectState) => void;
199
224
  interface UpdateCallbackData {
200
225
  /** All dynamic objects (objects with 'falling' tag) */
201
226
  objects: DynamicObject[];
@@ -509,7 +534,6 @@ declare class OverlayScene {
509
534
  private objects;
510
535
  private boundaries;
511
536
  private updateCallbacks;
512
- private mouseX;
513
537
  private config;
514
538
  private animationFrameId;
515
539
  private mouse;
@@ -525,6 +549,8 @@ declare class OverlayScene {
525
549
  private floorSegmentPressure;
526
550
  private collapsedSegments;
527
551
  private backgroundManager;
552
+ private lifecycleCallbacks;
553
+ private followTargets;
528
554
  static createContainer(parent: HTMLElement, options?: ContainerOptions): {
529
555
  canvas: HTMLCanvasElement;
530
556
  bounds: Bounds;
@@ -544,6 +570,11 @@ declare class OverlayScene {
544
570
  * Draws transparency/frosted glass layer after physics objects.
545
571
  */
546
572
  private handleAfterRender;
573
+ /**
574
+ * Handler for Matter.js collision events.
575
+ * Emits objectCollision lifecycle events.
576
+ */
577
+ private handleCollisionStart;
547
578
  /** Get a display name for an obstacle (letter char or short ID) */
548
579
  private getObstacleDisplayName;
549
580
  /** Update pressure tracking - check which dynamic objects rest on static obstacles */
@@ -638,7 +669,111 @@ declare class OverlayScene {
638
669
  * Get all unique tags currently in use by objects in the scene.
639
670
  */
640
671
  getAllTags(): string[];
641
- setMousePosition(x: number, _y: number): void;
672
+ /**
673
+ * Set the mouse position for follow behavior.
674
+ * This overrides the browser mouse position for the 'follow' and 'follow-mouse' tags.
675
+ * @deprecated Use setFollowTarget('mouse', x, y) instead
676
+ */
677
+ setMousePosition(x: number, y: number): void;
678
+ /**
679
+ * Set a follow target position. Objects with 'follow-{key}' tag will
680
+ * automatically move toward this target each frame.
681
+ * @param key - The target key (e.g., 'absolute' for 'follow-absolute' tag)
682
+ * @param x - Target X position
683
+ * @param y - Target Y position
684
+ */
685
+ setFollowTarget(key: string, x: number, y: number): void;
686
+ /**
687
+ * Remove a follow target. Objects with the corresponding tag will stop following.
688
+ * @param key - The target key to remove
689
+ */
690
+ removeFollowTarget(key: string): void;
691
+ /**
692
+ * Get all registered follow target keys.
693
+ * @returns Array of follow target keys
694
+ */
695
+ getFollowTargetKeys(): string[];
696
+ /**
697
+ * Programmatically grab an object at the current mouse position.
698
+ * Uses the externally set mouse position (via setFollowTarget('mouse', x, y))
699
+ * or the native canvas mouse position if no external position is set.
700
+ * Only objects with the 'grabable' tag can be grabbed.
701
+ * @returns The ID of the grabbed object, or null if no grabable object at position
702
+ */
703
+ startGrab(): string | null;
704
+ /**
705
+ * Release any currently grabbed object.
706
+ */
707
+ endGrab(): void;
708
+ /**
709
+ * Get the ID of the currently grabbed object.
710
+ * @returns The ID of the grabbed object, or null if nothing is grabbed
711
+ */
712
+ getGrabbedObject(): string | null;
713
+ /**
714
+ * Apply a force to an object.
715
+ * @param objectId - The ID of the object
716
+ * @param force - The force vector to apply
717
+ */
718
+ applyForce(objectId: string, force: {
719
+ x: number;
720
+ y: number;
721
+ }): void;
722
+ /**
723
+ * Apply a force to all objects with a specific tag.
724
+ * @param tag - The tag to match
725
+ * @param force - The force vector to apply
726
+ */
727
+ applyForceToTag(tag: string, force: {
728
+ x: number;
729
+ y: number;
730
+ }): void;
731
+ /**
732
+ * Set the velocity of an object.
733
+ * @param objectId - The ID of the object
734
+ * @param velocity - The velocity vector to set
735
+ */
736
+ setVelocity(objectId: string, velocity: {
737
+ x: number;
738
+ y: number;
739
+ }): void;
740
+ /**
741
+ * Set the position of an object.
742
+ * @param objectId - The ID of the object
743
+ * @param position - The position to set
744
+ */
745
+ setPosition(objectId: string, position: {
746
+ x: number;
747
+ y: number;
748
+ }): void;
749
+ /**
750
+ * Get the current state of an object.
751
+ * @param id - The ID of the object
752
+ * @returns The object state, or null if not found
753
+ */
754
+ getObject(id: string): ObjectState | null;
755
+ /**
756
+ * Get the current state of all objects with a specific tag.
757
+ * @param tag - The tag to match
758
+ * @returns Array of object states
759
+ */
760
+ getObjectsByTag(tag: string): ObjectState[];
761
+ /**
762
+ * Subscribe to a lifecycle event.
763
+ * @param event - The event type to subscribe to
764
+ * @param callback - The callback to invoke when the event occurs
765
+ */
766
+ on<T extends LifecycleEvent>(event: T, callback: LifecycleCallback<T>): void;
767
+ /**
768
+ * Unsubscribe from a lifecycle event.
769
+ * @param event - The event type to unsubscribe from
770
+ * @param callback - The callback to remove
771
+ */
772
+ off<T extends LifecycleEvent>(event: T, callback: LifecycleCallback<T>): void;
773
+ /** Create ObjectState from an ObjectEntry */
774
+ private toObjectState;
775
+ /** Emit a lifecycle event to all registered callbacks */
776
+ private emitLifecycleEvent;
642
777
  /**
643
778
  * Get the current pressure (number of objects resting) on an obstacle.
644
779
  * @param obstacleId - The ID of the obstacle
@@ -967,4 +1102,4 @@ declare class BackgroundManager {
967
1102
  static clearCache(): void;
968
1103
  }
969
1104
 
970
- export { type BackgroundConfig, type BackgroundImageConfig, type BackgroundImageSizing, BackgroundManager, type BackgroundTransparencyConfig, type BaseEffectConfig, type Bounds, type BurstEffectConfig, type ClickToFallConfig, type ContainerOptions, type DespawnEffectConfig, type DynamicObject, type EffectConfig, type EffectObjectConfig, type EffectType, type FloorConfig, type FontInfo, type FontManifest, type GlyphData, type LoadedFont, type LogLevel, type ObjectConfig, OverlayScene, type OverlaySceneConfig, type PressureThresholdConfig, type RainEffectConfig, type ShadowConfig, type ShapeConfig, type ShapePreset, type StreamEffectConfig, type TTFTextObstacleConfig, type TextAlign, type TextBounds, type TextObstacleConfig, type TextObstacleResult, type UpdateCallback, type UpdateCallbackData, type WeightConfig, clearFontCache, getGlyphData, getKerning, getLogLevel, loadFont, logger, measureText, setLogLevel };
1105
+ export { type BackgroundConfig, type BackgroundImageConfig, type BackgroundImageSizing, BackgroundManager, type BackgroundTransparencyConfig, type BaseEffectConfig, type Bounds, type BurstEffectConfig, type ClickToFallConfig, type ContainerOptions, type DespawnEffectConfig, type DynamicObject, type EffectConfig, type EffectObjectConfig, type EffectType, type FloorConfig, type FontInfo, type FontManifest, type GlyphData, type LifecycleCallback, type LifecycleEvent, type LoadedFont, type LogLevel, type ObjectConfig, type ObjectState, OverlayScene, type OverlaySceneConfig, type PressureThresholdConfig, type RainEffectConfig, type ShadowConfig, type ShapeConfig, type ShapePreset, type StreamEffectConfig, type TTFTextObstacleConfig, type TextAlign, type TextBounds, type TextObstacleConfig, type TextObstacleResult, type UpdateCallback, type UpdateCallbackData, type WeightConfig, clearFontCache, getGlyphData, getKerning, getLogLevel, loadFont, logger, measureText, setLogLevel };