add components

This commit is contained in:
ez_lcw 2023-07-04 19:33:22 +08:00
parent e78ee9eda3
commit 4b7d6ce3b8
3 changed files with 36 additions and 2 deletions

View File

@ -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;
}
}
};

18
src/components.js Normal file
View File

@ -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);
}
};

16
src/main.js Normal file
View File

@ -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();