space-browser / commits / 45f983a
fix: a series of fixes around the UI and core mechanics
@@ -200,11 +200,10 @@ export class HUD {
200200
201201 // --- radio ----------------------------------------------------------------
202202
203- addRadio(from: string, text: string, ch: string) {
203+ addRadio(from: string, text: string) {
204204 const line = el("div", "line");
205205 line.append(el("span", "from", from + ": "));
206206 line.append(document.createTextNode(text));
207- line.append(el("span", "ch", ` [${ch}]`));
208207 this.radioLog.append(line);
209208 while (this.radioLog.childElementCount > 8) this.radioLog.firstElementChild?.remove();
210209 }
@@ -158,7 +158,7 @@ class Game {
158158 this.hud.toast(m.msg);
159159 break;
160160 case "radio":
161- this.hud.addRadio(m.from, m.text, m.ch);
161+ this.hud.addRadio(m.from, m.text);
162162 break;
163163 case "error":
164164 if (this.onboarding) this.onboarding.error(m.msg);
@@ -291,6 +291,10 @@ class Game {
291291 el.addEventListener("pointerup", (e) => {
292292 const d = this.drag;
293293 this.drag = null;
294+ if (this.attachItem) {
295+ if (e.button === 0) this.placePiece(this.camera.screenToWorld(e.clientX, e.clientY));
296+ return;
297+ }
294298 if (!d || d.moved) return;
295299 if (e.button === 0) this.leftClick(e.clientX, e.clientY, e.shiftKey);
296300 });
@@ -335,12 +339,9 @@ class Game {
335339 return;
336340 }
337341 const world = this.camera.screenToWorld(sx, sy);
338- if (this.attachItem) {
339- this.placePiece(world);
340- return;
341- }
342342 const hit = this.pick(sx, sy);
343343 if (hit) {
344+ if (hit.kind === "piece") return;
344345 if (hit.kind === "npc" || (hit.kind === "player" && hit.id !== this.selfId)) {
345346 this.follow = true;
346347 this.send({ t: "attack", target: hit.id });
@@ -463,6 +464,22 @@ class Game {
463464 }
464465 };
465466 const ppu = this.camera.pixelsPerUnit();
467+ if (this.self) {
468+ const a = this.self.a;
469+ for (const p of Object.values(this.self.ship.pieces)) {
470+ const wx = this.self.x + Math.cos(a) * p.x - Math.sin(a) * p.y;
471+ const wy = this.self.y + Math.sin(a) * p.x + Math.cos(a) * p.y;
472+ const s = this.camera.worldToScreen(wx, wy);
473+ consider({
474+ kind: "piece",
475+ id: String(p.id),
476+ sx: s.x,
477+ sy: s.y,
478+ screenR: Math.max(8, 20 * ppu),
479+ hint: `${p.shape} ${p.q}\u2605 ${Math.round(p.hp)}/${Math.round(p.maxHp)}`,
480+ });
481+ }
482+ }
466483 for (const p of this.players.values()) {
467484 const s = this.camera.worldToScreen(p.x, p.y);
468485 consider({
@@ -522,6 +539,7 @@ class Game {
522539 else if (hit.kind === "item") this.itemMenu(sx, sy, hit.id);
523540 else if (hit.kind === "player") this.playerMenu(sx, sy, hit.id);
524541 else if (hit.kind === "npc") this.npcMenu(sx, sy, hit.id);
542+ else if (hit.kind === "piece") this.pieceMenu(sx, sy, Number(hit.id));
525543 }
526544
527545 private poiMenu(sx: number, sy: number, id: string) {
@@ -578,7 +596,7 @@ class Game {
578596 action: () => {
579597 this.attachItem = id;
580598 this.follow = true;
581- this.hud.toast("move to a glowing spot and click");
599+ this.hud.toast("drag the piece onto a glowing spot");
582600 },
583601 });
584602 }
@@ -607,80 +625,61 @@ class Game {
607625 ]);
608626 }
609627
628+ private atShipyard(): boolean {
629+ return [...this.pois.values()].some((p) => p.kind === "shipyard" && this.inOrbit(p));
630+ }
631+
610632 private selfMenu(sx: number, sy: number) {
611633 if (!this.self) return;
612- const atShipyard = [...this.pois.values()].some(
613- (p) => p.kind === "shipyard" && this.inOrbit(p),
614- );
615- const items: CtxItem[] = [
616- { label: "detach piece", disabled: false, action: () => this.pieceMenu(sx, sy) },
634+ this.hud.openContext(sx, sy, this.self.callsign, [
617635 { label: "stop", action: () => this.send({ t: "stop" }) },
618- ];
619- if (atShipyard) {
636+ {
637+ label: "self-destruct",
638+ action: () =>
639+ this.hud.confirm("Self-destruct and leave a wreck? This ends your run.", () =>
640+ this.send({ t: "selfdestruct" }),
641+ ),
642+ },
643+ ]);
644+ }
645+
646+ private pieceMenu(sx: number, sy: number, id: number) {
647+ if (!this.self) return;
648+ const piece = this.self.ship.pieces[String(id)];
649+ if (!piece) return;
650+ const shipyard = this.atShipyard();
651+ const items: CtxItem[] = [];
652+ if (id !== this.self.ship.root) {
653+ items.push({ label: "detach piece", action: () => this.send({ t: "detach", piece: id }) });
654+ }
655+ if (shipyard && piece.q < 3) {
620656 items.push({
621- label: "upgrade piece",
622- action: () => this.upgradeMenu(sx, sy),
657+ label: `upgrade to ${piece.q + 1}\u2605 (${this.cfg.cargo.upgradeCostPerLevel * piece.q} ore)`,
658+ action: () => this.send({ t: "upgrade", piece: id }),
623659 });
660+ }
661+ if (shipyard && piece.hp < piece.maxHp) {
624662 items.push({
625- label: "repair piece",
626- action: () => this.repairMenu(sx, sy),
663+ label: `repair (${this.cfg.cargo.repairCost} ore)`,
664+ action: () => this.send({ t: "repair", piece: id }),
627665 });
628666 }
629- items.push({
630- label: "self-destruct",
631- action: () =>
632- this.hud.confirm("Self-destruct and leave a wreck? This ends your run.", () =>
633- this.send({ t: "selfdestruct" }),
634- ),
635- });
636- this.hud.openContext(sx, sy, this.self.callsign, items);
637- }
638-
639- private pieceMenu(sx: number, sy: number) {
640- if (!this.self) return;
641- const pieces = Object.values(this.self.ship.pieces).filter((p) => p.id !== this.self!.ship.root);
642- this.hud.openContext(
643- sx,
644- sy,
645- "detach which piece",
646- pieces.map((p) => ({
647- label: `#${p.id} ${p.shape} ${Math.round(p.hp)}hp`,
648- action: () => this.send({ t: "detach", piece: p.id }),
649- })),
650- );
651- }
652-
653- private upgradeMenu(sx: number, sy: number) {
654- if (!this.self) return;
655- const pieces = Object.values(this.self.ship.pieces).filter((p) => p.q < 3);
656- this.hud.openContext(
657- sx,
658- sy,
659- "upgrade which piece",
660- pieces.map((p) => ({
661- label: `#${p.id} ${p.shape} ${p.q}\u2605 -> ${p.q + 1}\u2605 (${this.cfg.cargo.upgradeCostPerLevel * p.q} ore)`,
662- action: () => this.send({ t: "upgrade", piece: p.id }),
663- })),
664- );
667+ if (id === this.self.ship.root) {
668+ items.push({
669+ label: "self-destruct",
670+ action: () =>
671+ this.hud.confirm("Self-destruct and leave a wreck? This ends your run.", () =>
672+ this.send({ t: "selfdestruct" }),
673+ ),
674+ });
675+ }
676+ this.hud.openContext(sx, sy, `#${id} ${piece.shape} ${piece.q}\u2605`, items);
665677 }
666678
667- private repairMenu(sx: number, sy: number) {
668- if (!this.self) return;
669- const pieces = Object.values(this.self.ship.pieces).filter((p) => p.hp < p.maxHp);
670- this.hud.openContext(
671- sx,
672- sy,
673- "repair which piece",
674- pieces.map((p) => ({
675- label: `#${p.id} ${p.shape} ${Math.round(p.hp)}/${Math.round(p.maxHp)}`,
676- action: () => this.send({ t: "repair", piece: p.id }),
677- })),
678- );
679- }
680679 }
681680
682681 interface Pick {
683- kind: "poi" | "item" | "player" | "npc";
682+ kind: "poi" | "item" | "player" | "npc" | "piece";
684683 id: string;
685684 sx: number;
686685 sy: number;
@@ -291,7 +291,15 @@ func (w *World) attach(p *Player, itemID string, to int, x, y float64) {
291291 }
292292 p.Ship.nextID++
293293 p.Ship.add(piece)
294+ // a piece must attach to one or more existing pieces: always the anchor,
295+ // plus every other piece it lands adjacent to.
294296 p.Ship.connect(to, piece.ID)
297+ link := config.Ship.MinSeparation * 1.2
298+ for _, q := range p.Ship.Pieces {
299+ if q.ID != piece.ID && np.Dist(Vec2{q.X, q.Y}) <= link {
300+ p.Ship.connect(q.ID, piece.ID)
301+ }
302+ }
295303 delete(w.items, itemID)
296304 }