UNPKG

@fboes/aerofly-custom-missions

Version:

Builder for Aerofly FS4 Custom Missions Files

103 lines (102 loc) 3.59 kB
import { AeroflyConfigurationNode, AeroflyConfigurationNodeComment } from "../node/AeroflyConfigurationNode.js"; import { feetPerMeter } from "./AeroflyMission.js"; /** * @class * A cloud layer for the current flight plan's weather data * * The purpose of this class is to collect data needed for Aerofly FS4's * `custom_missions_user.tmc` flight plan file format, and export the structure * for this file via the `toString()` method. */ export class AeroflyMissionConditionsCloud { /** * @param {number} cover normalized value [0,1] * @param {number} base altitude in meters AGL */ constructor(cover, base) { this.cover = cover; this.base = base; } /** * @param {number} cover normalized value [0,1] * @param {number} base_feet altitude, but in feet AGL instead of meters AGL * @returns {AeroflyMissionConditionsCloud} self */ static createInFeet(cover, base_feet) { return new AeroflyMissionConditionsCloud(cover, base_feet / feetPerMeter); } /** * @param {number} base_feet `this.base` in feet instead of meters */ set base_feet(base_feet) { this.base = base_feet / feetPerMeter; } /** * @returns {number} `this.base` in feet instead of meters */ get base_feet() { return this.base * feetPerMeter; } /** * @returns {string} Cloud coverage as text representation like "OVC" for `this.cover` */ get cover_code() { if (this.cover < 1 / 8) { return "CLR"; } else if (this.cover <= 2 / 8) { return "FEW"; } else if (this.cover <= 4 / 8) { return "SCT"; } else if (this.cover <= 7 / 8) { return "BKN"; } return "OVC"; } /** * @param {number} index if used in an array will set the array index * @returns {AeroflyConfigurationNode[]} to use in Aerofly FS4's `custom_missions_user.tmc` */ getElements(index = 0) { const getIndexString = (index) => { switch (index) { case 0: return "cloud"; case 1: return "cirrus"; case 2: return "cumulus_mediocris"; default: return "more_clouds"; } }; const indexString = getIndexString(index); return index <= 1 ? [ new AeroflyConfigurationNode("float64", `${indexString}_cover`, this.cover ?? 0, this.cover_code), new AeroflyConfigurationNode("float64", `${indexString}_base`, this.base, `${this.base_feet} ft AGL`), ] : [ new AeroflyConfigurationNodeComment("float64", `${indexString}_cover`, this.cover ?? 0, this.cover_code), new AeroflyConfigurationNodeComment("float64", `${indexString}_base`, this.base, `${this.base_feet} ft AGL`), ]; } /** * @param {number} index if used in an array will set the array index * @returns {string} to use in Aerofly FS4's `custom_missions_user.tmc` */ toString(index = 0) { return this.getElements() .map((element) => element.toString(index)) .join("\n"); } static fromJSON(json) { if (typeof json !== "object" || json === null) { throw new Error("Invalid mission condition cloud data"); } const data = json; return new this(Number(data.cover ?? 0), Number(data.base ?? 0)); } }