fixed bugs

This commit is contained in:
Vasyl Kazakov
2026-06-17 17:06:13 +03:00
parent adf4e4b9bf
commit 930ed00c95
20 changed files with 627 additions and 415 deletions
@@ -0,0 +1,39 @@
import { PlayerContext } from "./PlayerContext";
import { IPlayerState } from "./IPlayerState";
import { PlayerStateType } from "./PlayerStateType";
export class PlayerStateMachine {
private activeState: IPlayerState;
private currentStateType: PlayerStateType;
private readonly states: Map<PlayerStateType, IPlayerState>;
get currentState(): PlayerStateType {
return this.currentStateType;
}
constructor(
private readonly context: PlayerContext,
states: Map<PlayerStateType, IPlayerState>,
initialState: PlayerStateType = PlayerStateType.Idle,
) {
this.states = states;
this.activeState = states.get(initialState)!;
this.currentStateType = initialState;
this.activeState.enter(this.context);
}
setState(stateType: PlayerStateType) {
if (this.currentStateType === stateType) return;
console.log(`[PlayerState] ${this.currentStateType} -> ${stateType}`);
this.activeState.exit(this.context);
this.activeState = this.states.get(stateType)!;
this.currentStateType = stateType;
this.activeState.enter(this.context);
}
update(deltaTime: number) {
this.activeState.update(this.context, deltaTime);
}
}