replaced files

This commit is contained in:
Vasyl Kazakov
2026-06-18 16:47:23 +03:00
parent 7cc63dbba9
commit ce3f254bc1
49 changed files with 141 additions and 181 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>;
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);
}
get currentState(): PlayerStateType {
return this.currentStateType;
}
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);
}
}