@rpgjs/server 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 (219) hide show
  1. package/{lib → dist}/Gui/DialogGui.d.ts +1 -2
  2. package/{lib → dist}/Gui/Gui.d.ts +1 -2
  3. package/dist/Player/BattleManager.d.ts +22 -0
  4. package/dist/Player/ClassManager.d.ts +18 -0
  5. package/dist/Player/Event.d.ts +0 -0
  6. package/dist/Player/ItemManager.d.ts +17 -0
  7. package/dist/Player/MoveManager.d.ts +177 -0
  8. package/dist/Player/ParameterManager.d.ts +42 -0
  9. package/dist/Player/Player.d.ts +73 -0
  10. package/dist/Player/SkillManager.d.ts +23 -0
  11. package/dist/Player/StateManager.d.ts +39 -0
  12. package/{lib → dist}/RpgServer.d.ts +32 -15
  13. package/dist/RpgServerEngine.d.ts +5 -0
  14. package/dist/core/context.d.ts +2 -0
  15. package/dist/core/inject.d.ts +5 -0
  16. package/dist/core/setup.d.ts +6 -0
  17. package/dist/index.d.ts +9 -0
  18. package/dist/index.js +29673 -0
  19. package/dist/index.js.map +1 -0
  20. package/dist/module.d.ts +2 -0
  21. package/dist/rooms/lobby.d.ts +6 -0
  22. package/dist/rooms/map.d.ts +110 -0
  23. package/package.json +15 -37
  24. package/src/Gui/DialogGui.ts +3 -4
  25. package/src/Gui/Gui.ts +4 -6
  26. package/src/Player/BattleManager.ts +108 -99
  27. package/src/Player/ClassManager.ts +47 -46
  28. package/src/Player/ComponentManager.ts +26 -535
  29. package/src/Player/EffectManager.ts +44 -59
  30. package/src/Player/ElementManager.ts +70 -72
  31. package/src/Player/Event.ts +0 -0
  32. package/src/Player/GoldManager.ts +24 -6
  33. package/src/Player/GuiManager.ts +106 -97
  34. package/src/Player/ItemFixture.ts +22 -17
  35. package/src/Player/ItemManager.ts +234 -225
  36. package/src/Player/MoveManager.ts +1047 -457
  37. package/src/Player/ParameterManager.ts +59 -37
  38. package/src/Player/Player.ts +272 -1058
  39. package/src/Player/SkillManager.ts +146 -137
  40. package/src/Player/StateManager.ts +124 -98
  41. package/src/Player/VariableManager.ts +23 -3
  42. package/src/RpgServer.ts +36 -12
  43. package/src/RpgServerEngine.ts +7 -0
  44. package/src/core/context.ts +3 -0
  45. package/src/core/inject.ts +17 -0
  46. package/src/core/setup.ts +20 -0
  47. package/src/index.ts +9 -31
  48. package/src/module.ts +32 -0
  49. package/src/presets/index.ts +1 -3
  50. package/src/rooms/lobby.ts +24 -0
  51. package/src/rooms/map.ts +311 -0
  52. package/tsconfig.json +26 -23
  53. package/vite.config.ts +21 -0
  54. package/CHANGELOG.md +0 -174
  55. package/LICENSE +0 -19
  56. package/browser/manifest.json +0 -7
  57. package/browser/rpg.server.js +0 -22379
  58. package/browser/rpg.server.umd.cjs +0 -22410
  59. package/lib/Game/EventManager.d.ts +0 -54
  60. package/lib/Game/EventManager.js +0 -107
  61. package/lib/Game/EventManager.js.map +0 -1
  62. package/lib/Game/Map.d.ts +0 -191
  63. package/lib/Game/Map.js +0 -419
  64. package/lib/Game/Map.js.map +0 -1
  65. package/lib/Game/WorldMaps.d.ts +0 -17
  66. package/lib/Game/WorldMaps.js +0 -28
  67. package/lib/Game/WorldMaps.js.map +0 -1
  68. package/lib/Gui/DialogGui.js +0 -57
  69. package/lib/Gui/DialogGui.js.map +0 -1
  70. package/lib/Gui/Gui.js +0 -37
  71. package/lib/Gui/Gui.js.map +0 -1
  72. package/lib/Gui/MenuGui.js +0 -23
  73. package/lib/Gui/MenuGui.js.map +0 -1
  74. package/lib/Gui/NotificationGui.js +0 -8
  75. package/lib/Gui/NotificationGui.js.map +0 -1
  76. package/lib/Gui/ShopGui.js +0 -40
  77. package/lib/Gui/ShopGui.js.map +0 -1
  78. package/lib/Gui/index.js +0 -7
  79. package/lib/Gui/index.js.map +0 -1
  80. package/lib/Interfaces/Gui.d.ts +0 -4
  81. package/lib/Interfaces/Gui.js +0 -2
  82. package/lib/Interfaces/Gui.js.map +0 -1
  83. package/lib/Interfaces/StateStore.d.ts +0 -5
  84. package/lib/Interfaces/StateStore.js +0 -2
  85. package/lib/Interfaces/StateStore.js.map +0 -1
  86. package/lib/MatchMaker.d.ts +0 -24
  87. package/lib/MatchMaker.js +0 -42
  88. package/lib/MatchMaker.js.map +0 -1
  89. package/lib/Monitor/index.d.ts +0 -17
  90. package/lib/Monitor/index.js +0 -72
  91. package/lib/Monitor/index.js.map +0 -1
  92. package/lib/Player/BattleManager.d.ts +0 -36
  93. package/lib/Player/BattleManager.js +0 -101
  94. package/lib/Player/BattleManager.js.map +0 -1
  95. package/lib/Player/ClassManager.d.ts +0 -47
  96. package/lib/Player/ClassManager.js +0 -65
  97. package/lib/Player/ClassManager.js.map +0 -1
  98. package/lib/Player/ComponentManager.d.ts +0 -397
  99. package/lib/Player/ComponentManager.js +0 -505
  100. package/lib/Player/ComponentManager.js.map +0 -1
  101. package/lib/Player/EffectManager.d.ts +0 -46
  102. package/lib/Player/EffectManager.js +0 -75
  103. package/lib/Player/EffectManager.js.map +0 -1
  104. package/lib/Player/ElementManager.d.ts +0 -108
  105. package/lib/Player/ElementManager.js +0 -120
  106. package/lib/Player/ElementManager.js.map +0 -1
  107. package/lib/Player/GoldManager.d.ts +0 -17
  108. package/lib/Player/GoldManager.js +0 -27
  109. package/lib/Player/GoldManager.js.map +0 -1
  110. package/lib/Player/GuiManager.d.ts +0 -247
  111. package/lib/Player/GuiManager.js +0 -291
  112. package/lib/Player/GuiManager.js.map +0 -1
  113. package/lib/Player/ItemFixture.d.ts +0 -7
  114. package/lib/Player/ItemFixture.js +0 -19
  115. package/lib/Player/ItemFixture.js.map +0 -1
  116. package/lib/Player/ItemManager.d.ts +0 -331
  117. package/lib/Player/ItemManager.js +0 -475
  118. package/lib/Player/ItemManager.js.map +0 -1
  119. package/lib/Player/MoveManager.d.ts +0 -333
  120. package/lib/Player/MoveManager.js +0 -450
  121. package/lib/Player/MoveManager.js.map +0 -1
  122. package/lib/Player/ParameterManager.d.ts +0 -316
  123. package/lib/Player/ParameterManager.js +0 -408
  124. package/lib/Player/ParameterManager.js.map +0 -1
  125. package/lib/Player/Player.d.ts +0 -828
  126. package/lib/Player/Player.js +0 -927
  127. package/lib/Player/Player.js.map +0 -1
  128. package/lib/Player/SkillManager.d.ts +0 -163
  129. package/lib/Player/SkillManager.js +0 -223
  130. package/lib/Player/SkillManager.js.map +0 -1
  131. package/lib/Player/StateManager.d.ts +0 -185
  132. package/lib/Player/StateManager.js +0 -228
  133. package/lib/Player/StateManager.js.map +0 -1
  134. package/lib/Player/VariableManager.d.ts +0 -46
  135. package/lib/Player/VariableManager.js +0 -52
  136. package/lib/Player/VariableManager.js.map +0 -1
  137. package/lib/Query.d.ts +0 -127
  138. package/lib/Query.js +0 -159
  139. package/lib/Query.js.map +0 -1
  140. package/lib/RpgServer.js +0 -2
  141. package/lib/RpgServer.js.map +0 -1
  142. package/lib/Scenes/Map.d.ts +0 -136
  143. package/lib/Scenes/Map.js +0 -273
  144. package/lib/Scenes/Map.js.map +0 -1
  145. package/lib/decorators/event.d.ts +0 -43
  146. package/lib/decorators/event.js +0 -13
  147. package/lib/decorators/event.js.map +0 -1
  148. package/lib/decorators/map.d.ts +0 -178
  149. package/lib/decorators/map.js +0 -43
  150. package/lib/decorators/map.js.map +0 -1
  151. package/lib/entry-point.d.ts +0 -34
  152. package/lib/entry-point.js +0 -70
  153. package/lib/entry-point.js.map +0 -1
  154. package/lib/express/api.d.ts +0 -3
  155. package/lib/express/api.js +0 -105
  156. package/lib/express/api.js.map +0 -1
  157. package/lib/express/errors/NotAuthorized.d.ts +0 -4
  158. package/lib/express/errors/NotAuthorized.js +0 -7
  159. package/lib/express/errors/NotAuthorized.js.map +0 -1
  160. package/lib/express/errors/NotFound.d.ts +0 -4
  161. package/lib/express/errors/NotFound.js +0 -7
  162. package/lib/express/errors/NotFound.js.map +0 -1
  163. package/lib/express/server.d.ts +0 -18
  164. package/lib/express/server.js +0 -70
  165. package/lib/express/server.js.map +0 -1
  166. package/lib/index.d.ts +0 -20
  167. package/lib/index.js +0 -19
  168. package/lib/index.js.map +0 -1
  169. package/lib/inject.d.ts +0 -22
  170. package/lib/inject.js +0 -29
  171. package/lib/inject.js.map +0 -1
  172. package/lib/logs/index.js +0 -6
  173. package/lib/logs/index.js.map +0 -1
  174. package/lib/logs/item.js +0 -34
  175. package/lib/logs/item.js.map +0 -1
  176. package/lib/logs/log.js +0 -7
  177. package/lib/logs/log.js.map +0 -1
  178. package/lib/logs/skill.js +0 -19
  179. package/lib/logs/skill.js.map +0 -1
  180. package/lib/logs/state.js +0 -13
  181. package/lib/logs/state.js.map +0 -1
  182. package/lib/models/Item.d.ts +0 -10
  183. package/lib/models/Item.js +0 -2
  184. package/lib/models/Item.js.map +0 -1
  185. package/lib/presets/index.js +0 -65
  186. package/lib/presets/index.js.map +0 -1
  187. package/lib/server.d.ts +0 -137
  188. package/lib/server.js +0 -443
  189. package/lib/server.js.map +0 -1
  190. package/rpg.toml +0 -14
  191. package/src/Game/EventManager.ts +0 -125
  192. package/src/Game/Map.ts +0 -500
  193. package/src/Game/WorldMaps.ts +0 -45
  194. package/src/Interfaces/Gui.ts +0 -4
  195. package/src/Interfaces/StateStore.ts +0 -5
  196. package/src/MatchMaker.ts +0 -65
  197. package/src/Monitor/index.ts +0 -78
  198. package/src/Query.ts +0 -172
  199. package/src/Scenes/Map.ts +0 -310
  200. package/src/decorators/event.ts +0 -58
  201. package/src/decorators/map.ts +0 -226
  202. package/src/entry-point.ts +0 -111
  203. package/src/express/api.ts +0 -118
  204. package/src/express/errors/NotAuthorized.ts +0 -6
  205. package/src/express/errors/NotFound.ts +0 -6
  206. package/src/express/server.ts +0 -94
  207. package/src/inject.ts +0 -33
  208. package/src/models/Item.ts +0 -11
  209. package/src/server.ts +0 -459
  210. /package/{lib → dist}/Gui/MenuGui.d.ts +0 -0
  211. /package/{lib → dist}/Gui/NotificationGui.d.ts +0 -0
  212. /package/{lib → dist}/Gui/ShopGui.d.ts +0 -0
  213. /package/{lib → dist}/Gui/index.d.ts +0 -0
  214. /package/{lib → dist}/logs/index.d.ts +0 -0
  215. /package/{lib → dist}/logs/item.d.ts +0 -0
  216. /package/{lib → dist}/logs/log.d.ts +0 -0
  217. /package/{lib → dist}/logs/skill.d.ts +0 -0
  218. /package/{lib → dist}/logs/state.d.ts +0 -0
  219. /package/{lib → dist}/presets/index.d.ts +0 -0
@@ -1,927 +0,0 @@
1
- import { RpgCommonPlayer, Utils, RpgPlugin, RpgCommonGame, RpgCommonMap, Direction } from '@rpgjs/common';
2
- import { Room } from 'simple-room';
3
- import merge from 'lodash.merge';
4
- import { ItemManager } from './ItemManager.js';
5
- import { GoldManager } from './GoldManager.js';
6
- import { StateManager } from './StateManager.js';
7
- import { SkillManager } from './SkillManager.js';
8
- import { ParameterManager } from './ParameterManager.js';
9
- import { EffectManager } from './EffectManager.js';
10
- import { ClassManager } from './ClassManager.js';
11
- import { ElementManager } from './ElementManager.js';
12
- import { GuiManager } from './GuiManager.js';
13
- import { VariableManager } from './VariableManager.js';
14
- import { Frequency, MoveManager, Speed } from './MoveManager.js';
15
- import { BattleManager } from './BattleManager.js';
16
- import { MAXHP, MAXSP, STR, INT, DEX, AGI, MAXHP_CURVE, MAXSP_CURVE, STR_CURVE, INT_CURVE, DEX_CURVE, AGI_CURVE } from '../presets/index.js';
17
- import { RpgServerEngine } from '../server.js';
18
- import { SocketEvents, SocketMethods } from '@rpgjs/types';
19
- import { ComponentManager } from './ComponentManager.js';
20
- import { EventManager, EventMode } from '../Game/EventManager.js';
21
- import { inject } from '../inject.js';
22
- const { isPromise, applyMixins, isString } = Utils;
23
- const commonSchemaFeature = {
24
- name: {
25
- $permanent: false
26
- },
27
- description: {
28
- $permanent: false
29
- },
30
- id: String
31
- };
32
- const itemSchemas = {
33
- price: {
34
- $permanent: false
35
- },
36
- consumable: {
37
- $permanent: false
38
- },
39
- ...commonSchemaFeature
40
- };
41
- export const componentSchema = { id: String, value: String };
42
- export const layoutSchema = {
43
- width: Number,
44
- height: Number,
45
- marginBottom: Number,
46
- marginTop: Number,
47
- marginLeft: Number,
48
- marginRight: Number,
49
- lines: [{
50
- col: [componentSchema]
51
- }]
52
- };
53
- const playerSchemas = {
54
- position: {
55
- x: Number,
56
- y: Number,
57
- z: Number
58
- },
59
- direction: Number,
60
- teleported: {
61
- $permanent: false
62
- },
63
- deleted: {
64
- $permanent: false
65
- },
66
- moving: {
67
- $permanent: false
68
- },
69
- param: Object,
70
- hp: Number,
71
- sp: Number,
72
- gold: Number,
73
- level: {
74
- $effects: ['$this.expForNextlevel', '$this.param']
75
- },
76
- expForNextlevel: {
77
- $permanent: false
78
- },
79
- exp: Number,
80
- name: String,
81
- items: [{ nb: Number, item: itemSchemas }],
82
- _class: commonSchemaFeature,
83
- equipments: [itemSchemas],
84
- skills: [
85
- {
86
- spCost: {
87
- $permanent: false
88
- },
89
- ...commonSchemaFeature
90
- }
91
- ],
92
- states: [commonSchemaFeature],
93
- effects: [String],
94
- layout: {
95
- top: layoutSchema,
96
- bottom: layoutSchema,
97
- left: layoutSchema,
98
- right: layoutSchema,
99
- center: layoutSchema
100
- },
101
- action: Number,
102
- map: String,
103
- speed: Number,
104
- frequency: Number,
105
- canMove: Boolean,
106
- through: Boolean,
107
- throughOtherPlayer: Boolean,
108
- width: Number,
109
- height: Number,
110
- wHitbox: Number,
111
- hHitbox: Number,
112
- // only for server
113
- _statesEfficiency: [{
114
- rate: {
115
- $syncWithClient: false
116
- },
117
- state: {
118
- $syncWithClient: false
119
- }
120
- }],
121
- tmpPositions: {
122
- $syncWithClient: false
123
- },
124
- initialLevel: {
125
- $syncWithClient: false
126
- },
127
- finalLevel: {
128
- $syncWithClient: false
129
- },
130
- };
131
- const layoutObject = {
132
- lines: []
133
- };
134
- class RpgPlayer extends RpgCommonPlayer {
135
- // redefine type (as RpgPlayer)
136
- get otherPlayersCollision() {
137
- return super.otherPlayersCollision;
138
- }
139
- get world() {
140
- return this.server?.world;
141
- }
142
- constructor(playerId) {
143
- super(inject(RpgCommonGame), playerId);
144
- this.playerId = playerId;
145
- this.type = 'player';
146
- this.layout = {
147
- top: layoutObject,
148
- bottom: layoutObject,
149
- left: layoutObject,
150
- right: layoutObject,
151
- center: layoutObject
152
- };
153
- this.events = {};
154
- this._rooms = [];
155
- this.session = null;
156
- this.prevMap = '';
157
- this.touchSide = false;
158
- /** @internal */
159
- this.tmpPositions = null;
160
- this.otherPossessedPlayer = null;
161
- this.followingId = null;
162
- // Indicates whether to load data with load(). In this case, hooks are not triggered.
163
- this._dataLoading = false;
164
- /**
165
- * @deprecated Use `inject(RpgCommonGame)` instead. Will be removed in v5
166
- */
167
- this.gameEngine = inject(RpgCommonGame);
168
- // As soon as a teleport has been made, the value is changed to force the client to change the positions on the map without making a move.
169
- this.teleported = 0;
170
- // a flag that lets the client know if the event is suppressed. The client can, for example, end animations before completely deleting the object (client side).
171
- this.deleted = false;
172
- this.initialize();
173
- }
174
- /** @internal */
175
- initialize() {
176
- this.server = inject(RpgServerEngine);
177
- this.expCurve = {
178
- basis: 30,
179
- extra: 20,
180
- accelerationA: 30,
181
- accelerationB: 30
182
- };
183
- this.parameters = new Map();
184
- this.variables = new Map();
185
- this.states = [];
186
- this.equipments = [];
187
- this._effects = [];
188
- this.items = [];
189
- this.skills = [];
190
- this.gold = 0;
191
- this.exp = 0;
192
- this.speed = Speed.Normal;
193
- this.frequency = Frequency.None;
194
- this.canMove = true;
195
- this.through = false;
196
- this.throughOtherPlayer = true;
197
- this.initialLevel = 1;
198
- this.finalLevel = 99;
199
- this.level = this.initialLevel;
200
- this._gui = {};
201
- this._elementsEfficiency = [];
202
- this._statesEfficiency = [];
203
- this.moving = false;
204
- this.addParameter(MAXHP, MAXHP_CURVE);
205
- this.addParameter(MAXSP, MAXSP_CURVE);
206
- this.addParameter(STR, STR_CURVE);
207
- this.addParameter(INT, INT_CURVE);
208
- this.addParameter(DEX, DEX_CURVE);
209
- this.addParameter(AGI, AGI_CURVE);
210
- this.allRecovery();
211
- }
212
- _init() {
213
- this._socket.on('gui.interaction', ({ guiId, name, data }) => {
214
- if (this._gui[guiId]) {
215
- this._gui[guiId].emit(name, data);
216
- this.syncChanges();
217
- }
218
- });
219
- this._socket.on('gui.exit', ({ guiId, data }) => {
220
- this.removeGui(guiId, data);
221
- });
222
- }
223
- get schema() {
224
- return {
225
- ...RpgPlayer.schemas,
226
- ...this.server['playerProps']
227
- };
228
- }
229
- /**
230
- * ```ts
231
- * player.name = 'Link'
232
- * ```
233
- * @title Read/Give a name
234
- * @prop {string} player.name
235
- * @memberof Player
236
- * */
237
- get name() {
238
- return this._name;
239
- }
240
- set name(val) {
241
- this._name = val;
242
- }
243
- /**
244
- * Change your map. Indicate the positions to put the player at a place on the map
245
- *
246
- * > The map must be added to RpgServer beforehand. Guide: [Create Map](/guide/create-map.html)
247
- *
248
- * You don't have to give positions but you can put a starting position in the TMX file. Guide: [Start Position](/guide/player-start.html)
249
- *
250
- * @title Change Map
251
- * @method player.changeMap(mapId,positions)
252
- * @param {string} mapId
253
- * @param { {x: number, y: number, z?: number} | string } [positions]
254
- * @returns {Promise<RpgMap | null>} null if map not exists
255
- * @memberof Player
256
- */
257
- changeMap(mapId, positions) {
258
- return this.server.sceneMap.changeMap(mapId, this, positions);
259
- }
260
- async autoChangeMap(nextPosition) {
261
- const map = this.getCurrentMap();
262
- const worldMaps = map?.getInWorldMaps();
263
- let ret = false;
264
- if (worldMaps && map) {
265
- const direction = this.getDirection();
266
- const marginLeftRight = map.tileWidth / 2;
267
- const marginTopDown = map.tileHeight / 2;
268
- const changeMap = async (adjacent, to) => {
269
- if (this.touchSide) {
270
- return false;
271
- }
272
- this.touchSide = true;
273
- const [nextMap] = worldMaps.getAdjacentMaps(map, adjacent);
274
- if (!nextMap)
275
- return false;
276
- const id = nextMap.id;
277
- const nextMapInfo = worldMaps.getMapInfo(id);
278
- return !!(await this.changeMap(id, to(nextMapInfo)));
279
- };
280
- if (nextPosition.x < marginLeftRight && direction == Direction.Left) {
281
- ret = await changeMap({
282
- x: map.worldX - 1,
283
- y: this.worldPositionY + 1
284
- }, nextMapInfo => ({
285
- x: (nextMapInfo.width) - this.wHitbox - marginLeftRight,
286
- y: map.worldY - nextMapInfo.y + nextPosition.y
287
- }));
288
- }
289
- else if (nextPosition.x > map.widthPx - this.wHitbox - marginLeftRight && direction == Direction.Right) {
290
- ret = await changeMap({
291
- x: map.worldX + map.widthPx + 1,
292
- y: this.worldPositionY + 1
293
- }, nextMapInfo => ({
294
- x: marginLeftRight,
295
- y: map.worldY - nextMapInfo.y + nextPosition.y
296
- }));
297
- }
298
- else if (nextPosition.y < marginTopDown && direction == Direction.Up) {
299
- ret = await changeMap({
300
- x: this.worldPositionX + 1,
301
- y: map.worldY - 1
302
- }, nextMapInfo => ({
303
- x: map.worldX - nextMapInfo.x + nextPosition.x,
304
- y: (nextMapInfo.height) - this.hHitbox - marginTopDown,
305
- }));
306
- }
307
- else if (nextPosition.y > map.heightPx - this.hHitbox - marginTopDown && direction == Direction.Down) {
308
- ret = await changeMap({
309
- x: this.worldPositionX + 1,
310
- y: map.worldY + map.heightPx + 1
311
- }, nextMapInfo => ({
312
- x: map.worldX - nextMapInfo.x + nextPosition.x,
313
- y: marginTopDown,
314
- }));
315
- }
316
- else {
317
- this.touchSide = false;
318
- }
319
- }
320
- return ret;
321
- }
322
- /**
323
- * Dynamically create an event in Scenario mode on the current map
324
- *
325
- * ```ts
326
- * @EventData({
327
- * name: 'EV-1'
328
- * })
329
- * class MyEvent extends RpgEvent {
330
- * onAction() {
331
- * console.log('ok')
332
- * }
333
- * }
334
- *
335
- * player.createDynamicEvent({
336
- * x: 100,
337
- * y: 100,
338
- * event: MyEvent
339
- * })
340
- * ```
341
- *
342
- * You can also put an array of objects to create several events at once
343
- *
344
- * @title Create Dynamic Event
345
- * @since 3.0.0-beta.4
346
- * @method player.createDynamicEvent(eventObj | eventObj[])
347
- * @param { { x: number, y: number, z?: number, event: eventClass } } [eventsList]
348
- * @returns { { [eventId: string]: RpgEvent } }
349
- * @memberof Player
350
- */
351
- createDynamicEvent(eventsList, forceMode = true) {
352
- if (!eventsList)
353
- return {};
354
- const mapInstance = this.getCurrentMap();
355
- if (!mapInstance) {
356
- throw 'The player is not assigned to any map';
357
- }
358
- if (!Utils.isArray(eventsList)) {
359
- eventsList = [eventsList];
360
- }
361
- let eventsListMode = eventsList;
362
- if (forceMode) {
363
- eventsListMode = eventsList.map(event => {
364
- event.event.mode = EventMode.Scenario;
365
- return event;
366
- });
367
- }
368
- const events = mapInstance.createEvents(eventsListMode, EventMode.Scenario);
369
- let ret = {};
370
- for (let key in events) {
371
- this.events[key] = events[key];
372
- this.events[key].playerRelated = this;
373
- this.events[key].execMethod('onInit', [this]);
374
- // force to get Proxy object to sync with client
375
- ret = { ...ret, [key]: this.events[key] };
376
- }
377
- return ret;
378
- }
379
- /**
380
- * Allows to change the positions of the player on the current map.
381
- * You can put the X and Y positions or the name of the created shape on Tiled Map Editor.
382
- * If you have several shapes with the same name, one position will be chosen randomly.
383
- *
384
- * ```ts
385
- * player.teleport({ x: 100, y: 500 })
386
- * ```
387
- *
388
- * or
389
- *
390
- * ```ts
391
- * player.teleport('my-shape-name')
392
- * ```
393
- *
394
- * If no parameter:
395
- *
396
- * ```ts
397
- * player.teleport() // { x: 0, y: 0, z: 0 }
398
- * ```
399
- *
400
- * @title Teleport on the map
401
- * @method player.teleport(positions)
402
- * @param { {x: number, y: number, z?: number} | string } [positions]
403
- * @returns {Promise<{ {x: number, y: number, z: number} }>}
404
- * @memberof Player
405
- */
406
- async teleport(positions) {
407
- if (isString(positions))
408
- positions = this.getCurrentMap()?.getPositionByShape(shape => shape.name == positions || shape.getType() == positions);
409
- if (!positions)
410
- positions = { x: 0, y: 0, z: 0 };
411
- if (!positions.z)
412
- positions.z = 0;
413
- this.teleported++;
414
- this.position = positions;
415
- // delete last frame positions because when the character is teleported, no server reconciliation is needed on the client side
416
- this._lastFramePositions = undefined;
417
- // force interaction with event or shape
418
- await this.isCollided(this.position);
419
- return positions;
420
- }
421
- /**
422
- * Load the saved data with the method save()
423
- * If the player was on a map, it repositions the player on the map.
424
- *
425
- * ```ts
426
- * const json = player.save()
427
- * player.load(json)
428
- * ```
429
- *
430
- * @title Load progress
431
- * @method player.load(json)
432
- * @param {string} json The JSON sent by the method save()
433
- * @returns {Promise<boolean | RpgMap | null>}
434
- * @memberof Player
435
- */
436
- async load(json) {
437
- this._dataLoading = true;
438
- if (isString(json))
439
- json = JSON.parse(json);
440
- const getData = (id) => new (this.databaseById(id))();
441
- for (let key in json) {
442
- const val = json[key];
443
- if (Utils.isObject(val) && val.hasOwnProperty('0')) {
444
- json[key] = Object.values(val);
445
- }
446
- }
447
- const items = {};
448
- if (json.items) {
449
- for (let it of json.items) {
450
- items[it.item.id] = getData(it.item.id);
451
- }
452
- json.items = json.items.map(it => ({ nb: it.nb, item: items[it.item.id] }));
453
- if (Array.isArray(json.equipments)) {
454
- json.equipments = json.equipments.map(it => {
455
- items[it.id].equipped = true;
456
- return items[it.id];
457
- });
458
- }
459
- }
460
- if (json.states)
461
- json.states = json.states.map(state => getData(state.id));
462
- if (json.skills)
463
- json.skills = json.skills.map(skill => getData(skill.id));
464
- if (json.variables)
465
- json.variables = new Map(json.variables);
466
- merge(this, json);
467
- this.position = json.position;
468
- if (json.map) {
469
- this.map = '';
470
- const map = await this.changeMap(json.map, json.tmpPositions || json.position);
471
- this._dataLoading = false;
472
- return map;
473
- }
474
- this._dataLoading = false;
475
- return null;
476
- }
477
- /**
478
- * Returns a JSON with all the data to keep in memory. Then use the `load()` method to load the data
479
- *
480
- * You can also use the JSON.stringify
481
- *
482
- * ```ts
483
- * const json = player.save() // or JSON.stringify(player)
484
- * player.load(json)
485
- * ```
486
- *
487
- * @title Save progress
488
- * @method player.save()
489
- * @returns {string}
490
- * @memberof Player
491
- */
492
- save() {
493
- return JSON.stringify(this);
494
- }
495
- // TODO
496
- toObject() {
497
- return {
498
- direction: this.direction,
499
- id: this.id,
500
- canMove: this.canMove,
501
- position: {
502
- x: this.position.x,
503
- y: this.position.y,
504
- z: this.position.z
505
- },
506
- hitbox: {
507
- width: this.wHitbox,
508
- height: this.hHitbox
509
- },
510
- map: this.map,
511
- pendingMove: this.pendingMove,
512
- speed: this.speed
513
- };
514
- }
515
- toJSON() {
516
- const { permanentObject } = Room.compileSchema(this.schema);
517
- const snapshot = Room.extractObjectOfRoom(this, permanentObject);
518
- snapshot.variables = [...this.variables];
519
- return snapshot;
520
- }
521
- /**
522
- * Run the change detection cycle. Normally, as soon as a hook is called in a class, the cycle is started. But you can start it manually
523
- * The method calls the `onChanges` method on events and synchronizes all map data with the client.
524
-
525
- * @title Run Sync Changes
526
- * @method player.syncChanges()
527
- * @returns {void}
528
- * @memberof Player
529
- */
530
- syncChanges() {
531
- this._eventChanges();
532
- }
533
- databaseById(id) {
534
- const data = this.server.database[id];
535
- if (!data)
536
- throw new Error(`The ID=${id} data is not found in the database. Add the data in the property "database" of @RpgServer decorator.`);
537
- return data;
538
- }
539
- /**
540
- * Retrieves data from the current map
541
- *
542
- * returns null if the player is not assigned to a map
543
- *
544
- * @title Get Current Map
545
- * @method player.getCurrentMap()
546
- * @returns {RpgMap | null}
547
- * @memberof Player
548
- */
549
- getCurrentMap() {
550
- return this._getMap(this.map);
551
- }
552
- loadScene(name, data) {
553
- this.emit(SocketEvents.LoadScene, {
554
- name,
555
- data
556
- });
557
- }
558
- emitSceneMap() {
559
- const mapInstance = this.getCurrentMap();
560
- if (!mapInstance) {
561
- throw '[Emit] The player is not assigned to any map';
562
- }
563
- let { data: serializeMap } = Object.assign({}, mapInstance);
564
- delete serializeMap.shapes;
565
- delete serializeMap.events;
566
- delete serializeMap._events;
567
- for (let layer of serializeMap.layers) {
568
- delete layer.map;
569
- }
570
- this.loadScene('map', {
571
- id: mapInstance.id,
572
- sounds: mapInstance.sounds,
573
- ...serializeMap
574
- });
575
- }
576
- gameReload() {
577
- this.emit(SocketEvents.GameReload);
578
- }
579
- changeServer(url, port) {
580
- this.emit(SocketEvents.ChangeServer, {
581
- url,
582
- port
583
- });
584
- }
585
- _getMap(id) {
586
- return RpgCommonMap.buffer.get(id);
587
- }
588
- /**
589
- * Calls the showAnimation() method on the client side to display an animation on the player
590
- * You must remember to create the spritesheet beforehand
591
- *
592
- * For this type of spritesheet:
593
- *
594
- * ```ts
595
- * @Spritesheet({
596
- * id: 'fire',
597
- * image: require('')
598
- * textures: {
599
- * default: {
600
- * animations: [
601
- *
602
- * ]
603
- * }
604
- * }
605
- * })
606
- * export class FireAnimation {}
607
- * ```
608
- *
609
- * Here is the call of the method:
610
- *
611
- * ```ts
612
- * player.showAnimation('fire', 'default')
613
- * ```
614
- *
615
- * If you don't want to put an animation on top of the event but replace the event graphic with another one, set true as last parameter.
616
- * This is useful, if for example, you want to make an animated character (sword stroke when pressing a key)
617
- * When the animation is finished, the original graphic is displayed again
618
- *
619
- *
620
- * ```ts
621
- * player.showAnimation('sword_stroke', 'default', true)
622
- * ```
623
- *
624
- * Since version 3.0.0-rc, you can define several graphic elements. This allows you to animate them all at once
625
- *
626
- * ```ts
627
- * player.showAnimation(['body', 'sword_stroke'], 'default', true)
628
- * ```
629
- *
630
- * ::: tip
631
- * For this to work, the animations must have been previously defined in `setGraphic`.
632
- * :::
633
- *
634
- * @title Show Animation
635
- * @method player.showAnimation(graphic,animationName,replaceGraphic=false)
636
- * @param {string | string[]} graphic spritesheet identifier
637
- * @param {string} animationName Name of the animation in the spritesheet
638
- * @param {boolean} [replaceGraphic] Replace the event graphic with the animation. After the end of the animation, the original graphic is reapplied
639
- * @returns {void}
640
- * @memberof Player
641
- */
642
- showAnimation(graphic, animationName, replaceGraphic = false) {
643
- this.emitToMap('callMethod', {
644
- name: SocketMethods.ShowAnimation,
645
- params: [graphic, animationName, replaceGraphic]
646
- });
647
- }
648
- /**
649
- * TODO:
650
- * 1. It is necessary, on the client side, to make the character move even if controlled by someone else (problem: same playerId so, one will not move because of the client side prediction. Solution: create a new Id ? like session Id ?
651
- * 2. You would need several sockets per character. If the character changes map or changes server, all players controlling the character must be able to see it
652
- * 3. If the player regains control, what happens, do we return to the previous map?
653
- * 4. If it's an event, you must be able to get the event by id in GameEngine
654
- */
655
- takePossessionOf(otherPlayer) {
656
- this.otherPossessedPlayer = otherPlayer;
657
- this._socket.emit('playerJoined', { playerId: otherPlayer.id, session: this.session });
658
- this.cameraFollow(otherPlayer);
659
- }
660
- /**
661
- * Sends the client which event or player the camera should follow. You can set options to perform a motion animation
662
- *
663
- * @title Camera Follow
664
- * @method player.cameraFollow(otherPlayer,options)
665
- * @param {RpgPlayer | RpgEvent} otherPlayer
666
- * @param {options} options
667
- * @param {object | boolean} [options.smoothMove] - animate. Set a boolean to use default parameters
668
- * @param {number} [options.smoothMove.time=1000] - time to animate
669
- * @param {string} [options.smoothMove.ease=linear] - easing to use. Go to https://easings.net to get function name
670
- * @returns {void}
671
- * @since 3.1.0
672
- * @memberof Player
673
- */
674
- cameraFollow(otherPlayer, options = {}) {
675
- if (otherPlayer.id == this.id) {
676
- this.followingId = null;
677
- }
678
- else {
679
- this.followingId = otherPlayer.id;
680
- }
681
- this.emit(SocketEvents.CallMethod, {
682
- objectId: this.playerId,
683
- name: SocketMethods.CameraFollow,
684
- params: [otherPlayer.id, options]
685
- });
686
- }
687
- /**
688
- * Emit data to clients with socket
689
- *
690
- * @title Emit to client
691
- * @method player.emit(key,value)
692
- * @param {string} key
693
- * @param {any} value
694
- * @returns {void}
695
- * @memberof Player
696
- */
697
- emit(key, value) {
698
- if (this._socket)
699
- this._socket.emit(key, value);
700
- }
701
- /**
702
- * Listen to the data (socket) sent by the client
703
- *
704
- * @title Listen to data from the client
705
- * @method player.on(key,cb)
706
- * @param {string} key
707
- * @param {function} cb
708
- * @returns {void}
709
- * @memberof Player
710
- */
711
- on(key, cb) {
712
- if (this._socket)
713
- this._socket.on(key, cb);
714
- }
715
- /**
716
- * Adds a one-time listener function for the event named eventName
717
- *
718
- * @title Listen one-time to data from the client
719
- * @method player.once(key,cb)
720
- * @since 3.0.0-beta.5
721
- * @param {string} key
722
- * @param {function} cb
723
- * @returns {void}
724
- * @memberof Player
725
- */
726
- once(key, cb) {
727
- if (this._socket)
728
- this._socket.once(key, cb);
729
- }
730
- /**
731
- * Removes all listeners of the specified eventName.
732
- *
733
- * @title Removes all listeners of the client
734
- * @method player.off(key)
735
- * @since 3.0.0-beta.5
736
- * @param {string} key
737
- * @returns {void}
738
- * @memberof Player
739
- */
740
- off(key) {
741
- if (this._socket)
742
- this._socket.removeAllListeners(key);
743
- }
744
- disconnect() {
745
- if (this._socket)
746
- this._socket.disconnect();
747
- }
748
- emitToMap(key, value) {
749
- const map = this.getCurrentMap();
750
- if (map) {
751
- map.$setCurrentState(`users.${this.id}.${key}`, value);
752
- }
753
- }
754
- execMethodSync(methodName, methodData = [], target) {
755
- let ret;
756
- if (target && target[methodName]) {
757
- ret = target[methodName](...methodData);
758
- }
759
- return ret;
760
- }
761
- async execMethod(methodName, methodData = [], target) {
762
- const ignoreIfDataLoading = ['onLevelUp', 'onDead'];
763
- if (ignoreIfDataLoading.includes(methodName) && this._dataLoading) {
764
- return;
765
- }
766
- let ret;
767
- if (target && target[methodName]) {
768
- ret = target[methodName](...methodData);
769
- if (isPromise(ret))
770
- await ret;
771
- }
772
- else {
773
- ret = await RpgPlugin.emit(`Server.${methodName}`, [this, ...methodData], true);
774
- }
775
- this.syncChanges();
776
- return ret;
777
- }
778
- _triggerHook(name, val) {
779
- if (this[name])
780
- this[name](val);
781
- this.emit('Player.' + name, val);
782
- }
783
- // @internal
784
- /**
785
- * Allows you to manually update a status in the rooms that will then be sent to the customer.
786
- * @param path
787
- * @example
788
- * ```ts
789
- * player.changeRoomState('hp')
790
- * ```
791
- */
792
- changeRoomState(path) {
793
- this.world?.forEachUserRooms(this.id, (room) => {
794
- room.$setCurrentState(`users.${this.id}.${path}`);
795
- });
796
- }
797
- _eventChanges() {
798
- if (!this._getMap(this.map))
799
- return;
800
- const { events } = this._getMap(this.map);
801
- const arrayEvents = [...Object.values(this.events), ...Object.values(events)];
802
- for (let event of arrayEvents) {
803
- if (event.onChanges)
804
- event.onChanges(this);
805
- }
806
- }
807
- /**
808
- * Allows to play a sound, heard only by the player or by the players of the map
809
- *
810
- * Here is a sound, client side:
811
- *
812
- * ```ts
813
- * import { Sound } from '@rpgjs/client'
814
- * @Sound({
815
- * id: 'town-music',
816
- * sound: require('./sound/town.ogg')
817
- * })
818
- * export class TownMusic {}
819
- * ```
820
- *
821
- * Here is the call of the method, server side:
822
- *
823
- * ```ts
824
- * player.playSound('town-music')
825
- * ```
826
- *
827
- * If you want everyone to listen to the sound on the map:
828
- *
829
- * ```ts
830
- * player.playSound('town-music', true)
831
- * ```
832
- *
833
- * @title Play Sound
834
- * @method player.playSound(soundId,allMap=false)
835
- * @param {string} soundId Sound identifier, defined on the client side
836
- * @param {boolean} [forEveryone=false] Indicate if the sound is heard by the players on the map
837
- * @since 3.0.0-alpha.9
838
- * @returns {void}
839
- * @memberof Player
840
- */
841
- playSound(soundId, forEveryone = false) {
842
- const obj = {
843
- objectId: this.playerId,
844
- name: SocketMethods.PlaySound,
845
- params: [soundId]
846
- };
847
- if (!forEveryone) {
848
- this.emit(SocketEvents.CallMethod, obj);
849
- return;
850
- }
851
- this.emitToMap(SocketEvents.CallMethod, obj);
852
- }
853
- }
854
- RpgPlayer.schemas = {
855
- ...playerSchemas,
856
- events: [playerSchemas]
857
- };
858
- export { RpgPlayer };
859
- applyMixins(RpgPlayer, [
860
- EventManager,
861
- ItemManager,
862
- GoldManager,
863
- StateManager,
864
- SkillManager,
865
- ParameterManager,
866
- EffectManager,
867
- ClassManager,
868
- ElementManager,
869
- GuiManager,
870
- VariableManager,
871
- MoveManager,
872
- BattleManager,
873
- ComponentManager
874
- ]);
875
- export class RpgEvent extends RpgPlayer {
876
- constructor(playerId) {
877
- super(playerId);
878
- this.type = 'event';
879
- this.properties = {};
880
- this.playerRelated = null;
881
- }
882
- async execMethod(methodName, methodData = []) {
883
- if (!this[methodName]) {
884
- return;
885
- }
886
- const ret = this[methodName](...methodData);
887
- return ret;
888
- }
889
- changeRoomState(path) {
890
- const room = this.getCurrentMap();
891
- if (room) {
892
- room.$setCurrentState(`events.${this.id}.${path}`);
893
- }
894
- }
895
- /**
896
- * Deletes the event from the map (in shared or scenario mode)
897
- *
898
- * @title Remove
899
- * @since 4.0.0
900
- * @method event.remove()
901
- * @returns {boolean} true if the event has been removed. If false, the event is not on the map
902
- * @memberof RpgEvent
903
- */
904
- remove() {
905
- let bool = false;
906
- if (this.playerRelated)
907
- bool = this.playerRelated.removeEvent(this.id);
908
- const map = this.getCurrentMap();
909
- if (map) {
910
- bool = map.removeEvent(this.id);
911
- }
912
- return bool;
913
- }
914
- emitToMap(key, value) {
915
- const map = this.getCurrentMap();
916
- if (map) {
917
- const eventPath = `events.${this.id}.${key}`;
918
- if (this.playerRelated) {
919
- map.$setCurrentState(`users.${this.playerRelated.id}.${eventPath}`, value);
920
- }
921
- else {
922
- map.$setCurrentState(eventPath, value);
923
- }
924
- }
925
- }
926
- }
927
- //# sourceMappingURL=Player.js.map