You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

31 lines
682 B

export class StateMachine {
constructor(owner) {
this.owner = owner;
this.state = null;
}
get() {
return this.state;
}
set(next) {
if (next === this.state) return;
const prev = this.state;
if (prev && typeof prev.exit === 'function') {
prev.exit(this.owner, next);
}
this.state = next || null;
if (this.state && typeof this.state.enter === 'function') {
this.state.enter(this.owner, prev);
}
}
update(dt) {
if (this.state && typeof this.state.update === 'function') {
this.state?.update?.(this.owner, dt);
}
}
}