1// What the server reports, plus the eased copies we actually draw: it says where a 2// thing is at tick rate, we only smooth the way it gets there. Nothing here decides 3// anything, and the camera that follows our ship rides along with it. 4import { connect, type State } from './net'; 5import { pull } from './pull'; 6import type { Body, Loose, Ore, Ship, Shot } from './sim'; 7import { cam, snap } from './stage'; 8 9export let state: State | null = null; 10export const ships = new Map<number, Ship>(); 11export const loose = new Map<number, Loose>(); 12export const ore = new Map<number, Ore>(); 13export const bodies = new Map<number, Body>(); 14export const shots = new Map<number, Shot>(); 15 16const ease = (a: number, b: number, k: number) => a + (b - a) * k; 17const turn = (a: number, b: number, k: number) => a + Math.atan2(Math.sin(b - a), Math.cos(b - a)) * k; 18 19// Everything drawn comes from the server. We only send intents: pick up what is 20// under the cursor, report the drag, drop it and let the server decide. 21const net = connect((s) => { 22 const was = state?.you; 23 state = s; 24 const me = s.players.find((p) => p.id === s.you); 25 // A ship we have never followed (the first, or the one we come back as) puts the 26 // camera on it at once, so a death never slides the view across the system. 27 if (me && s.you !== was) snap(me.x, me.y); 28}); 29export const send = net.send; 30 31// The ship we are flying, if we know it yet. 32export const own = (): Ship | undefined => (state ? ships.get(state.you) : undefined); 33 34// The server reports at tick rate; we ease our copies toward it so movement reads 35// smooth without ever deciding anything ourselves. 36export function apply(dt: number): void { 37 const k = 1 - Math.exp(-dt * 12); 38 if (state) { 39 const live = new Set<number>(); 40 for (const p of state.players) { 41 live.add(p.id); 42 let s = ships.get(p.id); 43 if (!s) { 44 ships.set(p.id, (s = { ...p })); 45 } else { 46 s.x = ease(s.x, p.x, k); 47 s.y = ease(s.y, p.y, k); 48 s.h = turn(s.h, p.h, k); 49 s.tx = p.tx; 50 s.ty = p.ty; 51 s.order = p.order; 52 s.fuel = p.fuel; // a gauge has to jump, not ease, or it lies about the tank 53 s.fuelmax = p.fuelmax; 54 s.foe = p.foe; 55 s.side = p.side; 56 s.pieces = p.pieces; // a hull is never eased: a piece is where the server says 57 } 58 } 59 for (const id of ships.keys()) if (!live.has(id)) ships.delete(id); 60 61 const looseLive = new Set<number>(); 62 for (const l of state.loose) { 63 looseLive.add(l.id); 64 const d = loose.get(l.id); 65 if (!d) loose.set(l.id, { ...l }); 66 else if (l.id === state.held && pull.to) { 67 d.x = pull.to.x; // in hand: exactly where the pointer put it, no easing 68 d.y = pull.to.y; 69 d.h = l.h; 70 } else { 71 d.x = ease(d.x, l.x, k); 72 d.y = ease(d.y, l.y, k); 73 d.h = turn(d.h, l.h, k); 74 } 75 } 76 for (const id of loose.keys()) if (!looseLive.has(id)) loose.delete(id); 77 78 const oreLive = new Set<number>(); 79 for (const o of state.ore) { 80 oreLive.add(o.id); 81 const d = ore.get(o.id); 82 if (!d) ore.set(o.id, { ...o }); 83 else { 84 d.x = ease(d.x, o.x, k); 85 d.y = ease(d.y, o.y, k); 86 d.h = turn(d.h, o.h, k); 87 d.towed = o.towed; 88 } 89 } 90 for (const id of ore.keys()) if (!oreLive.has(id)) ore.delete(id); 91 92 // A planet revolves at tick rate too, so its drawn copy is eased just like a 93 // hull: a body that jumped a whole tick would step its limb across cells. 94 const bodyLive = new Set<number>(); 95 for (const b of state.bodies) { 96 bodyLive.add(b.id); 97 const d = bodies.get(b.id); 98 bodies.set(b.id, d ? { ...b, x: ease(d.x, b.x, k), y: ease(d.y, b.y, k) } : { ...b }); 99 } 100 for (const id of bodies.keys()) if (!bodyLive.has(id)) bodies.delete(id); 101 102 const shotLive = new Set<number>(); 103 for (const s of state.shots ?? []) { 104 shotLive.add(s.id); 105 shots.set(s.id, s); // a round is never eased, it is where the server put it 106 } 107 for (const id of shots.keys()) if (!shotLive.has(id)) shots.delete(id); 108 } 109} 110 111// Keep our ship on screen, unless the player has panned away from it. The map is 112// a still picture of the whole galaxy: nothing to follow there. 113export function follow(dt: number): void { 114 const me = own(); 115 if (cam.scene !== 'local' || !cam.follow || !me) return; 116 const c = 1 - Math.exp(-dt * 8); 117 cam.x = ease(cam.x, me.x, c); 118 cam.y = ease(cam.y, me.y, c); 119}