diff --git a/src/Ecs/Lockstep.ts b/src/Ecs/Lockstep.ts index c9e54ec..1a70613 100644 --- a/src/Ecs/Lockstep.ts +++ b/src/Ecs/Lockstep.ts @@ -29,9 +29,13 @@ export const enum TickType { PREDICTED, } -export type Advancer = (state: State, input: Input) => void; +interface LockstepProcessor { + compareInput(a: Input, b: Input): boolean; + cloneState(source: State): State; + advanceState(state: State, input: Input): void; +} -export class LockstepState> { +export class LockstepState { private inputIndex = -1; private inputLog: Input[] = []; @@ -40,21 +44,21 @@ export class LockstepState> private renderIndex = -1; private renderState: State; - constructor(private initialState: State, private advancer: Advancer) { - this.canonState = initialState.deepCopy({}); - this.renderState = initialState.deepCopy({}); + constructor(private initialState: State, private engine: LockstepProcessor) { + this.canonState = engine.cloneState(initialState); + this.renderState = engine.cloneState(initialState); } public addCanonInput(input: Input): void { this.canonIndex++; // advance canonical game state - this.advancer(this.canonState, input); + this.engine.advanceState(this.canonState, input); if(this.canonIndex <= this.renderIndex) { // we're rendering predicted states, so if the input changes we need to invalidate the rendered state - if(!equals(this.inputLog[this.canonIndex], input)) { - this.renderState = this.canonState.deepCopy({}); + if(!this.engine.compareInput(this.inputLog[this.canonIndex], input)) { + this.renderState = this.engine.cloneState(this.canonState); this.renderIndex = this.canonIndex; } } @@ -81,7 +85,7 @@ export class LockstepState> while(this.renderIndex < targetIndex) { this.renderIndex++; - this.advancer(this.renderState, this.inputLog[this.renderIndex]); + this.engine.advanceState(this.renderState, this.inputLog[this.renderIndex]); } return this.renderState;