@rpgjs/client 3.3.2 → 4.0.0-beta.10

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 (165) hide show
  1. package/LICENSE +19 -0
  2. package/lib/Components/AbstractComponent.d.ts +3 -2
  3. package/lib/Components/AbstractComponent.js +17 -28
  4. package/lib/Components/AbstractComponent.js.map +1 -1
  5. package/lib/Components/BarComponent.d.ts +2 -1
  6. package/lib/Components/BarComponent.js +32 -33
  7. package/lib/Components/BarComponent.js.map +1 -1
  8. package/lib/Components/Component.d.ts +3 -3
  9. package/lib/Components/Component.js +84 -96
  10. package/lib/Components/Component.js.map +1 -1
  11. package/lib/Components/DebugComponent.d.ts +2 -1
  12. package/lib/Components/DebugComponent.js +9 -10
  13. package/lib/Components/DebugComponent.js.map +1 -1
  14. package/lib/Components/ImageComponent.d.ts +2 -1
  15. package/lib/Components/ImageComponent.js +5 -7
  16. package/lib/Components/ImageComponent.js.map +1 -1
  17. package/lib/Components/ShapeComponent.d.ts +2 -1
  18. package/lib/Components/ShapeComponent.js +14 -14
  19. package/lib/Components/ShapeComponent.js.map +1 -1
  20. package/lib/Components/TextComponent.d.ts +3 -2
  21. package/lib/Components/TextComponent.js +10 -9
  22. package/lib/Components/TextComponent.js.map +1 -1
  23. package/lib/Components/TileComponent.d.ts +2 -1
  24. package/lib/Components/TileComponent.js +12 -16
  25. package/lib/Components/TileComponent.js.map +1 -1
  26. package/lib/Effects/Animation.d.ts +10 -9
  27. package/lib/Effects/Animation.js +36 -36
  28. package/lib/Effects/Animation.js.map +1 -1
  29. package/lib/Effects/AnimationCharacter.js +2 -5
  30. package/lib/Effects/AnimationCharacter.js.map +1 -1
  31. package/lib/Effects/Spinner.d.ts +3 -2
  32. package/lib/Effects/Spinner.js +2 -5
  33. package/lib/Effects/Spinner.js.map +1 -1
  34. package/lib/Effects/Timeline.d.ts +1 -1
  35. package/lib/Effects/Timeline.js +6 -10
  36. package/lib/Effects/Timeline.js.map +1 -1
  37. package/lib/Effects/TransitionScene.d.ts +2 -1
  38. package/lib/Effects/TransitionScene.js +3 -7
  39. package/lib/Effects/TransitionScene.js.map +1 -1
  40. package/lib/GameEngine.js +51 -26
  41. package/lib/GameEngine.js.map +1 -1
  42. package/lib/Interfaces/Character.js +1 -2
  43. package/lib/Interfaces/Scene.js +1 -2
  44. package/lib/KeyboardControls.d.ts +2 -1
  45. package/lib/KeyboardControls.js +47 -45
  46. package/lib/KeyboardControls.js.map +1 -1
  47. package/lib/Logger.js +1 -5
  48. package/lib/Logger.js.map +1 -1
  49. package/lib/Presets/AnimationSpritesheet.js +9 -13
  50. package/lib/Presets/AnimationSpritesheet.js.map +1 -1
  51. package/lib/Presets/Scene.js +2 -5
  52. package/lib/Presets/Scene.js.map +1 -1
  53. package/lib/Renderer.d.ts +5 -3
  54. package/lib/Renderer.js +68 -60
  55. package/lib/Renderer.js.map +1 -1
  56. package/lib/Resources.js +1 -5
  57. package/lib/Resources.js.map +1 -1
  58. package/lib/RpgClient.d.ts +3 -2
  59. package/lib/RpgClient.js +1 -2
  60. package/lib/RpgClientEngine.d.ts +3 -1
  61. package/lib/RpgClientEngine.js +194 -176
  62. package/lib/RpgClientEngine.js.map +1 -1
  63. package/lib/RpgGui.js +40 -41
  64. package/lib/RpgGui.js.map +1 -1
  65. package/lib/Scene/EventLayer.d.ts +4 -0
  66. package/lib/Scene/EventLayer.js +8 -0
  67. package/lib/Scene/EventLayer.js.map +1 -0
  68. package/lib/Scene/Map.d.ts +17 -3
  69. package/lib/Scene/Map.js +157 -113
  70. package/lib/Scene/Map.js.map +1 -1
  71. package/lib/Scene/Scene.d.ts +3 -3
  72. package/lib/Scene/Scene.js +24 -20
  73. package/lib/Scene/Scene.js.map +1 -1
  74. package/lib/Scene/SceneData.js +1 -5
  75. package/lib/Scene/SceneData.js.map +1 -1
  76. package/lib/Sound/RpgSound.js +8 -11
  77. package/lib/Sound/RpgSound.js.map +1 -1
  78. package/lib/Sound/Sound.d.ts +2 -2
  79. package/lib/Sound/Sound.js +1 -5
  80. package/lib/Sound/Sound.js.map +1 -1
  81. package/lib/Sound/Sounds.js +4 -8
  82. package/lib/Sound/Sounds.js.map +1 -1
  83. package/lib/Sprite/Character.d.ts +3 -2
  84. package/lib/Sprite/Character.js +15 -16
  85. package/lib/Sprite/Character.js.map +1 -1
  86. package/lib/Sprite/Player.js +2 -9
  87. package/lib/Sprite/Player.js.map +1 -1
  88. package/lib/Sprite/Spritesheet.d.ts +4 -4
  89. package/lib/Sprite/Spritesheet.js +1 -5
  90. package/lib/Sprite/Spritesheet.js.map +1 -1
  91. package/lib/Sprite/Spritesheets.js +4 -8
  92. package/lib/Sprite/Spritesheets.js.map +1 -1
  93. package/lib/Tilemap/CommonLayer.d.ts +2 -1
  94. package/lib/Tilemap/CommonLayer.js +7 -11
  95. package/lib/Tilemap/CommonLayer.js.map +1 -1
  96. package/lib/Tilemap/ImageLayer.js +5 -7
  97. package/lib/Tilemap/ImageLayer.js.map +1 -1
  98. package/lib/Tilemap/Tile.d.ts +5 -5
  99. package/lib/Tilemap/Tile.js +19 -21
  100. package/lib/Tilemap/Tile.js.map +1 -1
  101. package/lib/Tilemap/TileLayer.d.ts +0 -7
  102. package/lib/Tilemap/TileLayer.js +27 -29
  103. package/lib/Tilemap/TileLayer.js.map +1 -1
  104. package/lib/Tilemap/TileSet.d.ts +2 -1
  105. package/lib/Tilemap/TileSet.js +9 -12
  106. package/lib/Tilemap/TileSet.js.map +1 -1
  107. package/lib/Tilemap/index.d.ts +3 -11
  108. package/lib/Tilemap/index.js +22 -61
  109. package/lib/Tilemap/index.js.map +1 -1
  110. package/lib/clientEntryPoint.d.ts +1 -0
  111. package/lib/clientEntryPoint.js +26 -28
  112. package/lib/clientEntryPoint.js.map +1 -1
  113. package/lib/index.js +21 -75
  114. package/lib/index.js.map +1 -1
  115. package/package.json +19 -19
  116. package/src/Components/AbstractComponent.ts +120 -0
  117. package/src/Components/BarComponent.ts +179 -0
  118. package/src/Components/Component.ts +506 -0
  119. package/src/Components/DebugComponent.ts +36 -0
  120. package/src/Components/ImageComponent.ts +30 -0
  121. package/src/Components/ShapeComponent.ts +64 -0
  122. package/src/Components/TextComponent.ts +33 -0
  123. package/src/Components/TileComponent.ts +43 -0
  124. package/src/Effects/Animation.ts +297 -0
  125. package/src/Effects/AnimationCharacter.ts +7 -0
  126. package/src/Effects/Spinner.ts +19 -0
  127. package/src/Effects/Timeline.ts +294 -0
  128. package/src/Effects/TransitionScene.ts +57 -0
  129. package/src/GameEngine.ts +284 -0
  130. package/src/Interfaces/Character.ts +7 -0
  131. package/src/Interfaces/Scene.ts +9 -0
  132. package/src/KeyboardControls.ts +552 -0
  133. package/src/Logger.ts +3 -0
  134. package/src/Presets/AnimationSpritesheet.ts +36 -0
  135. package/src/Presets/Scene.ts +3 -0
  136. package/src/Renderer.ts +263 -0
  137. package/src/Resources.ts +40 -0
  138. package/src/RpgClient.ts +333 -0
  139. package/src/RpgClientEngine.ts +712 -0
  140. package/src/RpgGui.ts +553 -0
  141. package/src/RpgGuiCompiled.ts +43 -0
  142. package/src/Scene/EventLayer.ts +9 -0
  143. package/src/Scene/Map.ts +393 -0
  144. package/src/Scene/Scene.ts +270 -0
  145. package/src/Scene/SceneData.ts +13 -0
  146. package/src/Sound/RpgSound.ts +50 -0
  147. package/src/Sound/Sound.ts +91 -0
  148. package/src/Sound/Sounds.ts +7 -0
  149. package/src/Sprite/Character.ts +149 -0
  150. package/src/Sprite/Player.ts +3 -0
  151. package/src/Sprite/Spritesheet.ts +392 -0
  152. package/src/Sprite/Spritesheets.ts +8 -0
  153. package/src/Tilemap/CommonLayer.ts +20 -0
  154. package/src/Tilemap/ImageLayer.ts +20 -0
  155. package/src/Tilemap/Tile.ts +80 -0
  156. package/src/Tilemap/TileLayer.ts +142 -0
  157. package/src/Tilemap/TileSet.ts +40 -0
  158. package/src/Tilemap/index.ts +173 -0
  159. package/src/clientEntryPoint.ts +143 -0
  160. package/src/index.ts +25 -0
  161. package/src/types/howler.d.ts +73 -0
  162. package/tsconfig.json +30 -0
  163. package/lib/Components/ColorComponent.d.ts +0 -9
  164. package/lib/Components/ColorComponent.js +0 -18
  165. package/lib/Components/ColorComponent.js.map +0 -1
@@ -1,36 +1,26 @@
1
- "use strict";
2
1
  var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
2
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
3
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
4
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
5
  return c > 3 && r && Object.defineProperty(target, key, r), r;
7
6
  };
8
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
9
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
10
- return new (P || (P = Promise))(function (resolve, reject) {
11
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
12
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
13
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
14
- step((generator = generator.apply(thisArg, _arguments || [])).next());
15
- });
16
- };
17
- Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.RpgClientEngine = void 0;
19
- const KeyboardControls_1 = require("./KeyboardControls");
20
- const Renderer_1 = require("./Renderer");
21
- const Spritesheets_1 = require("./Sprite/Spritesheets");
22
- const Sounds_1 = require("./Sound/Sounds");
23
- const simple_room_client_1 = require("simple-room-client");
24
- const rxjs_1 = require("rxjs");
25
- const ajax_1 = require("rxjs/ajax");
26
- const RpgGui_1 = require("./RpgGui");
27
- const common_1 = require("@rpgjs/common");
28
- const RpgSound_1 = require("./Sound/RpgSound");
29
- const Spritesheet_1 = require("./Sprite/Spritesheet");
30
- const Logger_1 = require("./Logger");
31
- const Sound_1 = require("./Sound/Sound");
32
- const types_1 = require("@rpgjs/types");
33
- class RpgClientEngine {
7
+ import { KeyboardControls } from './KeyboardControls';
8
+ import { RpgRenderer } from './Renderer';
9
+ import { _initSpritesheet, spritesheets } from './Sprite/Spritesheets';
10
+ import { _initSound, sounds } from './Sound/Sounds';
11
+ import { World } from 'simple-room-client';
12
+ import { BehaviorSubject, Subject, lastValueFrom } from 'rxjs';
13
+ import { ajax } from 'rxjs/ajax';
14
+ import { RpgGui } from './RpgGui';
15
+ import { PrebuiltGui, Utils, RpgPlugin, HookClient, RpgCommonMap, Scheduler, } from '@rpgjs/common';
16
+ import { RpgSound } from './Sound/RpgSound';
17
+ import { Spritesheet } from './Sprite/Spritesheet';
18
+ import { log } from './Logger';
19
+ import { Sound } from './Sound/Sound';
20
+ import { PlayerType, SocketEvents, SocketMethods } from '@rpgjs/types';
21
+ import { Assets, utils } from 'pixi.js';
22
+ import * as PIXI from 'pixi.js';
23
+ export class RpgClientEngine {
34
24
  constructor(gameEngine, options) {
35
25
  this.gameEngine = gameEngine;
36
26
  this.options = options;
@@ -42,14 +32,14 @@ class RpgClientEngine {
42
32
  * @memberof RpgClientEngine
43
33
  * */
44
34
  this.globalConfig = {};
45
- this._tick = new rxjs_1.BehaviorSubject({
35
+ this._tick = new BehaviorSubject({
46
36
  timestamp: -1,
47
37
  deltaTime: 0,
48
38
  frame: 0,
49
39
  deltaRatio: 1
50
40
  });
51
- this.keyChange = new rxjs_1.Subject();
52
- this.roomJoin = new rxjs_1.Subject();
41
+ this.keyChange = new Subject();
42
+ this.roomJoin = new Subject();
53
43
  this.hasBeenDisconnected = false;
54
44
  this.serverChanging = false;
55
45
  this.isTeleported = false;
@@ -62,7 +52,7 @@ class RpgClientEngine {
62
52
  this.matchMakerService = null;
63
53
  this.assetsPath = 'assets';
64
54
  this.serverFps = 60;
65
- this.scheduler = new common_1.Scheduler();
55
+ this.scheduler = new Scheduler();
66
56
  /**
67
57
  * Read objects synchronized with the server
68
58
  *
@@ -76,49 +66,58 @@ class RpgClientEngine {
76
66
  * @memberof RpgClientEngine
77
67
  */
78
68
  this.objects = this.gameEngine.objects;
69
+ this.envs = {};
70
+ this.envs = options.envs || {};
79
71
  this.tick.subscribe(({ timestamp, deltaTime }) => {
80
72
  if (timestamp != -1)
81
73
  this.step(timestamp, deltaTime);
82
74
  });
83
75
  }
84
- _init() {
85
- return __awaiter(this, void 0, void 0, function* () {
86
- this.renderer = new Renderer_1.RpgRenderer(this);
87
- const pluginLoadRessource = (hookName, type) => __awaiter(this, void 0, void 0, function* () {
88
- const resource = this.options[type] || [];
89
- this.options[type] = [
90
- ...common_1.Utils.arrayFlat(yield common_1.RpgPlugin.emit(hookName, resource)) || [],
91
- ...resource
92
- ];
93
- });
94
- yield pluginLoadRessource(common_1.HookClient.AddSpriteSheet, 'spritesheets');
95
- yield pluginLoadRessource(common_1.HookClient.AddGui, 'gui');
96
- yield pluginLoadRessource(common_1.HookClient.AddSound, 'sounds');
97
- this.renderer.options = Object.assign({ selector: '#rpg', selectorCanvas: '#canvas', selectorGui: '#gui', canvas: {}, gui: [], spritesheets: [], sounds: [] }, this.options);
98
- this.io = this.options.io;
99
- if (this.options.serverFps)
100
- this.serverFps = this.options.serverFps;
101
- this.globalConfig = this.options.globalConfig;
102
- if (this.globalConfig.assetsPath)
103
- this.assetsPath = this.globalConfig.assetsPath;
104
- this.gameEngine.standalone = this.options.standalone;
105
- this.gameEngine.renderer = this.renderer;
106
- this.gameEngine.clientEngine = this;
107
- this.addSpriteSheet(this.renderer.options.spritesheets);
108
- this.addSound(this.renderer.options.sounds);
109
- if (typeof __RPGJS_PRODUCTION__ != 'undefined' && __RPGJS_PRODUCTION__) {
110
- if ('serviceWorker' in navigator) {
111
- window.addEventListener('load', () => {
112
- navigator.serviceWorker.register('/service-worker.js');
113
- });
114
- }
76
+ async _init() {
77
+ this.renderer = new RpgRenderer(this);
78
+ const pluginLoadRessource = async (hookName, type) => {
79
+ const resource = this.options[type] || [];
80
+ this.options[type] = [
81
+ ...Utils.arrayFlat(await RpgPlugin.emit(hookName, resource)) || [],
82
+ ...resource
83
+ ];
84
+ };
85
+ await pluginLoadRessource(HookClient.AddSpriteSheet, 'spritesheets');
86
+ await pluginLoadRessource(HookClient.AddGui, 'gui');
87
+ await pluginLoadRessource(HookClient.AddSound, 'sounds');
88
+ this.renderer.options = {
89
+ selector: '#rpg',
90
+ selectorCanvas: '#canvas',
91
+ selectorGui: '#gui',
92
+ canvas: {},
93
+ gui: [],
94
+ spritesheets: [],
95
+ sounds: [],
96
+ ...this.options
97
+ };
98
+ this.io = this.options.io;
99
+ if (this.options.serverFps)
100
+ this.serverFps = this.options.serverFps;
101
+ this.globalConfig = this.options.globalConfig;
102
+ if (this.globalConfig.assetsPath)
103
+ this.assetsPath = this.globalConfig.assetsPath;
104
+ this.gameEngine.standalone = this.options.standalone;
105
+ this.gameEngine.renderer = this.renderer;
106
+ this.gameEngine.clientEngine = this;
107
+ this.addSpriteSheet(this.renderer.options.spritesheets);
108
+ this.addSound(this.renderer.options.sounds);
109
+ if (typeof __RPGJS_PRODUCTION__ != 'undefined' && __RPGJS_PRODUCTION__) {
110
+ if ('serviceWorker' in navigator) {
111
+ window.addEventListener('load', () => {
112
+ navigator.serviceWorker.register('/service-worker.js');
113
+ });
115
114
  }
116
- this.controls = new KeyboardControls_1.KeyboardControls(this);
117
- });
115
+ }
116
+ this.controls = new KeyboardControls(this);
118
117
  }
119
118
  addResource(resourceClass, cb) {
120
119
  let array = resourceClass;
121
- if (!common_1.Utils.isArray(resourceClass)) {
120
+ if (!Utils.isArray(resourceClass)) {
122
121
  array = [resourceClass];
123
122
  }
124
123
  cb(array, this);
@@ -144,44 +143,49 @@ class RpgClientEngine {
144
143
  addSpriteSheet(spritesheetClass, id) {
145
144
  if (typeof spritesheetClass === 'string') {
146
145
  if (!id) {
147
- throw (0, Logger_1.log)('Please, specify the resource ID (second parameter)');
146
+ throw log('Please, specify the resource ID (second parameter)');
148
147
  }
149
148
  let AutoSpritesheet = class AutoSpritesheet {
150
149
  };
151
150
  AutoSpritesheet = __decorate([
152
- (0, Spritesheet_1.Spritesheet)({
151
+ Spritesheet({
153
152
  id,
154
153
  image: this.getResourceUrl(spritesheetClass)
155
154
  })
156
155
  ], AutoSpritesheet);
157
156
  spritesheetClass = AutoSpritesheet;
158
157
  }
159
- this.addResource(spritesheetClass, Spritesheets_1._initSpritesheet);
158
+ this.addResource(spritesheetClass, _initSpritesheet);
160
159
  return spritesheetClass;
161
160
  }
162
161
  addSound(soundClass, id) {
163
162
  if (typeof soundClass === 'string') {
164
163
  if (!id) {
165
- throw (0, Logger_1.log)('Please, specify the resource ID (second parameter)');
164
+ throw log('Please, specify the resource ID (second parameter)');
166
165
  }
167
166
  let AutoSound = class AutoSound {
168
167
  };
169
168
  AutoSound = __decorate([
170
- (0, Sound_1.Sound)({
169
+ Sound({
171
170
  id,
172
171
  sound: this.getResourceUrl(soundClass)
173
172
  })
174
173
  ], AutoSound);
175
174
  soundClass = AutoSound;
176
175
  }
177
- this.addResource(soundClass, Sounds_1._initSound);
176
+ this.addResource(soundClass, _initSound);
178
177
  return soundClass;
179
178
  }
180
179
  getResourceUrl(source) {
181
180
  if (source.startsWith('data:')) {
182
181
  return source;
183
182
  }
184
- return this.assetsPath + '/' + common_1.Utils.basename(source);
183
+ // @ts-ignore
184
+ const staticDir = this.envs.VITE_BUILT;
185
+ if (staticDir) {
186
+ return this.assetsPath + '/' + Utils.basename(source);
187
+ }
188
+ return source;
185
189
  }
186
190
  /**
187
191
  * Starts the client side and connects to the server
@@ -191,40 +195,39 @@ class RpgClientEngine {
191
195
  * @returns {Promise< void >}
192
196
  * @memberof RpgClientEngine
193
197
  */
194
- start(options = {
198
+ async start(options = {
195
199
  renderLoop: true
196
200
  }) {
197
- return __awaiter(this, void 0, void 0, function* () {
198
- PIXI.utils.skipHello();
199
- yield this._init();
200
- yield this.renderer.init();
201
- const { maxFps } = this.options;
202
- if (options.renderLoop) {
203
- this.scheduler.start({
204
- maxFps
205
- });
206
- // The processing is outside the rendering loop because if the FPS are lower (or higher) then the sending to the server would be slower or faster. Here it is constant
207
- setInterval(() => {
208
- this.processInput();
209
- }, common_1.Utils.fps2ms(this.serverFps));
210
- }
211
- const ret = yield common_1.RpgPlugin.emit(common_1.HookClient.Start, this);
212
- this.matchMakerService = this.options.globalConfig.matchMakerService;
213
- const hasFalseValue = ret.findIndex(el => el === false) != -1;
214
- if (!hasFalseValue) {
215
- let serverUri = {};
216
- if (this.matchMakerService) {
217
- if (common_1.Utils.isFunction(this.matchMakerService)) {
218
- serverUri = this.matchMakerService();
219
- }
220
- else {
221
- // todo: change toPromise (RXJS v7+)
222
- serverUri = yield ajax_1.ajax.getJSON(this.matchMakerService).toPromise();
223
- }
201
+ await this._init();
202
+ await this.renderer.init();
203
+ const { maxFps } = this.options;
204
+ if (options.renderLoop) {
205
+ this.scheduler.start({
206
+ maxFps
207
+ });
208
+ // The processing is outside the rendering loop because if the FPS are lower (or higher) then the sending to the server would be slower or faster. Here it is constant
209
+ setInterval(() => {
210
+ this.processInput();
211
+ }, Utils.fps2ms(this.serverFps));
212
+ }
213
+ const ret = await RpgPlugin.emit(HookClient.Start, this);
214
+ this.matchMakerService = this.options.globalConfig.matchMakerService;
215
+ const hasFalseValue = ret.findIndex(el => el === false) != -1;
216
+ if (!hasFalseValue) {
217
+ let serverUri = {};
218
+ if (this.matchMakerService) {
219
+ if (Utils.isFunction(this.matchMakerService)) {
220
+ serverUri = this.matchMakerService();
221
+ }
222
+ else {
223
+ serverUri = await lastValueFrom(ajax.getJSON(this.matchMakerService));
224
224
  }
225
- this.connection(serverUri.url ? serverUri.url + ':' + serverUri.port : undefined);
226
225
  }
227
- });
226
+ // @ts-ignore
227
+ const envUrl = this.envs.VITE_SERVER_URL;
228
+ this.connection(serverUri.url ? serverUri.url + ':' + serverUri.port :
229
+ envUrl ? envUrl : undefined);
230
+ }
228
231
  }
229
232
  /**
230
233
  * Display the next frame. Useful for unit tests
@@ -238,18 +241,16 @@ class RpgClientEngine {
238
241
  nextFrame(timestamp) {
239
242
  this.scheduler.nextTick(timestamp);
240
243
  }
241
- sendInput(actionName) {
242
- return __awaiter(this, void 0, void 0, function* () {
243
- const player = this.player;
244
- if (!player)
245
- return;
246
- if (player.canMove) {
247
- player.pendingMove.push({
248
- input: actionName,
249
- frame: this.scheduler.frame
250
- });
251
- }
252
- });
244
+ async sendInput(actionName) {
245
+ const player = this.player;
246
+ if (!player)
247
+ return;
248
+ if (player.canMove) {
249
+ player.pendingMove.push({
250
+ input: actionName,
251
+ frame: this.scheduler.frame
252
+ });
253
+ }
253
254
  }
254
255
  get player() {
255
256
  return this.gameEngine.world.getObject(this.gameEngine.playerId);
@@ -274,34 +275,30 @@ class RpgClientEngine {
274
275
  });
275
276
  garbage = [];
276
277
  }
277
- step(t, dt) {
278
- return __awaiter(this, void 0, void 0, function* () {
279
- common_1.RpgPlugin.emit(common_1.HookClient.Step, [this, t, dt], true);
280
- });
278
+ async step(t, dt) {
279
+ RpgPlugin.emit(HookClient.Step, [this, t, dt], true);
281
280
  }
282
- processInput() {
283
- return __awaiter(this, void 0, void 0, function* () {
284
- const player = this.player;
285
- this.controls.preStep();
286
- if (player) {
287
- if (player.pendingMove.length > 0) {
288
- const { inputs: inputEvent } = yield this.gameEngine.processInput(this.gameEngine.playerId, this.controls.options);
289
- if (inputEvent.length == 0)
290
- return;
291
- const frame = Date.now();
292
- this.clientFrames.set(frame, {
293
- data: player.position.copy(),
294
- time: frame
295
- });
296
- if (this.socket) {
297
- this.socket.emit('move', { input: inputEvent, frame });
298
- }
299
- common_1.RpgPlugin.emit(common_1.HookClient.SendInput, [this, inputEvent], true);
281
+ async processInput() {
282
+ const player = this.player;
283
+ this.controls.preStep();
284
+ if (player) {
285
+ if (player.pendingMove.length > 0) {
286
+ const { inputs: inputEvent } = await this.gameEngine.processInput(this.gameEngine.playerId, this.controls.options);
287
+ if (inputEvent.length == 0)
288
+ return;
289
+ const frame = Date.now();
290
+ this.clientFrames.set(frame, {
291
+ data: player.position.copy(),
292
+ time: frame
293
+ });
294
+ if (this.socket) {
295
+ this.socket.emit('move', { input: inputEvent, frame });
300
296
  }
301
- if (player.canMove)
302
- this.serverReconciliation(player);
297
+ RpgPlugin.emit(HookClient.SendInput, [this, inputEvent], true);
303
298
  }
304
- });
299
+ if (player.canMove)
300
+ this.serverReconciliation(player);
301
+ }
305
302
  }
306
303
  /**
307
304
  *Connect to the server
@@ -324,9 +321,9 @@ class RpgClientEngine {
324
321
  this.socket = this.io;
325
322
  }
326
323
  this.socket.on('connect', () => {
327
- if (RpgGui_1.RpgGui.exists(common_1.PrebuiltGui.Disconnect))
328
- RpgGui_1.RpgGui.hide(common_1.PrebuiltGui.Disconnect);
329
- common_1.RpgPlugin.emit(common_1.HookClient.Connected, [this, this.socket], true);
324
+ if (RpgGui.exists(PrebuiltGui.Disconnect))
325
+ RpgGui.hide(PrebuiltGui.Disconnect);
326
+ RpgPlugin.emit(HookClient.Connected, [this, this.socket], true);
330
327
  this.hasBeenDisconnected = false;
331
328
  });
332
329
  this.socket.on('playerJoined', (playerEvent) => {
@@ -334,7 +331,7 @@ class RpgClientEngine {
334
331
  this.session = playerEvent.session;
335
332
  });
336
333
  this.socket.on('connect_error', (err) => {
337
- common_1.RpgPlugin.emit(common_1.HookClient.ConnectedError, [this, err, this.socket], true);
334
+ RpgPlugin.emit(HookClient.ConnectedError, [this, err, this.socket], true);
338
335
  });
339
336
  this.socket.on('preLoadScene', (name) => {
340
337
  if (this.lastScene == name) {
@@ -343,10 +340,13 @@ class RpgClientEngine {
343
340
  this.lastScene = name;
344
341
  this.renderer.transitionScene(name);
345
342
  });
346
- this.socket.on('loadScene', ({ name, data }) => {
343
+ this.socket.on(SocketEvents.GameReload, () => {
344
+ window.location.reload();
345
+ });
346
+ this.socket.on(SocketEvents.LoadScene, ({ name, data }) => {
347
347
  this.renderer.loadScene(name, data);
348
348
  });
349
- this.socket.on('changeServer', ({ url, port }) => {
349
+ this.socket.on(SocketEvents.ChangeServer, ({ url, port }) => {
350
350
  const connection = url + ':' + port;
351
351
  if (this.lastConnection == connection) {
352
352
  return;
@@ -361,30 +361,30 @@ class RpgClientEngine {
361
361
  });
362
362
  this.socket.on('changeTile', ({ tiles, x, y }) => {
363
363
  const scene = this.renderer.getScene();
364
- scene === null || scene === void 0 ? void 0 : scene.changeTile(x, y, tiles);
364
+ scene?.changeTile(x, y, tiles);
365
365
  });
366
- this.socket.on(types_1.SocketEvents.CallMethod, ({ objectId, params, name }) => {
366
+ this.socket.on(SocketEvents.CallMethod, ({ objectId, params, name }) => {
367
367
  const scene = this.renderer.getScene();
368
- const sprite = scene === null || scene === void 0 ? void 0 : scene.getPlayer(objectId);
368
+ const sprite = scene?.getPlayer(objectId);
369
369
  if (!sprite)
370
370
  return;
371
371
  switch (name) {
372
- case types_1.SocketMethods.ShowAnimation:
373
- scene === null || scene === void 0 ? void 0 : scene.showAnimation({
372
+ case SocketMethods.ShowAnimation:
373
+ scene?.showAnimation({
374
374
  attachTo: sprite,
375
375
  graphic: params[0],
376
376
  animationName: params[1],
377
377
  replaceGraphic: params[2]
378
378
  });
379
379
  break;
380
- case types_1.SocketMethods.CameraFollow:
380
+ case SocketMethods.CameraFollow:
381
381
  const [spriteId, options] = params;
382
- scene === null || scene === void 0 ? void 0 : scene.cameraFollowSprite(spriteId, options);
382
+ scene?.cameraFollowSprite(spriteId, options);
383
383
  break;
384
- case types_1.SocketMethods.PlaySound:
385
- RpgSound_1.RpgSound.play(params[0]);
384
+ case SocketMethods.PlaySound:
385
+ RpgSound.play(params[0]);
386
386
  break;
387
- case types_1.SocketMethods.ModeMove:
387
+ case SocketMethods.ModeMove:
388
388
  const player = this.player;
389
389
  const { checkCollision } = params[0];
390
390
  if (player) {
@@ -394,9 +394,9 @@ class RpgClientEngine {
394
394
  }
395
395
  });
396
396
  let lastRoomId = '';
397
- this.subscriptionWorld = simple_room_client_1.World.listen(this.socket)
397
+ this.subscriptionWorld = World.listen(this.socket)
398
398
  .value
399
- .subscribe((val) => __awaiter(this, void 0, void 0, function* () {
399
+ .subscribe(async (val) => {
400
400
  const scene = this.renderer.getScene();
401
401
  if (!val.data) {
402
402
  return;
@@ -413,6 +413,19 @@ class RpgClientEngine {
413
413
  const change = (prop, root = val, localEvent = false) => {
414
414
  const list = root.data[prop];
415
415
  const partial = root.partial[prop];
416
+ const isShape = prop == 'shapes';
417
+ if (!partial) {
418
+ return;
419
+ }
420
+ if (val.resetProps.indexOf(prop) != -1) {
421
+ const objects = isShape ? this.gameEngine.getShapes() : this.gameEngine.getObjects();
422
+ for (let key in objects) {
423
+ const obj = objects[key];
424
+ if (obj) {
425
+ this.gameEngine.removeObjectAndShape(key);
426
+ }
427
+ }
428
+ }
416
429
  for (let key in partial) {
417
430
  const obj = list[key];
418
431
  const paramsChanged = partial ? partial[key] : undefined;
@@ -421,11 +434,10 @@ class RpgClientEngine {
421
434
  }
422
435
  if (!obj)
423
436
  continue;
424
- const isShape = prop == 'shapes';
425
437
  if (!isShape) {
426
438
  obj.type = {
427
- users: types_1.PlayerType.Player,
428
- events: types_1.PlayerType.Event
439
+ users: PlayerType.Player,
440
+ events: PlayerType.Event
429
441
  }[prop];
430
442
  }
431
443
  if (prop == 'users' && this.gameEngine.playerId == key) {
@@ -439,11 +451,12 @@ class RpgClientEngine {
439
451
  data: obj,
440
452
  partial: paramsChanged,
441
453
  time: val.time,
442
- roomId: val.roomId
454
+ roomId: val.roomId,
455
+ resetProps: val.resetProps
443
456
  }, true);
444
457
  }
445
458
  }
446
- if ((partialRoom === null || partialRoom === void 0 ? void 0 : partialRoom.pos) && (partialRoom === null || partialRoom === void 0 ? void 0 : partialRoom.frame) !== undefined) {
459
+ if (partialRoom?.pos && partialRoom?.frame !== undefined) {
447
460
  this.serverFrames.set(partialRoom.frame, {
448
461
  data: partialRoom.pos,
449
462
  time: Date.now()
@@ -470,17 +483,17 @@ class RpgClientEngine {
470
483
  if (scene) {
471
484
  scene.update(val);
472
485
  }
473
- }));
486
+ });
474
487
  this.socket.on('disconnect', (reason) => {
475
488
  if (this.serverChanging) {
476
489
  return;
477
490
  }
478
- if (RpgGui_1.RpgGui.exists(common_1.PrebuiltGui.Disconnect))
479
- RpgGui_1.RpgGui.display(common_1.PrebuiltGui.Disconnect);
480
- common_1.RpgPlugin.emit(common_1.HookClient.Disconnect, [this, reason, this.socket], true);
491
+ if (RpgGui.exists(PrebuiltGui.Disconnect))
492
+ RpgGui.display(PrebuiltGui.Disconnect);
493
+ RpgPlugin.emit(HookClient.Disconnect, [this, reason, this.socket], true);
481
494
  this.hasBeenDisconnected = true;
482
495
  });
483
- RpgGui_1.RpgGui._setSocket(this.socket);
496
+ RpgGui._setSocket(this.socket);
484
497
  if (standalone) {
485
498
  this.socket.connection({
486
499
  auth: {
@@ -491,7 +504,7 @@ class RpgClientEngine {
491
504
  this.serverChanging = false;
492
505
  }
493
506
  get world() {
494
- return simple_room_client_1.World;
507
+ return World;
495
508
  }
496
509
  // shortcuts
497
510
  /**
@@ -549,14 +562,19 @@ class RpgClientEngine {
549
562
  reset() {
550
563
  this.subscriptionWorld.unsubscribe();
551
564
  this.world.reset();
552
- Spritesheets_1.spritesheets.clear();
553
- Sounds_1.sounds.clear();
554
- PIXI.Loader.shared.reset();
555
- PIXI.utils.clearTextureCache();
556
- RpgGui_1.RpgGui.clear();
557
- common_1.RpgCommonMap.bufferClient.clear();
558
- RpgSound_1.RpgSound.clear();
565
+ spritesheets.clear();
566
+ sounds.clear();
567
+ Assets.reset();
568
+ utils.clearTextureCache();
569
+ for (let textureUrl in utils.BaseTextureCache) {
570
+ delete utils.BaseTextureCache[textureUrl];
571
+ }
572
+ for (let textureUrl in utils.TextureCache) {
573
+ delete utils.TextureCache[textureUrl];
574
+ }
575
+ RpgGui.clear();
576
+ RpgCommonMap.bufferClient.clear();
577
+ RpgSound.clear();
559
578
  }
560
579
  }
561
- exports.RpgClientEngine = RpgClientEngine;
562
580
  //# sourceMappingURL=RpgClientEngine.js.map