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,113 +0,0 @@
1
- import { describe, it, expect, vi, beforeEach } from 'vitest';
2
- import p5 from 'p5';
3
- import CoordinateHelper from './CoordinateHelper';
4
- import RelativeValuesHelper from './RelativeValuesHelper';
5
-
6
- // [ARRANGE] Mock dependencies
7
- vi.mock('./RelativeValuesHelper');
8
- vi.mock('../../config/configs', () => ({
9
- default: {
10
- screenLayout: {
11
- display: {
12
- margin: 0.05,
13
- },
14
- },
15
- },
16
- }));
17
-
18
- describe('CoordinateHelper', () => {
19
- const mockP5 = {
20
- width: 1000,
21
- height: 2000,
22
- } as unknown as p5;
23
-
24
- beforeEach(() => {
25
- vi.clearAllMocks();
26
- });
27
-
28
- describe('getRelativeCoordinate', () => {
29
- it('should scale coordinates using RelativeValuesHelper', () => {
30
- // [ARRANGE]
31
- const coord = { x: 0.5, y: 0.5 };
32
- vi.mocked(RelativeValuesHelper.getRelativeWidth).mockReturnValue(500);
33
- vi.mocked(RelativeValuesHelper.getRelativeHeight).mockReturnValue(1000);
34
-
35
- // [ACT]
36
- const result = CoordinateHelper.getRelativeCoordinate(mockP5, coord);
37
-
38
- // [ASSERT]
39
- expect(result).toEqual({ x: 500, y: 1000 });
40
- expect(RelativeValuesHelper.getRelativeWidth).toHaveBeenCalledWith(mockP5, 0.5);
41
- expect(RelativeValuesHelper.getRelativeHeight).toHaveBeenCalledWith(mockP5, 0.5);
42
- });
43
- });
44
-
45
- describe('getDisplayPosX', () => {
46
- it('should calculate absolute X with margin', () => {
47
- // [ARRANGE]
48
- const x = 0.5;
49
- const displayWidth = 600;
50
- // margin is 0.05 in mock. RelativeWidth(0.05) -> 50
51
- vi.mocked(RelativeValuesHelper.getRelativeWidth).mockReturnValue(50);
52
-
53
- // [ACT]
54
- const result = CoordinateHelper.getDisplayPosX(mockP5, x, displayWidth);
55
-
56
- // [ASSERT]
57
- // result = 600 * 0.5 + 50 = 350
58
- expect(result).toBe(350);
59
- expect(RelativeValuesHelper.getRelativeWidth).toHaveBeenCalledWith(mockP5, 0.05);
60
- });
61
- });
62
-
63
- describe('getDisplayPosY', () => {
64
- it('should calculate absolute Y with margin', () => {
65
- // [ARRANGE]
66
- const y = 0.5;
67
- const displayHeight = 1000;
68
- vi.mocked(RelativeValuesHelper.getRelativeHeight).mockReturnValue(100);
69
-
70
- // [ACT]
71
- const result = CoordinateHelper.getDisplayPosY(mockP5, y, displayHeight);
72
-
73
- // [ASSERT]
74
- // result = 1000 * 0.5 + 100 = 600
75
- expect(result).toBe(600);
76
- expect(RelativeValuesHelper.getRelativeHeight).toHaveBeenCalledWith(mockP5, 0.05);
77
- });
78
- });
79
-
80
- describe('getHudPosX', () => {
81
- it('should calculate absolute X for HUD', () => {
82
- // [ARRANGE]
83
- const x = 0.5;
84
- const displayWidth = 600;
85
- // zero = displayWidth + margin*2 = 600 + 50*2 = 700
86
- // width = p.width - zero - margin = 1000 - 700 - 50 = 250
87
- // result = 250 * 0.5 + 700 = 125 + 700 = 825
88
- vi.mocked(RelativeValuesHelper.getRelativeWidth).mockReturnValue(50);
89
-
90
- // [ACT]
91
- const result = CoordinateHelper.getHudPosX(mockP5, x, displayWidth);
92
-
93
- // [ASSERT]
94
- expect(result).toBe(825);
95
- });
96
- });
97
-
98
- describe('getHudPosY', () => {
99
- it('should calculate absolute Y for HUD (same logic as display)', () => {
100
- // [ARRANGE]
101
- const y = 0.1;
102
- const displayHeight = 1000;
103
- vi.mocked(RelativeValuesHelper.getRelativeHeight).mockReturnValue(100);
104
-
105
- // [ACT]
106
- const result = CoordinateHelper.getHudPosY(mockP5, y, displayHeight);
107
-
108
- // [ASSERT]
109
- // 1000 * 0.1 + 100 = 200
110
- expect(result).toBe(200);
111
- });
112
- });
113
- });
@@ -1,82 +0,0 @@
1
- import p5 from 'p5';
2
- import { Coordinate } from '../types/Types';
3
- import configs from '../../config/configs';
4
- import RelativeValuesHelper from './RelativeValuesHelper';
5
-
6
- /**
7
- * Static utility class for coordinate transformations.
8
- * Handles conversions between relative (0-1) coordinates and absolute pixel positions
9
- * for both the main display and the HUD.
10
- */
11
- export default class CoordinateHelper {
12
- /**
13
- * Converts a simplified coordinate object to relative screen dimensions.
14
- *
15
- * @param {p5} p - The P5 instance.
16
- * @param {Coordinate} coordinate - The normal coordinate.
17
- * @returns {Coordinate} A new coordinate scaled to the canvas width/height.
18
- */
19
- static getRelativeCoordinate(p: p5, coordinate: Coordinate): Coordinate {
20
- return {
21
- x: RelativeValuesHelper.getRelativeWidth(p, coordinate.x),
22
- y: RelativeValuesHelper.getRelativeHeight(p, coordinate.y),
23
- };
24
- }
25
-
26
- /**
27
- * Calculates the absolute X pixel position for an element on the Main Display.
28
- *
29
- * @param {p5} p - The p5 instance.
30
- * @param {number} x - The relative or normalized X position.
31
- * @param {number} displayWidth - The calculated width of the display area.
32
- * @returns {number} The absolute X pixel coordinate.
33
- */
34
- static getDisplayPosX(p: p5, x: number, displayWidth: number): number {
35
- const { margin } = configs.screenLayout.display;
36
-
37
- return displayWidth * x + RelativeValuesHelper.getRelativeWidth(p, margin);
38
- }
39
-
40
- /**
41
- * Calculates the absolute Y pixel position for an element on the Main Display.
42
- *
43
- * @param {p5} p - The p5 instance.
44
- * @param {number} y - The relative or normalized Y position.
45
- * @param {number} displayHeight - The calculated height of the display area.
46
- * @returns {number} The absolute Y pixel coordinate.
47
- */
48
- static getDisplayPosY(p: p5, y: number, displayHeight: number): number {
49
- const { margin } = configs.screenLayout.display;
50
- return displayHeight * y + RelativeValuesHelper.getRelativeHeight(p, margin);
51
- }
52
-
53
- /**
54
- * Calculates the absolute X pixel position for an element on the HUD.
55
- * Use this to position elements in the sidebar.
56
- *
57
- * @param {p5} p - The p5 instance.
58
- * @param {number} x - The relative or normalized X position within the HUD.
59
- * @param {number} displayWidth - The width of the main display (used for offset calculation).
60
- * @returns {number} The absolute X pixel coordinate.
61
- */
62
- static getHudPosX(p: p5, x: number, displayWidth: number): number {
63
- const { margin } = configs.screenLayout.display;
64
- const zero = displayWidth + RelativeValuesHelper.getRelativeWidth(p, margin) * 2;
65
- const width = p.width - zero - RelativeValuesHelper.getRelativeWidth(p, margin);
66
- return width * x + zero;
67
- }
68
-
69
- /**
70
- * Calculates the absolute Y pixel position for an element on the HUD.
71
- *
72
- * @param {p5} p - The p5 instance.
73
- * @param {number} y - The relative or normalized Y position within the HUD.
74
- * @param {number} displayHeight - The height of the main display (used for scaling).
75
- * @returns {number} The absolute Y pixel coordinate.
76
- */
77
- static getHudPosY(p: p5, y: number, displayHeight: number): number {
78
- const { margin } = configs.screenLayout.display;
79
-
80
- return displayHeight * y + RelativeValuesHelper.getRelativeHeight(p, margin);
81
- }
82
- }
@@ -1,122 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import InterfaceIdentifierHelper from './InterfaceIdentifierHelper';
3
- import GameState from '../module/state/GameState';
4
-
5
- describe('InterfaceIdentifierHelper', () => {
6
- describe('isSerializable', () => {
7
- it('should return true when object has serialize, deserialize and serialId', () => {
8
- // [ARRANGE]
9
- const validModule = {
10
- serialize: () => '{}',
11
- deserialize: () => {},
12
- serialId: 'test-id',
13
- };
14
-
15
- // [ACT]
16
- const result = InterfaceIdentifierHelper.isSerializable(validModule);
17
-
18
- // [ASSERT]
19
- expect(result).toBe(true);
20
- });
21
-
22
- it('should return false when object is missing required methods or properties', () => {
23
- // [ARRANGE]
24
- const invalidModule = { serialize: () => '{}' };
25
-
26
- // [ACT]
27
- const result = InterfaceIdentifierHelper.isSerializable(invalidModule);
28
-
29
- // [ASSERT]
30
- expect(result).toBe(false);
31
- });
32
-
33
- it('should return false when object is null or undefined', () => {
34
- // [ACT] & [ASSERT]
35
- expect(InterfaceIdentifierHelper.isSerializable(null as unknown as object)).toBe(false);
36
- expect(InterfaceIdentifierHelper.isSerializable(undefined as unknown as object)).toBe(false);
37
- });
38
- });
39
-
40
- describe('isInitializable', () => {
41
- it('should return true when object has a setup function', () => {
42
- // [ARRANGE]
43
- const validModule = { setup: () => {} };
44
-
45
- // [ACT]
46
- const result = InterfaceIdentifierHelper.isInitializable(validModule);
47
-
48
- // [ASSERT]
49
- expect(result).toBe(true);
50
- });
51
-
52
- it('should return false when object lacks a setup function', () => {
53
- // [ARRANGE]
54
- const invalidModule = { otherMethod: () => {} };
55
-
56
- // [ACT]
57
- const result = InterfaceIdentifierHelper.isInitializable(invalidModule);
58
-
59
- // [ASSERT]
60
- expect(result).toBe(false);
61
- });
62
- });
63
-
64
- describe('isStateSyncable', () => {
65
- it('should return true when object has syncState and is NOT GameState', () => {
66
- // [ARRANGE]
67
- const validModule = { syncState: () => {} };
68
-
69
- // [ACT]
70
- const result = InterfaceIdentifierHelper.isStateSyncable(validModule);
71
-
72
- // [ASSERT]
73
- expect(result).toBe(true);
74
- });
75
-
76
- it('should return false when object is an instance of GameState', () => {
77
- // [ARRANGE]
78
- const gameStateInstance = new GameState();
79
-
80
- // [ACT]
81
- const result = InterfaceIdentifierHelper.isStateSyncable(gameStateInstance);
82
-
83
- // [ASSERT]
84
- expect(result).toBe(false);
85
- });
86
-
87
- it('should return false when object lacks syncState method', () => {
88
- // [ARRANGE]
89
- const invalidModule = { otherMethod: () => {} };
90
-
91
- // [ACT]
92
- const result = InterfaceIdentifierHelper.isStateSyncable(invalidModule);
93
-
94
- // [ASSERT]
95
- expect(result).toBe(false);
96
- });
97
- });
98
-
99
- describe('isDebuggable', () => {
100
- it('should return true when object has getDebugData function', () => {
101
- // [ARRANGE]
102
- const validModule = { getDebugData: () => ({}) };
103
-
104
- // [ACT]
105
- const result = InterfaceIdentifierHelper.isDebuggable(validModule);
106
-
107
- // [ASSERT]
108
- expect(result).toBe(true);
109
- });
110
-
111
- it('should return false when object lacks getDebugData function', () => {
112
- // [ARRANGE]
113
- const invalidModule = { otherMethod: () => {} };
114
-
115
- // [ACT]
116
- const result = InterfaceIdentifierHelper.isDebuggable(invalidModule);
117
-
118
- // [ASSERT]
119
- expect(result).toBe(false);
120
- });
121
- });
122
- });
@@ -1,43 +0,0 @@
1
- import GameState from '../module/state/GameState';
2
- import { Debuggable, Initializable, StateSyncable, Serializable } from '../types/Interfaces';
3
-
4
- export default class InterfaceIdentifierHelper {
5
- static isSerializable(module: object): module is Serializable {
6
- if (module === null || module === undefined) return false;
7
-
8
- const serializable = module as Serializable;
9
-
10
- const hasSerialize = typeof serializable.serialize === 'function';
11
- const hasDeserialize = typeof serializable.deserialize === 'function';
12
- const hasSerialId = typeof serializable.serialId === 'string';
13
-
14
- return hasSerialize && hasDeserialize && hasSerialId;
15
- }
16
-
17
- static isInitializable(module: object): module is Initializable {
18
- if (module === null || module === undefined) return false;
19
-
20
- const initializable = module as Initializable;
21
-
22
- return typeof initializable.setup === 'function';
23
- }
24
-
25
- static isStateSyncable(module: object): module is StateSyncable {
26
- if (module === null || module === undefined) return false;
27
-
28
- const stateSyncable = module as StateSyncable;
29
-
30
- const hasSyncState = typeof stateSyncable.syncState === 'function';
31
- const isGameState = stateSyncable instanceof GameState;
32
-
33
- return hasSyncState && !isGameState;
34
- }
35
-
36
- static isDebuggable(module: object): module is Debuggable {
37
- if (module === null || module === undefined) return false;
38
-
39
- const debuggable = module as Debuggable;
40
-
41
- return typeof debuggable.getDebugData === 'function';
42
- }
43
- }
@@ -1,47 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import p5 from 'p5';
3
- import RelativeValuesHelper from './RelativeValuesHelper';
4
-
5
- describe('RelativeValuesHelper', () => {
6
- // [ARRANGE]
7
- const mockP5 = {
8
- width: 1000,
9
- height: 2000,
10
- } as unknown as p5;
11
-
12
- describe('getRelativeWidth', () => {
13
- it('should return 500 when size is 0.5 and p5.width is 1000', () => {
14
- // [ACT]
15
- const result = RelativeValuesHelper.getRelativeWidth(mockP5, 0.5);
16
-
17
- // [ASSERT]
18
- expect(result).toBe(500);
19
- });
20
-
21
- it('should return 0 when size is 0', () => {
22
- // [ACT]
23
- const result = RelativeValuesHelper.getRelativeWidth(mockP5, 0);
24
-
25
- // [ASSERT]
26
- expect(result).toBe(0);
27
- });
28
- });
29
-
30
- describe('getRelativeHeight', () => {
31
- it('should return 1000 when size is 0.5 and p5.height is 2000', () => {
32
- // [ACT]
33
- const result = RelativeValuesHelper.getRelativeHeight(mockP5, 0.5);
34
-
35
- // [ASSERT]
36
- expect(result).toBe(1000);
37
- });
38
-
39
- it('should return 2000 when size is 1', () => {
40
- // [ACT]
41
- const result = RelativeValuesHelper.getRelativeHeight(mockP5, 1);
42
-
43
- // [ASSERT]
44
- expect(result).toBe(2000);
45
- });
46
- });
47
- });
@@ -1,29 +0,0 @@
1
- import p5 from 'p5';
2
-
3
- /**
4
- * Static utility for calculating pixel values relative to the canvas size.
5
- * Allows for responsive design by defining sizes as percentages (0.0 - 1.0).
6
- */
7
- export default class RelativeValuesHelper {
8
- /**
9
- * Calculates a pixel width based on a percentage of the canvas width.
10
- *
11
- * @param {p5} p - The P5 instance.
12
- * @param {number} size - The percentage (0.0 to 1.0).
13
- * @returns {number} The absolute width in pixels.
14
- */
15
- static getRelativeWidth(p: p5, size: number): number {
16
- return size * p.width;
17
- }
18
-
19
- /**
20
- * Calculates a pixel height based on a percentage of the canvas height.
21
- *
22
- * @param {p5} p - The P5 instance.
23
- * @param {number} size - The percentage (0.0 to 1.0).
24
- * @returns {number} The absolute height in pixels.
25
- */
26
- static getRelativeHeight(p: p5, size: number): number {
27
- return size * p.height;
28
- }
29
- }
@@ -1,82 +0,0 @@
1
- import { describe, it, expect, vi, beforeEach } from 'vitest';
2
- import GameControl from './GameControl';
3
- import { ControlKey, ControlEventType } from '../../types/enums';
4
- import { GameModules } from '../../types/Types';
5
- import { State } from '../../types/modules';
6
-
7
- describe('GameControl', () => {
8
- let control: GameControl;
9
- let mockModules: GameModules;
10
-
11
- beforeEach(() => {
12
- control = new GameControl();
13
- mockModules = {
14
- state: {
15
- isOn: vi.fn().mockReturnValue(true),
16
- isOff: vi.fn().mockReturnValue(false),
17
- isStarted: vi.fn().mockReturnValue(false),
18
- isPlaying: vi.fn().mockReturnValue(false),
19
- isPaused: vi.fn().mockReturnValue(false),
20
- isGameOver: vi.fn().mockReturnValue(false),
21
- } as unknown as State,
22
- } as GameModules;
23
- control.setModules(mockModules);
24
- });
25
-
26
- describe('notify', () => {
27
- it('should throw error if modules are not set', () => {
28
- // [ARRANGE]
29
- const freshControl = new GameControl();
30
-
31
- // [ACT & ASSERT]
32
- expect(() => freshControl.notify(ControlKey.UP, ControlEventType.PRESSED)).toThrow('Modules not initialized');
33
- });
34
-
35
- it('should notify all subscribers of a key event', () => {
36
- // [ARRANGE]
37
- const callback = vi.fn();
38
- control.subscribe(ControlKey.UP, ControlEventType.PRESSED, callback);
39
-
40
- // [ACT]
41
- control.notify(ControlKey.UP, ControlEventType.PRESSED);
42
-
43
- // [ASSERT]
44
- expect(callback).toHaveBeenCalledWith(
45
- expect.objectContaining({
46
- key: ControlKey.UP,
47
- type: ControlEventType.PRESSED,
48
- modules: mockModules,
49
- }),
50
- );
51
- });
52
-
53
- it('should not notify unsubscribed callbacks', () => {
54
- // [ARRANGE]
55
- const callback = vi.fn();
56
- control.subscribe(ControlKey.UP, ControlEventType.PRESSED, callback);
57
- control.unsubscribe(ControlKey.UP, ControlEventType.PRESSED, callback);
58
-
59
- // [ACT]
60
- control.notify(ControlKey.UP, ControlEventType.PRESSED);
61
-
62
- // [ASSERT]
63
- expect(callback).not.toHaveBeenCalled();
64
- });
65
- });
66
-
67
- describe('getDebugData', () => {
68
- it('should return subscriber counts', () => {
69
- // [ARRANGE]
70
- control.subscribe(ControlKey.UP, ControlEventType.PRESSED, vi.fn());
71
- control.subscribe(ControlKey.DOWN, ControlEventType.PRESSED, vi.fn());
72
- control.subscribe(ControlKey.DOWN, ControlEventType.HELD, vi.fn());
73
-
74
- // [ACT]
75
- const debug = control.getDebugData();
76
-
77
- // [ASSERT]
78
- expect(debug.total_subscribers).toBe(3);
79
- expect(debug.tracked_keys).toBe(2); // UP and DOWN
80
- });
81
- });
82
- });
@@ -1,142 +0,0 @@
1
- import { ControlKey } from '../../types/enums';
2
- import { Control } from '../../types/modules';
3
- import { Debuggable } from '../../types/Interfaces';
4
- import { ControlCallback, ControlEventType, GameEvent, GameModules } from '../../types/Types';
5
- import GameControlKeyBinding from './GameControlKeyBinding';
6
-
7
- /**
8
- * Manages game control inputs and event dispatching.
9
- * Acts as the central hub for handling user input and notifying subscribers.
10
- */
11
- export default class GameControl implements Control, Debuggable {
12
- private _modules: GameModules;
13
-
14
- private _keyBinding: GameControlKeyBinding;
15
- private _subscribers: Map<ControlKey, Map<ControlEventType, Set<ControlCallback>>> = new Map();
16
-
17
- /**
18
- * Initializes the control system.
19
- * Sets up key bindings and prepares the input handler.
20
- */
21
- setup() {
22
- this._keyBinding = new GameControlKeyBinding(this);
23
-
24
- this._keyBinding.bindControls();
25
- }
26
-
27
- /**
28
- * Unbinds all control event listeners.
29
- */
30
- unbindControls() {
31
- this._keyBinding.unbindControls();
32
- this._subscribers.clear();
33
- }
34
-
35
- /**
36
- * Binds all control event listeners.
37
- */
38
- bindControls() {
39
- this._keyBinding.bindControls();
40
- }
41
-
42
- /**
43
- * Injects the game modules for context-aware events.
44
- *
45
- * @param {GameModules} modules - The collected game modules.
46
- */
47
- setModules(modules: GameModules): void {
48
- this._modules = modules;
49
- }
50
-
51
- /**
52
- * Subscribes a callback to a specific control event.
53
- *
54
- * @param {ControlKey} key - The key to listen for.
55
- * @param {ControlEventType} type - The event type (pressed/held).
56
- * @param {ControlCallback} callback - The function to execute.
57
- */
58
- subscribe(key: ControlKey, type: ControlEventType, callback: ControlCallback): void {
59
- if (!this._subscribers.has(key)) {
60
- this._subscribers.set(key, new Map());
61
- }
62
-
63
- const keySubscribers = this._subscribers.get(key)!;
64
- if (!keySubscribers.has(type)) {
65
- keySubscribers.set(type, new Set());
66
- }
67
-
68
- keySubscribers.get(type)!.add(callback);
69
- }
70
-
71
- /**
72
- * Unsubscribes a callback from a control event.
73
- *
74
- * @param {ControlKey} key - The key to stop listening for.
75
- * @param {ControlEventType} type - The event type.
76
- * @param {ControlCallback} callback - The function to remove.
77
- */
78
- unsubscribe(key: ControlKey, type: ControlEventType, callback: ControlCallback): void {
79
- const keySubscribers = this._subscribers.get(key);
80
- if (keySubscribers) {
81
- const callbacks = keySubscribers.get(type);
82
- if (callbacks) {
83
- callbacks.delete(callback);
84
- }
85
- }
86
- }
87
-
88
- /**
89
- * Notifies subscribers of a control event.
90
- *
91
- * @param {ControlKey} key - The key triggering the event.
92
- * @param {ControlEventType} type - The type of event.
93
- * @throws {Error} If modules have not been initialized.
94
- */
95
- notify(key: ControlKey, type: ControlEventType): void {
96
- if (!this._modules) {
97
- throw new Error('Modules not initialized');
98
- }
99
-
100
- const event: GameEvent = {
101
- key,
102
- type,
103
- modules: this._modules,
104
- };
105
-
106
- const state = this._modules.state;
107
-
108
- let isAllowed = false;
109
-
110
- if (state.isOff()) {
111
- isAllowed = key === ControlKey.POWER;
112
- } else if (!state.isStarted() || state.isPlaying() || state.isGameOver()) {
113
- isAllowed = true;
114
- } else if (state.isPaused()) {
115
- isAllowed = key.endsWith(';system');
116
- }
117
-
118
- const callbacks = this._subscribers.get(key)?.get(type);
119
- if (callbacks && isAllowed) {
120
- callbacks.forEach(callback => callback(event));
121
- }
122
- }
123
-
124
- /**
125
- * Retrieves debug information about the control system.
126
- *
127
- * @returns {Record<string, string | number | boolean>} The debug data.
128
- */
129
- getDebugData(): Record<string, string | number | boolean> {
130
- let totalSubscribers = 0;
131
- this._subscribers.forEach(keyMap => {
132
- keyMap.forEach(typeSet => {
133
- totalSubscribers += typeSet.size;
134
- });
135
- });
136
-
137
- return {
138
- total_subscribers: totalSubscribers,
139
- tracked_keys: this._subscribers.size,
140
- };
141
- }
142
- }