From aa2885c930add6c8dadd8940a0701b7f7a6c1faa Mon Sep 17 00:00:00 2001 From: James Turk Date: Sun, 9 Mar 2025 23:41:13 -0500 Subject: [PATCH] moving in the direction of independent worlds --- artworld/drawable.js | 31 +++++++++++++++---------------- artworld/shapes.js | 4 ++++ artworld/world.js | 4 ++-- examples/circles.html | 10 +++++----- examples/copies.html | 10 +++++----- examples/lines.html | 10 +++++----- examples/rects.html | 16 ++++++++-------- examples/spiral.html | 13 +++++++------ 8 files changed, 51 insertions(+), 47 deletions(-) diff --git a/artworld/drawable.js b/artworld/drawable.js index 096fe9f..27100a6 100644 --- a/artworld/drawable.js +++ b/artworld/drawable.js @@ -2,18 +2,17 @@ import { Vector2 } from "./math.js"; import { Color } from "./color.js"; import { Random } from "./random.js"; import { World } from "./world.js"; +import { getVal } from "./variables.js"; export class Drawable { constructor(parent) { this._parent = null; this._world = null; - // if parent is provided, set directly, then delegate to parent class + // if parent is provided, then delegate to parent class if (parent instanceof World) { - this._world = parent; - this._world.drawable(this); + parent.registerDrawable(this); } else if (parent) { - this._parent = parent; - this._parent.addChild(this); + parent.addChild(this); } this._fill = parent ? parent._fill : null; this._stroke = parent ? parent._stroke : null; @@ -110,14 +109,14 @@ export class Drawable { get worldPos() { // offset from parent if needed return this._parent - ? this._parent.worldPos.add(this._posVec) - : this._posVec; + ? this._parent.worldPos.add(getVal(this._posVec)) + : getVal(this._posVec); } } export class Group extends Drawable { - constructor() { - super(); + constructor(parent) { + super(parent); this._children = []; } @@ -138,22 +137,22 @@ export class Group extends Drawable { newGroup._parent = this._parent; newGroup._parent.addChild(newGroup); } + if (this._world) { + this._world.registerDrawable(newGroup); + } return newGroup; } - draw() { - for (let c of this._children) { - c.draw(); - } - } + draw() {} addChild(drawable) { this._children.push(drawable); // probably alraedy true, but make sure drawable._parent = this; - // ensure we're in the same world - drawable.world(this._world); + if (this._world) { + this._world.registerDrawable(drawable); + } return this; } diff --git a/artworld/shapes.js b/artworld/shapes.js index b979860..c8ef009 100644 --- a/artworld/shapes.js +++ b/artworld/shapes.js @@ -12,6 +12,10 @@ function makeCopy(Cls, obj, override) { if (newObj._parent) { newObj._parent.addChild(newObj); } + // attach to world + if (newObj._world) { + newObj._world.registerDrawable(newObj); + } return newObj; } diff --git a/artworld/world.js b/artworld/world.js index aa29a73..3c451b0 100644 --- a/artworld/world.js +++ b/artworld/world.js @@ -29,9 +29,9 @@ export class World { return v; } - drawable(thing) { + registerDrawable(thing) { this.drawables.push(thing); - thing._world = this; // set world for access to renderer (TODO: revisit) + thing._world = this; return thing; } diff --git a/examples/circles.html b/examples/circles.html index 8c5c577..b47ce57 100644 --- a/examples/circles.html +++ b/examples/circles.html @@ -4,7 +4,7 @@ diff --git a/examples/copies.html b/examples/copies.html index 540349d..c268b7d 100644 --- a/examples/copies.html +++ b/examples/copies.html @@ -4,7 +4,7 @@ diff --git a/examples/lines.html b/examples/lines.html index 41b0fb3..402dca9 100644 --- a/examples/lines.html +++ b/examples/lines.html @@ -3,17 +3,17 @@ diff --git a/examples/rects.html b/examples/rects.html index 8174c5d..41c1c0c 100644 --- a/examples/rects.html +++ b/examples/rects.html @@ -4,7 +4,7 @@ diff --git a/examples/spiral.html b/examples/spiral.html index 807d985..bd651c1 100644 --- a/examples/spiral.html +++ b/examples/spiral.html @@ -4,23 +4,24 @@