brick-engine-js 1.0.1 → 1.0.2

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 (225) hide show
  1. package/dist/docs/GAME_DEVELOPER_GUIDE.html +1 -1
  2. package/dist/docs/brick-engine-guide.html +1 -1
  3. package/dist/docs/getting-started.html +1 -1
  4. package/dist/docs/jsdoc_standard.html +1 -1
  5. package/dist/docs/publishing.html +1 -1
  6. package/dist/docs/reference/interfaces/modules/Session.html +1 -1
  7. package/dist/docs/reference/modules/GameMenu.html +1 -1
  8. package/dist/docs/reference/modules/GameSession.html +1 -1
  9. package/dist/docs/reference/modules/InitialStateSnapshot.html +1 -1
  10. package/dist/docs/reference/modules/SessionModal.html +1 -1
  11. package/dist/docs/testing_best_practices.html +1 -1
  12. package/package.json +7 -1
  13. package/.env.local.example +0 -2
  14. package/.github/workflows/publish.yml +0 -73
  15. package/.prettierignore +0 -2
  16. package/.prettierrc.json +0 -8
  17. package/eslint.config.mjs +0 -29
  18. package/public/CNAME +0 -1
  19. package/public/docs/GAME_DEVELOPER_GUIDE.html +0 -727
  20. package/public/docs/brick-engine-guide.html +0 -610
  21. package/public/docs/diagrams/lifecycle.mmd +0 -19
  22. package/public/docs/documentation_style_guide.html +0 -994
  23. package/public/docs/getting-started.html +0 -648
  24. package/public/docs/images/lifecycle.svg +0 -1
  25. package/public/docs/index.html +0 -593
  26. package/public/docs/jsdoc_standard.html +0 -656
  27. package/public/docs/publishing.html +0 -573
  28. package/public/docs/reference/enums/Color.html +0 -533
  29. package/public/docs/reference/enums/ControlEventType.html +0 -505
  30. package/public/docs/reference/enums/ControlKey.html +0 -529
  31. package/public/docs/reference/enums/FontAlignment.html +0 -545
  32. package/public/docs/reference/enums/FontSize.html +0 -517
  33. package/public/docs/reference/enums/Sound.html +0 -558
  34. package/public/docs/reference/enums/StateProperty.html +0 -525
  35. package/public/docs/reference/helpers/CellHelper.html +0 -520
  36. package/public/docs/reference/helpers/ControlInputHandlerHelper.html +0 -569
  37. package/public/docs/reference/helpers/CoordinateHelper.html +0 -703
  38. package/public/docs/reference/helpers/RelativeValuesHelper.html +0 -560
  39. package/public/docs/reference/interfaces/Debuggable.html +0 -501
  40. package/public/docs/reference/interfaces/GameModules.html +0 -544
  41. package/public/docs/reference/interfaces/Initializable.html +0 -495
  42. package/public/docs/reference/interfaces/RendererInitializable.html +0 -517
  43. package/public/docs/reference/interfaces/StateSyncable.html +0 -542
  44. package/public/docs/reference/interfaces/modules/Control.html +0 -648
  45. package/public/docs/reference/interfaces/modules/Grid.html +0 -1256
  46. package/public/docs/reference/interfaces/modules/Renderer.html +0 -522
  47. package/public/docs/reference/interfaces/modules/RendererComposite.html +0 -577
  48. package/public/docs/reference/interfaces/modules/Score.html +0 -669
  49. package/public/docs/reference/interfaces/modules/Session.html +0 -585
  50. package/public/docs/reference/interfaces/modules/State.html +0 -897
  51. package/public/docs/reference/interfaces/modules/Text.html +0 -668
  52. package/public/docs/reference/interfaces/modules/Time.html +0 -684
  53. package/public/docs/reference/modules/Debugger.html +0 -579
  54. package/public/docs/reference/modules/DisplayRenderer.html +0 -557
  55. package/public/docs/reference/modules/Game.html +0 -909
  56. package/public/docs/reference/modules/GameControl.html +0 -716
  57. package/public/docs/reference/modules/GameGrid.html +0 -1910
  58. package/public/docs/reference/modules/GameHudGrid.html +0 -508
  59. package/public/docs/reference/modules/GameMenu.html +0 -538
  60. package/public/docs/reference/modules/GameRenderer.html +0 -589
  61. package/public/docs/reference/modules/GameScore.html +0 -664
  62. package/public/docs/reference/modules/GameSession.html +0 -533
  63. package/public/docs/reference/modules/GameSound.html +0 -636
  64. package/public/docs/reference/modules/GameState.html +0 -922
  65. package/public/docs/reference/modules/GameText.html +0 -701
  66. package/public/docs/reference/modules/GameTime.html +0 -696
  67. package/public/docs/reference/modules/HudRenderer.html +0 -568
  68. package/public/docs/reference/modules/InitialStateSnapshot.html +0 -557
  69. package/public/docs/reference/modules/SessionModal.html +0 -520
  70. package/public/docs/reference/types/Axis.html +0 -505
  71. package/public/docs/reference/types/Cell.html +0 -514
  72. package/public/docs/reference/types/ControlCallback.html +0 -488
  73. package/public/docs/reference/types/Coordinate.html +0 -510
  74. package/public/docs/reference/types/GameEntry.html +0 -514
  75. package/public/docs/reference/types/GameEvent.html +0 -514
  76. package/public/docs/reference/types/Piece.html +0 -506
  77. package/public/docs/reference/types/RendererMetrics.html +0 -514
  78. package/public/docs/reference/types/Vector.html +0 -509
  79. package/public/docs/testing_best_practices.html +0 -770
  80. package/public/favicon.ico +0 -0
  81. package/public/fonts/digital-7.monoitalic.ttf +0 -0
  82. package/public/images/cell.svg +0 -32
  83. package/public/images/close.png +0 -0
  84. package/public/images/games.png +0 -0
  85. package/public/images/github.png +0 -0
  86. package/public/images/letter-a.png +0 -0
  87. package/public/images/letter-d.png +0 -0
  88. package/public/images/letter-j.png +0 -0
  89. package/public/images/letter-s.png +0 -0
  90. package/public/images/letter-w.png +0 -0
  91. package/public/images/meta-image.png +0 -0
  92. package/public/images/number-1.png +0 -0
  93. package/public/images/number-2.png +0 -0
  94. package/public/images/number-3.png +0 -0
  95. package/public/images/number-4.png +0 -0
  96. package/public/images/number-5.png +0 -0
  97. package/public/images/number-6.png +0 -0
  98. package/public/images/splash.gif +0 -0
  99. package/public/index.html +0 -15
  100. package/public/sounds/sound_00.wav +0 -0
  101. package/public/sounds/sound_01.wav +0 -0
  102. package/public/sounds/sound_02.wav +0 -0
  103. package/public/sounds/sound_03.wav +0 -0
  104. package/public/sounds/sound_04.wav +0 -0
  105. package/public/sounds/sound_05.wav +0 -0
  106. package/public/sounds/sound_06.wav +0 -0
  107. package/public/sounds/sound_07.wav +0 -0
  108. package/public/sounds/sound_08.wav +0 -0
  109. package/public/sounds/sound_09.wav +0 -0
  110. package/public/sounds/sound_10.wav +0 -0
  111. package/public/sounds/sound_11.wav +0 -0
  112. package/public/sounds/sound_12.wav +0 -0
  113. package/public/sounds/sound_13.wav +0 -0
  114. package/public/sounds/sound_14.wav +0 -0
  115. package/public/sounds/sound_15.wav +0 -0
  116. package/public/style/body.css +0 -86
  117. package/public/style/buttons.css +0 -233
  118. package/public/style/debugger.css +0 -117
  119. package/public/style/sessionModal.css +0 -155
  120. package/public/style/sourceCodeAndCommands.css +0 -74
  121. package/public/style/splash.css +0 -13
  122. package/public/style/theme.css +0 -137
  123. package/scripts/generate-diagrams.sh +0 -20
  124. package/scripts/generate-docs.js +0 -111
  125. package/src/client-game.d.ts +0 -1
  126. package/src/config/configs.test.ts +0 -20
  127. package/src/config/configs.ts +0 -197
  128. package/src/config/env.test.ts +0 -59
  129. package/src/config/env.ts +0 -7
  130. package/src/config/styles.ts +0 -5
  131. package/src/core/Game.test.ts +0 -167
  132. package/src/core/Game.ts +0 -307
  133. package/src/core/InitialStateSnapshot.test.ts +0 -51
  134. package/src/core/InitialStateSnapshot.ts +0 -46
  135. package/src/core/helpers/CellHelper.test.ts +0 -33
  136. package/src/core/helpers/CellHelper.ts +0 -21
  137. package/src/core/helpers/ControlInputHandlerHelper.test.ts +0 -116
  138. package/src/core/helpers/ControlInputHandlerHelper.ts +0 -68
  139. package/src/core/helpers/CoordinateHelper.test.ts +0 -113
  140. package/src/core/helpers/CoordinateHelper.ts +0 -82
  141. package/src/core/helpers/InterfaceIdentifierHelper.test.ts +0 -122
  142. package/src/core/helpers/InterfaceIdentifierHelper.ts +0 -43
  143. package/src/core/helpers/RelativeValuesHelper.test.ts +0 -47
  144. package/src/core/helpers/RelativeValuesHelper.ts +0 -29
  145. package/src/core/module/control/GameControl.test.ts +0 -82
  146. package/src/core/module/control/GameControl.ts +0 -142
  147. package/src/core/module/control/GameControlKeyBinding.test.ts +0 -59
  148. package/src/core/module/control/GameControlKeyBinding.ts +0 -92
  149. package/src/core/module/grid/GameGrid.test.ts +0 -83
  150. package/src/core/module/grid/GameGrid.ts +0 -610
  151. package/src/core/module/grid/GameHudGrid.test.ts +0 -22
  152. package/src/core/module/grid/GameHudGrid.ts +0 -40
  153. package/src/core/module/grid/engines/GridAnalysisEngine.test.ts +0 -157
  154. package/src/core/module/grid/engines/GridAnalysisEngine.ts +0 -124
  155. package/src/core/module/grid/engines/GridLineEngine.test.ts +0 -132
  156. package/src/core/module/grid/engines/GridLineEngine.ts +0 -165
  157. package/src/core/module/grid/engines/GridMovementEngine.test.ts +0 -125
  158. package/src/core/module/grid/engines/GridMovementEngine.ts +0 -113
  159. package/src/core/module/grid/engines/GridRegionEngine.test.ts +0 -136
  160. package/src/core/module/grid/engines/GridRegionEngine.ts +0 -52
  161. package/src/core/module/grid/engines/GridTransformEngine.test.ts +0 -98
  162. package/src/core/module/grid/engines/GridTransformEngine.ts +0 -70
  163. package/src/core/module/renderer/DisplayRenderer.test.ts +0 -86
  164. package/src/core/module/renderer/DisplayRenderer.ts +0 -152
  165. package/src/core/module/renderer/GameRenderer.test.ts +0 -103
  166. package/src/core/module/renderer/GameRenderer.ts +0 -144
  167. package/src/core/module/renderer/HudRenderer.test.ts +0 -108
  168. package/src/core/module/renderer/HudRenderer.ts +0 -203
  169. package/src/core/module/score/GameScore.test.ts +0 -71
  170. package/src/core/module/score/GameScore.ts +0 -188
  171. package/src/core/module/session/GameSession.test.ts +0 -176
  172. package/src/core/module/session/GameSession.ts +0 -103
  173. package/src/core/module/sound/GameSound.test.ts +0 -117
  174. package/src/core/module/sound/GameSound.ts +0 -229
  175. package/src/core/module/state/GameState.test.ts +0 -101
  176. package/src/core/module/state/GameState.ts +0 -339
  177. package/src/core/module/text/GameText.test.ts +0 -87
  178. package/src/core/module/text/GameText.ts +0 -150
  179. package/src/core/module/time/GameTime.test.ts +0 -86
  180. package/src/core/module/time/GameTime.ts +0 -144
  181. package/src/core/types/Interfaces.ts +0 -59
  182. package/src/core/types/Types.ts +0 -124
  183. package/src/core/types/enums.ts +0 -113
  184. package/src/core/types/modules.ts +0 -841
  185. package/src/index.test.ts +0 -15
  186. package/src/index.ts +0 -9
  187. package/src/main.test.ts +0 -137
  188. package/src/main.ts +0 -77
  189. package/src/menu/GameMenu.test.ts +0 -157
  190. package/src/menu/GameMenu.ts +0 -124
  191. package/src/menu/GameMenuSingleton.test.ts +0 -26
  192. package/src/menu/GameMenuSingleton.ts +0 -13
  193. package/src/menu/GameRepository.test.ts +0 -46
  194. package/src/menu/GameRepository.ts +0 -47
  195. package/src/menu/manager/GameManager.test.ts +0 -68
  196. package/src/menu/manager/GameManager.ts +0 -50
  197. package/src/types/global.d.ts +0 -8
  198. package/src/types/interfaces.ts +0 -5
  199. package/src/view/Debugger.test.ts +0 -152
  200. package/src/view/Debugger.ts +0 -124
  201. package/src/view/GameView.test.ts +0 -95
  202. package/src/view/GameView.ts +0 -244
  203. package/src/view/SessionModal.test.ts +0 -141
  204. package/src/view/SessionModal.ts +0 -73
  205. package/src/view/components/layout/ButtonLayout.test.ts +0 -28
  206. package/src/view/components/layout/ButtonLayout.ts +0 -63
  207. package/src/view/components/layout/ContainerLayout.test.ts +0 -48
  208. package/src/view/components/layout/ContainerLayout.ts +0 -50
  209. package/src/view/components/layout/FrameLayout.test.ts +0 -24
  210. package/src/view/components/layout/FrameLayout.ts +0 -25
  211. package/src/view/components/ui/BigButton.test.ts +0 -28
  212. package/src/view/components/ui/BigButton.ts +0 -31
  213. package/src/view/components/ui/Button.test.ts +0 -30
  214. package/src/view/components/ui/Button.ts +0 -30
  215. package/src/view/components/ui/Canvas.test.ts +0 -32
  216. package/src/view/components/ui/Canvas.ts +0 -34
  217. package/src/view/components/ui/SmallButton.test.ts +0 -48
  218. package/src/view/components/ui/SmallButton.ts +0 -32
  219. package/src/view/theme/applyColors.test.ts +0 -47
  220. package/src/view/theme/applyColors.ts +0 -38
  221. package/src/view/theme/dimensions.test.ts +0 -34
  222. package/src/view/theme/dimensions.ts +0 -53
  223. package/tsconfig.json +0 -16
  224. package/vitest.config.ts +0 -14
  225. package/webpack.config.js +0 -133
@@ -1,339 +0,0 @@
1
- import { Debuggable } from '../../types/Interfaces';
2
- import { State } from '../../types/modules';
3
- import { StateProperty } from '../../types/Types';
4
-
5
- type StateMetadata = {
6
- defaultValue: boolean | number;
7
- storageKey?: string;
8
- };
9
-
10
- // prettier-ignore
11
- const STATE_CONFIG: Record<StateProperty, StateMetadata> = {
12
- [StateProperty.ON] : { defaultValue: false },
13
- [StateProperty.START] : { defaultValue: false },
14
- [StateProperty.PLAYING] : { defaultValue: false },
15
- [StateProperty.GAME_OVER] : { defaultValue: false },
16
- [StateProperty.COLOR_ENABLED] : { defaultValue: true , storageKey: 'colorEnabled' },
17
- [StateProperty.MUTED] : { defaultValue: false , storageKey: 'muted' },
18
- };
19
-
20
- /**
21
- * Manages the core boolean states of the game and handles state-change events.
22
- *
23
- * Provides a central hub for tracking game lifecycle states (on, running, gameOver)
24
- * and user preferences (color enabled).
25
- *
26
- *
27
- * **Persistence Responsibility:**
28
- * This class is the SOLE responsible for persisting and loading generic, cross-session
29
- * player preferences (like mute settings and color toggle) from LocalStorage.
30
- * It does NOT handle transient session data (GameSession) or specific scoring logic (GameScore).
31
- */
32
- export default class GameState implements State, Debuggable {
33
- private _state: Map<StateProperty, boolean | number> = new Map();
34
-
35
- /** Map to store property names and their associated subscription callbacks. */
36
-
37
- private _subscribers: Map<StateProperty, Array<(value: boolean | number) => void>> = new Map();
38
-
39
- /**
40
- * Initializes the state module.
41
- * Sets default values for all state properties and loads persisted data.
42
- */
43
- setup(): void {
44
- // Initialize default values
45
- Object.values(StateProperty).forEach(property => {
46
- const config = STATE_CONFIG[property];
47
- this._state.set(property, config.defaultValue);
48
- });
49
-
50
- this._loadPersistentState();
51
- }
52
-
53
- /**
54
- * Loads persistent state from LocalStorage.
55
- */
56
- private _loadPersistentState(): void {
57
- Object.values(StateProperty).forEach(property => {
58
- const config = STATE_CONFIG[property];
59
- if (config.storageKey) {
60
- const storedValue = localStorage.getItem(config.storageKey);
61
- if (storedValue !== null) {
62
- this._state.set(property, JSON.parse(storedValue));
63
- }
64
- }
65
- });
66
- }
67
-
68
- /**
69
- * Sets a state property value, handling persistence and notification.
70
- *
71
- * @param {StateProperty} property - The property to set.
72
- * @param {boolean | number} value - The value to set.
73
- */
74
- private _set(property: StateProperty, value: boolean | number): void {
75
- const currentValue = this._state.get(property);
76
- if (currentValue !== value) {
77
- this._state.set(property, value);
78
-
79
- const config = STATE_CONFIG[property];
80
- if (config.storageKey) {
81
- localStorage.setItem(config.storageKey, JSON.stringify(value));
82
- }
83
-
84
- this._notify(property, value);
85
- }
86
- }
87
-
88
- /**
89
- * Notifies all subscribers of a property change.
90
- *
91
- * @param {StateProperty} property - The property that changed.
92
- * @param {boolean | number} value - The new value of the property.
93
- */
94
- private _notify(property: StateProperty, value: boolean | number): void {
95
- const callbacks = this._subscribers.get(property);
96
- if (callbacks) {
97
- callbacks.forEach(callback => callback(value));
98
- }
99
- }
100
-
101
- /**
102
- * Checks if the game machine is powered on.
103
- *
104
- * @returns {boolean} True if powered on.
105
- */
106
- isOn(): boolean {
107
- return this._state.get(StateProperty.ON) as boolean;
108
- }
109
-
110
- /**
111
- * Checks if the game machine is powered off.
112
- *
113
- * @returns {boolean} True if powered off.
114
- */
115
- isOff(): boolean {
116
- return !this.isOn();
117
- }
118
-
119
- /**
120
- * Checks if a game session has currently started.
121
- * This includes both playing and paused states.
122
- *
123
- * @returns {boolean} True if a game is active.
124
- */
125
- isStarted(): boolean {
126
- return this._state.get(StateProperty.START) as boolean;
127
- }
128
-
129
- /**
130
- * Checks if the game is currently running (not paused, not game over).
131
- *
132
- * @returns {boolean} True if gameplay is active.
133
- */
134
- isPlaying(): boolean {
135
- return this._state.get(StateProperty.PLAYING) as boolean;
136
- }
137
-
138
- /**
139
- * Checks if the game is paused.
140
- *
141
- * @returns {boolean} True if the game is started but not playing (and not game over).
142
- */
143
- isPaused(): boolean {
144
- return this.isStarted() && !this.isPlaying() && !this.isGameOver();
145
- }
146
-
147
- /**
148
- * Checks if the game is in a "Game Over" state.
149
- *
150
- * @returns {boolean} True if the game is over.
151
- */
152
- isGameOver(): boolean {
153
- return this._state.get(StateProperty.GAME_OVER) as boolean;
154
- }
155
-
156
- /**
157
- * Powers on the game machine.
158
- * Resets all session states (start, playing, game over).
159
- */
160
- turnOn(): void {
161
- this._set(StateProperty.ON, true);
162
- this._set(StateProperty.START, false);
163
- this._set(StateProperty.PLAYING, false);
164
- this._set(StateProperty.GAME_OVER, false);
165
- }
166
-
167
- /**
168
- * Powers off the game machine.
169
- * Resets all states.
170
- */
171
- turnOff(): void {
172
- this._set(StateProperty.ON, false);
173
- this._set(StateProperty.START, false);
174
- this._set(StateProperty.PLAYING, false);
175
- this._set(StateProperty.GAME_OVER, false);
176
- }
177
-
178
- /**
179
- * Starts a new game session.
180
- * Only works if the machine is powered on.
181
- */
182
- startGame(): void {
183
- if (!this.isOn()) return;
184
- this._set(StateProperty.START, true);
185
- this._set(StateProperty.PLAYING, true);
186
- this._set(StateProperty.GAME_OVER, false);
187
- }
188
-
189
- /**
190
- * Resets the game over state and starts the game again.
191
- */
192
- resetGameOver(): void {
193
- this._set(StateProperty.GAME_OVER, false);
194
- this._set(StateProperty.START, true);
195
- this._set(StateProperty.PLAYING, true);
196
- }
197
-
198
- /**
199
- * Exits the current game session, returning to the "On" state.
200
- */
201
- exitGame(): void {
202
- this._set(StateProperty.START, false);
203
- this._set(StateProperty.PLAYING, false);
204
- this._set(StateProperty.GAME_OVER, false);
205
- }
206
-
207
- /**
208
- * Pauses the current game.
209
- */
210
- pause(): void {
211
- if (this.isStarted() && !this.isGameOver()) {
212
- this._set(StateProperty.PLAYING, false);
213
- }
214
- }
215
-
216
- /**
217
- * Resumes the game from a paused state.
218
- */
219
- resume(): void {
220
- if (this.isStarted() && !this.isGameOver()) {
221
- this._set(StateProperty.PLAYING, true);
222
- }
223
- }
224
-
225
- /**
226
- * Triggers the "Game Over" state.
227
- * Stops gameplay.
228
- */
229
- triggerGameOver(): void {
230
- this._set(StateProperty.PLAYING, false);
231
- this._set(StateProperty.GAME_OVER, true);
232
- }
233
-
234
- /**
235
- * Resets the game to the initial state (restarts).
236
- */
237
- resetGame(): void {
238
- if (!this.isOn()) return;
239
- this._set(StateProperty.GAME_OVER, false);
240
- this._set(StateProperty.START, true);
241
- this._set(StateProperty.PLAYING, true);
242
- }
243
-
244
- /**
245
- * Checks if color mode is enabled.
246
- *
247
- * @returns {boolean} True if color is enabled.
248
- */
249
- isColorEnabled(): boolean {
250
- return this._state.get(StateProperty.COLOR_ENABLED) as boolean;
251
- }
252
-
253
- /**
254
- * Enables or disables color mode.
255
- *
256
- * @param {boolean} value - True to enable, false to disable.
257
- */
258
- setColorEnabled(value: boolean): void {
259
- this._set(StateProperty.COLOR_ENABLED, value);
260
- }
261
-
262
- /**
263
- * Checks if audio is muted.
264
- *
265
- * @returns {boolean} True if muted.
266
- */
267
- isMuted(): boolean {
268
- return this._state.get(StateProperty.MUTED) as boolean;
269
- }
270
-
271
- /**
272
- * Mutes or unmutes the audio.
273
- *
274
- * @param {boolean} value - True to mute, false to unmute.
275
- */
276
- setMuted(value: boolean): void {
277
- this._set(StateProperty.MUTED, value);
278
- }
279
-
280
- /**
281
- * Toggles the color enabled state.
282
- */
283
- toggleColorEnabled(): void {
284
- this.setColorEnabled(!this.isColorEnabled());
285
- }
286
-
287
- /**
288
- * Toggles the muted state.
289
- */
290
- toggleMuted(): void {
291
- this.setMuted(!this.isMuted());
292
- }
293
-
294
- /**
295
- * Subscribes to changes in specific state properties.
296
- *
297
- * @param {StateProperty} property - The state property to monitor.
298
- * @param {function(boolean | number): void} callback - The function to execute when the property changes.
299
- */
300
- subscribe(property: StateProperty, callback: (value: boolean | number) => void): void {
301
- if (!this._subscribers.has(property)) {
302
- this._subscribers.set(property, []);
303
- }
304
- this._subscribers.get(property)?.push(callback);
305
- }
306
-
307
- /**
308
- * Unsubscribes from changes in specific state properties.
309
- *
310
- * @param {StateProperty} property - The state property to monitor.
311
- * @param {function(boolean | number): void} callback - The function to remove.
312
- */
313
- unsubscribe(property: StateProperty, callback: (value: boolean | number) => void): void {
314
- const callbacks = this._subscribers.get(property);
315
- if (callbacks) {
316
- this._subscribers.set(
317
- property,
318
- callbacks.filter(cb => cb !== callback),
319
- );
320
- }
321
- }
322
-
323
- /**
324
- * Retrieves debug information about the game state.
325
- *
326
- * @returns {Record<string, string | number | boolean>} The debug data.
327
- */
328
- getDebugData(): Record<string, string | number | boolean> {
329
- return {
330
- on: this.isOn(),
331
- start: this.isStarted(),
332
- playing: this.isPlaying(),
333
- paused: this.isPaused(),
334
- game_over: this.isGameOver(),
335
- color_enabled: this.isColorEnabled(),
336
- muted: this.isMuted(),
337
- };
338
- }
339
- }
@@ -1,87 +0,0 @@
1
- import { describe, it, expect, vi, beforeEach } from 'vitest';
2
- import GameText from './GameText';
3
- import p5 from 'p5';
4
- import { FontSize, FontAlign, FontVerticalAlign } from '../../types/enums';
5
- import CoordinateHelper from '../../helpers/CoordinateHelper';
6
-
7
- // [ARRANGE] Mock CoordinateHelper
8
- vi.mock('../../helpers/CoordinateHelper', () => ({
9
- default: {
10
- getHudPosX: vi.fn().mockReturnValue(10),
11
- getHudPosY: vi.fn().mockReturnValue(20),
12
- getDisplayPosX: vi.fn().mockReturnValue(30),
13
- getDisplayPosY: vi.fn().mockReturnValue(40),
14
- },
15
- }));
16
-
17
- describe('GameText', () => {
18
- let gameText: GameText;
19
- let mockP5: p5;
20
-
21
- beforeEach(() => {
22
- mockP5 = {
23
- textFont: vi.fn(),
24
- fill: vi.fn(),
25
- textSize: vi.fn(),
26
- textAlign: vi.fn(),
27
- text: vi.fn(),
28
- width: 1000,
29
- height: 1000,
30
- } as unknown as p5;
31
-
32
- gameText = new GameText(mockP5);
33
- gameText.setRendererMetrics({
34
- display: { width: 600, height: 900, origin: { x: 0, y: 0 } },
35
- hud: { width: 400, height: 900, origin: { x: 600, y: 0 } },
36
- cell: { size: 50 },
37
- });
38
- });
39
-
40
- describe('setup', () => {
41
- it('should set font and calculate font sizes', () => {
42
- // [ACT]
43
- gameText.setup();
44
-
45
- // [ASSERT]
46
- expect(mockP5.textFont).toHaveBeenCalledWith('retro-gamming');
47
- // Check if some sizes were set (e.g. textSize(FontSize.MEDIUM))
48
- gameText.setTextSize(FontSize.MEDIUM);
49
- expect(mockP5.textSize).toHaveBeenCalled();
50
- });
51
- });
52
-
53
- describe('Rendering positions', () => {
54
- beforeEach(() => {
55
- gameText.setup();
56
- });
57
-
58
- it('should call CoordinateHelper.getHudPos and p5.text for HUD text', () => {
59
- // [ACT]
60
- gameText.textOnHud('Hello', { x: 0.5, y: 0.5 });
61
-
62
- // [ASSERT]
63
- expect(CoordinateHelper.getHudPosX).toHaveBeenCalled();
64
- expect(mockP5.text).toHaveBeenCalledWith('Hello', 10, 20);
65
- });
66
-
67
- it('should call CoordinateHelper.getDisplayPos and p5.text for Display text', () => {
68
- // [ACT]
69
- gameText.textOnDisplay('Score', { x: 0.1, y: 0.1 });
70
-
71
- // [ASSERT]
72
- expect(CoordinateHelper.getDisplayPosX).toHaveBeenCalled();
73
- expect(mockP5.text).toHaveBeenCalledWith('Score', 30, 40);
74
- });
75
- });
76
-
77
- describe('Alignment & Sizing', () => {
78
- it('should delegate to p5.textAlign and p5.textSize', () => {
79
- // [ACT]
80
- gameText.setTextAlign(FontAlign.CENTER, FontVerticalAlign.CENTER);
81
- gameText.setTextSize(FontSize.LARGE);
82
-
83
- // [ASSERT]
84
- expect(mockP5.textAlign).toHaveBeenCalledWith(FontAlign.CENTER, FontVerticalAlign.CENTER);
85
- });
86
- });
87
- });
@@ -1,150 +0,0 @@
1
- import p5 from 'p5';
2
- import configs from '../../../config/configs';
3
- import { FontSize, FontAlign, FontVerticalAlign } from '../../types/enums';
4
- import { Coordinate, RendererMetrics } from '../../types/Types';
5
- import { Debuggable } from '../../types/Interfaces';
6
- import CoordinateHelper from '../../helpers/CoordinateHelper';
7
- import RelativeValuesHelper from '../../helpers/RelativeValuesHelper';
8
- import { Text } from '../../types/modules';
9
-
10
- /**
11
- * Handles text rendering and font management within the game.
12
- *
13
- * This class orchestrates font initialization, sizing, alignment, and
14
- * provides methods to render text on specific areas (HUD or Display).
15
- */
16
- export default class GameText implements Text, Debuggable {
17
- /** The default font family used for game text. */
18
- protected defaultFontFamily: string = 'retro-gamming';
19
-
20
- /** Array containing pre-calculated font sizes in pixels. */
21
- private fontSizes: number[] = [0];
22
-
23
- /** Stores the current display metrics for relative positioning. */
24
- private _rendererMetrics: RendererMetrics;
25
-
26
- /** The p5 instance used for rendering. */
27
- private _p: p5;
28
-
29
- /**
30
- * Creates an instance of GameText.
31
- *
32
- * @param {p5} p - The p5 instance.
33
- */
34
- constructor(p: p5) {
35
- this._p = p;
36
- }
37
-
38
- // prettier-ignore
39
- /**
40
- * Setup the font and pre-calculates relative font sizes.
41
- *
42
- * Uses configuration values to define a set of pixel-perfect font sizes
43
- * based on current container width.
44
- */
45
- setup(): void {
46
- const { extraSmall, small, medium, large, extraLarge } = configs.screenLayout.fontSize;
47
-
48
- this._p.textFont(this.defaultFontFamily);
49
-
50
- // Define font sizes
51
- this.fontSizes = [];
52
-
53
- this.fontSizes[FontSize.EXTRA_SMALL] = RelativeValuesHelper.getRelativeWidth(this._p, extraSmall);
54
- this.fontSizes[FontSize.SMALL] = RelativeValuesHelper.getRelativeWidth(this._p, small);
55
- this.fontSizes[FontSize.MEDIUM] = RelativeValuesHelper.getRelativeWidth(this._p, medium);
56
- this.fontSizes[FontSize.LARGE] = RelativeValuesHelper.getRelativeWidth(this._p, large);
57
- this.fontSizes[FontSize.EXTRA_LARGE] = RelativeValuesHelper.getRelativeWidth(this._p, extraLarge);
58
- }
59
-
60
- /**
61
- * Sets the display metrics used for relative text positioning.
62
- *
63
- * @param {RendererMetrics} rendererMetrics - The current renderer dimensions and origin.
64
- */
65
- setRendererMetrics(rendererMetrics: RendererMetrics): void {
66
- this._rendererMetrics = rendererMetrics;
67
- }
68
-
69
- /**
70
- * Sets the fill color of the text based on its state.
71
- *
72
- * @param {boolean} state - Whether the text should use the active or inactive color.
73
- */
74
- protected setTextState(state: boolean): void {
75
- const { active, inactive } = configs.colors;
76
- this._p.fill(state ? active : inactive);
77
- }
78
-
79
- /**
80
- * Sets the text color to the active theme color.
81
- */
82
- setActiveText(): void {
83
- this.setTextState(true);
84
- }
85
-
86
- /**
87
- * Sets the text color to the inactive theme color.
88
- */
89
- setInactiveText(): void {
90
- this.setTextState(false);
91
- }
92
-
93
- /**
94
- * Sets the current font size from predefined values.
95
- *
96
- * @param {FontSize} fontSize - The enum value representing the desired size.
97
- */
98
- setTextSize(fontSize: FontSize): void {
99
- this._p.textSize(this.fontSizes[fontSize]);
100
- }
101
-
102
- /**
103
- * Configures horizontal and vertical text alignment.
104
- *
105
- * @param {FontAlign} fontAlign - Horizontal alignment value.
106
- * @param {FontVerticalAlign} fontVerticalAlign - Vertical alignment value.
107
- */
108
- setTextAlign(fontAlign: FontAlign, fontVerticalAlign: FontVerticalAlign): void {
109
- this._p.textAlign(fontAlign, fontVerticalAlign);
110
- }
111
-
112
- /**
113
- * Renders text on the HUD area using relative coordinates.
114
- *
115
- * @param {string} text - The string to be rendered.
116
- * @param {Coordinate} coordinate - Normalized coordinates (0 to 1) within the HUD area.
117
- */
118
- textOnHud(text: string, coordinate: Coordinate): void {
119
- const x = CoordinateHelper.getHudPosX(this._p, coordinate.x, this._rendererMetrics.display.width);
120
- const y = CoordinateHelper.getHudPosY(this._p, coordinate.y, this._rendererMetrics.display.height);
121
-
122
- this._p.text(text, x, y);
123
- }
124
-
125
- /**
126
- * Renders text on the main display area using relative coordinates.
127
- *
128
- * @param {string} text - The string to be rendered.
129
- * @param {Coordinate} coordinate - Normalized coordinates (0 to 1) within the display area.
130
- */
131
- textOnDisplay(text: string, coordinate: Coordinate): void {
132
- // Fixed argument order: (p, x, displayWidth)
133
- const x = CoordinateHelper.getDisplayPosX(this._p, coordinate.x, this._rendererMetrics.display.width);
134
- const y = CoordinateHelper.getDisplayPosY(this._p, coordinate.y, this._rendererMetrics.display.height);
135
-
136
- this._p.text(text, x, y);
137
- }
138
-
139
- /**
140
- * Retrieves debug information about the text system.
141
- *
142
- * @returns {Record<string, string | number | boolean>} The debug data.
143
- */
144
- getDebugData(): Record<string, string | number | boolean> {
145
- return {
146
- current_font_family: this.defaultFontFamily,
147
- font_sizes_count: this.fontSizes.length,
148
- };
149
- }
150
- }
@@ -1,86 +0,0 @@
1
- import { describe, it, expect, beforeEach } from 'vitest';
2
- import GameTime from './GameTime';
3
-
4
- describe('GameTime', () => {
5
- let gameTime: GameTime;
6
-
7
- beforeEach(() => {
8
- gameTime = new GameTime();
9
- gameTime.setup();
10
- gameTime.setTickInterval(100);
11
- gameTime.setMinTickInterval(10);
12
- });
13
-
14
- describe('update & shouldTick', () => {
15
- it('should accumulate time and trigger tick when interval is reached', () => {
16
- // [ACT]
17
- gameTime.update(50);
18
- const firstCheck = gameTime.shouldTick();
19
-
20
- gameTime.update(50);
21
- const secondCheck = gameTime.shouldTick();
22
-
23
- // [ASSERT]
24
- expect(firstCheck).toBe(false);
25
- expect(secondCheck).toBe(true);
26
- });
27
-
28
- it('should handle multiple ticks in one large update', () => {
29
- // [ACT]
30
- gameTime.update(250);
31
-
32
- const tick1 = gameTime.shouldTick(); // consumes 100, rem 150
33
- const tick2 = gameTime.shouldTick(); // consumes 100, rem 50
34
- const tick3 = gameTime.shouldTick();
35
-
36
- // [ASSERT]
37
- expect(tick1).toBe(true);
38
- expect(tick2).toBe(true);
39
- expect(tick3).toBe(false);
40
- });
41
- });
42
-
43
- describe('FPS & TPS calculation', () => {
44
- it('should calculate FPS correctly', () => {
45
- // [ACT]
46
- gameTime.update(16.666); // roughly 60fps
47
-
48
- // [ASSERT]
49
- const debug = gameTime.getDebugData();
50
- expect(debug.fps).toBe(60);
51
- });
52
-
53
- it('should update TPS every 1000ms', () => {
54
- // [ARRANGE] Trigger 5 ticks
55
- for (let i = 0; i < 5; i++) {
56
- gameTime.update(100);
57
- gameTime.shouldTick();
58
- }
59
-
60
- // [ACT]
61
- gameTime.update(500); // 5*100 + 500 = 1000ms elapsed
62
-
63
- // [ASSERT]
64
- const debug = gameTime.getDebugData();
65
- expect(debug.tps).toBe(5);
66
- });
67
- });
68
-
69
- describe('Interval management', () => {
70
- it('should decrement interval but stay above minimum', () => {
71
- // [ACT]
72
- gameTime.decrementTickInterval(200);
73
-
74
- // [ASSERT]
75
- expect(gameTime.tickInterval).toBe(10);
76
- });
77
-
78
- it('should increment interval', () => {
79
- // [ACT]
80
- gameTime.incrementTickInterval(50);
81
-
82
- // [ASSERT]
83
- expect(gameTime.tickInterval).toBe(150);
84
- });
85
- });
86
- });