Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiplayer state #1485

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading