refactor: readScript

This commit is contained in:
cha0s 2024-07-01 17:23:04 -05:00
parent 7009f398f5
commit 02d2c4b604
3 changed files with 18 additions and 25 deletions

View File

@ -20,27 +20,20 @@ export default class Plant extends Component {
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);
}
});
instance.growScriptInstance = await this.ecs.readScript(
instance.growScript,
{
ecs: this.ecs,
plant: instance,
},
);
instance.mayGrowScriptInstance = await this.ecs.readScript(
instance.mayGrowScript,
{
ecs: this.ecs,
plant: instance,
},
);
}
// heavy handed...
markChange() {}

View File

@ -41,10 +41,10 @@ export default class Engine {
const chars = await this.readAsset(uri);
return chars.byteLength > 0 ? JSON.parse((new TextDecoder()).decode(chars)) : {};
}
async readScript(uri) {
async readScript(uri, context) {
const code = await this.readAsset(uri);
if (code.byteLength > 0) {
return Script.fromCode((new TextDecoder()).decode(code));
return Script.fromCode((new TextDecoder()).decode(code), context);
}
}
}

View File

@ -41,10 +41,10 @@ class ClientEcs extends Ecs {
const chars = await this.readAsset(uri);
return chars.byteLength > 0 ? JSON.parse((new TextDecoder()).decode(chars)) : {};
}
async readScript(uri) {
async readScript(uri, context = {}) {
const code = await this.readAsset(uri);
if (code.byteLength > 0) {
return Script.fromCode((new TextDecoder()).decode(code));
return Script.fromCode((new TextDecoder()).decode(code), context);
}
}
}