Interactive 2D orbital flight simulator (HTML/CSS/JS, no dependencies). Newtonian gravity with RK4 integration, three scenarios (Surface Launch, Low Orbit, Station Rendezvous), trajectory prediction, camera/zoom, keyboard + touch controls, and automated unit + smoke tests.
216 lines
8.7 KiB
JavaScript
216 lines
8.7 KiB
JavaScript
/*
|
||
* Orbital Flight Simulator - UI layer.
|
||
* Owns all DOM overlays (menu, tutorial, pause, end screens) and the HUD.
|
||
* Keeps a reference to the game and pushes state into the DOM each frame.
|
||
*/
|
||
(function (global) {
|
||
'use strict';
|
||
|
||
var P = global.OrbitalPhysics;
|
||
|
||
var TUTORIAL = [
|
||
{
|
||
title: 'Welcome, pilot',
|
||
body: 'You are in low orbit around a planet. Your goal is to reach the space station and dock with it.\n\n' +
|
||
'Use the controls to maneuver. Watch your fuel — it is limited.'
|
||
},
|
||
{
|
||
title: 'Thrust & rotation',
|
||
body: 'W / ↑ or the THRUST button fires your engine in the direction you are facing.\n' +
|
||
'A / D or ← / → rotate the ship.\n\n' +
|
||
'The cyan arrow shows your velocity (prograde). Burning along it speeds you up; burning opposite (retrograde) slows you down.'
|
||
},
|
||
{
|
||
title: 'Orbits & the flight path',
|
||
body: 'The dashed line is your predicted coast path (same physics as the sim).\n\n' +
|
||
'Green = a closed, bound orbit. Orange = you will escape the planet. The HUD shows periapsis (lowest) and apoapsis (highest) altitude.\n\n' +
|
||
'To raise an orbit, burn prograde. To lower it, burn retrograde.'
|
||
},
|
||
{
|
||
title: 'Time & docking',
|
||
body: 'Press 1–4 (or the WARP button) to change time acceleration. It automatically slows near the surface and near the station for precise docking.\n\n' +
|
||
'To dock: match the station orbit, then close the distance inside the blue ring while your relative velocity is low.\n\n' +
|
||
'Good luck. Press H anytime to re-read this.'
|
||
}
|
||
];
|
||
|
||
function UI(game) {
|
||
this.game = game;
|
||
this.el = {};
|
||
var ids = [
|
||
'hud', 'hud-left', 'hud-right', 'hud-top', 'hud-dock',
|
||
'alt', 'spd', 'fuel-text', 'fuel-bar',
|
||
'orbit-bound', 'orbit-peri', 'orbit-apo', 'orbit-ecc', 'orbit-period', 'timescale', 'auto-limit',
|
||
'scenario-name', 'goal-text', 'dock-dist', 'dock-rel',
|
||
'menu', 'btn-launch', 'btn-orbit', 'btn-rendez',
|
||
'tutorial', 'tut-title', 'tut-text', 'tut-step', 'tut-next', 'tut-skip',
|
||
'pause-overlay', 'ready-overlay', 'end-overlay', 'end-title', 'end-detail', 'end-reset', 'end-menu',
|
||
'btn-pause', 'btn-reset', 'btn-zoom-in', 'btn-zoom-out', 'btn-help',
|
||
'touch-controls', 'btn-thrust', 'btn-left', 'btn-right', 'btn-time'
|
||
];
|
||
for (var i = 0; i < ids.length; i++) {
|
||
this.el[ids[i]] = document.getElementById(ids[i]);
|
||
}
|
||
this.tutIndex = 0;
|
||
this.lastBound = null;
|
||
this._wire();
|
||
}
|
||
|
||
UI.prototype._wire = function () {
|
||
var self = this;
|
||
var g = this.game;
|
||
|
||
function on(id, ev, fn) {
|
||
var el = self.el[id];
|
||
if (el) el.addEventListener(ev, fn);
|
||
}
|
||
|
||
on('btn-launch', 'click', function () { self.hideMenu(); g.loadScenario('launch'); });
|
||
on('btn-orbit', 'click', function () { self.hideMenu(); g.loadScenario('loworbit'); });
|
||
on('btn-rendez', 'click', function () { self.hideMenu(); g.loadScenario('rendezvous'); });
|
||
|
||
on('tut-next', 'click', function () { self.nextTutorial(); });
|
||
on('tut-skip', 'click', function () { self.hideTutorial(); });
|
||
|
||
on('end-reset', 'click', function () { self.hideEnd(); g.reset(); });
|
||
on('end-menu', 'click', function () { self.hideEnd(); g.toMenu(); self.showMenu(); });
|
||
|
||
on('btn-pause', 'click', function () { g.togglePause(); });
|
||
on('btn-reset', 'click', function () { g.reset(); });
|
||
on('btn-zoom-in', 'click', function () { g.zoomBy(1.3); });
|
||
on('btn-zoom-out', 'click', function () { g.zoomBy(1 / 1.3); });
|
||
on('btn-help', 'click', function () { self.showTutorial(); });
|
||
};
|
||
|
||
UI.prototype._set = function (id, text) {
|
||
var el = this.el[id];
|
||
if (el) el.textContent = text;
|
||
};
|
||
|
||
UI.prototype.showMenu = function () {
|
||
if (this.el.menu) this.el.menu.classList.remove('hidden');
|
||
};
|
||
UI.prototype.hideMenu = function () {
|
||
if (this.el.menu) this.el.menu.classList.add('hidden');
|
||
};
|
||
|
||
UI.prototype.showScenario = function (sc) {
|
||
this._set('scenario-name', sc.name);
|
||
this._set('goal-text', sc.goalText);
|
||
if (this.el.hud) this.el.hud.classList.remove('hidden');
|
||
};
|
||
UI.prototype.hideHud = function () {
|
||
if (this.el.hud) this.el.hud.classList.add('hidden');
|
||
};
|
||
|
||
UI.prototype.showTutorial = function () {
|
||
this.tutIndex = 0;
|
||
this.renderTutorial();
|
||
if (this.el.tutorial) this.el.tutorial.classList.remove('hidden');
|
||
this.game.paused = true;
|
||
};
|
||
UI.prototype.hideTutorial = function () {
|
||
if (this.el.tutorial) this.el.tutorial.classList.add('hidden');
|
||
this.game.paused = false;
|
||
};
|
||
UI.prototype.nextTutorial = function () {
|
||
this.tutIndex++;
|
||
if (this.tutIndex >= TUTORIAL.length) { this.hideTutorial(); return; }
|
||
this.renderTutorial();
|
||
};
|
||
UI.prototype.renderTutorial = function () {
|
||
var t = TUTORIAL[this.tutIndex];
|
||
this._set('tut-title', t.title);
|
||
this._set('tut-text', t.body);
|
||
this._set('tut-step', (this.tutIndex + 1) + ' / ' + TUTORIAL.length);
|
||
var next = this.el['tut-next'];
|
||
if (next) next.textContent = this.tutIndex === TUTORIAL.length - 1 ? 'Start' : 'Next';
|
||
};
|
||
|
||
UI.prototype.showEnd = function (title, detail, success) {
|
||
this._set('end-title', title);
|
||
this._set('end-detail', detail);
|
||
var t = this.el['end-title'];
|
||
if (t) t.style.color = success ? '#7dff9a' : '#ff7b6b';
|
||
if (this.el['end-overlay']) this.el['end-overlay'].classList.remove('hidden');
|
||
};
|
||
UI.prototype.hideEnd = function () {
|
||
if (this.el['end-overlay']) this.el['end-overlay'].classList.add('hidden');
|
||
};
|
||
|
||
UI.prototype.update = function (game) {
|
||
var ship = game.ship;
|
||
if (!ship) return;
|
||
var world = game.world;
|
||
var info = P.orbitInfo(world, ship.pos, ship.vel);
|
||
|
||
this._set('alt', Math.max(0, info.altitude).toFixed(0) + ' m');
|
||
this._set('spd', info.speed.toFixed(1) + ' m/s');
|
||
|
||
var f = ship.maxFuel > 0 ? ship.fuel / ship.maxFuel : 0;
|
||
this._set('fuel-text', ship.fuel.toFixed(0) + ' / ' + ship.maxFuel.toFixed(0));
|
||
var bar = this.el['fuel-bar'];
|
||
if (bar) bar.style.width = (f * 100).toFixed(1) + '%';
|
||
|
||
var boundEl = this.el['orbit-bound'];
|
||
if (boundEl) {
|
||
if (info.bound) {
|
||
boundEl.textContent = 'BOUND ORBIT';
|
||
boundEl.style.color = '#7dff9a';
|
||
} else {
|
||
boundEl.textContent = 'ESCAPE TRAJECTORY';
|
||
boundEl.style.color = '#ffb347';
|
||
}
|
||
}
|
||
this._set('orbit-peri', info.bound ? Math.max(0, info.periapsis - world.planetRadius).toFixed(0) + ' m' : '—');
|
||
this._set('orbit-apo', info.bound ? Math.max(0, info.apoapsis - world.planetRadius).toFixed(0) + ' m' : '∞');
|
||
this._set('orbit-ecc', info.eccentricity.toFixed(3));
|
||
this._set('orbit-period', info.bound ? info.period.toFixed(1) + ' s' : '∞');
|
||
|
||
var limited = game.timeScale < game.userTimeScale;
|
||
this._set('timescale', game.timeScale.toFixed(0) + 'x' + (limited ? ' (auto)' : ''));
|
||
var auto = this.el['auto-limit'];
|
||
if (auto) auto.classList.toggle('hidden', !limited);
|
||
|
||
var st = game.station;
|
||
if (st && (game.scenarioId === 'rendezvous' || game.scenarioId === 'loworbit')) {
|
||
var d = Math.sqrt(
|
||
(st.pos.x - ship.pos.x) * (st.pos.x - ship.pos.x) +
|
||
(st.pos.y - ship.pos.y) * (st.pos.y - ship.pos.y)
|
||
);
|
||
var rel = Math.sqrt(
|
||
(st.vel.x - ship.vel.x) * (st.vel.x - ship.vel.x) +
|
||
(st.vel.y - ship.vel.y) * (st.vel.y - ship.vel.y)
|
||
);
|
||
this._set('dock-dist', 'STATION ' + d.toFixed(0) + ' m');
|
||
this._set('dock-rel', 'REL ' + rel.toFixed(1) + ' m/s');
|
||
if (this.el['hud-dock']) this.el['hud-dock'].classList.remove('hidden');
|
||
} else if (this.el['hud-dock']) {
|
||
this.el['hud-dock'].classList.add('hidden');
|
||
}
|
||
|
||
// end / status transitions
|
||
if (game.status === 'crashed' && this.lastBound !== 'crashed') {
|
||
this.lastBound = 'crashed';
|
||
this.showEnd('CRASHED', 'Impact speed ' + (game.crashInfo ? game.crashInfo.impactSpeed.toFixed(0) : '?') + ' m/s. The planet wins this round.', false);
|
||
} else if (game.status === 'docked' && this.lastBound !== 'docked') {
|
||
this.lastBound = 'docked';
|
||
this.showEnd('DOCKED', 'Rendezvous complete. Welcome aboard the station, pilot.', true);
|
||
} else if (game.status === 'landed' && this.lastBound !== 'landed') {
|
||
this.lastBound = 'landed';
|
||
this.showEnd('LANDED', 'You set down gently. A fine landing — but the mission is orbit.', true);
|
||
} else if (game.status === 'flying') {
|
||
this.lastBound = null;
|
||
this.hideEnd();
|
||
}
|
||
|
||
var pauseEl = this.el['pause-overlay'];
|
||
if (pauseEl) pauseEl.classList.toggle('hidden', !game.paused || this.el.tutorial && !this.el.tutorial.classList.contains('hidden'));
|
||
|
||
var readyEl = this.el['ready-overlay'];
|
||
if (readyEl) readyEl.classList.toggle('hidden', game.status !== 'ready');
|
||
};
|
||
|
||
global.UI = UI;
|
||
})(typeof window !== 'undefined' ? window : globalThis);
|