fix: ensure clean script context
This commit is contained in:
parent
271b944796
commit
fcefe1a620
|
@ -19,6 +19,15 @@ export default class Sandbox {
|
|||
this.compile();
|
||||
}
|
||||
|
||||
clone() {
|
||||
return new this.constructor(
|
||||
this.ast,
|
||||
{
|
||||
...this.$$context,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
compile() {
|
||||
let scope = new Scope();
|
||||
scope.context = this.$$context;
|
||||
|
|
|
@ -5,9 +5,10 @@ export default class Interactive extends Component {
|
|||
const {ecs} = this;
|
||||
return class ControlledInstance extends super.instanceFromSchema() {
|
||||
interact(initiator) {
|
||||
this.interactScriptInstance.context.initiator = initiator;
|
||||
const script = this.interactScriptInstance.clone();
|
||||
script.context.initiator = initiator;
|
||||
const {Ticking} = ecs.get(this.entity);
|
||||
Ticking.addTickingPromise(this.interactScriptInstance.tickingPromise());
|
||||
Ticking.addTickingPromise(script.tickingPromise());
|
||||
}
|
||||
get interacting() {
|
||||
return !!this.$$interacting;
|
||||
|
|
|
@ -15,8 +15,9 @@ export default class Wielder extends Component {
|
|||
const activeItem = this.activeItem();
|
||||
if (activeItem) {
|
||||
const {startInstance, stopInstance} = activeItem.scripts;
|
||||
const script = state ? startInstance : stopInstance;
|
||||
let script = state ? startInstance : stopInstance;
|
||||
if (script) {
|
||||
script = script.clone();
|
||||
script.context.ecs = ecs;
|
||||
script.context.item = activeItem;
|
||||
script.context.wielder = entity;
|
||||
|
|
|
@ -66,15 +66,17 @@ export default class Colliders extends System {
|
|||
other.Collider.collidingWith[entity.id] = true;
|
||||
if (!wasCollidingWith[other.id]) {
|
||||
if (entity.Collider.collisionStartScriptInstance) {
|
||||
entity.Collider.collisionStartScriptInstance.context.intersections = intersections;
|
||||
entity.Collider.collisionStartScriptInstance.context.other = other;
|
||||
entity.Ticking.addTickingPromise(entity.Collider.collisionStartScriptInstance.tickingPromise());
|
||||
const script = entity.Collider.collisionStartScriptInstance.clone();
|
||||
script.context.intersections = intersections;
|
||||
script.context.other = other;
|
||||
entity.Ticking.addTickingPromise(script.tickingPromise());
|
||||
}
|
||||
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]);
|
||||
other.Collider.collisionStartScriptInstance.context.other = entity;
|
||||
other.Ticking.addTickingPromise(other.Collider.collisionStartScriptInstance.tickingPromise());
|
||||
script.context.other = entity;
|
||||
other.Ticking.addTickingPromise(script.tickingPromise());
|
||||
}
|
||||
}
|
||||
for (const [, {impassable}] of intersections) {
|
||||
|
@ -93,12 +95,14 @@ export default class Colliders extends System {
|
|||
continue;
|
||||
}
|
||||
if (entity.Collider.collisionEndScriptInstance) {
|
||||
entity.Collider.collisionEndScriptInstance.context.other = other;
|
||||
entity.Ticking.addTickingPromise(entity.Collider.collisionEndScriptInstance.tickingPromise());
|
||||
const script = entity.Collider.collisionEndScriptInstance.clone();
|
||||
script.context.other = other;
|
||||
entity.Ticking.addTickingPromise(script.tickingPromise());
|
||||
}
|
||||
if (other.Collider.collisionEndScriptInstance) {
|
||||
other.Collider.collisionEndScriptInstance.context.other = entity;
|
||||
other.Ticking.addTickingPromise(other.Collider.collisionEndScriptInstance.tickingPromise());
|
||||
const script = other.Collider.collisionEndScriptInstance.clone();
|
||||
script.context.other = entity;
|
||||
other.Ticking.addTickingPromise(script.tickingPromise());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,6 +29,10 @@ export default class Script {
|
|||
this.promise = null;
|
||||
}
|
||||
|
||||
clone() {
|
||||
return new this.constructor(this.sandbox.clone());
|
||||
}
|
||||
|
||||
get context() {
|
||||
return this.sandbox.context;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user