Skip to content

Commit

Permalink
Fix multiplayer state (#1485)
Browse files Browse the repository at this point in the history
  • Loading branch information
riknoll authored Nov 15, 2024
1 parent d1ff8bf commit 7deed7d
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions libs/multiplayer/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 7deed7d

Please sign in to comment.