@urso/core 0.7.46 → 0.7.48

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.7.46",
3
+ "version": "0.7.48",
4
4
  "description": "HTML5 game engine",
5
5
  "main": "build/js/index.js",
6
6
  "author": "Megbrimef",
@@ -11,56 +11,107 @@ class ModulesObserverController {
11
11
  this._getUid = this._getUid.bind(this);
12
12
  }
13
13
 
14
+ /**
15
+ * fire event
16
+ * @param {String} eventName
17
+ * @param {Mixed} params
18
+ * @param {Number} delay
19
+ */
14
20
  fire(eventName, params, delay) {
15
- if (!eventName)
16
- return Urso.logger.error('ModulesObserverController fire error:', eventName);
21
+ if (!eventName) {
22
+ Urso.logger.error('ModulesObserverController fire error:', eventName);
23
+ return;
24
+ }
17
25
 
18
- if (delay)
19
- return setTimeout((() => { this.fire(eventName, params); }).bind(this), delay)
26
+ if (delay) {
27
+ setTimeout((() => { this.fire(eventName, params); }).bind(this), delay);
28
+ return;
29
+ }
20
30
 
21
31
  this._fireLocal(eventName, params);
22
- this._fireLocal(eventName + this._getCurPrefix(), params);
32
+ this._fireLocal(eventName + this._getLocalSuffix(), params);
23
33
  };
24
34
 
35
+ /**
36
+ * add listener
37
+ * @param {String} eventName
38
+ * @param {Function} callback
39
+ * @param {Boolean} global
40
+ */
25
41
  add(eventName, callback, global) {
26
- if (!eventName || !callback)
27
- return Urso.logger.error('ModulesObserverController add error:', eventName, callback);
42
+ if (!eventName || !callback) {
43
+ Urso.logger.error('ModulesObserverController add error:', eventName, callback);
44
+ return
45
+ }
28
46
 
29
47
  if (global)
30
48
  this._addLocal(eventName, callback);
31
49
  else
32
- this._addLocal(eventName + this._getCurPrefix(), callback);
50
+ this._addLocal(eventName + this._getLocalSuffix(), callback);
33
51
  }
34
52
 
53
+ /**
54
+ * remove listener
55
+ * @param {String} eventName
56
+ * @param {Function} callback
57
+ * @param {Boolean} global
58
+ */
35
59
  remove(eventName, callback, global) {
36
60
  if (global)
37
61
  this._removeLocal(eventName, callback);
38
62
  else
39
- this._removeLocal(eventName + this._getCurPrefix(), callback);
63
+ this._removeLocal(eventName + this._getLocalSuffix(), callback);
40
64
  }
41
65
 
66
+ /**
67
+ * set local events prefix
68
+ * @param {String} p
69
+ */
42
70
  setPrefix(p) {
43
71
  this._prefix = p;
44
72
  };
45
73
 
46
- _getUid() {
74
+ /**
75
+ * get callback uid
76
+ * @param {Function} callback
77
+ * @returns {String}
78
+ */
79
+ _getUid(callback) {
80
+ if (callback._ouid) //observer callback uid alredy exists
81
+ return callback._ouid;
82
+
47
83
  this._counter++;
48
84
  return 'observer_' + this._counter;
49
85
  }
50
86
 
87
+ /**
88
+ * add local listener
89
+ * @param {String} name
90
+ * @param {Function} callback
91
+ */
51
92
  _addLocal(name, callback) {
52
93
  if (!this._observers[name])
53
94
  this._observers[name] = {};
54
95
 
55
- const uid = this._getUid();
56
- callback._ouid = uid;
96
+ const uid = this._getUid(callback);
97
+
98
+ if (!callback._ouid) {
99
+ callback._ouid = uid;
100
+ }
57
101
 
58
102
  this._observers[name][uid] = callback;
59
103
  }
60
104
 
105
+ /**
106
+ * remove local listener
107
+ * @param {String} name
108
+ * @param {Function} callback
109
+ */
61
110
  _removeLocal(name, callback) {
62
- if (!this._observers[name] || !callback._ouid)
63
- return Urso.logger.error('ModulesObserverController remove error, no observer with', name, callback);
111
+ if (!this._observers[name] || !callback._ouid) {
112
+ Urso.logger.error('ModulesObserverController remove error, no observer with', name, callback);
113
+ return;
114
+ }
64
115
 
65
116
  const uid = callback._ouid;
66
117
  delete this._observers[name][uid];
@@ -69,6 +120,12 @@ class ModulesObserverController {
69
120
  delete this._observers[name];
70
121
  }
71
122
 
123
+ /**
124
+ * fire local event
125
+ * @param {String} name
126
+ * @param {Mixed} params
127
+ * @returns {Boolean}
128
+ */
72
129
  _fireLocal(name, params) {
73
130
  if (!this._observers[name])
74
131
  return false;
@@ -79,20 +136,31 @@ class ModulesObserverController {
79
136
  return true;
80
137
  }
81
138
 
139
+ /**
140
+ * clear all local listeners (can be used on scene switch)
141
+ */
82
142
  clearAllLocal() {
83
143
  for (let name in this._observers) {
84
- if (name.endsWith(this._getCurPrefix()))
144
+ if (name.endsWith(this._getLocalSuffix()))
85
145
  delete this._observers[name];
86
146
  }
87
147
  }
88
148
 
149
+ /**
150
+ * clear all listeners
151
+ * @returns {Boolean}
152
+ */
89
153
  clear() {
90
154
  this._observers = {};
91
155
 
92
156
  return true;
93
157
  }
94
158
 
95
- _getCurPrefix() {
159
+ /**
160
+ * get local events suffix
161
+ * @returns {String}
162
+ */
163
+ _getLocalSuffix() {
96
164
  return this._prefixDelimiter + this._prefix;
97
165
  };
98
166
  };
@@ -3,6 +3,12 @@ class ModulesStatesManagerConfigStates {
3
3
  this.singleton = true;
4
4
 
5
5
  this.contents = {
6
+ START_GAME: {
7
+ action: 'gameInit',
8
+ nextState: ["IDLE"], //next state to go after finish this state
9
+ callLimit: 1 //auto guard will return false, if limit is reached
10
+ },
11
+
6
12
  IDLE: { action: 'startSpin' },
7
13
 
8
14
  SPIN_START: {
@@ -18,6 +18,7 @@ class ModulesStatesManagerController {
18
18
 
19
19
  this._iterator; //will be defined after start
20
20
  this._nextState = this._nextState.bind(this);
21
+ this._statesCallStatistic = {};
21
22
  }
22
23
 
23
24
  start() {
@@ -117,6 +118,10 @@ class ModulesStatesManagerController {
117
118
 
118
119
  this._currentState = this._iterator.next();
119
120
 
121
+ //fill states call statistic
122
+ if (!this._statesCallStatistic[this._currentState]) this._statesCallStatistic[this._currentState] = 0;
123
+ this._statesCallStatistic[this._currentState]++;
124
+
120
125
  this.emit(Urso.events.MODULES_STATES_MANAGER_STATE_CHANGE, this._currentState);
121
126
 
122
127
  log('%c State ' + this._currentState, 'background: #bada55; color: #000')
@@ -176,6 +181,14 @@ class ModulesStatesManagerController {
176
181
  }
177
182
 
178
183
  checkStateGuard = (key) => {
184
+ //auto guard will check callLimit and return false, if limit is reached
185
+ const callLimit = this._configStates[this._currentState].callLimit;
186
+
187
+ if (
188
+ callLimit &&
189
+ callLimit >= (this._statesCallStatistic[this._currentState] || 0)
190
+ ) { return false; }
191
+
179
192
  const guardResult = this.statesGuards.checkGuard(key);
180
193
  log('%c State guard ' + key + ' is ' + guardResult, 'background: #DA55C4; color: #000')
181
194
  return guardResult;