the-world-engine
Version:
three.js based, unity like game engine for browser
89 lines (88 loc) • 2.4 kB
JavaScript
import { WaitForEndOfFrame, WaitForSeconds, WaitUntil, WaitWhile } from "./YieldInstruction";
export class CoroutineProcessor {
_;
Wt;
Ct;
static _needToCompactCount=16;
constructor(t) {
this._ = t;
this.Wt = [];
this.Ct = 0;
}
addCoroutine(t) {
this.Wt.push(t);
this.Ct += 1;
}
removeCoroutine(t) {
const i = this.Wt.indexOf(t);
if (i >= 0) {
this.Wt[i] = null;
this.Ct -= 1;
}
}
tryCompact() {
const t = this.Wt;
if (CoroutineProcessor._needToCompactCount <= t.length - this.Ct) {
let i = 0;
for (let e = 0; e < t.length; ++e) {
const o = t[e];
if (o === null) continue;
t[i] = o;
i += 1;
}
t.length = this.Ct;
}
}
updateAfterProcess() {
const t = this.Wt;
for (let i = 0; i < t.length; ++i) {
const e = t[i];
if (e === null) continue;
if (e.currentYieldInstructionExist) {
this.Ft(e);
} else {
t[i] = null;
this.Ct -= 1;
}
}
}
Ft(t) {
const i = t.currentYieldInstruction;
if (i instanceof WaitForSeconds) {
t.elapsedTime += this._.deltaTime;
if (t.elapsedTime >= i.seconds) {
t.elapsedTime = 0;
t.fatchNextInstruction();
}
} else if (i instanceof WaitUntil) {
if (i.predicate()) {
t.fatchNextInstruction();
}
} else if (i instanceof WaitWhile) {
if (!i.predicate()) {
t.fatchNextInstruction();
}
} else if (i === null) {
t.fatchNextInstruction();
}
}
endFrameAfterProcess() {
const t = this.Wt;
for (let i = 0; i < t.length; ++i) {
const e = t[i];
if (e === null) continue;
if (e.currentYieldInstructionExist) {
this.Pt(e);
} else {
t[i] = null;
this.Ct -= 1;
}
}
}
Pt(t) {
const i = t.currentYieldInstruction;
if (i instanceof WaitForEndOfFrame) {
t.fatchNextInstruction();
}
}
}