moving in the direction of independent worlds
This commit is contained in:
parent
1011baf587
commit
aa2885c930
@ -2,18 +2,17 @@ import { Vector2 } from "./math.js";
|
|||||||
import { Color } from "./color.js";
|
import { Color } from "./color.js";
|
||||||
import { Random } from "./random.js";
|
import { Random } from "./random.js";
|
||||||
import { World } from "./world.js";
|
import { World } from "./world.js";
|
||||||
|
import { getVal } from "./variables.js";
|
||||||
|
|
||||||
export class Drawable {
|
export class Drawable {
|
||||||
constructor(parent) {
|
constructor(parent) {
|
||||||
this._parent = null;
|
this._parent = null;
|
||||||
this._world = 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) {
|
if (parent instanceof World) {
|
||||||
this._world = parent;
|
parent.registerDrawable(this);
|
||||||
this._world.drawable(this);
|
|
||||||
} else if (parent) {
|
} else if (parent) {
|
||||||
this._parent = parent;
|
parent.addChild(this);
|
||||||
this._parent.addChild(this);
|
|
||||||
}
|
}
|
||||||
this._fill = parent ? parent._fill : null;
|
this._fill = parent ? parent._fill : null;
|
||||||
this._stroke = parent ? parent._stroke : null;
|
this._stroke = parent ? parent._stroke : null;
|
||||||
@ -110,14 +109,14 @@ export class Drawable {
|
|||||||
get worldPos() {
|
get worldPos() {
|
||||||
// offset from parent if needed
|
// offset from parent if needed
|
||||||
return this._parent
|
return this._parent
|
||||||
? this._parent.worldPos.add(this._posVec)
|
? this._parent.worldPos.add(getVal(this._posVec))
|
||||||
: this._posVec;
|
: getVal(this._posVec);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Group extends Drawable {
|
export class Group extends Drawable {
|
||||||
constructor() {
|
constructor(parent) {
|
||||||
super();
|
super(parent);
|
||||||
this._children = [];
|
this._children = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,22 +137,22 @@ export class Group extends Drawable {
|
|||||||
newGroup._parent = this._parent;
|
newGroup._parent = this._parent;
|
||||||
newGroup._parent.addChild(newGroup);
|
newGroup._parent.addChild(newGroup);
|
||||||
}
|
}
|
||||||
|
if (this._world) {
|
||||||
|
this._world.registerDrawable(newGroup);
|
||||||
|
}
|
||||||
|
|
||||||
return newGroup;
|
return newGroup;
|
||||||
}
|
}
|
||||||
|
|
||||||
draw() {
|
draw() {}
|
||||||
for (let c of this._children) {
|
|
||||||
c.draw();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
addChild(drawable) {
|
addChild(drawable) {
|
||||||
this._children.push(drawable);
|
this._children.push(drawable);
|
||||||
// probably alraedy true, but make sure
|
// probably alraedy true, but make sure
|
||||||
drawable._parent = this;
|
drawable._parent = this;
|
||||||
// ensure we're in the same world
|
if (this._world) {
|
||||||
drawable.world(this._world);
|
this._world.registerDrawable(drawable);
|
||||||
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,6 +12,10 @@ function makeCopy(Cls, obj, override) {
|
|||||||
if (newObj._parent) {
|
if (newObj._parent) {
|
||||||
newObj._parent.addChild(newObj);
|
newObj._parent.addChild(newObj);
|
||||||
}
|
}
|
||||||
|
// attach to world
|
||||||
|
if (newObj._world) {
|
||||||
|
newObj._world.registerDrawable(newObj);
|
||||||
|
}
|
||||||
return newObj;
|
return newObj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,9 +29,9 @@ export class World {
|
|||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
drawable(thing) {
|
registerDrawable(thing) {
|
||||||
this.drawables.push(thing);
|
this.drawables.push(thing);
|
||||||
thing._world = this; // set world for access to renderer (TODO: revisit)
|
thing._world = this;
|
||||||
return thing;
|
return thing;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
<canvas id="mainCanvas" width="500" height="500"></canvas>
|
<canvas id="mainCanvas" width="500" height="500"></canvas>
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import {
|
import {
|
||||||
artworld,
|
World,
|
||||||
Color,
|
Color,
|
||||||
Pico8,
|
Pico8,
|
||||||
Group,
|
Group,
|
||||||
@ -13,8 +13,8 @@
|
|||||||
Vector2,
|
Vector2,
|
||||||
degToRad,
|
degToRad,
|
||||||
} from "../artworld/index.js";
|
} from "../artworld/index.js";
|
||||||
artworld.bindCanvas("mainCanvas");
|
let world = new World("mainCanvas");
|
||||||
window.artworld = artworld;
|
window.world = world;
|
||||||
|
|
||||||
function* color_cycle() {
|
function* color_cycle() {
|
||||||
while (true) {
|
while (true) {
|
||||||
@ -25,14 +25,14 @@
|
|||||||
}
|
}
|
||||||
let cycle = color_cycle();
|
let cycle = color_cycle();
|
||||||
|
|
||||||
let g = new Group().pos(new Vector2(250, 250));
|
let g = new Group(world).pos(new Vector2(250, 250));
|
||||||
for (let r = 20; r < 100; r += 12) {
|
for (let r = 20; r < 100; r += 12) {
|
||||||
new Circle(g).radius(r).fill(cycle.next().value).z(-r).strokeWeight(0);
|
new Circle(g).radius(r).fill(cycle.next().value).z(-r).strokeWeight(0);
|
||||||
}
|
}
|
||||||
for (let r = 100; r < 250; r += 12) {
|
for (let r = 100; r < 250; r += 12) {
|
||||||
new Circle(g).radius(r).fill(cycle.next().value).z(-r).strokeWeight(0);
|
new Circle(g).radius(r).fill(cycle.next().value).z(-r).strokeWeight(0);
|
||||||
}
|
}
|
||||||
artworld.draw();
|
world.draw();
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
<canvas id="mainCanvas" width="500" height="500"></canvas>
|
<canvas id="mainCanvas" width="500" height="500"></canvas>
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import {
|
import {
|
||||||
artworld,
|
World,
|
||||||
Color,
|
Color,
|
||||||
Pico8,
|
Pico8,
|
||||||
Group,
|
Group,
|
||||||
@ -12,17 +12,17 @@
|
|||||||
Random,
|
Random,
|
||||||
Vector2,
|
Vector2,
|
||||||
} from "../artworld/index.js";
|
} from "../artworld/index.js";
|
||||||
artworld.bindCanvas("mainCanvas");
|
let world = new World("mainCanvas");
|
||||||
window.artworld = artworld;
|
window.world = world;
|
||||||
|
|
||||||
let g = new Group().pos(new Vector2(0, 0));
|
let g = new Group(world).pos(new Vector2(0, 0));
|
||||||
let c = new Circle(g).radius(80).stroke(Pico8.RED).strokeWeight(5);
|
let c = new Circle(g).radius(80).stroke(Pico8.RED).strokeWeight(5);
|
||||||
for (let i = 0; i < 15; i++) {
|
for (let i = 0; i < 15; i++) {
|
||||||
c = c.copy().move(new Vector2(45, 45));
|
c = c.copy().move(new Vector2(45, 45));
|
||||||
}
|
}
|
||||||
g.copy().move(new Vector2(200, 0)).stroke(Pico8.GREEN);
|
g.copy().move(new Vector2(200, 0)).stroke(Pico8.GREEN);
|
||||||
g.copy().move(new Vector2(-200, 0)).stroke(Pico8.BLUE);
|
g.copy().move(new Vector2(-200, 0)).stroke(Pico8.BLUE);
|
||||||
artworld.draw();
|
world.draw();
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
@ -3,17 +3,17 @@
|
|||||||
<body>
|
<body>
|
||||||
<canvas id="mainCanvas" width="800" height="800"></canvas>
|
<canvas id="mainCanvas" width="800" height="800"></canvas>
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import { artworld, Color, Line, Vector2 } from "../artworld/index.js";
|
import { World, Color, Line, Vector2 } from "../artworld/index.js";
|
||||||
artworld.bindCanvas("mainCanvas");
|
let world = new World("mainCanvas");
|
||||||
window.artworld = artworld;
|
window.world = world;
|
||||||
for (let i = 0; i < 10; i++) {
|
for (let i = 0; i < 10; i++) {
|
||||||
new Line()
|
new Line(world)
|
||||||
.pos(new Vector2(5 * i * 14, 5))
|
.pos(new Vector2(5 * i * 14, 5))
|
||||||
.to(new Vector2(5 * i * 14, 140))
|
.to(new Vector2(5 * i * 14, 140))
|
||||||
.stroke(new Color(20 * i, 50, 50))
|
.stroke(new Color(20 * i, 50, 50))
|
||||||
.strokeWeight(1 + i);
|
.strokeWeight(1 + i);
|
||||||
}
|
}
|
||||||
artworld.draw();
|
world.draw();
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
<canvas id="mainCanvas" width="500" height="500"></canvas>
|
<canvas id="mainCanvas" width="500" height="500"></canvas>
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import {
|
import {
|
||||||
artworld,
|
World,
|
||||||
Color,
|
Color,
|
||||||
Pico8,
|
Pico8,
|
||||||
Group,
|
Group,
|
||||||
@ -13,16 +13,16 @@
|
|||||||
Vector2,
|
Vector2,
|
||||||
degToRad,
|
degToRad,
|
||||||
} from "../artworld/index.js";
|
} from "../artworld/index.js";
|
||||||
artworld.bindCanvas("mainCanvas");
|
let world = new World("mainCanvas");
|
||||||
window.artworld = artworld;
|
window.world = world;
|
||||||
|
|
||||||
for (let i = 0; i < 50; i++) {
|
for (let i = 0; i < 50; i++) {
|
||||||
new Rect().random(200).fill(Pico8.BLACK).z(10);
|
new Rect(world).random(200).fill(Pico8.BLACK).z(10);
|
||||||
new Rect().random(150).fill(Pico8.DARK_BLUE).z(15);
|
new Rect(world).random(150).fill(Pico8.DARK_BLUE).z(15);
|
||||||
new Rect().random(100).fill(Pico8.DARK_GREY).z(20);
|
new Rect(world).random(100).fill(Pico8.DARK_GREY).z(20);
|
||||||
new Rect().random(50).fill(Pico8.LIGHT_GREY).z(30);
|
new Rect(world).random(50).fill(Pico8.LIGHT_GREY).z(30);
|
||||||
}
|
}
|
||||||
artworld.draw();
|
world.draw();
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
@ -4,23 +4,24 @@
|
|||||||
<canvas id="mainCanvas" width="800" height="800"></canvas>
|
<canvas id="mainCanvas" width="800" height="800"></canvas>
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import {
|
import {
|
||||||
artworld,
|
World,
|
||||||
Color,
|
Color,
|
||||||
|
Pico8,
|
||||||
Group,
|
Group,
|
||||||
Line,
|
Line,
|
||||||
Random,
|
Random,
|
||||||
Vector2,
|
Vector2,
|
||||||
degToRad,
|
degToRad,
|
||||||
} from "../artworld/index.js";
|
} from "../artworld/index.js";
|
||||||
artworld.bindCanvas("mainCanvas");
|
const world = new World("mainCanvas");
|
||||||
window.artworld = artworld;
|
|
||||||
|
|
||||||
function* spirals() {
|
function* spirals() {
|
||||||
while (true) {
|
while (true) {
|
||||||
let g = new Group();
|
let g = new Group(world);
|
||||||
|
let rc = Random.choice(Object.values(Pico8));
|
||||||
for (let d = 0; d < 180; d += 10) {
|
for (let d = 0; d < 180; d += 10) {
|
||||||
if (Random.chance(0.9)) {
|
if (Random.chance(0.9)) {
|
||||||
new Line(g).to(Vector2.polar(190 - d, degToRad(d)));
|
new Line(g).to(Vector2.polar(190 - d, degToRad(d))).stroke(rc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
yield g;
|
yield g;
|
||||||
@ -52,7 +53,7 @@
|
|||||||
yOff: 10,
|
yOff: 10,
|
||||||
});
|
});
|
||||||
|
|
||||||
artworld.draw();
|
world.draw();
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
Loading…
Reference in New Issue
Block a user