@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.
- package/dist/Physic.d.ts +619 -0
- package/dist/Player.d.ts +198 -0
- package/{lib → dist}/Utils.d.ts +19 -2
- package/dist/database/Item.d.ts +10 -0
- package/dist/database/index.d.ts +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +16741 -0
- package/dist/index.js.map +1 -0
- package/dist/modules.d.ts +92 -0
- package/dist/movement/MovementManager.d.ts +84 -0
- package/dist/movement/MovementStrategy.d.ts +39 -0
- package/dist/movement/index.d.ts +12 -0
- package/dist/movement/strategies/CompositeMovement.d.ts +76 -0
- package/dist/movement/strategies/Dash.d.ts +52 -0
- package/dist/movement/strategies/IceMovement.d.ts +87 -0
- package/dist/movement/strategies/Knockback.d.ts +50 -0
- package/dist/movement/strategies/LinearMove.d.ts +43 -0
- package/dist/movement/strategies/LinearRepulsion.d.ts +55 -0
- package/dist/movement/strategies/Oscillate.d.ts +60 -0
- package/dist/movement/strategies/PathFollow.d.ts +78 -0
- package/dist/movement/strategies/ProjectileMovement.d.ts +138 -0
- package/dist/movement/strategies/SeekAvoid.d.ts +27 -0
- package/dist/rooms/Map.d.ts +109 -0
- package/dist/services/updateMap.d.ts +7 -0
- package/package.json +17 -17
- package/src/Physic.ts +1644 -0
- package/src/Player.ts +262 -26
- package/src/{gui/PrebuiltGui.ts → PrebuiltGui.ts} +1 -1
- package/src/Utils.ts +184 -123
- package/src/database/Item.ts +19 -0
- package/src/database/index.ts +1 -0
- package/src/index.ts +9 -25
- package/src/modules.ts +230 -0
- package/src/movement/MovementManager.ts +142 -0
- package/src/movement/MovementStrategy.ts +42 -0
- package/src/movement/index.ts +15 -0
- package/src/movement/strategies/CompositeMovement.ts +173 -0
- package/src/movement/strategies/Dash.ts +82 -0
- package/src/movement/strategies/IceMovement.ts +158 -0
- package/src/movement/strategies/Knockback.ts +81 -0
- package/src/movement/strategies/LinearMove.ts +58 -0
- package/src/movement/strategies/LinearRepulsion.ts +128 -0
- package/src/movement/strategies/Oscillate.ts +144 -0
- package/src/movement/strategies/PathFollow.ts +156 -0
- package/src/movement/strategies/ProjectileMovement.ts +322 -0
- package/src/movement/strategies/SeekAvoid.ts +123 -0
- package/src/rooms/Map.ts +272 -0
- package/src/services/updateMap.ts +9 -0
- package/tests/physic.spec.ts +454 -0
- package/tsconfig.json +8 -3
- package/vite.config.ts +21 -0
- package/CHANGELOG.md +0 -160
- package/LICENSE +0 -19
- package/browser/manifest.json +0 -7
- package/browser/rpg.common.js +0 -11357
- package/browser/rpg.common.umd.cjs +0 -11358
- package/lib/AbstractObject.d.ts +0 -322
- package/lib/AbstractObject.js +0 -872
- package/lib/AbstractObject.js.map +0 -1
- package/lib/Color.d.ts +0 -1
- package/lib/Color.js +0 -25
- package/lib/Color.js.map +0 -1
- package/lib/DefaultInput.d.ts +0 -2
- package/lib/DefaultInput.js +0 -26
- package/lib/DefaultInput.js.map +0 -1
- package/lib/Event.d.ts +0 -3
- package/lib/Event.js +0 -4
- package/lib/Event.js.map +0 -1
- package/lib/EventEmitter.d.ts +0 -10
- package/lib/EventEmitter.js +0 -61
- package/lib/EventEmitter.js.map +0 -1
- package/lib/Game.d.ts +0 -28
- package/lib/Game.js +0 -127
- package/lib/Game.js.map +0 -1
- package/lib/Hit.d.ts +0 -16
- package/lib/Hit.js +0 -65
- package/lib/Hit.js.map +0 -1
- package/lib/Inject.d.ts +0 -9
- package/lib/Inject.js +0 -17
- package/lib/Inject.js.map +0 -1
- package/lib/Logger.d.ts +0 -2
- package/lib/Logger.js +0 -7
- package/lib/Logger.js.map +0 -1
- package/lib/Map.d.ts +0 -174
- package/lib/Map.js +0 -263
- package/lib/Map.js.map +0 -1
- package/lib/Module.d.ts +0 -16
- package/lib/Module.js +0 -139
- package/lib/Module.js.map +0 -1
- package/lib/Player.d.ts +0 -26
- package/lib/Player.js +0 -19
- package/lib/Player.js.map +0 -1
- package/lib/Plugin.d.ts +0 -67
- package/lib/Plugin.js +0 -92
- package/lib/Plugin.js.map +0 -1
- package/lib/Scheduler.d.ts +0 -26
- package/lib/Scheduler.js +0 -90
- package/lib/Scheduler.js.map +0 -1
- package/lib/Shape.d.ts +0 -127
- package/lib/Shape.js +0 -261
- package/lib/Shape.js.map +0 -1
- package/lib/Utils.js +0 -181
- package/lib/Utils.js.map +0 -1
- package/lib/Vector2d.d.ts +0 -20
- package/lib/Vector2d.js +0 -63
- package/lib/Vector2d.js.map +0 -1
- package/lib/VirtualGrid.d.ts +0 -26
- package/lib/VirtualGrid.js +0 -68
- package/lib/VirtualGrid.js.map +0 -1
- package/lib/Worker.d.ts +0 -7
- package/lib/Worker.js +0 -13
- package/lib/Worker.js.map +0 -1
- package/lib/WorldMaps.d.ts +0 -105
- package/lib/WorldMaps.js +0 -184
- package/lib/WorldMaps.js.map +0 -1
- package/lib/gui/PrebuiltGui.js +0 -29
- package/lib/gui/PrebuiltGui.js.map +0 -1
- package/lib/index.d.ts +0 -25
- package/lib/index.js +0 -26
- package/lib/index.js.map +0 -1
- package/lib/transports/io.d.ts +0 -22
- package/lib/transports/io.js +0 -82
- package/lib/transports/io.js.map +0 -1
- package/lib/workers/move.d.ts +0 -1
- package/lib/workers/move.js +0 -57
- package/lib/workers/move.js.map +0 -1
- package/rpg.toml +0 -11
- package/src/AbstractObject.ts +0 -973
- package/src/Color.ts +0 -29
- package/src/DefaultInput.ts +0 -26
- package/src/Event.ts +0 -3
- package/src/EventEmitter.ts +0 -65
- package/src/Game.ts +0 -159
- package/src/Hit.ts +0 -70
- package/src/Inject.ts +0 -22
- package/src/Logger.ts +0 -7
- package/src/Map.ts +0 -335
- package/src/Module.ts +0 -144
- package/src/Plugin.ts +0 -100
- package/src/Scheduler.ts +0 -95
- package/src/Shape.ts +0 -302
- package/src/Vector2d.ts +0 -70
- package/src/VirtualGrid.ts +0 -78
- package/src/Worker.ts +0 -17
- package/src/WorldMaps.ts +0 -204
- package/src/transports/io.ts +0 -91
- package/src/workers/move.ts +0 -61
- /package/{lib/gui → dist}/PrebuiltGui.d.ts +0 -0
package/src/Player.ts
CHANGED
|
@@ -1,30 +1,266 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
|
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
|
+
}
|