refactor: script names and body bounds

This commit is contained in:
cha0s 2024-07-25 16:39:05 -05:00
parent 123c95ca1c
commit a2b30d2f8b
8 changed files with 49 additions and 45 deletions

View File

@ -9,7 +9,9 @@ export default class Collider extends Component {
return class ColliderInstance extends super.instanceFromSchema() {
$$aabb = {x0: Infinity, x1: -Infinity, y0: Infinity, y1: -Infinity};
$$aabbs = [];
collidingWith = {};
$$collisionStart;
$$collisionEnd;
$$collidingWith = {};
get aabb() {
const {Position: {x: px, y: py}} = ecs.get(this.entity);
return {
@ -42,10 +44,10 @@ export default class Collider extends Component {
}
destroy() {
const entity = ecs.get(this.entity);
for (const otherId in this.collidingWith) {
for (const otherId in this.$$collidingWith) {
const other = ecs.get(otherId);
delete entity.Collider.collidingWith[other.id];
delete other.Collider.collidingWith[entity.id];
delete this.$$collidingWith[other.id];
delete other.Collider.$$collidingWith[entity.id];
}
}
isCollidingWith(other) {
@ -112,14 +114,14 @@ export default class Collider extends Component {
if ('undefined' !== typeof window) {
return;
}
instance.collisionEndScriptInstance = await this.ecs.readScript(
instance.$$collisionEnd = await this.ecs.readScript(
instance.collisionEndScript,
{
ecs: this.ecs,
entity: this.ecs.get(instance.entity),
},
);
instance.collisionStartScriptInstance = await this.ecs.readScript(
instance.$$collisionStart = await this.ecs.readScript(
instance.collisionStartScript,
{
ecs: this.ecs,

View File

@ -4,8 +4,9 @@ export default class Interactive extends Component {
instanceFromSchema() {
const {ecs} = this;
return class ControlledInstance extends super.instanceFromSchema() {
$$interact;
interact(initiator) {
const script = this.interactScriptInstance.clone();
const script = this.$$interact.clone();
script.context.initiator = initiator;
const {Ticking} = ecs.get(this.entity);
Ticking.add(script.ticker());
@ -23,7 +24,7 @@ export default class Interactive extends Component {
if ('undefined' !== typeof window) {
return;
}
instance.interactScriptInstance = await this.ecs.readScript(
instance.$$interact = await this.ecs.readScript(
instance.interactScript,
{
ecs: this.ecs,

View File

@ -1,17 +1,18 @@
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.evaluate();
}
$$grow;
$$mayGrow;
grow() {
const {Ticking} = ecs.get(this.entity);
Ticking.add(this.growScriptInstance.ticker());
Ticking.add(this.$$grow.ticker());
}
mayGrow() {
return this.$$mayGrow.evaluate();
}
};
}
@ -20,14 +21,14 @@ export default class Plant extends Component {
if ('undefined' !== typeof window) {
return;
}
instance.growScriptInstance = await this.ecs.readScript(
instance.$$grow = await this.ecs.readScript(
instance.growScript,
{
ecs: this.ecs,
plant: instance,
},
);
instance.mayGrowScriptInstance = await this.ecs.readScript(
instance.$$mayGrow = await this.ecs.readScript(
instance.mayGrowScript,
{
ecs: this.ecs,

View File

@ -70,17 +70,17 @@ export default class Colliders extends System {
const intersections = entity.Collider.isCollidingWith(other.Collider);
if (intersections.length > 0) {
collisions.get(entity).add(other);
if (!entity.Collider.collidingWith[other.id]) {
entity.Collider.collidingWith[other.id] = true;
other.Collider.collidingWith[entity.id] = true;
if (entity.Collider.collisionStartScriptInstance) {
const script = entity.Collider.collisionStartScriptInstance.clone();
if (!entity.Collider.$$collidingWith[other.id]) {
entity.Collider.$$collidingWith[other.id] = true;
other.Collider.$$collidingWith[entity.id] = true;
if (entity.Collider.$$collisionStart) {
const script = entity.Collider.$$collisionStart.clone();
script.context.intersections = intersections;
script.context.other = other;
entity.Ticking.add(script.ticker());
}
if (other.Collider.collisionStartScriptInstance) {
const script = other.Collider.collisionStartScriptInstance.clone();
if (other.Collider.$$collisionStart) {
const script = other.Collider.$$collisionStart.clone();
script.context.intersections = intersections
.map(([l, r]) => [r, l]);
script.context.other = entity;
@ -127,19 +127,19 @@ export default class Colliders extends System {
}
}
else {
if (entity.Collider.collidingWith[other.id]) {
if (entity.Collider.collisionEndScriptInstance) {
const script = entity.Collider.collisionEndScriptInstance.clone();
if (entity.Collider.$$collidingWith[other.id]) {
if (entity.Collider.$$collisionEnd) {
const script = entity.Collider.$$collisionEnd.clone();
script.context.other = other;
entity.Ticking.add(script.ticker());
}
if (other.Collider.collisionEndScriptInstance) {
const script = other.Collider.collisionEndScriptInstance.clone();
if (other.Collider.$$collisionEnd) {
const script = other.Collider.$$collisionEnd.clone();
script.context.other = entity;
other.Ticking.add(script.ticker());
}
delete entity.Collider.collidingWith[other.id];
delete other.Collider.collidingWith[entity.id];
delete entity.Collider.$$collidingWith[other.id];
delete other.Collider.$$collidingWith[entity.id];
}
}
}

View File

@ -13,13 +13,13 @@ function Aabb({color, width = 0.5, x0, y0, x1, y1, ...rest}) {
g.clear();
g.lineStyle(width, color);
g.moveTo(x0, y0);
g.lineTo(x1 + 1, y0);
g.lineTo(x1 + 1, y1 + 1);
g.lineTo(x0, y1 + 1);
g.lineTo(x1, y0);
g.lineTo(x1, y1);
g.lineTo(x0, y1);
g.lineTo(x0, y0);
}, [color, width, x0, x1, y0, y1]);
return (
<Graphics draw={draw} x={0.5} y={0.5} {...rest} />
<Graphics draw={draw} x={0} y={0} {...rest} />
);
}

View File

@ -127,10 +127,10 @@ export default async function createHomestead(id) {
bodies: [
{
points: [
{x: -4, y: -4},
{x: 3, y: -4},
{x: 3, y: 3},
{x: -4, y: 3},
{x: -3.5, y: -3.5},
{x: 3.5, y: -3.5},
{x: 3.5, y: 3.5},
{x: -3.5, y: 3.5},
],
},
],

View File

@ -5,10 +5,10 @@ export default async function createPlayer(id) {
bodies: [
{
points: [
{x: -4, y: -4},
{x: 3, y: -4},
{x: 3, y: 3},
{x: -4, y: 3},
{x: -3.5, y: -3.5},
{x: 3.5, y: -3.5},
{x: 3.5, y: 3.5},
{x: -3.5, y: 3.5},
],
},
],

View File

@ -15,10 +15,10 @@ for (let i = 0; i < N; ++i) {
bodies: [
{
points: [
{x: -3, y: -2},
{x: 12, y: -2},
{x: 12, y: 2},
{x: -3, y: 2},
{x: -2.5, y: -2.5},
{x: 14, y: -2.5},
{x: 14, y: 2.5},
{x: -2.5, y: 2.5},
],
unstoppable: 1,
},