364 lines
22 KiB
C#
364 lines
22 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using KernelPanic.Combat;
|
|
using KernelPanic.Run;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace KernelPanic.UI
|
|
{
|
|
internal static class RunMapView
|
|
{
|
|
public static VisualElement Build(RunManager run)
|
|
{
|
|
var layout = new VisualElement { name = "RunMap" };
|
|
layout.AddToClassList("route-map-layout");
|
|
var map = new VisualElement { name = "RouteGraph" };
|
|
map.AddToClassList("route-map-graph");
|
|
if (run.Journey.mapStartWave > 2)
|
|
map.Add(Text("Earlier decisions are unavailable for this older save. Your map starts here.", "route-map-legend"));
|
|
|
|
int current = run.CurrentWaveNumber, seed = run.CurrentConfig.RunSeed;
|
|
int revision = run.Journey.mapRevision, startWave = run.Journey.mapStartWave;
|
|
int first = run.Journey.endless ? Math.Max(startWave, current - 3) : startWave;
|
|
int last = run.Journey.endless ? current + 5 : run.Journey.FinalWave;
|
|
IReadOnlyList<RouteNode> Row(int wave) => RunRouteMap.Row(seed, wave, startWave, revision);
|
|
bool Connected(RouteNode a, RouteNode b) => RunRouteMap.Connected(seed, a, b, startWave, revision);
|
|
var detail = new VisualElement { name = "RouteDetails" };
|
|
detail.AddToClassList("route-map-detail");
|
|
var preview = new VisualElement { name = "RoutePreview" };
|
|
detail.Add(preview);
|
|
void Preview(RouteNode node)
|
|
{
|
|
if (node == null) return;
|
|
preview.Clear();
|
|
bool boss = node.Wave == run.Journey.FinalWave && !run.Journey.endless;
|
|
string phase = run.Journey.endless ? "SURVIVAL" : node.Wave <= 3 ? "BOOT SECTOR"
|
|
: node.Wave <= 6 ? "ESCALATION" : boss ? "FINAL ENCOUNTER" : "KERNEL PERIMETER";
|
|
string progress = run.Journey.endless ? $"WAVE {node.Wave:00}" : $"WAVE {node.Wave:00} OF {run.Journey.FinalWave:00}";
|
|
preview.Add(Text($"{progress}\n{phase}", "route-map-eyebrow"));
|
|
string state = node.Wave < current ? run.Journey.visits.Any(v => v.nodeId == node.Id) ? "CLEARED" : "NOT TAKEN"
|
|
: node.Wave > current ? "FUTURE STOP" : node.Id == run.Journey.selectedNodeId ? "SELECTED DESTINATION"
|
|
: run.AvailableRouteNodes.Any(n => n.Id == node.Id) ? "AVAILABLE" : "UNREACHABLE FROM HERE";
|
|
preview.Add(Text(state, "route-preview-state"));
|
|
var encounter = EncounterDirector.Plan(node.Wave, node.Route, seed, run.Journey.encounterRevision);
|
|
preview.Add(Text(boss ? EnemyArchetypeCatalog.Get(encounter.Enemies[0]).DisplayName : RunRouteMap.Title(node.Route), "route-detail-title"));
|
|
preview.Add(Text(boss ? "Defeat the final boss to complete the route. Bank your rewards, or continue this build in endless survival."
|
|
: RunRouteMap.Detail(node.Route), "route-detail-copy"));
|
|
preview.Add(Text("BATTLE: " + encounter.Title, "route-map-eyebrow"));
|
|
preview.Add(Text(encounter.Advice, "route-detail-copy"));
|
|
if (node.Wave == current && run.Journey.resolvedEventWave == current && node.Id == run.Journey.selectedNodeId)
|
|
preview.Add(Text("[ resolved ]\n" + run.Journey.eventOutcome, "route-map-history"));
|
|
}
|
|
void SelectedPreview() => Preview(Row(current).FirstOrDefault(n => n.Id == run.Journey.selectedNodeId));
|
|
var available = run.AvailableRouteNodes.Select(n => n.Id).ToHashSet();
|
|
var chosen = run.Journey.visits.ToDictionary(v => v.wave, v => v.nodeId);
|
|
chosen[current] = run.Journey.selectedNodeId;
|
|
var potential = new HashSet<int> { run.Journey.selectedNodeId };
|
|
var lit = new HashSet<int>(potential);
|
|
for (int wave = current + 1; wave <= last; wave++)
|
|
{
|
|
var before = Row(wave - 1);
|
|
var after = Row(wave);
|
|
potential = after.Where(to => before.Any(from => potential.Contains(from.Id)
|
|
&& Connected(from, to))).Select(n => n.Id).ToHashSet();
|
|
lit.UnionWith(potential);
|
|
}
|
|
// One coordinate space for both nodes and trails. Future is up; travelled ground is below.
|
|
bool origin = first == startWave;
|
|
int bottom = origin ? first - 1 : first;
|
|
const float pitch = 96, radius = 30, top = 48;
|
|
var scroll = new ScrollView(ScrollViewMode.Vertical) { name = "RouteMapScroll" };
|
|
scroll.AddToClassList("route-map-scroll");
|
|
scroll.horizontalScrollerVisibility = ScrollerVisibility.Hidden;
|
|
var canvas = new VisualElement { name = "RouteCanvas" };
|
|
canvas.AddToClassList("route-map-canvas");
|
|
canvas.style.height = top + (last - bottom) * pitch + 62;
|
|
var nodesById = new Dictionary<int, VisualElement>();
|
|
var points = new Dictionary<int, Vector2>();
|
|
var rows = new Dictionary<int, IReadOnlyList<RouteNode>>();
|
|
void AddMarker(int id, int wave, float position, VisualElement marker)
|
|
{
|
|
marker.style.position = Position.Absolute;
|
|
marker.style.top = top + (last - wave) * pitch - radius;
|
|
marker.style.left = new Length(12 + position * 76, LengthUnit.Percent);
|
|
marker.style.marginLeft = -radius;
|
|
marker.userData = id;
|
|
nodesById.Add(id, marker); canvas.Add(marker);
|
|
}
|
|
for (int wave = first; wave <= last; wave++)
|
|
{
|
|
var nodes = Row(wave);
|
|
rows.Add(wave, nodes);
|
|
var waveLabel = Text(wave.ToString("00"), "route-map-wave");
|
|
waveLabel.style.top = top + (last - wave) * pitch - 42;
|
|
canvas.Add(waveLabel);
|
|
foreach (var node in nodes)
|
|
{
|
|
bool selected = chosen.TryGetValue(wave, out int id) && id == node.Id;
|
|
bool reachable = wave == current ? available.Contains(node.Id) : wave > current && lit.Contains(node.Id);
|
|
VisualElement tile;
|
|
if (wave == current)
|
|
{
|
|
var button = new Button(() => run.SelectRouteNode(node.Id)) { name = "Route" + node.Route };
|
|
button.SetEnabled(reachable && run.Journey.resolvedEventWave != current);
|
|
tile = button;
|
|
}
|
|
else tile = new VisualElement { name = "MapNode" + node.Id };
|
|
tile.AddToClassList("route-map-node");
|
|
tile.EnableInClassList("map-choice", wave == current && reachable);
|
|
tile.EnableInClassList("map-selected", wave == current && selected);
|
|
tile.EnableInClassList("map-taken", wave < current && selected);
|
|
tile.EnableInClassList("map-muted", !selected && !reachable);
|
|
tile.EnableInClassList("map-future", wave > current);
|
|
tile.EnableInClassList("map-boss", wave == run.Journey.FinalWave && !run.Journey.endless);
|
|
tile.Add(Glyph(node.Route));
|
|
tile.Add(Text(wave == run.Journey.FinalWave && !run.Journey.endless ? "BOSS" : CompactTitle(node.Route), "route-map-name"));
|
|
tile.tooltip = $"Wave {wave}: {RunRouteMap.Title(node.Route)}\n{RunRouteMap.Detail(node.Route)}";
|
|
tile.RegisterCallback<MouseEnterEvent>(_ => Preview(node));
|
|
tile.RegisterCallback<MouseLeaveEvent>(_ => SelectedPreview());
|
|
tile.RegisterCallback<FocusInEvent>(_ => Preview(node));
|
|
tile.RegisterCallback<FocusOutEvent>(_ => SelectedPreview());
|
|
AddMarker(node.Id, wave, nodes.Count == 1 ? .5f : (float)nodes.ToList().IndexOf(node) / (nodes.Count - 1), tile);
|
|
if (selected && wave == current - 1)
|
|
{
|
|
tile.name = "CurrentMapPosition";
|
|
var here = Text("YOU ARE HERE", "route-map-here"); tile.Add(here);
|
|
}
|
|
}
|
|
}
|
|
if (origin)
|
|
{
|
|
var start = new VisualElement { name = current == startWave ? "CurrentMapPosition" : "RouteStart" };
|
|
start.AddToClassList("route-map-node"); start.AddToClassList("map-taken");
|
|
start.Add(Text("OK", "route-map-symbol")); start.Add(Text("Cleared", "route-map-name"));
|
|
if (current == startWave) start.Add(Text("YOU ARE HERE", "route-map-here"));
|
|
start.tooltip = $"Wave {startWave - 1} cleared. The route begins here.";
|
|
AddMarker(-1, bottom, .5f, start);
|
|
}
|
|
var edges = new List<(int from, int to, bool taken, bool active, bool future)>();
|
|
foreach (var row in rows)
|
|
{
|
|
if (!rows.TryGetValue(row.Key + 1, out var next)) continue;
|
|
foreach (var a in row.Value)
|
|
foreach (var b in next)
|
|
{
|
|
if (!Connected(a, b)) continue;
|
|
bool taken = b.Wave < current && chosen.TryGetValue(a.Wave, out int aId) && aId == a.Id
|
|
&& chosen.TryGetValue(b.Wave, out int bId) && bId == b.Id;
|
|
bool active = a.Wave == current - 1 ? chosen.GetValueOrDefault(a.Wave) == a.Id && b.Id == run.Journey.selectedNodeId
|
|
: a.Wave >= current && lit.Contains(a.Id);
|
|
edges.Add((a.Id, b.Id, taken, active, a.Wave >= current));
|
|
}
|
|
}
|
|
if (origin) foreach (var node in rows[first])
|
|
edges.Add((-1, node.Id, first < current && chosen.GetValueOrDefault(first) == node.Id,
|
|
first == current && node.Id == run.Journey.selectedNodeId, false));
|
|
canvas.generateVisualContent += context =>
|
|
{
|
|
points.Clear();
|
|
foreach (var entry in nodesById)
|
|
points[entry.Key] = entry.Value.layout.center;
|
|
var painter = context.painter2D;
|
|
// Muted trails first, then the player's path. At crossings the active trail stays legible.
|
|
foreach (var edge in edges.OrderBy(e => e.taken || e.active))
|
|
{
|
|
Vector2 a = points[edge.from], b = points[edge.to];
|
|
var direction = (b - a).normalized;
|
|
a += direction * radius; b -= direction * radius;
|
|
Color color = edge.taken ? new Color(.48f, .76f, .59f) : edge.active ? new Color(.91f, .72f, .43f) : new Color(.24f, .33f, .28f);
|
|
// A dark under-stroke separates real crossings without inventing junctions.
|
|
Trail(painter, a, b, new Color(.055f, .085f, .067f), 7, false);
|
|
Trail(painter, a, b, color, edge.active || edge.taken ? 2.5f : 1.5f, edge.future);
|
|
}
|
|
};
|
|
canvas.RegisterCallback<GeometryChangedEvent>(_ => canvas.MarkDirtyRepaint());
|
|
scroll.Add(canvas); map.Add(scroll);
|
|
bool positioned = false;
|
|
scroll.RegisterCallback<GeometryChangedEvent>(_ =>
|
|
{
|
|
if (positioned || scroll.contentViewport.layout.height < 20) return;
|
|
positioned = true;
|
|
scroll.schedule.Execute(() =>
|
|
{
|
|
var here = canvas.Q("CurrentMapPosition");
|
|
if (here != null)
|
|
scroll.scrollOffset = new Vector2(0, Mathf.Max(0, here.layout.yMax + 36 - scroll.contentViewport.layout.height));
|
|
});
|
|
});
|
|
var choices = nodesById.Values.OfType<Button>().Where(b => b.enabledSelf).OrderBy(b => (int)b.userData).ToList();
|
|
void MoveFocus(int direction)
|
|
{
|
|
if (choices.Count == 0) return;
|
|
int index = choices.FindIndex(b => b == layout.focusController?.focusedElement);
|
|
var next = choices[(index + direction + choices.Count) % choices.Count];
|
|
next.Focus(); scroll.ScrollTo(next);
|
|
}
|
|
canvas.RegisterCallback<NavigationMoveEvent>(evt =>
|
|
{
|
|
if (evt.direction is not (NavigationMoveEvent.Direction.Left or NavigationMoveEvent.Direction.Right
|
|
or NavigationMoveEvent.Direction.Up or NavigationMoveEvent.Direction.Down)) return;
|
|
MoveFocus(evt.direction is NavigationMoveEvent.Direction.Left or NavigationMoveEvent.Direction.Up ? -1 : 1);
|
|
evt.StopPropagation(); evt.PreventDefault();
|
|
});
|
|
canvas.RegisterCallback<KeyDownEvent>(evt =>
|
|
{
|
|
if (evt.keyCode is not (KeyCode.W or KeyCode.A or KeyCode.S or KeyCode.D)) return;
|
|
MoveFocus(evt.keyCode is KeyCode.W or KeyCode.A ? -1 : 1);
|
|
evt.StopPropagation();
|
|
});
|
|
if (run.Journey.visits.Count > 0)
|
|
{
|
|
var history = new Foldout { text = $"Travel journal ({run.Journey.visits.Count})", value = false, name = "RouteHistory" };
|
|
history.AddToClassList("route-map-journal");
|
|
foreach (var visit in run.Journey.visits)
|
|
history.Add(Text($"Wave {visit.wave}: {RunRouteMap.Title(visit.route)}\n{visit.outcome}", "route-map-history"));
|
|
detail.Add(history);
|
|
}
|
|
layout.Add(map);
|
|
SelectedPreview();
|
|
layout.Add(detail);
|
|
return layout;
|
|
}
|
|
|
|
private static void Trail(Painter2D painter, Vector2 a, Vector2 b, Color color, float width, bool dotted)
|
|
{
|
|
painter.strokeColor = color; painter.lineWidth = width;
|
|
if (!dotted)
|
|
{
|
|
painter.BeginPath(); painter.MoveTo(a); painter.LineTo(b); painter.Stroke();
|
|
return;
|
|
}
|
|
float length = Vector2.Distance(a, b);
|
|
for (float offset = 0; offset < length; offset += 10)
|
|
{
|
|
painter.BeginPath(); painter.MoveTo(Vector2.Lerp(a, b, offset / length));
|
|
painter.LineTo(Vector2.Lerp(a, b, Mathf.Min(offset + 4, length) / length)); painter.Stroke();
|
|
}
|
|
}
|
|
|
|
private static VisualElement Glyph(WaveRoute route)
|
|
{
|
|
var glyph = new VisualElement { pickingMode = PickingMode.Ignore };
|
|
glyph.AddToClassList("route-map-glyph");
|
|
glyph.generateVisualContent += context =>
|
|
{
|
|
var p = context.painter2D; p.strokeColor = glyph.resolvedStyle.color; p.lineWidth = 1.8f;
|
|
void Line(params Vector2[] points)
|
|
{
|
|
p.BeginPath(); p.MoveTo(points[0]); foreach (var point in points.Skip(1)) p.LineTo(point); p.Stroke();
|
|
}
|
|
switch (route)
|
|
{
|
|
case WaveRoute.Repair:
|
|
Line(new(12, 3), new(12, 21)); Line(new(3, 12), new(21, 12)); break;
|
|
case WaveRoute.Challenge:
|
|
Line(new(4, 3), new(20, 3), new(20, 13), new(12, 22), new(4, 13), new(4, 3));
|
|
Line(new(12, 7), new(12, 13)); Line(new(12, 16), new(12, 17)); break;
|
|
case WaveRoute.Cache:
|
|
Line(new(3, 8), new(12, 3), new(21, 8), new(21, 20), new(3, 20), new(3, 8), new(21, 8));
|
|
Line(new(12, 9), new(12, 14)); break;
|
|
case WaveRoute.Forge:
|
|
Line(new(5, 19), new(16, 8)); Line(new(10, 4), new(14, 2), new(22, 10), new(19, 14), new(10, 4)); break;
|
|
case WaveRoute.Sacrifice:
|
|
Line(new(12, 2), new(18, 9), new(12, 16), new(6, 9), new(12, 2));
|
|
Line(new(3, 21), new(5, 17), new(19, 17), new(21, 21), new(3, 21)); break;
|
|
case WaveRoute.Recycle:
|
|
Line(new(4, 8), new(9, 3), new(19, 3), new(21, 8)); Line(new(16, 8), new(21, 8), new(21, 2));
|
|
Line(new(20, 16), new(15, 21), new(5, 21), new(3, 16)); Line(new(8, 16), new(3, 16), new(3, 22)); break;
|
|
default:
|
|
Line(new(6, 14), new(12, 8), new(18, 14)); Line(new(6, 21), new(12, 15), new(18, 21)); break;
|
|
}
|
|
};
|
|
return glyph;
|
|
}
|
|
|
|
public static VisualElement BuildEvent(RunManager run)
|
|
{
|
|
var content = new VisualElement { name = "RouteEventContent" };
|
|
content.AddToClassList("departure-content");
|
|
content.Add(Text(RunRouteMap.Detail(run.Journey.selectedRoute), "route-detail-copy"));
|
|
if (run.RouteEventPending) content.Add(Event(run));
|
|
else content.Add(Text("STOP COMPLETE\n" + run.Journey.eventOutcome, "route-event-result"));
|
|
return content;
|
|
}
|
|
|
|
private static VisualElement Event(RunManager run)
|
|
{
|
|
var panel = new VisualElement { name = "RouteEvent" }; panel.AddToClassList("route-event");
|
|
var selectors = new VisualElement(); selectors.AddToClassList("departure-selectors"); panel.Add(selectors);
|
|
bool remove = run.Journey.selectedRoute is WaveRoute.Sacrifice or WaveRoute.Recycle;
|
|
bool upgrade = run.Journey.selectedRoute is WaveRoute.Sacrifice or WaveRoute.Forge;
|
|
int amount = run.Journey.selectedRoute == WaveRoute.Sacrifice ? 4 : 2;
|
|
var donors = run.RunDeck.Where(c => c.Definition != null && !c.Definition.IsToken
|
|
&& (run.Journey.selectedRoute != WaveRoute.Sacrifice || !c.Definition.IsJunk)).ToList();
|
|
var targets = run.RunDeck.Where(run.CanImproveRouteCard).ToList();
|
|
int donorIndex = 0, targetIndex = Math.Max(0, targets.FindIndex(c => !remove || donors.Count == 0 || c != donors[0]));
|
|
var previews = new VisualElement(); previews.AddToClassList("departure-previews");
|
|
var result = Text("", "route-event-result");
|
|
var commit = new Button { name = "ResolveRouteEventButton", text = run.Journey.selectedRoute switch
|
|
{ WaveRoute.Sacrifice => "sacrifice & empower", WaveRoute.Forge => "apply patch", _ => "recycle card" } };
|
|
commit.AddToClassList("primary-action");
|
|
CardInstance Donor() => remove && donors.Count > 0 ? donors[donorIndex] : null;
|
|
CardInstance Target() => upgrade && targets.Count > 0 ? targets[targetIndex] : null;
|
|
void Refresh()
|
|
{
|
|
previews.Clear();
|
|
var donor = Donor(); var target = Target();
|
|
if (donor != null) previews.Add(Preview(donor, run, "REMOVE FROM THIS RUN"));
|
|
if (target != null)
|
|
{
|
|
previews.Add(Preview(target, run, "BEFORE UPGRADE"));
|
|
var after = target.CopyForCombat(); after.ApplyMagnitudeUpgrade(amount);
|
|
previews.Add(Preview(after, run, $"AFTER: +{amount} EFFECT"));
|
|
}
|
|
bool valid = (!remove || (donor != null && run.RunDeck.Count > 1)) && (!upgrade || target != null)
|
|
&& (!remove || !upgrade || donor != target);
|
|
result.text = !valid ? run.Journey.selectedRoute == WaveRoute.Sacrifice
|
|
? "Choose two different cards, or pass if there are no eligible cards. Your deck must keep at least one card."
|
|
: "No eligible card is selected. You may pass; your deck must keep at least one card."
|
|
: remove ? $"{donor.Definition.DisplayName} will leave this run's deck. " + (upgrade ? $"{target.Definition.DisplayName}: +{target.MagnitudeBonus} -> +{target.MagnitudeBonus + amount} effect." : "Receive 4 Bits.")
|
|
: $"{target.Definition.DisplayName}: +{target.MagnitudeBonus} -> +{target.MagnitudeBonus + amount} effect.";
|
|
if (run.BuildRewardPending) result.text = "Choose your build reward first, then resolve this stop.";
|
|
commit.SetEnabled(valid && !run.BuildRewardPending);
|
|
}
|
|
if (remove && donors.Count > 0)
|
|
{
|
|
var picker = new DropdownField("Card to remove", donors.Select((c, i) => $"{i + 1}. {c.Definition.DisplayName}").ToList(), donorIndex) { name = "RouteDonor" };
|
|
picker.RegisterValueChangedCallback(_ => { donorIndex = picker.index; Refresh(); }); selectors.Add(picker);
|
|
}
|
|
if (upgrade && targets.Count > 0)
|
|
{
|
|
var picker = new DropdownField("Card to empower", targets.Select((c, i) => $"{i + 1}. {c.Definition.DisplayName}").ToList(), targetIndex) { name = "RouteTarget" };
|
|
picker.RegisterValueChangedCallback(_ => { targetIndex = picker.index; Refresh(); }); selectors.Add(picker);
|
|
}
|
|
panel.Add(Text("Run only; your collection is unaffected. Confirm or pass to commit this path.", "route-detail-copy"));
|
|
panel.Add(previews); panel.Add(result);
|
|
commit.clicked += () => run.ResolveRouteEvent(Donor(), Target());
|
|
panel.Add(commit);
|
|
var skip = new Button(() => run.ResolveRouteEvent(skip: true)) { text = "keep my deck", name = "SkipRouteEventButton" };
|
|
skip.AddToClassList("secondary-action"); skip.SetEnabled(!run.BuildRewardPending); panel.Add(skip);
|
|
Refresh();
|
|
return panel;
|
|
}
|
|
|
|
private static VisualElement Preview(CardInstance card, RunManager run, string heading)
|
|
{
|
|
var holder = new VisualElement(); holder.AddToClassList("route-event-card"); holder.AddToClassList("departure-card");
|
|
holder.Add(Text(heading, "route-map-eyebrow"));
|
|
var face = new VisualElement();
|
|
SoftwareCardView.Populate(face, card, CombatManager.GetCardCost(card), CardEffectFactory.GetRulesText(card, run.CurrentConfig));
|
|
holder.Add(face); return holder;
|
|
}
|
|
|
|
private static Label Text(string text, string style) => SoftwareCardView.Text(text, style);
|
|
|
|
private static string CompactTitle(WaveRoute route) => route switch
|
|
{
|
|
WaveRoute.Repair => "Repair", WaveRoute.Challenge => "Elite", WaveRoute.Cache => "Cache",
|
|
WaveRoute.Forge => "Patch", WaveRoute.Sacrifice => "Altar", WaveRoute.Recycle => "Scrap", _ => "Path"
|
|
};
|
|
}
|
|
}
|