From 7deed7df4193efe476db7cd652a1d06cef5f6e5b Mon Sep 17 00:00:00 2001 From: Richard Knoll Date: Fri, 15 Nov 2024 09:41:26 -0800 Subject: [PATCH] Fix multiplayer state (#1485) --- libs/multiplayer/player.ts | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/libs/multiplayer/player.ts b/libs/multiplayer/player.ts index af5355e15..75bfb1f83 100644 --- a/libs/multiplayer/player.ts +++ b/libs/multiplayer/player.ts @@ -49,12 +49,17 @@ namespace mp { } } + class StateEntry { + constructor(public key: number, public value: number) { + } + } + /** * A player in the game */ export class Player { _sprite: Sprite; - _state: number[]; + _state: StateEntry[]; _index: number; _data: any; _mwb: boolean; @@ -153,20 +158,11 @@ namespace mp { } _setState(key: number, val: number) { - this._ensureState(key); - if (this._state.length > key) - this._state[key] = val; + this._lookupOrCreateState(key).value = val; } _getState(key: number): number { - this._ensureState(key); - return (this._state.length > key) ? this._state[key] : 0; - } - - _ensureState(key: number) { - if (!this._state) this._state = []; - if (key < 0 || key > 255) return; - while (this._state.length < key) this._state.push(0); + return this._lookupOrCreateState(key).value; } _getInfo(): info.PlayerInfo { @@ -188,6 +184,17 @@ namespace mp { } return undefined; } + + _lookupOrCreateState(key: number) { + if (!this._state) this._state = []; + for (const entry of this._state) { + if (entry.key === key) return entry; + } + + const newEntry = new StateEntry(key, 0); + this._state.push(newEntry); + return newEntry; + } } class MPState {