@skewedaspect/sage 0.4.0 → 0.5.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.
Files changed (66) hide show
  1. package/dist/classes/bindings/toggle.d.ts +4 -4
  2. package/dist/classes/bindings/trigger.d.ts +4 -4
  3. package/dist/classes/bindings/value.d.ts +4 -4
  4. package/dist/classes/entity.d.ts +2 -2
  5. package/dist/classes/eventBus.d.ts +1 -1
  6. package/dist/classes/gameEngine.d.ts +8 -8
  7. package/dist/classes/input/gamepad.d.ts +1 -1
  8. package/dist/classes/input/keyboard.d.ts +1 -1
  9. package/dist/classes/input/mouse.d.ts +1 -1
  10. package/dist/classes/input/readers/gamepad.d.ts +1 -1
  11. package/dist/classes/input/readers/keyboard.d.ts +1 -1
  12. package/dist/classes/input/readers/mouse.d.ts +1 -1
  13. package/dist/classes/loggers/consoleBackend.d.ts +1 -1
  14. package/dist/classes/loggers/nullBackend.d.ts +1 -1
  15. package/dist/engines/scene.d.ts +2 -2
  16. package/dist/interfaces/binding.d.ts +4 -11
  17. package/dist/interfaces/entity.d.ts +5 -5
  18. package/dist/interfaces/game.d.ts +3 -3
  19. package/dist/managers/binding.d.ts +5 -5
  20. package/dist/managers/entity.d.ts +7 -7
  21. package/dist/managers/game.d.ts +5 -5
  22. package/dist/managers/input.d.ts +3 -3
  23. package/dist/managers/level.d.ts +1 -2
  24. package/dist/sage.d.ts +16 -14
  25. package/dist/sage.es.js +130 -72
  26. package/dist/sage.es.js.map +1 -1
  27. package/dist/sage.umd.js +1 -1
  28. package/dist/sage.umd.js.map +1 -1
  29. package/dist/utils/graphics.d.ts +1 -1
  30. package/dist/utils/logger.d.ts +3 -3
  31. package/package.json +11 -7
  32. package/src/classes/bindings/toggle.ts +0 -261
  33. package/src/classes/bindings/trigger.ts +0 -211
  34. package/src/classes/bindings/value.ts +0 -227
  35. package/src/classes/entity.ts +0 -256
  36. package/src/classes/eventBus.ts +0 -259
  37. package/src/classes/gameEngine.ts +0 -125
  38. package/src/classes/input/gamepad.ts +0 -388
  39. package/src/classes/input/keyboard.ts +0 -189
  40. package/src/classes/input/mouse.ts +0 -276
  41. package/src/classes/input/readers/gamepad.ts +0 -179
  42. package/src/classes/input/readers/keyboard.ts +0 -123
  43. package/src/classes/input/readers/mouse.ts +0 -133
  44. package/src/classes/loggers/consoleBackend.ts +0 -135
  45. package/src/classes/loggers/nullBackend.ts +0 -51
  46. package/src/engines/scene.ts +0 -112
  47. package/src/images/sage_logo.svg +0 -172
  48. package/src/images/sage_logo_shape.svg +0 -146
  49. package/src/interfaces/action.ts +0 -30
  50. package/src/interfaces/binding.ts +0 -191
  51. package/src/interfaces/entity.ts +0 -21
  52. package/src/interfaces/game.ts +0 -44
  53. package/src/interfaces/input.ts +0 -221
  54. package/src/interfaces/logger.ts +0 -118
  55. package/src/managers/binding.ts +0 -729
  56. package/src/managers/entity.ts +0 -252
  57. package/src/managers/game.ts +0 -111
  58. package/src/managers/input.ts +0 -233
  59. package/src/managers/level.ts +0 -261
  60. package/src/sage.ts +0 -122
  61. package/src/types/global.d.ts +0 -11
  62. package/src/utils/capabilities.ts +0 -16
  63. package/src/utils/graphics.ts +0 -148
  64. package/src/utils/logger.ts +0 -225
  65. package/src/utils/physics.ts +0 -16
  66. package/src/utils/version.ts +0 -11
@@ -1,259 +0,0 @@
1
- //----------------------------------------------------------------------------------------------------------------------
2
- // Event Bus
3
- //----------------------------------------------------------------------------------------------------------------------
4
-
5
- import type { LoggerInterface } from '../interfaces/logger.ts';
6
- import { LoggingUtility, SAGELogger } from '../utils/logger.ts';
7
-
8
- //----------------------------------------------------------------------------------------------------------------------
9
-
10
- /**
11
- * A generic game event type.
12
- *
13
- * @template P - The shape of the payload. Default is a generic object if omitted.
14
- */
15
- export interface GameEvent<P extends Record<string, any> = Record<string, any>>
16
- {
17
- /** The string identifier/category of the event (e.g. "input:keydown"). */
18
- type : string;
19
- /** The optional ID of whoever sent this event. */
20
- senderID ?: string;
21
- /** The optional ID of who should receive this event. */
22
- targetID ?: string;
23
- /** The payload, with user-defined shape. */
24
- payload ?: P;
25
- }
26
-
27
- /**
28
- * A callback function that receives a {@link GameEvent} with a given payload type.
29
- *
30
- * @template P - The shape of the payload in the event.
31
- */
32
- export type GameEventCallback<P extends Record<string, any> = Record<string, any>> =
33
- (event : GameEvent<P>) => void | Promise<void>;
34
-
35
- /**
36
- * Represents a subscription record internally within the EventBus.
37
- *
38
- * We store the callback as if it handles `GameEvent<any>`
39
- * because we can’t unify multiple different payload types in the same structure.
40
- */
41
- interface Subscription
42
- {
43
- /** Precompiled RegExp if this subscription is pattern-based. */
44
- pattern ?: RegExp;
45
- /** The callback, but stored to accept any payload type (due to the union nature). */
46
- callback : (event : GameEvent<any>) => void | Promise<void>;
47
- }
48
-
49
- /**
50
- * An unsubscribe function, returned by any subscribe method.
51
- */
52
- export type Unsubscribe = () => void;
53
-
54
- //----------------------------------------------------------------------------------------------------------------------
55
-
56
- /**
57
- * A high-performance event bus that supports exact or pattern-based subscriptions,
58
- * and allows subscribers to specify a more specific `payload` type for convenience.
59
- *
60
- * **Note**: This does NOT enforce that a given `type` always has a certain payload shape.
61
- * It's a best-effort approach that lets you avoid `as` casts in callbacks.
62
- */
63
- export class GameEventBus
64
- {
65
- /**
66
- * For exact event-type matches:
67
- * - Key = event type string (e.g. "input:keydown")
68
- * - Value = set of subscriptions for that exact type
69
- */
70
- private directMap = new Map<string, Set<Subscription>>();
71
-
72
- /**
73
- * For pattern-based (wildcard or RegExp) subscriptions.
74
- */
75
- private patternSubs = new Set<Subscription>();
76
-
77
- /**
78
- * Logger instance
79
- */
80
- private _log : LoggerInterface;
81
-
82
- /**
83
- * Creates a new GameEventBus instance
84
- *
85
- * @param logger - The logging utility to use
86
- */
87
- constructor(logger ?: LoggingUtility)
88
- {
89
- this._log = logger?.getLogger('EventBus') || new SAGELogger('EventBus');
90
- }
91
-
92
- /**
93
- * Subscribe with automatic detection:
94
- * - If `eventTypeOrPattern` is a `RegExp` or a string containing `*`, treat it as a pattern subscription.
95
- * - Otherwise, treat it as an exact subscription.
96
- *
97
- * @template P - The payload shape you want to expect in the callback.
98
- * @param {string | RegExp} eventTypeOrPattern - The exact event type or a wildcard/RegExp pattern.
99
- * @param {GameEventCallback<P>} callback - A callback expecting a `GameEvent<P>`.
100
- * @returns {Unsubscribe} function that, when called, removes this subscription.
101
- */
102
- public subscribe<P extends Record<string, any> = Record<string, any>>(
103
- eventTypeOrPattern : string | RegExp,
104
- callback : GameEventCallback<P>
105
- ) : Unsubscribe
106
- {
107
- this._log.trace(`Subscribe request for: ${ eventTypeOrPattern.toString() }`);
108
-
109
- // If it's explicitly a RegExp or includes '*', treat as pattern:
110
- if(
111
- eventTypeOrPattern instanceof RegExp
112
- || (typeof eventTypeOrPattern === 'string' && eventTypeOrPattern.includes('*'))
113
- )
114
- {
115
- return this.subscribePattern(eventTypeOrPattern, callback);
116
- }
117
- else
118
- {
119
- return this.subscribeExact(eventTypeOrPattern, callback);
120
- }
121
- }
122
-
123
- /**
124
- * Subscribes to an exact event type (e.g. "input:keydown").
125
- *
126
- * @template P - The payload shape you want to expect in the callback.
127
- * @param {string} eventType - The exact string to match.
128
- * @param {GameEventCallback<P>} callback - A callback expecting a `GameEvent<P>`.
129
- * @returns {Unsubscribe} function that unsubscribes this exact subscription.
130
- */
131
- public subscribeExact<P extends Record<string, any> = Record<string, any>>(
132
- eventType : string,
133
- callback : GameEventCallback<P>
134
- ) : Unsubscribe
135
- {
136
- this._log.debug(`Adding exact subscription for: ${ eventType }`);
137
-
138
- let subs = this.directMap.get(eventType);
139
- if(!subs)
140
- {
141
- subs = new Set();
142
- this.directMap.set(eventType, subs);
143
- }
144
-
145
- // Store as a Subscription that accepts `GameEvent<any>`
146
- const subscription : Subscription = {
147
- callback: (event) => callback(event as GameEvent<P>), // runtime "type-lie" cast
148
- };
149
- subs.add(subscription);
150
-
151
- return () =>
152
- {
153
- this._log.debug(`Removing exact subscription for: ${ eventType }`);
154
- subs.delete(subscription);
155
- if(subs.size === 0)
156
- {
157
- this.directMap.delete(eventType);
158
- }
159
- };
160
- }
161
-
162
- /**
163
- * Subscribes with either:
164
- * - a wildcard string (contains `*`) that gets converted to a RegExp
165
- * - a RegExp directly
166
- *
167
- * @template P - The payload shape you want to expect in the callback.
168
- * @param {string | RegExp} patternOrRegExp - The pattern to match (`"input:*"` or `/^input:/`).
169
- * @param {GameEventCallback<P>} callback - A callback expecting a `GameEvent<P>`.
170
- * @returns {Unsubscribe} function that unsubscribes this pattern-based subscription.
171
- */
172
- public subscribePattern<P extends Record<string, any> = Record<string, any>>(
173
- patternOrRegExp : string | RegExp,
174
- callback : GameEventCallback<P>
175
- ) : Unsubscribe
176
- {
177
- let regex : RegExp;
178
-
179
- if(typeof patternOrRegExp === 'string')
180
- {
181
- // MAIN BUG FIX:
182
- // We replace '*' with '.*' (not '\*' or '\\*').
183
- // The prior code was .replace('\\*', '.*'), which never matched 'input:*'
184
- const escaped = patternOrRegExp
185
- .replace(/[-/\\^$+?.()|[\]{}]/g, '\\$&') // escape special chars
186
- .replace(/\*/g, '.*'); // replace '*' with '.*'
187
-
188
- // e.g. "input:*" => "input:.*" => new RegExp("^input:.*$")
189
- regex = new RegExp(`^${ escaped }$`);
190
- this._log.debug(
191
- `Adding pattern subscription for string: ${ patternOrRegExp }, regex: ${ regex.toString() }`
192
- );
193
- }
194
- else
195
- {
196
- // If already a RegExp, use as-is
197
- regex = patternOrRegExp;
198
- this._log.debug(`Adding pattern subscription for regex: ${ regex.toString() }`);
199
- }
200
-
201
- const subscription : Subscription = {
202
- pattern: regex,
203
- callback: (event) => callback(event as GameEvent<P>),
204
- };
205
- this.patternSubs.add(subscription);
206
-
207
- return () =>
208
- {
209
- this._log.debug(`Removing pattern subscription: ${ regex.toString() }`);
210
- this.patternSubs.delete(subscription);
211
- };
212
- }
213
-
214
- /**
215
- * Publishes an event, invoking all matching callbacks.
216
- *
217
- * Callbacks are called asynchronously (microtask) via `Promise.resolve()`.
218
- *
219
- * @param {GameEvent<any>} event - The event object to publish.
220
- */
221
- public publish(event : GameEvent<any>) : void
222
- {
223
- this._log.trace(`Publishing event: ${ event.type }`, event);
224
-
225
- let callbackCount = 0;
226
-
227
- // 1) Exact match
228
- const directSubs = this.directMap.get(event.type);
229
- if(directSubs)
230
- {
231
- callbackCount += directSubs.size;
232
- for(const sub of directSubs)
233
- {
234
- Promise.resolve().then(() => sub.callback(event));
235
- }
236
- }
237
-
238
- // 2) Pattern subscriptions
239
- for(const sub of this.patternSubs)
240
- {
241
- if(sub.pattern && sub.pattern.test(event.type))
242
- {
243
- callbackCount++;
244
- Promise.resolve().then(() => sub.callback(event));
245
- }
246
- }
247
-
248
- if(callbackCount === 0)
249
- {
250
- this._log.debug(`No subscribers found for event: ${ event.type }`);
251
- }
252
- else
253
- {
254
- this._log.trace(`Event ${ event.type } dispatched to ${ callbackCount } subscribers`);
255
- }
256
- }
257
- }
258
-
259
- //----------------------------------------------------------------------------------------------------------------------
@@ -1,125 +0,0 @@
1
- //------------------------------------------------------------------------------------------------------------------
2
- // SkewedAspect Game Engine
3
- //------------------------------------------------------------------------------------------------------------------
4
-
5
- import { AbstractEngine, HavokPlugin } from '@babylonjs/core';
6
-
7
- // Interfaces
8
- import { GameCanvas } from '../interfaces/game.ts';
9
- import { LoggerInterface } from '../interfaces/logger.ts';
10
-
11
- // Engines
12
- import { SceneEngine } from '../engines/scene.ts';
13
-
14
- // Managers
15
- import { BindingManager } from '../managers/binding.ts';
16
- import { GameManager } from '../managers/game.ts';
17
- import { GameEntityManager } from '../managers/entity.ts';
18
- import { UserInputManager } from '../managers/input.ts';
19
-
20
- // Utils
21
- import { GameEventBus } from './eventBus.ts';
22
- import { LoggingUtility } from '../utils/logger.ts';
23
- import { VERSION } from '../utils/version.ts';
24
-
25
- //------------------------------------------------------------------------------------------------------------------
26
-
27
- /**
28
- * Interface representing the engines used in the game.
29
- */
30
- interface Engines
31
- {
32
- sceneEngine : SceneEngine;
33
- }
34
-
35
- /**
36
- * Interface representing the managers used in the game.
37
- */
38
- interface Managers
39
- {
40
- bindingManager : BindingManager;
41
- gameManager : GameManager;
42
- entityManager : GameEntityManager;
43
- inputManager : UserInputManager;
44
- }
45
-
46
- //----------------------------------------------------------------------------------------------------------------------
47
-
48
- /**
49
- * Class representing the SkewedAspect Game Engine.
50
- */
51
- export class SkewedAspectGameEngine
52
- {
53
- public canvas : GameCanvas;
54
- public renderEngine : AbstractEngine;
55
- public physics : HavokPlugin;
56
- public managers : Managers;
57
- public engines : Engines;
58
- public eventBus : GameEventBus;
59
- public logger : LoggingUtility;
60
-
61
- private _log : LoggerInterface;
62
-
63
- /**
64
- * Creates an instance of SkewedAspectGameEngine.
65
- * @param canvas - The game canvas.
66
- * @param renderEngine - The rendering engine.
67
- * @param physics - The physics engine.
68
- * @param eventBus - The event bus.
69
- * @param logger - The logging utility.
70
- * @param engines - The engines used in the game.
71
- * @param managers - The managers used in the game.
72
- */
73
- constructor(
74
- canvas : GameCanvas,
75
- renderEngine : AbstractEngine,
76
- physics : HavokPlugin,
77
- eventBus : GameEventBus,
78
- logger : LoggingUtility,
79
- engines : Engines,
80
- managers : Managers
81
- )
82
- {
83
- this.canvas = canvas;
84
- this.renderEngine = renderEngine;
85
- this.physics = physics;
86
- this.eventBus = eventBus;
87
- this.logger = logger;
88
- this.engines = engines;
89
- this.managers = managers;
90
-
91
- this._log = logger.getLogger('GameEngine');
92
- }
93
-
94
- //------------------------------------------------------------------------------------------------------------------
95
- // Public API
96
- //------------------------------------------------------------------------------------------------------------------
97
-
98
- /**
99
- * Starts the game engine.
100
- */
101
- async start() : Promise<void>
102
- {
103
- this._log.info(`Starting SkewedAspect Game Engine (Version: ${ VERSION })...`);
104
-
105
- // Start the game manager
106
- await this.managers.gameManager.start(this.canvas);
107
-
108
- this._log.info('Game engine started successfully');
109
- }
110
-
111
- /**
112
- * Stops the game engine.
113
- */
114
- async stop() : Promise<void>
115
- {
116
- this._log.info(`Stopping SkewedAspect Game Engine (Version: ${ VERSION })...`);
117
-
118
- // Stop the game manager
119
- await this.managers.gameManager.stop();
120
-
121
- this._log.info('Game engine stopped successfully');
122
- }
123
- }
124
-
125
- //------------------------------------------------------------------------------------------------------------------