UNPKG

the-world-engine

Version:

three.js based, unity like game engine for browser

147 lines (146 loc) 4.25 kB
import { GameObject } from "./GameObject"; export class GameObjectBuilder { bt; $; ee; ie=false; constructor(t, e, i, s, n) { this.bt = new GameObject(t, e); const r = this.bt.transform; if (i) r.localPosition.copy(i); if (s) r.localRotation.copy(s); if (n) r.localScale.copy(n); this.$ = []; this.ee = []; } active(t) { this.bt.activeSelf = t; return this; } getGameObject(t) { t.ref = this.bt; return this; } getComponent(t, e) { e.ref = this.bt.getComponent(t); return this; } getComponents(t, e) { if (e) { t.ref = this.bt.getComponents(e); } else { t.ref = this.bt.getComponents(); } return this; } getComponentInChildren(t, e) { e.ref = this.bt.getComponentInChildren(t); return this; } getComponentsInChildren(t, e) { if (e) { t.ref = this.bt.getComponentsInChildren(e); } else { t.ref = this.bt.getComponentsInChildren(); } return this; } withComponent(t, e) { const i = new t(this.bt); i.engine_internal_constructAfterProcess(); if (i.disallowMultipleComponent) { const e = this.bt.getComponent(t); if (e) { console.warn(`Component ${t.name} already exists on GameObject ${this.bt.name}`); return this; } } this.bt.components.push(i); this.bt.gameObjectEventContainer.registerComponent(i); if (e) { this.ee.push((() => e(i))); } return this; } withChild(t) { this.$.push(t); return this; } se(t) { let e = false; const i = t.components; for (let s = 0; s < i.length; ++s) { const n = i[s]; const r = n.requiredComponents; for (let i = 0; i < r.length; ++i) { const s = r[i]; const o = t.getComponent(s); if (!o) { console.warn(`Component ${s.name} is required by Component ${n.constructor.name} on GameObject ${t.name}`); t.removeComponent(n); e = true; } } } if (e) this.se(t); } build(t) { if (this.ie) throw new Error("GameObjectBuilder is already built"); this.ie = true; this.ne(t); this.re(); this.oe(); return this.bt; } ne(t) { this.bt.transform.setParent(t, false); const e = this.$; for (let t = 0; t < e.length; ++t) { const i = e[t]; i.ne(this.bt.transform); } } re() { this.se(this.bt); const t = this.$; for (let e = 0; e < t.length; ++e) { t[e].re(); } } oe() { const t = this.ee; for (let e = 0; e < t.length; ++e) { t[e](); } const e = this.$; for (let t = 0; t < e.length; ++t) { e[t].oe(); } this.bt._initialized = true; } processEvent() { GameObjectBuilder.processEventByGroup([ this ], this.bt.engine.sceneProcessor); } static processEventByGroup(t, e) { const i = []; for (let e = 0; e < t.length; ++e) { i.push(...t[e].bt.getComponentsInChildren()); } for (let t = 0; t < i.length; ++t) { const e = i[t]; if (e.gameObject.activeInHierarchy && e.enabled) { e._engine_internal_componentEventContainer.tryCallAwake(); } } for (let t = 0; t < i.length; ++t) { const e = i[t]; if (e._engine_internal_destroyed) continue; if (e.gameObject.activeInHierarchy && e.enabled) { e._engine_internal_componentEventContainer.tryRegisterOnEnable(); e._engine_internal_componentEventContainer.tryRegisterStart(); e._engine_internal_componentEventContainer.tryRegisterUpdate(); } } e.tryStartProcessSyncedEvent(); } }