fsmgasm-js
Version:
Port of FSMgasm by Minikloon for JS
220 lines (211 loc) • 4.72 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
State: () => State,
StateConditional: () => StateConditional,
StateGroup: () => StateGroup,
StateHolder: () => StateHolder,
StateProxy: () => StateProxy,
StateSeries: () => StateSeries,
StateSwitch: () => StateSwitch
});
module.exports = __toCommonJS(src_exports);
// src/states/State.ts
var State = class {
started = false;
ended = false;
frozen = false;
#updating = false;
constructor() {
}
start() {
if (this.started || this.ended)
return;
this.started = true;
try {
this.onStart();
} catch (err) {
console.warn(err);
}
}
onStart() {
}
update() {
if (!this.started || this.ended || this.#updating)
return;
this.#updating = true;
if (this.isReadyToEnd() && !this.frozen) {
this.end();
return;
}
try {
this.onUpdate();
} catch (err) {
console.warn(err);
}
this.#updating = false;
}
onUpdate() {
}
end() {
if (!this.started || this.ended)
return;
this.ended = true;
try {
this.onEnd();
} catch (err) {
console.warn(err);
}
}
onEnd() {
}
isReadyToEnd() {
return this.ended;
}
};
// src/states/StateConditional.ts
var StateConditional = class extends State {
constructor(condition) {
super();
this.isReadyToEnd = condition;
}
};
// src/states/StateHolder.ts
var StateHolder = class extends State {
states = [];
constructor() {
super();
}
add(state) {
this.states.push(state);
}
addAll(states) {
this.states = this.states.concat(states);
}
};
// src/states/StateGroup.ts
var StateGroup = class extends StateHolder {
constructor() {
super();
}
onUpdate() {
for (const state of this.states) {
if (!state.started)
state.start();
if (state.isReadyToEnd() && !state.ended && !this.frozen) {
state.end();
continue;
}
try {
state.update();
} catch (err) {
console.warn(err);
}
}
}
isReadyToEnd() {
for (const state of this.states) {
if (!state.ended)
return false;
}
return true;
}
};
// src/states/StateSeries.ts
var StateSeries = class extends StateHolder {
#index = 0;
constructor() {
super();
}
onUpdate() {
if (this.isReadyToEnd())
return;
const currentState = this.currentState();
if (!currentState.started)
currentState.start();
if (currentState.isReadyToEnd() && !this.frozen) {
currentState.end();
this.#index++;
return;
}
try {
currentState.update();
} catch (err) {
console.warn(err);
}
}
currentState() {
return this.states[this.#index];
}
isReadyToEnd() {
return this.#index == this.states.length;
}
};
// src/states/StateProxy.ts
var StateProxy = class extends StateSeries {
createStates;
constructor(createStates) {
super();
this.createStates = createStates;
}
onStart() {
this.addAll(this.createStates());
}
};
// src/states/StateSwitch.ts
var StateSwitch = class extends State {
currentState;
constructor() {
super();
}
changeState(state) {
if (this.currentState) {
this.currentState.end();
}
state.start();
this.currentState = state;
}
onUpdate() {
if (!this.currentState || this.currentState.ended || this.currentState.frozen || !this.currentState.started)
return;
if (this.currentState.isReadyToEnd() && !this.currentState.ended) {
this.currentState.end();
return;
}
try {
this.currentState.update();
} catch (err) {
console.warn(err);
}
}
isReadyToEnd() {
return false;
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
State,
StateConditional,
StateGroup,
StateHolder,
StateProxy,
StateSeries,
StateSwitch
});