pxt-common-packages 12.1.4 → 12.2.2

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.
@@ -49,12 +49,17 @@ namespace mp {
49
49
  }
50
50
  }
51
51
 
52
+ class StateEntry {
53
+ constructor(public key: number, public value: number) {
54
+ }
55
+ }
56
+
52
57
  /**
53
58
  * A player in the game
54
59
  */
55
60
  export class Player {
56
61
  _sprite: Sprite;
57
- _state: number[];
62
+ _state: StateEntry[];
58
63
  _index: number;
59
64
  _data: any;
60
65
  _mwb: boolean;
@@ -153,20 +158,11 @@ namespace mp {
153
158
  }
154
159
 
155
160
  _setState(key: number, val: number) {
156
- this._ensureState(key);
157
- if (this._state.length > key)
158
- this._state[key] = val;
161
+ this._lookupOrCreateState(key).value = val;
159
162
  }
160
163
 
161
164
  _getState(key: number): number {
162
- this._ensureState(key);
163
- return (this._state.length > key) ? this._state[key] : 0;
164
- }
165
-
166
- _ensureState(key: number) {
167
- if (!this._state) this._state = [];
168
- if (key < 0 || key > 255) return;
169
- while (this._state.length < key) this._state.push(0);
165
+ return this._lookupOrCreateState(key).value;
170
166
  }
171
167
 
172
168
  _getInfo(): info.PlayerInfo {
@@ -188,6 +184,17 @@ namespace mp {
188
184
  }
189
185
  return undefined;
190
186
  }
187
+
188
+ _lookupOrCreateState(key: number) {
189
+ if (!this._state) this._state = [];
190
+ for (const entry of this._state) {
191
+ if (entry.key === key) return entry;
192
+ }
193
+
194
+ const newEntry = new StateEntry(key, 0);
195
+ this._state.push(newEntry);
196
+ return newEntry;
197
+ }
191
198
  }
192
199
 
193
200
  class MPState {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pxt-common-packages",
3
- "version": "12.1.4",
3
+ "version": "12.2.2",
4
4
  "description": "Microsoft MakeCode (PXT) common packages",
5
5
  "keywords": [
6
6
  "MakeCode",