Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ Built something you're proud of? The share panel exports your architecture two w
- **Hide HUD:** Press `H` to toggle UI panels.
- **Category Tabs:** The toolbar groups services into five tabs — Front Door, Compute, Data, Async, Ops. Keys `1-5` switch between them.
- **Build Tools:** `V` Select, `C` Link, `X` Demolish, `U` Unlink. Each key is shown in the corner of its button.
- **Connect Tool:** Click two nodes to create a connection (flow direction matters!).
- **Select Tool:** Click a node to see its links, outgoing in cyan and incoming in magenta. Drag it to move it.
- _Several at once:_ `Shift`+click adds a node to the selection. Drag any of them to move the group, press `Delete` to remove them, or choose Link or Unlink and click a node to apply it to all of them.
- **Connect Tool:** Click two nodes to create a connection (flow direction matters!). Hold `Shift` while clicking the destination to keep linking from the same source.
- _Valid Flows:_ Internet -> (GeoDNS/Firewall/CDN/API Gateway) -> Load Balancer -> Queue -> Compute -> Cache -> (Search Engine/Read Replica/SQL DB/NoSQL DB/Storage)
- **Delete Tool:** Remove services to recover 50% of the cost.
- **Time Controls:** Pause, Play (1x), and Fast Forward (3x).
Expand Down
9 changes: 8 additions & 1 deletion game.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ function resetGame(mode = "survival") {
// player did not draw.
STATE.activeTool = "select";
STATE.selectedNodeId = null;
STATE.selection.clear();
// A run that ended while the main menu was open left its own speed here,
// and Resume restores it: a new run could start at the old run's 2x.
STATE.previousTimeScale = 1;
Expand Down Expand Up @@ -1005,6 +1006,12 @@ window.startSandbox = () => {
window.setTool = (t) => {
STATE.activeTool = t;
STATE.selectedNodeId = null;
// A multi-selection carries into Link and Unlink, which act on all of it.
// A single node does not: every drag leaves one selected, and the next
// Link click would become a wire the player never asked for.
if (STATE.selection.size < 2 || !["select", "connect", "unlink"].includes(t)) {
STATE.selection.clear();
}
document
.querySelectorAll(".service-btn")
.forEach((b) => b.classList.remove("active"));
Expand Down Expand Up @@ -1469,7 +1476,7 @@ function animate(time) {
}

if (STATE.internetNode.ring) {
if (STATE.selectedNodeId === "internet") {
if (STATE.selectedNodeId === "internet" || STATE.selection.has("internet")) {
STATE.internetNode.ring.material.opacity = 1.0;
} else {
STATE.internetNode.ring.material.opacity = 0.2;
Expand Down
7 changes: 5 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1261,8 +1261,11 @@ <h3 data-i18n="tools_controls" class="text-lg font-bold text-blue-400 mb-4">TOOL
<strong data-i18n="select" class="text-white">SELECT</strong>
</div>
<p data-i18n="tool_select_desc" class="text-gray-400">
Inspect services to see load and stats. Click Compute/DB to
upgrade.
Click a node to see its links:
<span class="text-cyan-400">outgoing</span> and
<span class="text-fuchsia-400">incoming</span>. Drag to move it.
Shift+click selects several: drag them together, use Link or
Unlink and click a node to act on all of them, or press Delete.
</p>
</div>
<div class="bg-gray-800/30 p-3 rounded border border-gray-700">
Expand Down
9 changes: 5 additions & 4 deletions src/entities/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -802,30 +802,31 @@ export class Service {
// teaching one colour language in the campaign and another in survival
// would be worse than the bug.
const ringLoad = this.smoothedLoad;
const selected = STATE.selectedNodeId === this.id || STATE.selection.has(this.id);
if (ringLoad > CONFIG.load.ringRed) {
this.loadRing.material.color.setHex(0xff0000);
if (STATE.selectedNodeId === this.id) {
if (selected) {
this.loadRing.material.opacity = 1.0;
} else {
this.loadRing.material.opacity = 0.8;
}
} else if (ringLoad > CONFIG.load.ringOrange) {
this.loadRing.material.color.setHex(0xffaa00);
if (STATE.selectedNodeId === this.id) {
if (selected) {
this.loadRing.material.opacity = 1.0;
} else {
this.loadRing.material.opacity = 0.6;
}
} else if (ringLoad > CONFIG.load.ringYellow) {
this.loadRing.material.color.setHex(0xffff00);
if (STATE.selectedNodeId === this.id) {
if (selected) {
this.loadRing.material.opacity = 1.0;
} else {
this.loadRing.material.opacity = 0.4;
}
} else {
this.loadRing.material.color.setHex(0x00ff00);
if (STATE.selectedNodeId === this.id) {
if (selected) {
this.loadRing.material.opacity = 1.0;
} else {
this.loadRing.material.opacity = 0.3;
Expand Down
135 changes: 102 additions & 33 deletions src/input/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import {
deleteConnection,
deleteObject,
getConnectionAtPoint,
linkTargets,
nodeById,
snapToGrid,
updateConnectionsForNode,
} from "../sim/topology.js";
Expand Down Expand Up @@ -86,6 +88,8 @@ let isDraggingNode = false;
let draggedNode = null;
let dragOffset = new THREE.Vector3();
let dragStartPos = new THREE.Vector3();
// The rest of a multi-selection, moving with draggedNode: { node, start }.
let dragGroup = [];

let isPanning = false;
let lastMouseX = 0;
Expand Down Expand Up @@ -417,7 +421,19 @@ function handlePrimaryDown(clientX, clientY, preventDefault = () => {}, shiftKey
const i = getIntersect(clientX, clientY);
if (STATE.activeTool === "select") {
const i = getIntersect(clientX, clientY);
STATE.selectedNodeId = i.type === "ground" ? null : i.id;
const nodeId = i.type === "ground" ? null : i.id;
if (shiftKey) {
// Shift+click only edits the selection: no grab, no repair.
if (nodeId && !STATE.selection.delete(nodeId)) STATE.selection.add(nodeId);
preventDefault();
return;
}
// A click inside a multi-selection keeps it, to drag the group.
// finishNodeDrag narrows it to this node if nothing moved.
if (!STATE.selection.has(nodeId)) {
STATE.selection.clear();
if (nodeId) STATE.selection.add(nodeId);
}
if (i.type === "service") {
const svc = STATE.services.find((s) => s.id === i.id);
// Use criticalHealth from config for consistency
Expand All @@ -440,6 +456,11 @@ function handlePrimaryDown(clientX, clientY, preventDefault = () => {}, shiftKey
if (draggedNode) {
isDraggingNode = true;
dragStartPos.copy(draggedNode.position);
dragGroup = [...STATE.selection]
.filter((id) => id !== draggedNode.id)
.map(nodeById)
.filter(Boolean)
.map((node) => ({ node, start: node.position.clone() }));
const hit = getIntersect(clientX, clientY);
if (hit.pos) {
dragOffset.copy(draggedNode.position).sub(hit.pos);
Expand All @@ -451,8 +472,15 @@ function handlePrimaryDown(clientX, clientY, preventDefault = () => {}, shiftKey
} else if (STATE.activeTool === "delete" && i.type === "service")
deleteObject(i.id);
else if (STATE.activeTool === "unlink") {
const onNode = i.type === "service" || i.type === "internet";
const conn = getConnectionAtPoint(clientX, clientY);
if (conn) {
if (STATE.selection.size > 1 && onNode) {
if (unlinkSelectionFrom(i.id)) {
if (!shiftKey) STATE.selection.clear();
} else {
new Audio("assets/sounds/click-9.mp3").play();
}
} else if (conn) {
deleteConnection(conn.from, conn.to);
} else {
new Audio("assets/sounds/click-9.mp3").play();
Expand All @@ -461,7 +489,10 @@ function handlePrimaryDown(clientX, clientY, preventDefault = () => {}, shiftKey
STATE.activeTool === "connect" &&
(i.type === "service" || i.type === "internet")
) {
if (STATE.selectedNodeId) {
if (STATE.selection.size > 1) {
linkSelectionTo(i.id);
if (!shiftKey) STATE.selection.clear();
} else if (STATE.selectedNodeId) {
createConnection(STATE.selectedNodeId, i.id);
// Shift keeps the source, to wire one node to several (#89).
if (!shiftKey) STATE.selectedNodeId = null;
Expand Down Expand Up @@ -503,6 +534,23 @@ function handlePrimaryDown(clientX, clientY, preventDefault = () => {}, shiftKey
}
}

// Links every selected node to `targetId`. When none of them may, the first one
// tries anyway, because its refusal is what explains the rule to the player.
function linkSelectionTo(targetId) {
const sources = [...STATE.selection];
const accepted = sources.filter((id) => linkTargets(id).has(targetId));
(accepted.length ? accepted : sources.slice(0, 1)).forEach((id) => createConnection(id, targetId));
}

// Removes the links between `id` and the selection, in either direction.
function unlinkSelectionFrom(id) {
const links = STATE.connections.filter(
(c) => (STATE.selection.has(c.from) && c.to === id) || (c.from === id && STATE.selection.has(c.to))
);
links.forEach((c) => deleteConnection(c.from, c.to));
return links.length;
}

container.addEventListener("mousedown", (e) => {
if (!STATE.isRunning) return;

Expand Down Expand Up @@ -547,20 +595,13 @@ function handlePointerMove(clientX, clientY) {
const newPos = hit.pos.clone().add(dragOffset);
newPos.y = 0;

draggedNode.position.copy(newPos);

if (draggedNode === STATE.internetNode) {
STATE.internetNode.mesh.position.x = newPos.x;
STATE.internetNode.mesh.position.z = newPos.z;
STATE.internetNode.ring.position.x = newPos.x;
STATE.internetNode.ring.position.z = newPos.z;
} else if (draggedNode.mesh) {
draggedNode.mesh.position.x = newPos.x;
draggedNode.mesh.position.z = newPos.z;
moveNodeTo(draggedNode, newPos);
for (const { node, start } of dragGroup) {
const pos = start.clone().add(newPos).sub(dragStartPos);
pos.y = 0;
moveNodeTo(node, pos);
}

updateConnectionsForNode(draggedNode.id);

container.style.cursor = "grabbing";
}
return;
Expand Down Expand Up @@ -747,7 +788,7 @@ function handlePointerMove(clientX, clientY) {
}
content += `</div>`;

if (STATE.activeTool === "select" && STATE.selectedNodeId === s.id) {
if (STATE.activeTool === "select" && STATE.selection.has(s.id)) {
content += `<div class="mt-1 border-t border-gray-700 pt-1">${selectedLinksLine(s.id)}</div>`;
}

Expand Down Expand Up @@ -928,36 +969,51 @@ window.addEventListener("toolbarRendered", setupUITooltips);
// if nothing is being dragged. Shared by every way a grab can end — mouse
// release, touch release, touch cancel — since it only ever acts on the
// module's own drag state and takes nothing from the event that ended it.
function moveNodeTo(node, pos) {
node.position.copy(pos);

if (node === STATE.internetNode) {
STATE.internetNode.mesh.position.x = pos.x;
STATE.internetNode.mesh.position.z = pos.z;
STATE.internetNode.ring.position.x = pos.x;
STATE.internetNode.ring.position.z = pos.z;
} else if (node.mesh) {
node.mesh.position.x = pos.x;
node.mesh.position.z = pos.z;
}

updateConnectionsForNode(node.id);
}

function finishNodeDrag() {
if (!(isDraggingNode && draggedNode)) return;
isDraggingNode = false;

let snapped = snapToGrid(draggedNode.position);
const home = snapToGrid(dragStartPos);
const snapped = snapToGrid(draggedNode.position);
const group = dragGroup;
dragGroup = [];
// Every member lands the same whole number of tiles from where it started.
const landings = group.map(({ start }) => snapToGrid(start.clone().add(snapped).sub(home)));

// Reject a drop onto a tile already occupied by another service —
// otherwise the two overlap and whichever mesh the raycaster hits first
// makes the other permanently unselectable (can't delete/upgrade it).
const occupied = STATE.services.some(
(s) => s !== draggedNode && s.position.distanceTo(snapped) < 1
// One blocked member sends the whole group back: a group that lands apart
// is not the shape the player was moving.
const moving = new Set([draggedNode, ...group.map((m) => m.node)]);
const occupied = [snapped, ...landings].some((pos) =>
STATE.services.some((s) => !moving.has(s) && s.position.distanceTo(pos) < 1)
);
if (occupied) {
snapped = snapToGrid(dragStartPos);
}

draggedNode.position.copy(snapped);
moveNodeTo(draggedNode, occupied ? home : snapped);
group.forEach(({ node, start }, k) => moveNodeTo(node, occupied ? snapToGrid(start) : landings[k]));

if (draggedNode === STATE.internetNode) {
STATE.internetNode.mesh.position.x = snapped.x;
STATE.internetNode.mesh.position.z = snapped.z;
STATE.internetNode.ring.position.x = snapped.x;
STATE.internetNode.ring.position.z = snapped.z;
} else if (draggedNode.mesh) {
draggedNode.mesh.position.x = snapped.x;
draggedNode.mesh.position.z = snapped.z;
if (group.length && !occupied && snapped.distanceTo(home) < 1) {
STATE.selection.clear();
STATE.selection.add(draggedNode.id);
}

updateConnectionsForNode(draggedNode.id);

draggedNode = null;
container.style.cursor = "default";
}
Expand Down Expand Up @@ -1001,6 +1057,7 @@ export function endPointerInteraction() {
const wasActive = isDraggingNode || isPanning || isOrbiting;
isDraggingNode = false;
draggedNode = null;
dragGroup = [];
isPanning = false;
isOrbiting = false;
if (container) container.style.cursor = "default";
Expand Down Expand Up @@ -1228,6 +1285,18 @@ document.addEventListener("keydown", (event) => {
return;
}
if (isTypingTarget(event.target)) return;
// Backspace is what a Mac keyboard calls its Delete key.
if (
(event.key === "Delete" || event.key === "Backspace") &&
STATE.activeTool === "select" &&
STATE.isRunning &&
!window.tutorial?.isActive &&
document.getElementById("main-menu-modal").classList.contains("hidden")
) {
[...STATE.selection].forEach(deleteObject);
STATE.selection.clear();
return;
}
if (event.key === "H" || event.key === "h") {
document.getElementById("statsPanel").classList.toggle("hidden");
document.getElementById("detailsPanel").classList.toggle("hidden");
Expand Down
2 changes: 1 addition & 1 deletion src/locales/de.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ export const DE_TRANSLATIONS = {
"cache_desc_short": "Cache-Layer ($60)",
"storage_desc_short": "Speichert Web-Daten ($25)",
"tools_controls": "WERKZEUGE & STEUERUNG",
"tool_select_desc": "Inspiziere Dienste für Last und Statistiken. Klicke Compute/DB zum Upgraden.",
"tool_select_desc": "Klicke auf einen Knoten, um seine Verbindungen zu sehen: <span class=\"text-cyan-400\">ausgehende</span> und <span class=\"text-fuchsia-400\">eingehende</span>. Ziehen verschiebt ihn. Shift+Klick wählt mehrere aus: gemeinsam ziehen, mit Verbinden oder Trennen einen Knoten anklicken, um auf alle zu wirken, oder Entf drücken.",
"tool_connect_desc": "Verbinde Knoten. Klicke <span class=\"text-green-400\">Quelle</span> dann <span class=\"text-blue-400\">Ziel</span>. Halte Shift beim Klick auf das Ziel, um von derselben Quelle weiter zu verbinden.",
"tool_delete_desc": "Entferne Komponenten oder Verbindungen. Komponenten erstatten 50% der Kosten.",
"tool_unlink": "TRENNEN",
Expand Down
2 changes: 1 addition & 1 deletion src/locales/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ export const EN_TRANSLATIONS = {
"cache_desc_short": "Cache layer ($60)",
"storage_desc_short": "Stores Web Data ($25)",
"tools_controls": "TOOLS & CONTROLS",
"tool_select_desc": "Inspect services to see load and stats. Click Compute/DB to upgrade.",
"tool_select_desc": "Click a node to see its links: <span class=\"text-cyan-400\">outgoing</span> and <span class=\"text-fuchsia-400\">incoming</span>. Drag to move it. Shift+click selects several: drag them together, use Link or Unlink and click a node to act on all of them, or press Delete.",
"tool_connect_desc": "Link nodes together. Click <span class=\"text-green-400\">Source</span> then <span class=\"text-blue-400\">Destination</span>. Hold Shift while clicking the destination to keep linking from the same source.",
"tool_delete_desc": "Remove components or links. Components refund 50% of their cost.",
"tool_unlink": "UNLINK",
Expand Down
2 changes: 1 addition & 1 deletion src/locales/fr.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ export const FR_TRANSLATIONS = {
"cache_desc_short": "Couche de cache ($60)",
"storage_desc_short": "Stocke les données web ($25)",
"tools_controls": "OUTILS & CONTRÔLES",
"tool_select_desc": "Inspectez les services pour voir la charge et les statistiques. Cliquez sur Calcul/DB pour améliorer.",
"tool_select_desc": "Cliquez sur un nœud pour voir ses liens : <span class=\"text-cyan-400\">sortants</span> et <span class=\"text-fuchsia-400\">entrants</span>. Faites-le glisser pour le déplacer. Maj+clic en sélectionne plusieurs : déplacez-les ensemble, utilisez Lien ou Délier puis cliquez sur un nœud pour agir sur tous, ou appuyez sur Suppr.",
"tool_connect_desc": "Liez les nœuds ensemble. Cliquez sur <span class=\"text-green-400\">Source</span> puis <span class=\"text-blue-400\">Destination</span>. Maintenez Maj en cliquant sur la destination pour continuer à lier depuis la même source.",
"tool_delete_desc": "Supprimez les composants ou les liens. Les composants remboursent 50% de leur coût.",
"tool_unlink": "DÉLIER",
Expand Down
2 changes: 1 addition & 1 deletion src/locales/hi.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ export const HI_TRANSLATIONS = {
"cache_desc_short": "Cache layer ($60)",
"storage_desc_short": "Web data store करता है ($25)",
"tools_controls": "TOOLS और CONTROLS",
"tool_select_desc": "Services inspect करके load और stats देखें। Upgrade के लिए Compute/DB पर क्लिक करें।",
"tool_select_desc": "किसी node पर क्लिक करके उसके links देखें: <span class=\"text-cyan-400\">outgoing</span> और <span class=\"text-fuchsia-400\">incoming</span>। Drag करके उसे हिलाएँ। Shift+क्लिक से कई nodes चुनें: उन्हें साथ में drag करें, Link या Unlink चुनकर किसी node पर क्लिक करें ताकि सभी पर लागू हो, या Delete दबाएँ।",
"tool_connect_desc": "Nodes को आपस में link करें। पहले <span class=\"text-green-400\">Source</span> पर क्लिक करें, फिर <span class=\"text-blue-400\">Destination</span> पर। Destination पर क्लिक करते समय Shift दबाए रखें, ताकि उसी Source से आगे भी link कर सकें।",
"tool_delete_desc": "Components या links हटाएँ। Components की 50% लागत वापस मिलती है।",
"tool_unlink": "UNLINK",
Expand Down
Loading
Loading