Factor strategies into helper object instead of requiring methods on types
This commit is contained in:
parent
2e0b0c4ff4
commit
4c337f3579
1 changed files with 13 additions and 9 deletions
|
@ -29,9 +29,13 @@ export const enum TickType {
|
|||
PREDICTED,
|
||||
}
|
||||
|
||||
export type Advancer<Input, State> = (state: State, input: Input) => void;
|
||||
interface LockstepProcessor<Input, State> {
|
||||
compareInput(a: Input, b: Input): boolean;
|
||||
cloneState(source: State): State;
|
||||
advanceState(state: State, input: Input): void;
|
||||
}
|
||||
|
||||
export class LockstepState<Input extends Equals, State extends DeepCopy<State>> {
|
||||
export class LockstepState<Input, State> {
|
||||
|
||||
private inputIndex = -1;
|
||||
private inputLog: Input[] = [];
|
||||
|
@ -40,21 +44,21 @@ export class LockstepState<Input extends Equals, State extends DeepCopy<State>>
|
|||
private renderIndex = -1;
|
||||
private renderState: State;
|
||||
|
||||
constructor(private initialState: State, private advancer: Advancer<Input, State>) {
|
||||
this.canonState = initialState.deepCopy({});
|
||||
this.renderState = initialState.deepCopy({});
|
||||
constructor(private initialState: State, private engine: LockstepProcessor<Input, State>) {
|
||||
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<Input extends Equals, State extends DeepCopy<State>>
|
|||
|
||||
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;
|
||||
|
|
Loading…
Reference in a new issue