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

View File

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

View File

@ -1,17 +1,18 @@
import Component from '@/ecs/component.js'; import Component from '@/ecs/component.js';
import Script from '@/util/script.js';
export default class Plant extends Component { export default class Plant extends Component {
instanceFromSchema() { instanceFromSchema() {
const {ecs} = this; const {ecs} = this;
const Instance = super.instanceFromSchema(); const Instance = super.instanceFromSchema();
return class PlantInstance extends Instance { return class PlantInstance extends Instance {
mayGrow() { $$grow;
return this.mayGrowScriptInstance.evaluate(); $$mayGrow;
}
grow() { grow() {
const {Ticking} = ecs.get(this.entity); 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) { if ('undefined' !== typeof window) {
return; return;
} }
instance.growScriptInstance = await this.ecs.readScript( instance.$$grow = await this.ecs.readScript(
instance.growScript, instance.growScript,
{ {
ecs: this.ecs, ecs: this.ecs,
plant: instance, plant: instance,
}, },
); );
instance.mayGrowScriptInstance = await this.ecs.readScript( instance.$$mayGrow = await this.ecs.readScript(
instance.mayGrowScript, instance.mayGrowScript,
{ {
ecs: this.ecs, ecs: this.ecs,

View File

@ -70,17 +70,17 @@ export default class Colliders extends System {
const intersections = entity.Collider.isCollidingWith(other.Collider); const intersections = entity.Collider.isCollidingWith(other.Collider);
if (intersections.length > 0) { if (intersections.length > 0) {
collisions.get(entity).add(other); collisions.get(entity).add(other);
if (!entity.Collider.collidingWith[other.id]) { if (!entity.Collider.$$collidingWith[other.id]) {
entity.Collider.collidingWith[other.id] = true; entity.Collider.$$collidingWith[other.id] = true;
other.Collider.collidingWith[entity.id] = true; other.Collider.$$collidingWith[entity.id] = true;
if (entity.Collider.collisionStartScriptInstance) { if (entity.Collider.$$collisionStart) {
const script = entity.Collider.collisionStartScriptInstance.clone(); const script = entity.Collider.$$collisionStart.clone();
script.context.intersections = intersections; script.context.intersections = intersections;
script.context.other = other; script.context.other = other;
entity.Ticking.add(script.ticker()); entity.Ticking.add(script.ticker());
} }
if (other.Collider.collisionStartScriptInstance) { if (other.Collider.$$collisionStart) {
const script = other.Collider.collisionStartScriptInstance.clone(); const script = other.Collider.$$collisionStart.clone();
script.context.intersections = intersections script.context.intersections = intersections
.map(([l, r]) => [r, l]); .map(([l, r]) => [r, l]);
script.context.other = entity; script.context.other = entity;
@ -127,19 +127,19 @@ export default class Colliders extends System {
} }
} }
else { else {
if (entity.Collider.collidingWith[other.id]) { if (entity.Collider.$$collidingWith[other.id]) {
if (entity.Collider.collisionEndScriptInstance) { if (entity.Collider.$$collisionEnd) {
const script = entity.Collider.collisionEndScriptInstance.clone(); const script = entity.Collider.$$collisionEnd.clone();
script.context.other = other; script.context.other = other;
entity.Ticking.add(script.ticker()); entity.Ticking.add(script.ticker());
} }
if (other.Collider.collisionEndScriptInstance) { if (other.Collider.$$collisionEnd) {
const script = other.Collider.collisionEndScriptInstance.clone(); const script = other.Collider.$$collisionEnd.clone();
script.context.other = entity; script.context.other = entity;
other.Ticking.add(script.ticker()); other.Ticking.add(script.ticker());
} }
delete entity.Collider.collidingWith[other.id]; delete entity.Collider.$$collidingWith[other.id];
delete other.Collider.collidingWith[entity.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.clear();
g.lineStyle(width, color); g.lineStyle(width, color);
g.moveTo(x0, y0); g.moveTo(x0, y0);
g.lineTo(x1 + 1, y0); g.lineTo(x1, y0);
g.lineTo(x1 + 1, y1 + 1); g.lineTo(x1, y1);
g.lineTo(x0, y1 + 1); g.lineTo(x0, y1);
g.lineTo(x0, y0); g.lineTo(x0, y0);
}, [color, width, x0, x1, y0, y1]); }, [color, width, x0, x1, y0, y1]);
return ( 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: [ bodies: [
{ {
points: [ points: [
{x: -4, y: -4}, {x: -3.5, y: -3.5},
{x: 3, y: -4}, {x: 3.5, y: -3.5},
{x: 3, y: 3}, {x: 3.5, y: 3.5},
{x: -4, y: 3}, {x: -3.5, y: 3.5},
], ],
}, },
], ],

View File

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

View File

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