@urso/core 0.6.3 → 0.6.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@urso/core",
3
- "version": "0.6.3",
3
+ "version": "0.6.4",
4
4
  "description": "HTML5 game engine",
5
5
  "main": "build/js/index.js",
6
6
  "author": "Megbrimef",
@@ -21,12 +21,14 @@ class ComponentsDebugFps {
21
21
  this.frames++;
22
22
 
23
23
  if (currentTime - this.lastUpdateTime < 1000)
24
- return
24
+ return;
25
25
 
26
26
  const fps = Math.round(1000 * this.frames / (currentTime - this.lastUpdateTime));
27
27
  this.lastUpdateTime = currentTime;
28
28
  this.frames = 0;
29
- this._coordsText.text = 'fps:' + fps;
29
+
30
+ const fpsData = Urso.scenes.getFpsData();
31
+ this._coordsText.text = `fps: ${fps}, sceneFps: ${fpsData.fps}, limit: ${fpsData.limit}`;
30
32
  };
31
33
 
32
34
  }
@@ -6,7 +6,11 @@ let ConfigMain = {
6
6
  extendingChain: ['Urso.Core'], //chain that will be set as Urso.Game
7
7
  defaultScene: 'play', //default scene to display
8
8
  useBinPath: false, // use assets from bin directory
9
- useTransport: false // use transport module for connetcting with server
9
+ useTransport: false, // use transport module for connetcting with server
10
+ fps: {
11
+ limit: 60, //max fps limit
12
+ optimizeLowPerformance: true //down to 30 fps if lower 60
13
+ }
10
14
  };
11
15
 
12
16
  module.exports = ConfigMain;
@@ -21,6 +21,22 @@ class ModulesScenesController {
21
21
  this._service.display(name);
22
22
  }
23
23
 
24
+ /**
25
+ * get fps
26
+ * @returns {Number}
27
+ */
28
+ getFps() {
29
+ return this.getInstance('PixiWrapper').getFps();
30
+ }
31
+
32
+ /**
33
+ * get fps data
34
+ * @returns {Object}
35
+ */
36
+ getFpsData() {
37
+ return this.getInstance('PixiWrapper').getFpsData();
38
+ }
39
+
24
40
  /**
25
41
  * pause scene
26
42
  */
@@ -1,3 +1,6 @@
1
+ const NORMAL_FPS_COUNT = 60;
2
+ const LOW_PERFORMANCE_FPS_COUNT = 30;
3
+
1
4
  class ModulesScenesPixiWrapper {
2
5
  constructor() {
3
6
  this.singleton = true;
@@ -13,10 +16,17 @@ class ModulesScenesPixiWrapper {
13
16
  this._loopPaused = false;
14
17
  this._loopLastCall = 0;
15
18
 
16
- this.loop = this.loop.bind(this);
19
+ this._loop = this._loop.bind(this);
17
20
  this.passiveCallIntervalId = null;
18
21
 
19
22
  this._mouseCoords = { x: 0, y: 0 };
23
+
24
+
25
+ this._maxFPSLimit = Urso.config.fps.limit;
26
+ this._lastUpdateTime = 0;
27
+ this._frames = 0;
28
+ this._currentFPS = Urso.config.fps.limit;
29
+ this._lastTimeCheckFPS = 0;
20
30
  }
21
31
 
22
32
  init() {
@@ -35,7 +45,7 @@ class ModulesScenesPixiWrapper {
35
45
  this.interaction = new PIXI.InteractionManager(this.renderer);
36
46
 
37
47
  this._loaderScene = this.getInstance('Model');
38
- this._requestAnimFrame(this.loop);
48
+ this._requestAnimFrame(this._loop);
39
49
 
40
50
  this.getInstance('Resolutions');
41
51
  }
@@ -62,10 +72,112 @@ class ModulesScenesPixiWrapper {
62
72
  resume() {
63
73
  this._loopLastCall = Date.now();
64
74
  this._loopPaused = false;
65
- this.update();
75
+ this._update();
66
76
  PIXI.spine.settings.GLOBAL_AUTO_UPDATE = true;
67
77
  }
68
78
 
79
+ /**
80
+ * resize renderer
81
+ * @param {Number} width
82
+ * @param {Number} height
83
+ */
84
+ resize(width, height) {
85
+ this.renderer.resize(width, height);
86
+ };
87
+
88
+ /**
89
+ * hide canvas
90
+ */
91
+ hideCanvas() {
92
+ this.renderer.view.style.display = 'none';
93
+ }
94
+
95
+ /**
96
+ * show canvas
97
+ */
98
+ showCanvas() {
99
+ this.renderer.view.style.display = '';
100
+ }
101
+
102
+ /**
103
+ * set world scale
104
+ * @param {Number} x
105
+ * @param {Number} y
106
+ */
107
+ setWorldScale(x, y) {
108
+ this.world.scale.x = x;
109
+ this.world.scale.y = y;
110
+ }
111
+
112
+ /**
113
+ * set canvas width
114
+ * @param {Number} val
115
+ */
116
+ setCanvasWidth(val) {
117
+ this.renderer.view.style.width = val + 'px';
118
+ };
119
+
120
+ /**
121
+ * set canvas height
122
+ * @param {Number} val
123
+ */
124
+ setCanvasHeight(val) {
125
+ this.renderer.view.style.height = val + 'px';
126
+ };
127
+
128
+ /**
129
+ * get pixi world (main Container)
130
+ * @returns {Object}
131
+ */
132
+ getPixiWorld() {
133
+ return this.world;
134
+ }
135
+
136
+ /**
137
+ * set new scene
138
+ * @param {Object} model
139
+ */
140
+ setNewScene(model) {
141
+ this._createWorld();
142
+ this.currentScene = model;
143
+ }
144
+
145
+ /**
146
+ * get fps
147
+ * @returns {Number}
148
+ */
149
+ getFps() {
150
+ return this._currentFPS;
151
+ }
152
+
153
+ /**
154
+ * get fps data
155
+ * @returns {Object}
156
+ */
157
+ getFpsData() {
158
+ return {
159
+ fps: this._currentFPS,
160
+ limit: this._maxFPSLimit
161
+ }
162
+ }
163
+
164
+ /**
165
+ * get cached mouse coords
166
+ * @returns {Object}
167
+ */
168
+ getCachedMouseCoords() {
169
+ return this._mouseCoords;
170
+ }
171
+
172
+ /**
173
+ * generateTexture from object
174
+ * @param {Object} obj
175
+ * @returns {Object} - pixi.Texture
176
+ */
177
+ generateTexture(obj) {
178
+ return this.renderer.generateTexture(obj);
179
+ }
180
+
69
181
  _setPixiSettings() {
70
182
  PIXI.settings.SCALE_MODE = PIXI.SCALE_MODES.LINEAR;
71
183
  PIXI.settings.TEXT_RESOLUTION = 1;
@@ -107,19 +219,53 @@ class ModulesScenesPixiWrapper {
107
219
  return deltaTime * 60 / 1000;
108
220
  };
109
221
 
110
- loop() {
222
+ _loop() {
111
223
  if (this._loopStopped)
112
224
  return false;
113
225
 
114
- this._requestAnimFrame(this.loop);
226
+ this._requestAnimFrame(this._loop);
115
227
 
116
- if (!this._loopPaused)
117
- this.update();
228
+ if (!this._loopPaused) {
229
+ if (!this._fpsCheckAllowUpdate())
230
+ return;
231
+
232
+ this._update();
233
+ }
118
234
 
119
235
  return true;
120
236
  };
121
237
 
122
- update() {
238
+ _fpsCheckAllowUpdate() {
239
+ const currentTime = Urso.time.get();
240
+ this._updateCurrentFPS(currentTime);
241
+
242
+ //setup maxFPSLimit
243
+ if (Urso.config.fps.optimizeLowPerformance)
244
+ if (this._currentFPS < NORMAL_FPS_COUNT)
245
+ this._maxFPSLimit = LOW_PERFORMANCE_FPS_COUNT;
246
+ else
247
+ this._maxFPSLimit = Urso.config.fps.limit;
248
+
249
+ //check need update
250
+ if (currentTime - this._lastUpdateTime < ~~(1000 / this._maxFPSLimit))
251
+ return false;
252
+
253
+ this._lastUpdateTime = currentTime;
254
+ return true;
255
+ }
256
+
257
+ _updateCurrentFPS(currentTime) {
258
+ this._frames++;
259
+
260
+ if (currentTime - this._lastTimeCheckFPS < 1000)
261
+ return;
262
+
263
+ this._currentFPS = Math.round(1000 * this._frames / (currentTime - this._lastTimeCheckFPS));
264
+ this._lastTimeCheckFPS = currentTime;
265
+ this._frames = 0;
266
+ }
267
+
268
+ _update() {
123
269
  if (!this.currentScene)
124
270
  return;
125
271
 
@@ -135,41 +281,6 @@ class ModulesScenesPixiWrapper {
135
281
  this.renderer.render(this._root);
136
282
  };
137
283
 
138
- //size
139
- resize(width, height) {
140
- this.renderer.resize(width, height);
141
- };
142
-
143
- hideCanvas() {
144
- this.renderer.view.style.display = 'none';
145
- }
146
-
147
- showCanvas() {
148
- this.renderer.view.style.display = '';
149
- }
150
-
151
- setWorldScale(x, y) {
152
- this.world.scale.x = x;
153
- this.world.scale.y = y;
154
- }
155
-
156
- setCanvasWidth(val) {
157
- this.renderer.view.style.width = val + 'px';
158
- };
159
-
160
- setCanvasHeight(val) {
161
- this.renderer.view.style.height = val + 'px';
162
- };
163
-
164
- getPixiWorld() {
165
- return this.world;
166
- }
167
-
168
- setNewScene(model) {
169
- this._createWorld();
170
- this.currentScene = model;
171
- }
172
-
173
284
  _checkMouse() {
174
285
  let newCoords = this._getMouseCoords();
175
286
 
@@ -180,10 +291,6 @@ class ModulesScenesPixiWrapper {
180
291
  this.emit(Urso.events.MODULES_SCENES_MOUSE_NEW_POSITION, this._mouseCoords);
181
292
  };
182
293
 
183
- getCachedMouseCoords() {
184
- return this._mouseCoords;
185
- }
186
-
187
294
  _getMouseCoords() {
188
295
  const coords = {
189
296
  x: ~~(this.interaction.mouse.global.x / this.world.scale.x),
@@ -204,7 +311,7 @@ class ModulesScenesPixiWrapper {
204
311
  * reserve loop, when browser tab is inactive
205
312
  * @param {Boolean} isVisible
206
313
  */
207
- visibilityChangeHandler(isVisible) {
314
+ _visibilityChangeHandler(isVisible) {
208
315
  if (isVisible) {
209
316
  if (this.passiveCallIntervalId) {
210
317
  clearInterval(this.passiveCallIntervalId);
@@ -215,22 +322,13 @@ class ModulesScenesPixiWrapper {
215
322
 
216
323
  this.passiveCallIntervalId = setInterval(() => {
217
324
  if (!this._loopStopped && !this._loopPaused) {
218
- this.update();
325
+ this._update();
219
326
  }
220
327
  }, 16);
221
328
  }
222
329
 
223
- /**
224
- * generateTexture from object
225
- * @param {Object} obj
226
- * @returns {Object} - pixi.Texture
227
- */
228
- generateTexture(obj) {
229
- return this.renderer.generateTexture(obj);
230
- }
231
-
232
330
  _subscribeOnce() {
233
- this.addListener(Urso.events.EXTRA_BROWSEREVENTS_WINDOW_VISIBILITYCHANGE, this.visibilityChangeHandler.bind(this), true);
331
+ this.addListener(Urso.events.EXTRA_BROWSEREVENTS_WINDOW_VISIBILITYCHANGE, this._visibilityChangeHandler.bind(this), true);
234
332
  }
235
333
  }
236
334