From 4b7d6ce3b8cd38c2580abe5d23e453e0ec9d0591 Mon Sep 17 00:00:00 2001 From: ez_lcw Date: Tue, 4 Jul 2023 19:33:22 +0800 Subject: [PATCH] add components --- src/2d.js | 4 ++-- src/components.js | 18 ++++++++++++++++++ src/main.js | 16 ++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 src/components.js create mode 100644 src/main.js diff --git a/src/2d.js b/src/2d.js index cfe66fa..0cc7b3b 100644 --- a/src/2d.js +++ b/src/2d.js @@ -30,7 +30,7 @@ export class Circle { b.o.addTo(v.adjust(b.r - bd)); return true; } -} +}; export class Vec2 { constructor(x, y) { @@ -120,4 +120,4 @@ export class Vec2 { cross(y) { return this.x * y.y - this.y * y.x; } -} \ No newline at end of file +}; \ No newline at end of file diff --git a/src/components.js b/src/components.js new file mode 100644 index 0000000..84af5b5 --- /dev/null +++ b/src/components.js @@ -0,0 +1,18 @@ +import { Circle } from '../src/2d.js'; + +export class Motion { + constructor(p, v) { + this.position = p; + this.velocity = v; + } +}; + +export class CollisionBox { + constructor(r) { + this.r = r; + } + + shape(o) { + return new Circle(o, this.r); + } +}; \ No newline at end of file diff --git a/src/main.js b/src/main.js new file mode 100644 index 0000000..cb18c29 --- /dev/null +++ b/src/main.js @@ -0,0 +1,16 @@ +import { Vec2 } from '../src/2d.js'; +import { Motion, CollisionBox } from '../src/components.js'; +import { EntityRegistry } from '../src/ecs.js'; + +function main() { + let registry = new EntityRegistry(); + let tank = registry.create(); + registry.assign(tank, new Motion(new Vec2(0, 0), new Vec2(1, 1))); + registry.assign(tank, new CollisionBox(1)); + registry.forEach([Motion], (id, motion) => { + motion.position.addTo(motion.velocity); + }); + console.log(`motion: ${JSON.stringify(registry.get(tank, Motion))}`); +} + +main(); \ No newline at end of file