@urso/core 0.5.9 → 0.5.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@urso/core",
3
- "version": "0.5.9",
3
+ "version": "0.5.10",
4
4
  "description": "HTML5 game engine",
5
5
  "main": "build/js/index.js",
6
6
  "author": "Megbrimef",
@@ -1,8 +1,14 @@
1
+ /**
2
+ * adapter Urso to Pixi objects
3
+ */
1
4
  class PropertyAdapter {
2
5
 
3
6
  constructor() {
4
7
  this.singleton = true;
5
8
 
9
+ /**
10
+ * dependencies, functions to handle changing property
11
+ */
6
12
  this._dependencies = {
7
13
  'x': this._updateHorizontal.bind(this),
8
14
  'y': this._updateVertical.bind(this),
@@ -15,10 +21,13 @@ class PropertyAdapter {
15
21
  'width': this._updateHorizontal.bind(this),
16
22
  'height': this._updateVertical.bind(this),
17
23
  'angle': this._updateAngle.bind(this),
18
- 'stretchingType': this.adaptStretchingType.bind(this), //todo check on parent change
19
- 'parent': this.parentChangeHandler.bind(this)
24
+ 'stretchingType': this._adaptStretchingType.bind(this), //todo check on parent change
25
+ 'parent': this._parentChangeHandler.bind(this)
20
26
  };
21
27
 
28
+ /**
29
+ * functions to handle changing parent property
30
+ */
22
31
  this._parentToChildDependencies = {
23
32
  'width': { children: ['width'] },
24
33
  'height': { children: ['height'] },
@@ -27,6 +36,9 @@ class PropertyAdapter {
27
36
  'stretchingType': { children: ['width', 'height'] }
28
37
  };
29
38
 
39
+ /**
40
+ * parent types list
41
+ */
30
42
  this._parentTypes = [
31
43
  Urso.types.objects.COMPONENT,
32
44
  Urso.types.objects.CONTAINER,
@@ -37,6 +49,9 @@ class PropertyAdapter {
37
49
  Urso.types.objects.WORLD
38
50
  ];
39
51
 
52
+ /**
53
+ * types without ahchor list
54
+ */
40
55
  this._typesWithoutAnchor = [
41
56
  Urso.types.objects.CHECKBOX,
42
57
  Urso.types.objects.EMITTER,
@@ -52,29 +67,52 @@ class PropertyAdapter {
52
67
  ];
53
68
  }
54
69
 
70
+ /**
71
+ * check is adaptive property
72
+ * @param {String} property
73
+ * @returns {Boolean}
74
+ */
55
75
  isAdaptiveProperty(property) {
56
76
  return Object.keys(this._dependencies).includes(property);
57
77
  };
58
78
 
79
+ /**
80
+ * property change handler (main function)
81
+ * @param {Object} object
82
+ * @param {String} propertyName
83
+ */
59
84
  propertyChangeHandler(object, propertyName) {
60
85
  this._adaptProperty(object, propertyName);
61
86
  this._adaptChildProperties(object, propertyName);
62
87
  }
63
88
 
89
+ /**
90
+ * adapt property (launch handler changing property)
91
+ * @param {Object} object
92
+ * @param {String} propertyName
93
+ */
64
94
  _adaptProperty(object, propertyName) {
65
95
  if (this._dependencies[propertyName])
66
96
  this._dependencies[propertyName](object);
67
97
  }
68
98
 
99
+ /**
100
+ * main function to calculate x and width
101
+ * @param {Object} object
102
+ */
69
103
  _updateHorizontal(object) {
70
- let x = this._getXAsNumber(object); //adaptX
71
- x += this.adaptAnchorX(object);
72
- x += this.adaptAlignX(object);
104
+ let x = this._getXAsNumber(object);
105
+ x += this._adaptAnchorX(object);
106
+ x += this._adaptAlignX(object);
73
107
  object._baseObject.x = x;
74
108
 
75
- this.adaptWidth(object);
109
+ this._adaptWidth(object);
76
110
  }
77
111
 
112
+ /**
113
+ * main function to calculate angle
114
+ * @param {Object} object
115
+ */
78
116
  _updateAngle(object) {
79
117
  object._baseObject.angle = object.angle;
80
118
 
@@ -86,19 +124,34 @@ class PropertyAdapter {
86
124
  this._updateVertical(object);
87
125
  }
88
126
 
127
+ /**
128
+ * main function to calculate y and height
129
+ * @param {Object} object
130
+ */
89
131
  _updateVertical(object) {
90
- let y = this._getYAsNumber(object); //adaptX
91
- y += this.adaptAnchorY(object);
92
- y += this.adaptAlignY(object);
132
+ let y = this._getYAsNumber(object);
133
+ y += this._adaptAnchorY(object);
134
+ y += this._adaptAlignY(object);
93
135
  object._baseObject.y = y;
94
136
 
95
- this.adaptHeight(object);
137
+ this._adaptHeight(object);
96
138
  }
97
139
 
140
+ /**
141
+ * set property value without adaptation
142
+ * @param {Object} object
143
+ * @param {String} propertyName
144
+ * @param {mixed} value
145
+ */
98
146
  _setPropertyWithoutAdaption(object, propertyName, value) { //in error case
99
147
  object[propertyName] = value;
100
148
  }
101
149
 
150
+ /**
151
+ * adapt child properties if parent properties was changed
152
+ * @param {Object} object
153
+ * @param {String} propertyName
154
+ */
102
155
  _adaptChildProperties(object, propertyName) {
103
156
  const { children } = this._parentToChildDependencies[propertyName] || {};
104
157
  const objectHasChildren = this._canBeParent(object) && object.hasOwnProperty('contents');
@@ -111,7 +164,11 @@ class PropertyAdapter {
111
164
  this.propertyChangeHandler(child, dependency);
112
165
  }
113
166
 
114
- parentChangeHandler(child) {
167
+ /**
168
+ * main handler when new parent was setted
169
+ * @param {Object} child
170
+ */
171
+ _parentChangeHandler(child) {
115
172
  if (child.parent == null)
116
173
  return;
117
174
 
@@ -119,8 +176,14 @@ class PropertyAdapter {
119
176
  this.propertyChangeHandler(child, propertyName);
120
177
  }
121
178
 
179
+ /**
180
+ * get array of properties dependent on parent
181
+ * //todo cache them and get just one time
182
+ * @returns {Array}
183
+ */
122
184
  _propertiesDependentOnParent() {
123
185
  const properties = [];
186
+
124
187
  for (let propertyName of Object.keys(this._parentToChildDependencies)) {
125
188
  const { children } = this._parentToChildDependencies[propertyName];
126
189
 
@@ -132,14 +195,16 @@ class PropertyAdapter {
132
195
  return properties;
133
196
  }
134
197
 
135
- adaptAnchorX(object) {
136
- const pixiObject = object._baseObject;
137
-
198
+ /**
199
+ * adapt anchorX
200
+ * @param {Object} object
201
+ */
202
+ _adaptAnchorX(object) {
138
203
  if (typeof object.anchorX !== 'number' || object.anchorX < 0 || object.anchorX > 1)
139
204
  Urso.logger.error('AnchorX value is not valid!', object);
140
205
 
141
- if (this._canBeParent(object)) {
142
- if (object.anchorY === 0)
206
+ if (this._canBeParent(object)) { //parent types
207
+ if (object.anchorX === 0)
143
208
  return 0;
144
209
 
145
210
  if (object.angle) {
@@ -148,7 +213,8 @@ class PropertyAdapter {
148
213
 
149
214
  const objectWidth = this._getWidthAsNumber(object);
150
215
  return - objectWidth * object.anchorX;
151
- } else if (!this._typesWithoutAnchor.includes(object.type)) {
216
+ } else if (!this._typesWithoutAnchor.includes(object.type)) { //regular type and not a type without anchor
217
+ const pixiObject = object._baseObject;
152
218
  pixiObject.anchor.x = object.anchorX;
153
219
  } else {
154
220
  Urso.logger.warn(); ('AnchorX value cannot be used with this object type !', object);
@@ -157,13 +223,15 @@ class PropertyAdapter {
157
223
  return 0;
158
224
  }
159
225
 
160
- adaptAnchorY(object) {
161
- const pixiObject = object._baseObject;
162
-
226
+ /**
227
+ * adapt anchorY
228
+ * @param {Object} object
229
+ */
230
+ _adaptAnchorY(object) {
163
231
  if (typeof object.anchorY !== 'number' || object.anchorY < 0 || object.anchorY > 1)
164
232
  Urso.logger.error('AnchorY value is not valid!', object);
165
233
 
166
- if (this._canBeParent(object)) {
234
+ if (this._canBeParent(object)) { //parent types
167
235
  if (object.anchorY === 0)
168
236
  return 0;
169
237
 
@@ -173,7 +241,8 @@ class PropertyAdapter {
173
241
 
174
242
  const objectHeight = this._getHeightAsNumber(object);
175
243
  return - objectHeight * object.anchorY;
176
- } else if (!this._typesWithoutAnchor.includes(object.type)) {
244
+ } else if (!this._typesWithoutAnchor.includes(object.type)) { //regular type and not a type without anchor
245
+ const pixiObject = object._baseObject;
177
246
  pixiObject.anchor.y = object.anchorY;
178
247
  } else {
179
248
  Urso.logger.warn(); ('AnchorY value cannot be used with this object type !', object);
@@ -182,6 +251,12 @@ class PropertyAdapter {
182
251
  return 0;
183
252
  }
184
253
 
254
+ /**
255
+ * get anchor offset by angle for parent types
256
+ * @param {Object} object
257
+ * @param {String} side - x or y
258
+ * @returns {Number}
259
+ */
185
260
  _getAnchorOffsetByAngle(object, side) { //side can be x or y
186
261
  const objectWidth = this._getWidthAsNumber(object);
187
262
  const objectHeight = this._getHeightAsNumber(object);
@@ -197,50 +272,59 @@ class PropertyAdapter {
197
272
  return angleOffset;
198
273
  }
199
274
 
275
+ /**
276
+ * adapt scaleX
277
+ * @param {Object} object
278
+ */
200
279
  _adaptScaleX(object) {
201
- const pixiObject = object._baseObject;
202
-
203
280
  if (object.scaleX !== 1 && typeof object.width !== 'boolean') {
204
281
  Urso.logger.error('ScaleX value cannot be set. Width already used!!');
205
282
  this._setPropertyWithoutAdaption(object, 'scaleX', 1);
206
283
  return;
207
284
  }
208
285
 
209
- if (typeof object.scaleX === 'number')
286
+ if (typeof object.scaleX === 'number') {
287
+ const pixiObject = object._baseObject;
210
288
  pixiObject.scale.x = object.scaleX;
211
- else
289
+ } else
212
290
  Urso.logger.error('ScaleX value is not valid!');
213
291
  }
214
292
 
293
+ /**
294
+ * adapt scaleY
295
+ * @param {Object} object
296
+ */
215
297
  _adaptScaleY(object) {
216
- const pixiObject = object._baseObject;
217
-
218
298
  if (object.scaleY !== 1 && typeof object.height !== 'boolean') {
219
299
  Urso.logger.error('ScaleY value cannot be set. Height already used!!');
220
300
  this._setPropertyWithoutAdaption(object, 'scaleY', 1);
221
301
  return;
222
302
  }
223
303
 
224
- if (typeof object.scaleY === 'number' && object.scaleY >= 0) // TODO: CHECK SCALE CAN BE NEGATIVE
304
+ if (typeof object.scaleY === 'number' && object.scaleY >= 0) { // TODO: CHECK SCALE CAN BE NEGATIVE
305
+ const pixiObject = object._baseObject;
225
306
  pixiObject.scale.y = object.scaleY;
226
- else
307
+ } else
227
308
  Urso.logger.error('ScaleY value is not valid!');
228
309
  }
229
310
 
230
- adaptAlignX(object) {
311
+ /**
312
+ * adapt alignX
313
+ * @param {Object} object
314
+ */
315
+ _adaptAlignX(object) {
231
316
  if (typeof object.alignX !== 'string') {
232
317
  Urso.logger.error('AlignX value is not string!');
233
318
  return 0;
234
319
  }
235
320
 
236
- const parentWidth = object.parent ? this._getWidthAsNumber(object.parent) : 0;
237
-
238
321
  switch (object.alignX) {
239
322
  case 'left':
240
323
  return 0;
241
324
  case 'right':
242
- return parentWidth;
325
+ return object.parent ? this._getWidthAsNumber(object.parent) : 0; //parentWidth
243
326
  case 'center':
327
+ const parentWidth = object.parent ? this._getWidthAsNumber(object.parent) : 0;
244
328
  return parentWidth / 2;
245
329
  default:
246
330
  Urso.logger.error('AlignX string is not valid!');
@@ -248,20 +332,23 @@ class PropertyAdapter {
248
332
  }
249
333
  }
250
334
 
251
- adaptAlignY(object) {
335
+ /**
336
+ * adapt alignY
337
+ * @param {Object} object
338
+ */
339
+ _adaptAlignY(object) {
252
340
  if (typeof object.alignY !== 'string') {
253
341
  Urso.logger.error('AlignY value is not string!');
254
342
  return 0;
255
343
  }
256
344
 
257
- const parentHeight = object.parent ? this._getHeightAsNumber(object.parent) : 0;
258
-
259
345
  switch (object.alignY) {
260
346
  case 'top':
261
347
  return 0;
262
348
  case 'bottom':
263
- return parentHeight;
349
+ return object.parent ? this._getHeightAsNumber(object.parent) : 0; //parentHeight
264
350
  case 'center':
351
+ const parentHeight = object.parent ? this._getHeightAsNumber(object.parent) : 0;
265
352
  return parentHeight / 2;
266
353
  default:
267
354
  Urso.logger.error('AlignY string is not valid!');
@@ -269,9 +356,11 @@ class PropertyAdapter {
269
356
  }
270
357
  }
271
358
 
272
- adaptWidth(object) {
273
- const pixiObject = object._baseObject;
274
-
359
+ /**
360
+ * adapt width
361
+ * @param {Object} object
362
+ */
363
+ _adaptWidth(object) {
275
364
  if (typeof object.width !== 'boolean' && object.scaleX !== 1) {
276
365
  Urso.logger.error('Width value cannot be set. ScaleX already used!!', object);
277
366
  this._setPropertyWithoutAdaption(object, 'width', false);
@@ -284,13 +373,17 @@ class PropertyAdapter {
284
373
  if (!this._isValueANumberOrPercentsString(object.width))
285
374
  return Urso.logger.error('Width value is not valid!!');
286
375
 
287
- if (!this._canBeParent(object))
376
+ if (!this._canBeParent(object)) {
377
+ const pixiObject = object._baseObject;
288
378
  pixiObject.width = this._getWidthAsNumber(object);
379
+ }
289
380
  }
290
381
 
291
- adaptHeight(object) {
292
- const pixiObject = object._baseObject;
293
-
382
+ /**
383
+ * adapt height
384
+ * @param {Object} object
385
+ */
386
+ _adaptHeight(object) {
294
387
  if (typeof object.height !== 'boolean' && object.scaleY !== 1) {
295
388
  Urso.logger.error('Height value cannot be set. ScaleY already used!!', object);
296
389
  this._setPropertyWithoutAdaption(object, 'height', false);
@@ -303,27 +396,55 @@ class PropertyAdapter {
303
396
  if (!this._isValueANumberOrPercentsString(object.height))
304
397
  return Urso.logger.error('Height value not valid!');
305
398
 
306
- if (!this._canBeParent(object))
399
+ if (!this._canBeParent(object)) {
400
+ const pixiObject = object._baseObject;
307
401
  pixiObject.height = this._getHeightAsNumber(object);
402
+ }
308
403
  }
309
404
 
405
+ /**
406
+ * get x number value
407
+ * @param {Object} object
408
+ * @returns {Number}
409
+ */
310
410
  _getXAsNumber(object) {
311
411
  return this._getPropertyAsNumber(object, 'x', 'width');
312
412
  }
313
413
 
414
+ /**
415
+ * get y number value
416
+ * @param {Object} object
417
+ * @returns {Number}
418
+ */
314
419
  _getYAsNumber(object) {
315
420
  return this._getPropertyAsNumber(object, 'y', 'height');
316
421
  }
317
422
 
423
+ /**
424
+ * get width number value
425
+ * @param {Object} object
426
+ * @returns {Number}
427
+ */
318
428
  _getWidthAsNumber(object) {
319
429
  return this._getPropertyAsNumber(object, 'width', 'width');
320
430
  }
321
431
 
432
+ /**
433
+ * get height number value
434
+ * @param {Object} object
435
+ * @returns {Number}
436
+ */
322
437
  _getHeightAsNumber(object) {
323
438
  return this._getPropertyAsNumber(object, 'height', 'height');
324
439
  }
325
440
 
326
- //x, y, width or height
441
+ /**
442
+ * get x, y, width or height number value
443
+ * @param {Object} object
444
+ * @param {String} propertyName
445
+ * @param {String} parentPropertyName
446
+ * @returns {Number}
447
+ */
327
448
  _getPropertyAsNumber(object, propertyName, parentPropertyName) {
328
449
  const propType = typeof object[propertyName];
329
450
 
@@ -344,21 +465,40 @@ class PropertyAdapter {
344
465
  }
345
466
  }
346
467
 
468
+ /**
469
+ * get rounded persents of number
470
+ * @param {String} percentsString
471
+ * @param {Number} number
472
+ * @returns {Number}
473
+ */
347
474
  _getRoundedPercentageOfNumber(percentsString, number) {
348
475
  const percentsFloat = parseFloat(percentsString);
349
476
  return ~~(percentsFloat * number / 100);
350
477
  }
351
478
 
479
+ /**
480
+ * check can be object a parent (by type)
481
+ * @param {Object} object
482
+ * @returns {Boolean}
483
+ */
352
484
  _canBeParent(object) {
353
485
  return this._parentTypes.includes(object.type);
354
486
  }
355
487
 
488
+ /**
489
+ * check is value a number or a percents string
490
+ * @param {mixed} value
491
+ * @returns {Boolean}
492
+ */
356
493
  _isValueANumberOrPercentsString(value) {
357
494
  return typeof value === 'number' || (typeof value === 'string' && value.endsWith('%'))
358
495
  }
359
496
 
360
- //stretchingType
361
- adaptStretchingType(object) {
497
+ /**
498
+ * main function to handle stretchingType
499
+ * @param {Object} object
500
+ */
501
+ _adaptStretchingType(object) {
362
502
  if (object.width !== '100%' || object.height !== '100%' || !object.stretchingType)
363
503
  return;
364
504
 
@@ -380,11 +520,22 @@ class PropertyAdapter {
380
520
  }
381
521
  }
382
522
 
523
+ /**
524
+ * set property and adapt it (only for stretchingType)
525
+ * @param {Object} object
526
+ * @param {String} propertyName
527
+ * @param {Number} value
528
+ */
383
529
  _setPropertyAndAdaptIt(object, propertyName, value) {
384
530
  object[propertyName] = value;
385
531
  this.propertyChangeHandler(object, propertyName);
386
532
  }
387
533
 
534
+ /**
535
+ * set streching (only for stretchingType)
536
+ * @param {Object} object
537
+ * @param {Object} params
538
+ */
388
539
  _setStreching(object, { scale, objectWidth, objectHeight }) {
389
540
  if (object.scaleX === 1)
390
541
  this._setPropertyAndAdaptIt(object, 'width', objectWidth * scale);
@@ -397,6 +548,10 @@ class PropertyAdapter {
397
548
  this._setPropertyAndAdaptIt(object, 'scaleY', scale);
398
549
  }
399
550
 
551
+ /**
552
+ * get object values for streching (only for stretchingType)
553
+ * @param {Object} object
554
+ */
400
555
  _getObjectValuesForStreching(object) {
401
556
  const objectWidth = this._getWidthAsNumber(object);
402
557
  const objectHeight = this._getHeightAsNumber(object);
@@ -409,12 +564,20 @@ class PropertyAdapter {
409
564
  return { objectWidth, objectHeight, scaleX, scaleY };
410
565
  }
411
566
 
567
+ /**
568
+ * inscribe handler (only for stretchingType)
569
+ * @param {Object} object
570
+ */
412
571
  _inscribe(object) {
413
572
  const { objectWidth, objectHeight, scaleX, scaleY } = this._getObjectValuesForStreching(object);
414
573
  const scale = Math.min(scaleX, scaleY);
415
574
  this._setStreching(object, { scale, objectWidth, objectHeight });
416
575
  }
417
576
 
577
+ /**
578
+ * circumscribe handler (only for stretchingType)
579
+ * @param {Object} object
580
+ */
418
581
  _circumscribe(object) {
419
582
  const { objectWidth, objectHeight, scaleX, scaleY } = this._getObjectValuesForStreching(object);
420
583
  const scale = Math.max(scaleX, scaleY);