fix: ensure clean script context

This commit is contained in:
cha0s 2024-07-04 09:24:49 -05:00
parent 271b944796
commit fcefe1a620
5 changed files with 32 additions and 13 deletions

View File

@ -19,6 +19,15 @@ export default class Sandbox {
this.compile(); this.compile();
} }
clone() {
return new this.constructor(
this.ast,
{
...this.$$context,
}
);
}
compile() { compile() {
let scope = new Scope(); let scope = new Scope();
scope.context = this.$$context; scope.context = this.$$context;

View File

@ -5,9 +5,10 @@ export default class Interactive extends Component {
const {ecs} = this; const {ecs} = this;
return class ControlledInstance extends super.instanceFromSchema() { return class ControlledInstance extends super.instanceFromSchema() {
interact(initiator) { interact(initiator) {
this.interactScriptInstance.context.initiator = initiator; const script = this.interactScriptInstance.clone();
script.context.initiator = initiator;
const {Ticking} = ecs.get(this.entity); const {Ticking} = ecs.get(this.entity);
Ticking.addTickingPromise(this.interactScriptInstance.tickingPromise()); Ticking.addTickingPromise(script.tickingPromise());
} }
get interacting() { get interacting() {
return !!this.$$interacting; return !!this.$$interacting;

View File

@ -15,8 +15,9 @@ export default class Wielder extends Component {
const activeItem = this.activeItem(); const activeItem = this.activeItem();
if (activeItem) { if (activeItem) {
const {startInstance, stopInstance} = activeItem.scripts; const {startInstance, stopInstance} = activeItem.scripts;
const script = state ? startInstance : stopInstance; let script = state ? startInstance : stopInstance;
if (script) { if (script) {
script = script.clone();
script.context.ecs = ecs; script.context.ecs = ecs;
script.context.item = activeItem; script.context.item = activeItem;
script.context.wielder = entity; script.context.wielder = entity;

View File

@ -66,15 +66,17 @@ export default class Colliders extends System {
other.Collider.collidingWith[entity.id] = true; other.Collider.collidingWith[entity.id] = true;
if (!wasCollidingWith[other.id]) { if (!wasCollidingWith[other.id]) {
if (entity.Collider.collisionStartScriptInstance) { if (entity.Collider.collisionStartScriptInstance) {
entity.Collider.collisionStartScriptInstance.context.intersections = intersections; const script = entity.Collider.collisionStartScriptInstance.clone();
entity.Collider.collisionStartScriptInstance.context.other = other; script.context.intersections = intersections;
entity.Ticking.addTickingPromise(entity.Collider.collisionStartScriptInstance.tickingPromise()); script.context.other = other;
entity.Ticking.addTickingPromise(script.tickingPromise());
} }
if (other.Collider.collisionStartScriptInstance) { if (other.Collider.collisionStartScriptInstance) {
other.Collider.collisionStartScriptInstance.context.intersections = intersections const script = other.Collider.collisionStartScriptInstance.clone();
script.context.intersections = intersections
.map(([l, r]) => [r, l]); .map(([l, r]) => [r, l]);
other.Collider.collisionStartScriptInstance.context.other = entity; script.context.other = entity;
other.Ticking.addTickingPromise(other.Collider.collisionStartScriptInstance.tickingPromise()); other.Ticking.addTickingPromise(script.tickingPromise());
} }
} }
for (const [, {impassable}] of intersections) { for (const [, {impassable}] of intersections) {
@ -93,12 +95,14 @@ export default class Colliders extends System {
continue; continue;
} }
if (entity.Collider.collisionEndScriptInstance) { if (entity.Collider.collisionEndScriptInstance) {
entity.Collider.collisionEndScriptInstance.context.other = other; const script = entity.Collider.collisionEndScriptInstance.clone();
entity.Ticking.addTickingPromise(entity.Collider.collisionEndScriptInstance.tickingPromise()); script.context.other = other;
entity.Ticking.addTickingPromise(script.tickingPromise());
} }
if (other.Collider.collisionEndScriptInstance) { if (other.Collider.collisionEndScriptInstance) {
other.Collider.collisionEndScriptInstance.context.other = entity; const script = other.Collider.collisionEndScriptInstance.clone();
other.Ticking.addTickingPromise(other.Collider.collisionEndScriptInstance.tickingPromise()); script.context.other = entity;
other.Ticking.addTickingPromise(script.tickingPromise());
} }
} }
} }

View File

@ -29,6 +29,10 @@ export default class Script {
this.promise = null; this.promise = null;
} }
clone() {
return new this.constructor(this.sandbox.clone());
}
get context() { get context() {
return this.sandbox.context; return this.sandbox.context;
} }