Files
KSP-MGA-Planner/dist/main/editor/body-selector.js
Krafpy 8da935b047 2D physics fix and error handling consistency
- Reimplemented 2D orbital mechanics functions in physics-2d.ts
and encaspulated inside a `Physics2D` namespace.
- Deleted physics.ts and moved the required functions into physics-3d.ts
Further cleaning and reimplemntation will be done on the physics-3d.ts
content.
- Forced consistency on error handling : every raised error throws
an `Error` object.
- Commented sections relative to 2D physics and sequence generation.
2021-12-19 17:19:30 +01:00

40 lines
1.2 KiB
JavaScript

import { Selector } from "./selector.js";
export class BodySelector extends Selector {
constructor(id, system) {
super(id);
this.system = system;
this.fill();
}
fill() {
fillSelectorWithBodies(this._selector, this.system.sun.orbiters);
}
get body() {
const bodyName = this.selected;
const body = this.system.bodyFromName(bodyName);
if (!body)
throw new Error("Invalid body selection.");
return body;
}
}
function fillSelectorWithBodies(selector, bodies) {
for (const body of bodies) {
appendBodyOption(body, selector);
const hasOrbiters = body.orbiters.length > 0;
if (hasOrbiters) {
const optGroup = createSatelliteOptGroup(body, selector);
fillSelectorWithBodies(optGroup, body.orbiters);
}
}
}
function appendBodyOption(body, selector) {
const option = document.createElement("option");
option.innerHTML = body.name;
selector.appendChild(option);
}
function createSatelliteOptGroup(body, selector) {
const optGroup = document.createElement("optgroup");
optGroup.label = `${body.name}'s moons:`;
selector.appendChild(optGroup);
return optGroup;
}