import Component from '@/ecs/component.js'; import Script from '@/util/script.js'; export default class Plant extends Component { instanceFromSchema() { const {ecs} = this; const Instance = super.instanceFromSchema(); return class PlantInstance extends Instance { mayGrow() { return this.mayGrowScriptInstance.evaluateSync(); } grow() { const {Ticking} = ecs.get(this.entity); Ticking.addTickingPromise(this.growScriptInstance.tickingPromise()); } }; } async load(instance) { // heavy handed... if ('undefined' !== typeof window) { return; } const {readAsset} = this.ecs; await readAsset(instance.growScript) .then(async (code) => { if (code.byteLength > 0) { const context = { ecs: this.ecs, plant: instance, }; instance.growScriptInstance = await Script.fromCode((new TextDecoder()).decode(code), context); } }); await readAsset(instance.mayGrowScript) .then(async (code) => { if (code.byteLength > 0) { const context = { ecs: this.ecs, plant: instance, }; instance.mayGrowScriptInstance = await Script.fromCode((new TextDecoder()).decode(code), context); } }); } // heavy handed... markChange() {} static properties = { growScript: {type: 'string'}, growth: {type: 'uint16'}, growthFactor: {defaultValue: 127, type: 'uint8'}, mayGrowScript: {type: 'string'}, stage: {type: 'uint8'}, stages: { type: 'array', subtype: {type: 'uint16'}, }, }; }