Upload src/main.ts
Browse files- src/main.ts +105 -0
src/main.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import './style.css';
|
| 2 |
+
import {createState,SYSTEMS} from './sim/state';
|
| 3 |
+
import {type System,log} from './sim/state';
|
| 4 |
+
import {tick} from './sim/physics';
|
| 5 |
+
import {telemetry,warnings,clock} from './sim/telemetry';
|
| 6 |
+
import {readiness,launch,stage,abort,toggleSystem} from './sim/mission';
|
| 7 |
+
import {STEP,clamp} from './sim/math';
|
| 8 |
+
import {cockpit} from './ui/cockpit';
|
| 9 |
+
import {horizon,orbitMap} from './ui/instruments';
|
| 10 |
+
import {World,type CameraMode} from './render/world';
|
| 11 |
+
import {FlightAudio} from './audio';
|
| 12 |
+
import {input} from './input';
|
| 13 |
+
|
| 14 |
+
document.querySelector('#app')!.innerHTML=cockpit();
|
| 15 |
+
let state=createState();
|
| 16 |
+
const $=<T extends HTMLElement=HTMLElement>(id:string)=>document.getElementById(id) as T;
|
| 17 |
+
const textCache=new Map<string,string>();
|
| 18 |
+
const text=(id:string,value:string)=>{if(textCache.get(id)!==value){$(id).textContent=value;textCache.set(id,value);}};
|
| 19 |
+
const audio=new FlightAudio();
|
| 20 |
+
let world:World|null=null;
|
| 21 |
+
try{world=new World($('world'));}catch(error){console.error('3D renderer unavailable',error);$('renderer-error').hidden=false;}
|
| 22 |
+
let completedDismissed=false,engineering=false,tab='flight',manualPreviousPause=false;
|
| 23 |
+
function setTab(next:string){tab=next;engineering=next==='engineering';$('engineering').hidden=!engineering;document.querySelector<HTMLElement>('.console')!.hidden=engineering;document.querySelector<HTMLElement>('.console')!.dataset.tab=next;document.querySelectorAll<HTMLButtonElement>('[data-tab]').forEach(b=>b.classList.toggle('selected',b.dataset.tab===next));}
|
| 24 |
+
function pause(){state.paused=!state.paused;audio.tone(380);updateUI();}
|
| 25 |
+
function reset(){state=createState();completedDismissed=false;textCache.clear();audio.lastCount=-1;audio.lastPhase='';if(world)world.setMode('COCKPIT');setCamera('COCKPIT');updateUI();}
|
| 26 |
+
function doLaunch(){if(launch(state))audio.tone(700,.2);updateUI();}
|
| 27 |
+
function doStage(){if(stage(state)){audio.tone(240,.3);}else log(state,'Stage interlock: below 10% fuel and above 1 km required.');updateUI();}
|
| 28 |
+
function setCamera(mode:CameraMode){world?.setMode(mode);document.querySelector<HTMLElement>('.flightdeck')!.dataset.view=mode;document.querySelectorAll<HTMLButtonElement>('[data-camera]').forEach(b=>{const selected=b.dataset.camera===mode;b.classList.toggle('selected',selected);b.setAttribute('aria-pressed',String(selected));});text('view-label',mode+(mode==='COCKPIT'?' / PILOT SEAT':' / OBSERVATION'));}
|
| 29 |
+
|
| 30 |
+
const controls=input(()=>state,{launch:doLaunch,stage:doStage,pause,reset,engineering:()=>setTab(engineering?'flight':'engineering')},(x,y)=>{if(world){world.look.x=clamp(world.look.x+x,-1,1);world.look.y=clamp(world.look.y+y,-1,1);}});
|
| 31 |
+
$('preflight').onclick=()=>{if(state.phase==='PRELAUNCH'){state.preparing=true;state.prepareTime=0;audio.tone();}};
|
| 32 |
+
document.querySelectorAll<HTMLButtonElement>('[data-system]').forEach(b=>b.onclick=()=>{toggleSystem(state,b.dataset.system as System);audio.tone();updateUI();});
|
| 33 |
+
$('engine-arm').onclick=()=>{if(state.systems.power&&state.systems.propulsion){state.engineArmed=!state.engineArmed;if(!state.engineArmed)state.launchArmed=false;audio.tone();}else log(state,'POWER and PROPULSION required before engine arming.');updateUI();};
|
| 34 |
+
$('launch-arm').onclick=()=>{if(SYSTEMS.every(k=>state.systems[k])&&state.engineArmed){state.launchArmed=!state.launchArmed;audio.tone();}else log(state,'Complete preflight and arm the engine first.');updateUI();};
|
| 35 |
+
$('launch').onclick=doLaunch;$('stage').onclick=doStage;$('abort').onclick=()=>{abort(state);audio.tone(180,.3);updateUI();};
|
| 36 |
+
$('guidance').onclick=()=>{state.guidance=!state.guidance;audio.tone();updateUI();};
|
| 37 |
+
$('throttle').oninput=()=>{state.guidance=false;state.throttle=Number($<HTMLInputElement>('throttle').value)/100;updateUI();};
|
| 38 |
+
$('pace').onchange=()=>state.speed=Number($<HTMLSelectElement>('pace').value);
|
| 39 |
+
$('engineering-toggle').onclick=()=>setTab(engineering?'flight':'engineering');
|
| 40 |
+
$('pause').onclick=pause;$('resume').onclick=pause;$('reset').onclick=reset;
|
| 41 |
+
$('audio').onclick=async()=>{try{const enabled=await audio.toggle();$('audio').innerHTML='AUDIO <span>'+(enabled?'ON':'OFF')+'</span>';if(enabled)audio.tone();}catch{log(state,'Audio unavailable. Visual flight indications remain active.');}};
|
| 42 |
+
const manual=$<HTMLDialogElement>('manual');
|
| 43 |
+
$('help').onclick=()=>{manualPreviousPause=state.paused;state.paused=true;manual.showModal();};
|
| 44 |
+
const closeManual=()=>{manual.close();state.paused=manualPreviousPause;};$('close-manual').onclick=closeManual;$('manual-ready').onclick=closeManual;manual.addEventListener('cancel',()=>state.paused=manualPreviousPause);
|
| 45 |
+
document.querySelectorAll<HTMLButtonElement>('[data-camera]').forEach(b=>b.onclick=()=>{setCamera(b.dataset.camera as CameraMode);audio.tone();});
|
| 46 |
+
document.querySelectorAll<HTMLButtonElement>('[data-tab]').forEach(b=>b.onclick=()=>setTab(b.dataset.tab!));
|
| 47 |
+
$('dismiss-complete').onclick=()=>{completedDismissed=true;};
|
| 48 |
+
document.addEventListener('visibilitychange',()=>{if(document.hidden)state.paused=true;});
|
| 49 |
+
|
| 50 |
+
function updateUI(){
|
| 51 |
+
const t=telemetry(state),ws=warnings(state);
|
| 52 |
+
text('phase',state.phase);text('alt',(t.altitude/1000).toFixed(2));text('velocity',(t.speed/1000).toFixed(3));
|
| 53 |
+
text('vs',(t.vertical>=0?'↑ ':'↓ ')+Math.abs(t.vertical).toFixed(0)+' M/S');text('gforce',state.g.toFixed(2)+' G');
|
| 54 |
+
text('heading',String(Math.round(state.yaw)).padStart(3,'0'));text('pitch',state.pitch.toFixed(1)+'°');text('roll',((state.roll+180)%360-180).toFixed(1)+'°');
|
| 55 |
+
text('apo',Number.isFinite(t.apoapsis)?Math.max(-6371,t.apoapsis/1000).toFixed(0):'ESC');
|
| 56 |
+
text('peri',(t.periapsis/1000).toFixed(0));text('thrust',(state.thrust/1e6).toFixed(2)+' MN');text('fuel',t.fuelPercent.toFixed(1)+' %');
|
| 57 |
+
text('temperature',state.engineTemperature.toFixed(0)+' °C');text('cabin',state.cabinPressure.toFixed(1));text('battery',state.battery.toFixed(0));text('met',clock(state.time));
|
| 58 |
+
text('throttle-value',Math.round(state.throttle*100)+'%');$<HTMLInputElement>('throttle').value=String(Math.round(state.throttle*100));$<HTMLSelectElement>('pace').value=String(state.speed);
|
| 59 |
+
$('fuel-bar').style.width=t.fuelPercent+'%';document.documentElement.style.setProperty('--burn',state.actualThrottle.toFixed(2));
|
| 60 |
+
text('stage-indicator','S'+(state.stage+1));text('systems-count',SYSTEMS.filter(k=>state.systems[k]).length+' / 7');
|
| 61 |
+
const context=state.phase==='PRELAUNCH'?(state.preparing?'Running vehicle checks…':readiness(state)==='READY FOR IGNITION'?'All stations go. Initiate launch when ready.':readiness(state)+'.'):
|
| 62 |
+
state.phase==='COUNTDOWN'?'All systems go. Hold for ignition.':state.phase==='ABORT'?state.abortReason+' / recovery active.':
|
| 63 |
+
state.phase==='IMPACT'?state.abortReason+' Reset to fly again.':state.phase==='LANDED'?'Capsule recovered. Reset to fly again.':state.orbitAchieved?'Stable orbit established. Continue with manual free flight.':
|
| 64 |
+
state.stage===0?'Ascent underway. Next event: first-stage separation.':'Upper stage active. Guidance targeting a stable low Earth orbit.';
|
| 65 |
+
text('context',context);
|
| 66 |
+
$('countdown').hidden=state.phase!=='COUNTDOWN';$('countdown').querySelector('strong')!.textContent=String(Math.ceil(state.countdown));
|
| 67 |
+
$('completion').hidden=!state.orbitAchieved||completedDismissed;
|
| 68 |
+
$('paused').hidden=!state.paused||manual.open;
|
| 69 |
+
$('pause').setAttribute('aria-label',state.paused?'Resume simulation':'Pause simulation');
|
| 70 |
+
$('guidance').setAttribute('aria-pressed',String(state.guidance));$('guidance').classList.toggle('active',state.guidance);$('guidance').querySelector('b')!.textContent=state.guidance?'ENGAGED':'MANUAL';
|
| 71 |
+
$('engine-arm').setAttribute('aria-pressed',String(state.engineArmed));$('launch-arm').setAttribute('aria-pressed',String(state.launchArmed));
|
| 72 |
+
$<HTMLButtonElement>('launch').disabled=state.phase!=='PRELAUNCH'||readiness(state)!=='READY FOR IGNITION';
|
| 73 |
+
$<HTMLButtonElement>('stage').disabled=state.stage!==0||state.fuel[0]>36000||t.altitude<1000||state.phase==='ABORT';
|
| 74 |
+
$<HTMLButtonElement>('preflight').disabled=state.phase!=='PRELAUNCH'||state.preparing;
|
| 75 |
+
document.querySelectorAll<HTMLButtonElement>('[data-system]').forEach(b=>{const enabled=state.systems[b.dataset.system as System];b.setAttribute('aria-pressed',String(enabled));b.querySelector('small')!.textContent=enabled?'ON':'OFF';});
|
| 76 |
+
const warningHTML=ws.slice(0,3).map(w=>'<span class="'+w.level+'">'+w.text+'</span>').join('');
|
| 77 |
+
if($('warnings').innerHTML!==warningHTML)$('warnings').innerHTML=warningHTML;
|
| 78 |
+
text('comms',state.events.at(-1)!.text);
|
| 79 |
+
if(engineering)text('engineering-data',[
|
| 80 |
+
'TIMESTEP '+STEP.toFixed(6)+' s','THRUST '+state.thrust.toFixed(0)+' N','MASS '+state.mass.toFixed(0)+' kg',
|
| 81 |
+
'FUEL FLOW '+state.fuelFlow.toFixed(2)+' kg/s','GRAVITY '+t.gravity.toFixed(4)+' m/s²','DRAG '+state.drag.toFixed(1)+' N',
|
| 82 |
+
'DYNAMIC Q '+(state.q/1000).toFixed(2)+' kPa','DENSITY '+t.density.toExponential(3)+' kg/m³',
|
| 83 |
+
'ACCELERATION '+state.acceleration.map(x=>x.toFixed(2)).join(', ')+' m/s²','POSITION '+state.position.map(x=>x.toFixed(0)).join(', ')+' m',
|
| 84 |
+
'VELOCITY '+state.velocity.map(x=>x.toFixed(1)).join(', ')+' m/s','ATTITUDE '+[state.pitch,state.yaw,state.roll].map(x=>x.toFixed(2)).join(', ')+' deg',
|
| 85 |
+
'ECCENTRICITY '+t.eccentricity.toFixed(6),'ORBITAL SPEED '+t.orbitalVelocity.toFixed(2)+' m/s','BATTERY '+state.battery.toFixed(2)+' %',
|
| 86 |
+
'CABIN TEMP '+state.cabinTemperature.toFixed(2)+' °C','APOAPSIS '+t.apoapsis.toFixed(0)+' m','PERIAPSIS '+t.periapsis.toFixed(0)+' m'
|
| 87 |
+
].join('\n'));
|
| 88 |
+
audio.update(state,ws.length>0);
|
| 89 |
+
}
|
| 90 |
+
let last=performance.now(),accumulator=0,uiTime=0,mapTime=0,raf=0,disposed=false;
|
| 91 |
+
function frame(now:number){
|
| 92 |
+
if(disposed)return;
|
| 93 |
+
const dt=Math.min((now-last)/1000,.1);last=now;controls.update(dt);
|
| 94 |
+
if(!state.paused){const pace=['PRELAUNCH','COUNTDOWN'].includes(state.phase)?1:state.speed;accumulator=Math.min(accumulator+dt*pace,STEP*100);
|
| 95 |
+
let iterations=0;while(accumulator>=STEP&&iterations<100){tick(state);accumulator-=STEP;iterations++;}}
|
| 96 |
+
else accumulator=0;
|
| 97 |
+
world?.render(state,dt);
|
| 98 |
+
uiTime+=dt;mapTime+=dt;
|
| 99 |
+
if(uiTime>=.08){updateUI();horizon($<HTMLCanvasElement>('horizon'),state);uiTime=0;}
|
| 100 |
+
if(mapTime>=.4){orbitMap($<HTMLCanvasElement>('orbit-map'),state);mapTime=0;}
|
| 101 |
+
raf=requestAnimationFrame(frame);
|
| 102 |
+
}
|
| 103 |
+
Object.defineProperty(window,'orbitalTelemetry',{get:()=>({...structuredClone(state),navigation:telemetry(state)})});
|
| 104 |
+
updateUI();horizon($<HTMLCanvasElement>('horizon'),state);orbitMap($<HTMLCanvasElement>('orbit-map'),state);raf=requestAnimationFrame(frame);
|
| 105 |
+
window.addEventListener('pagehide',()=>{disposed=true;cancelAnimationFrame(raf);controls.dispose();world?.dispose();audio.dispose();},{once:true});
|