Add start of ship rendering
This commit is contained in:
parent
4a253b0184
commit
704c82838a
15 changed files with 199 additions and 13 deletions
35
js/world/ship.mjs
Normal file
35
js/world/ship.mjs
Normal file
|
@ -0,0 +1,35 @@
|
|||
import Module from './module.mjs';
|
||||
import Body from './body.mjs';
|
||||
|
||||
export default class Ship extends Body {
|
||||
constructor(x, y) {
|
||||
super(x, y, 0);
|
||||
|
||||
this.com = [0, 0];
|
||||
this.modules = new Set();
|
||||
}
|
||||
|
||||
tick() {
|
||||
|
||||
}
|
||||
|
||||
addModule(x, y, properties, options) {
|
||||
let module = new Module(x, y, {...properties, ...options});
|
||||
this.modules.add(module);
|
||||
this.refreshShape();
|
||||
}
|
||||
|
||||
deleteModule(module) {
|
||||
this.modules.delete(module);
|
||||
this.refreshShape();
|
||||
}
|
||||
|
||||
refreshShape() {
|
||||
let points = [];
|
||||
this.modules.forEach(m => points.push([m.x, m.y, m.mass]));
|
||||
this.mass = points.reduce((a, [,,b]) => a + b, 0);
|
||||
this.com = points.reduce(([ax, ay], b) =>
|
||||
[ax + b.x * b.mass, ay + b.y * b.mass], [0, 0])
|
||||
.map(x => x / this.mass);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue