@rpgjs/common 4.3.0 → 5.0.0-alpha.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 (148) hide show
  1. package/dist/Physic.d.ts +619 -0
  2. package/dist/Player.d.ts +198 -0
  3. package/{lib → dist}/Utils.d.ts +19 -2
  4. package/dist/database/Item.d.ts +10 -0
  5. package/dist/database/index.d.ts +1 -0
  6. package/dist/index.d.ts +9 -0
  7. package/dist/index.js +16741 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/modules.d.ts +92 -0
  10. package/dist/movement/MovementManager.d.ts +84 -0
  11. package/dist/movement/MovementStrategy.d.ts +39 -0
  12. package/dist/movement/index.d.ts +12 -0
  13. package/dist/movement/strategies/CompositeMovement.d.ts +76 -0
  14. package/dist/movement/strategies/Dash.d.ts +52 -0
  15. package/dist/movement/strategies/IceMovement.d.ts +87 -0
  16. package/dist/movement/strategies/Knockback.d.ts +50 -0
  17. package/dist/movement/strategies/LinearMove.d.ts +43 -0
  18. package/dist/movement/strategies/LinearRepulsion.d.ts +55 -0
  19. package/dist/movement/strategies/Oscillate.d.ts +60 -0
  20. package/dist/movement/strategies/PathFollow.d.ts +78 -0
  21. package/dist/movement/strategies/ProjectileMovement.d.ts +138 -0
  22. package/dist/movement/strategies/SeekAvoid.d.ts +27 -0
  23. package/dist/rooms/Map.d.ts +109 -0
  24. package/dist/services/updateMap.d.ts +7 -0
  25. package/package.json +17 -17
  26. package/src/Physic.ts +1644 -0
  27. package/src/Player.ts +262 -26
  28. package/src/{gui/PrebuiltGui.ts → PrebuiltGui.ts} +1 -1
  29. package/src/Utils.ts +184 -123
  30. package/src/database/Item.ts +19 -0
  31. package/src/database/index.ts +1 -0
  32. package/src/index.ts +9 -25
  33. package/src/modules.ts +230 -0
  34. package/src/movement/MovementManager.ts +142 -0
  35. package/src/movement/MovementStrategy.ts +42 -0
  36. package/src/movement/index.ts +15 -0
  37. package/src/movement/strategies/CompositeMovement.ts +173 -0
  38. package/src/movement/strategies/Dash.ts +82 -0
  39. package/src/movement/strategies/IceMovement.ts +158 -0
  40. package/src/movement/strategies/Knockback.ts +81 -0
  41. package/src/movement/strategies/LinearMove.ts +58 -0
  42. package/src/movement/strategies/LinearRepulsion.ts +128 -0
  43. package/src/movement/strategies/Oscillate.ts +144 -0
  44. package/src/movement/strategies/PathFollow.ts +156 -0
  45. package/src/movement/strategies/ProjectileMovement.ts +322 -0
  46. package/src/movement/strategies/SeekAvoid.ts +123 -0
  47. package/src/rooms/Map.ts +272 -0
  48. package/src/services/updateMap.ts +9 -0
  49. package/tests/physic.spec.ts +454 -0
  50. package/tsconfig.json +8 -3
  51. package/vite.config.ts +21 -0
  52. package/CHANGELOG.md +0 -160
  53. package/LICENSE +0 -19
  54. package/browser/manifest.json +0 -7
  55. package/browser/rpg.common.js +0 -11357
  56. package/browser/rpg.common.umd.cjs +0 -11358
  57. package/lib/AbstractObject.d.ts +0 -322
  58. package/lib/AbstractObject.js +0 -872
  59. package/lib/AbstractObject.js.map +0 -1
  60. package/lib/Color.d.ts +0 -1
  61. package/lib/Color.js +0 -25
  62. package/lib/Color.js.map +0 -1
  63. package/lib/DefaultInput.d.ts +0 -2
  64. package/lib/DefaultInput.js +0 -26
  65. package/lib/DefaultInput.js.map +0 -1
  66. package/lib/Event.d.ts +0 -3
  67. package/lib/Event.js +0 -4
  68. package/lib/Event.js.map +0 -1
  69. package/lib/EventEmitter.d.ts +0 -10
  70. package/lib/EventEmitter.js +0 -61
  71. package/lib/EventEmitter.js.map +0 -1
  72. package/lib/Game.d.ts +0 -28
  73. package/lib/Game.js +0 -127
  74. package/lib/Game.js.map +0 -1
  75. package/lib/Hit.d.ts +0 -16
  76. package/lib/Hit.js +0 -65
  77. package/lib/Hit.js.map +0 -1
  78. package/lib/Inject.d.ts +0 -9
  79. package/lib/Inject.js +0 -17
  80. package/lib/Inject.js.map +0 -1
  81. package/lib/Logger.d.ts +0 -2
  82. package/lib/Logger.js +0 -7
  83. package/lib/Logger.js.map +0 -1
  84. package/lib/Map.d.ts +0 -174
  85. package/lib/Map.js +0 -263
  86. package/lib/Map.js.map +0 -1
  87. package/lib/Module.d.ts +0 -16
  88. package/lib/Module.js +0 -139
  89. package/lib/Module.js.map +0 -1
  90. package/lib/Player.d.ts +0 -26
  91. package/lib/Player.js +0 -19
  92. package/lib/Player.js.map +0 -1
  93. package/lib/Plugin.d.ts +0 -67
  94. package/lib/Plugin.js +0 -92
  95. package/lib/Plugin.js.map +0 -1
  96. package/lib/Scheduler.d.ts +0 -26
  97. package/lib/Scheduler.js +0 -90
  98. package/lib/Scheduler.js.map +0 -1
  99. package/lib/Shape.d.ts +0 -127
  100. package/lib/Shape.js +0 -261
  101. package/lib/Shape.js.map +0 -1
  102. package/lib/Utils.js +0 -181
  103. package/lib/Utils.js.map +0 -1
  104. package/lib/Vector2d.d.ts +0 -20
  105. package/lib/Vector2d.js +0 -63
  106. package/lib/Vector2d.js.map +0 -1
  107. package/lib/VirtualGrid.d.ts +0 -26
  108. package/lib/VirtualGrid.js +0 -68
  109. package/lib/VirtualGrid.js.map +0 -1
  110. package/lib/Worker.d.ts +0 -7
  111. package/lib/Worker.js +0 -13
  112. package/lib/Worker.js.map +0 -1
  113. package/lib/WorldMaps.d.ts +0 -105
  114. package/lib/WorldMaps.js +0 -184
  115. package/lib/WorldMaps.js.map +0 -1
  116. package/lib/gui/PrebuiltGui.js +0 -29
  117. package/lib/gui/PrebuiltGui.js.map +0 -1
  118. package/lib/index.d.ts +0 -25
  119. package/lib/index.js +0 -26
  120. package/lib/index.js.map +0 -1
  121. package/lib/transports/io.d.ts +0 -22
  122. package/lib/transports/io.js +0 -82
  123. package/lib/transports/io.js.map +0 -1
  124. package/lib/workers/move.d.ts +0 -1
  125. package/lib/workers/move.js +0 -57
  126. package/lib/workers/move.js.map +0 -1
  127. package/rpg.toml +0 -11
  128. package/src/AbstractObject.ts +0 -973
  129. package/src/Color.ts +0 -29
  130. package/src/DefaultInput.ts +0 -26
  131. package/src/Event.ts +0 -3
  132. package/src/EventEmitter.ts +0 -65
  133. package/src/Game.ts +0 -159
  134. package/src/Hit.ts +0 -70
  135. package/src/Inject.ts +0 -22
  136. package/src/Logger.ts +0 -7
  137. package/src/Map.ts +0 -335
  138. package/src/Module.ts +0 -144
  139. package/src/Plugin.ts +0 -100
  140. package/src/Scheduler.ts +0 -95
  141. package/src/Shape.ts +0 -302
  142. package/src/Vector2d.ts +0 -70
  143. package/src/VirtualGrid.ts +0 -78
  144. package/src/Worker.ts +0 -17
  145. package/src/WorldMaps.ts +0 -204
  146. package/src/transports/io.ts +0 -91
  147. package/src/workers/move.ts +0 -61
  148. /package/{lib/gui → dist}/PrebuiltGui.d.ts +0 -0
package/src/Player.ts CHANGED
@@ -1,30 +1,266 @@
1
- import { PendingMove } from '@rpgjs/types'
2
- import { AbstractObject } from './AbstractObject'
3
-
4
- export const LiteralDirection = {
5
- 1: 'up',
6
- 2: 'right',
7
- 3: 'down',
8
- 4: 'left'
1
+ import { signal } from "@signe/reactive";
2
+ import { id, sync, users } from "@signe/sync";
3
+ import * as Matter from 'matter-js';
4
+ import { MovementManager } from "./movement";
5
+ import { Item } from "./database";
6
+ import { Observable } from "rxjs";
7
+
8
+ export enum Direction {
9
+ Up = 'up',
10
+ Down = 'down',
11
+ Left = 'left',
12
+ Right = 'right'
13
+ }
14
+
15
+ export enum Animation {
16
+ Stand = 'stand',
17
+ Walk = 'walk',
18
+ Attack = 'attack',
19
+ Defense = 'defense',
20
+ Skill = 'skill'
21
+ }
22
+
23
+ export interface Hitbox {
24
+ w: number;
25
+ h: number;
9
26
  }
10
27
 
11
- export class RpgCommonPlayer extends AbstractObject {
12
- events: any[] = []
13
- layerName: string = ''
14
- data: any = {}
15
- pendingMove: PendingMove = []
16
- inputsTimestamp: {
17
- [inputName: string]: number
18
- } = {}
19
-
20
- /**
21
- * Display/Hide the GUI attached to this sprite
22
- *
23
- * @prop {boolean} guiDisplay
24
- * @since 3.0.0-beta.5
25
- * @memberof RpgSprite
26
- * */
27
- guiDisplay: boolean
28
+ export interface ShowAnimationParams {
29
+ graphic?: string | string[];
30
+ animationName: string;
31
+ loop?: boolean;
28
32
  }
29
33
 
30
- export default AbstractObject
34
+ export interface AttachShapeOptions {
35
+ /** Width of the shape in pixels */
36
+ width: number;
37
+ /** Height of the shape in pixels */
38
+ height: number;
39
+ /** Circle radius in pixels (for zone shapes) */
40
+ radius?: number;
41
+ /** Vision aperture in degrees. 360 = full circle, <360 = cone */
42
+ angle?: number;
43
+ /** Facing direction used when angle < 360 */
44
+ direction?: Direction;
45
+ /** If true, walls (static hitboxes) stop vision */
46
+ limitedByWalls?: boolean;
47
+ /** Indicate where the shape is placed relative to the player */
48
+ positioning?: 'center' | 'top' | 'bottom' | 'left' | 'right';
49
+ /** The name of the shape */
50
+ name?: string;
51
+ /** An object to retrieve information when interacting with the shape */
52
+ properties?: object;
53
+ }
54
+
55
+ export class RpgCommonPlayer {
56
+ @id() id: string;
57
+ @sync() x = signal(0);
58
+ @sync() y = signal(0);
59
+ @sync() z = signal(0);
60
+ @sync() tint = signal('white');
61
+ @sync() direction = signal(Direction.Down);
62
+ @sync() speed = signal(4);
63
+ @sync() graphics = signal<any>([]);
64
+ @sync() canMove = signal(true);
65
+ @sync() hitbox = signal<Hitbox>({
66
+ w: 32,
67
+ h: 32
68
+ });
69
+ @sync() _gold = signal(0);
70
+ @sync() animationName = signal('stand');
71
+ @sync() _hp = signal(0);
72
+ @sync() _sp = signal(0);
73
+ @sync() _exp = signal(0);
74
+ @sync() _level = signal(0);
75
+ @sync() _class = signal({});
76
+ @sync(Item) items = signal<Item[]>([]);
77
+ @sync() equipments = signal<any[]>([]);
78
+ @sync() states = signal<any[]>([]);
79
+ @sync() skills = signal<any[]>([]);
80
+ @sync() _effects = signal<any[]>([]);
81
+ @sync() _through = signal(false);
82
+ @sync() _throughOtherPlayer = signal(true);
83
+ @sync() _throughEvent = signal(false);
84
+ @sync() _frequency = signal(0);
85
+
86
+ // Store intended movement direction (not synced, only used locally)
87
+ private _intendedDirection: Direction | null = null;
88
+
89
+ /**
90
+ * Change the player's facing direction
91
+ *
92
+ * Updates the direction the player is facing, which affects animations
93
+ * and directional abilities. This should be called when the player
94
+ * intends to move in a specific direction, not when they are pushed
95
+ * by physics or sliding.
96
+ *
97
+ * @param direction - The new direction to face
98
+ *
99
+ * @example
100
+ * ```ts
101
+ * // Player presses right arrow key
102
+ * player.changeDirection(Direction.Right);
103
+ * ```
104
+ */
105
+ changeDirection(direction: Direction) {
106
+ this.direction.set(direction);
107
+ }
108
+
109
+ /**
110
+ * Get the current facing direction
111
+ *
112
+ * @returns Current direction the player is facing
113
+ *
114
+ * @example
115
+ * ```ts
116
+ * const currentDirection = player.getDirection();
117
+ * if (currentDirection === Direction.Up) {
118
+ * // Player is facing up
119
+ * }
120
+ * ```
121
+ */
122
+ getDirection() {
123
+ return this.direction();
124
+ }
125
+
126
+ /**
127
+ * Set the intended movement direction
128
+ *
129
+ * This should be called when the player intends to move in a direction,
130
+ * typically from input handling. This direction will be used to update
131
+ * the player's facing direction regardless of physics interactions.
132
+ *
133
+ * @param direction - The intended movement direction, or null if not moving
134
+ *
135
+ * @example
136
+ * ```ts
137
+ * // Player presses down arrow key
138
+ * player.setIntendedDirection(Direction.Down);
139
+ *
140
+ * // Player releases all movement keys
141
+ * player.setIntendedDirection(null);
142
+ * ```
143
+ */
144
+ setIntendedDirection(direction: Direction | null) {
145
+ this._intendedDirection = direction;
146
+ // Update facing direction immediately when player intends to move
147
+ if (direction !== null) {
148
+ this.changeDirection(direction);
149
+ }
150
+ }
151
+
152
+ /**
153
+ * Get the intended movement direction
154
+ *
155
+ * @returns The direction the player intends to move, or null if not moving
156
+ *
157
+ * @example
158
+ * ```ts
159
+ * const intended = player.getIntendedDirection();
160
+ * if (intended === Direction.Left) {
161
+ * // Player is trying to move left
162
+ * }
163
+ * ```
164
+ */
165
+ getIntendedDirection(): Direction | null {
166
+ return this._intendedDirection;
167
+ }
168
+
169
+ /**
170
+ * Apply physics body position to player coordinates
171
+ *
172
+ * Synchronizes the player's position with their physics body after
173
+ * physics calculations. This method no longer automatically changes
174
+ * the player's direction based on position changes, as direction
175
+ * should be controlled by intended movement instead.
176
+ *
177
+ * @param body - The Matter.js physics body
178
+ *
179
+ * @example
180
+ * ```ts
181
+ * // Called automatically by physics system
182
+ * player.applyPhysic(body);
183
+ * ```
184
+ */
185
+ applyPhysic(body: Matter.Body) {
186
+ // Convert body center position to top-left corner for consistency
187
+ const width = body.bounds.max.x - body.bounds.min.x;
188
+ const height = body.bounds.max.y - body.bounds.min.y;
189
+ const topLeftX = body.position.x - width / 2;
190
+ const topLeftY = body.position.y - height / 2;
191
+
192
+ const posChanged = Math.round(this.x()) !== Math.round(topLeftX) || Math.round(this.y()) !== Math.round(topLeftY);
193
+ if (posChanged) {
194
+ // Only update position, do not change direction based on physics movement
195
+ // Direction should be controlled by intended movement via setIntendedDirection()
196
+ this.x.set(topLeftX);
197
+ this.y.set(topLeftY);
198
+ }
199
+ }
200
+
201
+ _showAnimation(params: ShowAnimationParams) {
202
+ const { graphic, animationName, loop } = params;
203
+ if (graphic) {
204
+ if (Array.isArray(graphic)) {
205
+ this.graphics.set(graphic);
206
+ }
207
+ else {
208
+ this.graphics.set([graphic]);
209
+ }
210
+ }
211
+ }
212
+
213
+ /**
214
+ * Create a temporary and moving hitbox relative to the player's position
215
+ *
216
+ * Creates a temporary hitbox that moves through multiple positions sequentially,
217
+ * with all coordinates being relative to the player's current position.
218
+ * For example, you can use it for player attacks, spells, or area effects
219
+ * that should follow the player's position.
220
+ *
221
+ * The method creates a zone sensor that moves through the specified hitbox positions
222
+ * at the given speed, detecting collisions with other players and events at each step.
223
+ *
224
+ * @param hitboxes - Array of hitbox positions relative to player position
225
+ * @param options - Configuration options for the movement
226
+ * @param map - Reference to the map instance for physics access
227
+ * @returns Observable that emits arrays of hit entities and completes when movement is finished
228
+ *
229
+ * @example
230
+ * ```ts
231
+ * // Create a forward attack relative to player position
232
+ * player.createMovingHitbox([
233
+ * { x: 0, y: -32, width: 32, height: 32 }, // In front of player
234
+ * { x: 0, y: -64, width: 32, height: 32 } // Further in front
235
+ * ], { speed: 2 }, map).subscribe({
236
+ * next(hits) {
237
+ * // hits contains other RpgPlayer or RpgEvent objects that were hit
238
+ * console.log('Hit entities:', hits);
239
+ * },
240
+ * complete() {
241
+ * console.log('Attack finished');
242
+ * }
243
+ * });
244
+ * ```
245
+ */
246
+ createMovingHitbox(
247
+ hitboxes: Array<{ x: number; y: number; width: number; height: number }>,
248
+ options: { speed?: number } = {},
249
+ ): Observable<any[]> {
250
+ // Convert relative positions to absolute positions based on player's current position
251
+ const absoluteHitboxes = hitboxes.map(hitbox => ({
252
+ x: this.x() + hitbox.x,
253
+ y: this.y() + hitbox.y,
254
+ width: hitbox.width,
255
+ height: hitbox.height
256
+ }));
257
+
258
+ // Delegate to the map's createMovingHitbox method with absolute positions
259
+ return this.getCurrentMap().createMovingHitbox(absoluteHitboxes, options);
260
+ }
261
+
262
+ getCurrentMap() {
263
+ return this['map'];
264
+ }
265
+
266
+ }
@@ -15,7 +15,7 @@
15
15
  * PrebuiltGui.Notification | rpg-notification
16
16
  * @memberof PrebuiltGui
17
17
  * */
18
- export enum PrebuiltGui {
18
+ export enum PrebuiltGui {
19
19
  Dialog = 'rpg-dialog',
20
20
  MainMenu = 'rpg-main-menu',
21
21
  Shop = 'rpg-shop',