RustToDust/client/Hud.cs

791 lines
28 KiB
C#

using System;
using System.Collections.Generic;
using Godot;
using RustToDust.Core;
using RustToDust.Ymir;
namespace RustToDust.Client;
/// <summary>
/// Rust to Dust industrial HUD:
/// - Top-Left: Chassis badge (DORSAL/CORONAL), Core HP, Speed, Contenders Alive, Safe Circle status, Pickup prompt.
/// - Top-Right: Tactical Radar (Safe Circle, RadarSignature-scaled Spectator Wall, lock lines, contenders, salvage).
/// - Bottom-Left: Live clickable Aetheria Hardpoint & Heat Schematic + Cargo Salvage Rack (Tab expands Garage details).
/// - Bottom-Center: Imperial Broadcast Chyron (Weksa commentary).
/// </summary>
public partial class Hud : CanvasLayer
{
private static readonly Color BgDark = Color.FromHtml("#131613");
private static readonly Color BgInner = Color.FromHtml("#1b1f1b");
private static readonly Color BorderCol = Color.FromHtml("#3e4638");
private static readonly Color OliveCol = Color.FromHtml("#9bb06a");
private static readonly Color OrangeCol = Color.FromHtml("#e08a4a");
private static readonly Color TextLight = Color.FromHtml("#e4dfd2");
private static readonly Color TextMuted = Color.FromHtml("#989487");
private static readonly Color CyanCol = Color.FromHtml("#44d9e8");
private static readonly Color GoldCol = Color.FromHtml("#e6b838");
private static readonly Color RedCol = Color.FromHtml("#d93829");
private readonly Func<ArenaSession?> _getSession;
private readonly Action<int, string> _onEquipCargo;
private Label _chassisHeader = null!;
private Label _planeBadge = null!;
private ProgressBar _coreHpBar = null!;
private Label _coreHpLabel = null!;
private Label _statsLabel = null!;
private Label _safeLabel = null!;
private Label _promptLabel = null!;
private Label _controlsHint = null!;
private RadarWidget _radar = null!;
private SchematicPanel _schematic = null!;
private VBoxContainer _cargoList = null!;
private Label _cargoHeader = null!;
private Label _garageDetails = null!;
private PanelContainer _bottomLeftPanel = null!;
private VBoxContainer _chyronBox = null!;
private readonly Label[] _chyronLabels = new Label[3];
private BroadcastFeed? _lastBroadcastFeed;
private long _lastChyronVersion = -1;
private PanelContainer _outcomeBanner = null!;
private Label _outcomeLabel = null!;
private int _selectedCargoIndex = -1;
private int _lastCargoHash = -1;
private bool _garageExpanded;
public Hud(Func<ArenaSession?> getSession, Action<int, string> onEquipCargo)
{
_getSession = getSession;
_onEquipCargo = onEquipCargo;
Layer = 10;
}
public bool GarageExpanded => _garageExpanded;
public void ToggleGarage()
{
_garageExpanded = !_garageExpanded;
_garageDetails.Visible = _garageExpanded;
_schematic.CustomMinimumSize = _garageExpanded ? new Vector2(236f, 250f) : new Vector2(180f, 190f);
_schematic.QueueRedraw();
}
public override void _Ready()
{
BuildTopLeft();
BuildTopRightRadar();
BuildBottomLeftSchematic();
BuildBottomCenterChyron();
BuildOutcomeBanner();
}
public bool IsPointerOverInteractiveHud(Vector2 screenPos)
{
return _bottomLeftPanel.GetGlobalRect().HasPoint(screenPos);
}
public void Sync(ArenaSession session)
{
var state = session.State;
var player = state.Player;
// Top-Left Status
_chassisHeader.Text = player.Grid.Spec.Name.ToUpperInvariant();
bool isCoronal = player.Grid.Spec.Plane == GridPlane.Coronal;
_planeBadge.Text = isCoronal ? "CORONAL" : "DORSAL";
_planeBadge.Modulate = isCoronal ? CyanCol : OliveCol;
_coreHpBar.MaxValue = Math.Max(1f, player.Grid.MaxCoreHp);
_coreHpBar.Value = Math.Max(0f, player.Grid.CoreHp);
_coreHpLabel.Text = $"CORE {player.Grid.CoreHp:F0} / {player.Grid.MaxCoreHp:F0}";
int aliveCount = 0;
for (int i = 0; i < state.Contenders.Count; i++)
{
if (state.Contenders[i].Alive) aliveCount++;
}
_statsLabel.Text = $"SPD {player.Speed:F1} m/s | ALIVE {aliveCount} / {state.Contenders.Count} | KILLS {player.Kills}";
float distCenter = MathF.Sqrt(player.X * player.X + player.Z * player.Z);
float outside = distCenter - state.Spectators.SafeRadius;
int cleared = state.Spectators.CountClearedFor(player.Id);
int provoked = state.Spectators.CountProvokedFor(player.Id);
if (outside <= 0f)
{
_safeLabel.Text = $"RING SAFE ({-outside:F0}m margin)" + (provoked > 0 ? $" • {provoked} PROVOKED!" : "");
_safeLabel.Modulate = provoked > 0 ? OrangeCol : OliveCol;
}
else
{
_safeLabel.Text = $"OUTSIDE +{outside:F0}m — {cleared} CLEARED" + (provoked > 0 ? $" / {provoked} PROVOKED!" : "!");
_safeLabel.Modulate = RedCol;
}
_promptLabel.Text = BuildInteractionPrompt(state, player);
_promptLabel.Visible = !string.IsNullOrEmpty(_promptLabel.Text);
// Radar
_radar.SetState(state);
_radar.QueueRedraw();
// Schematic + Cargo
_schematic.SetGrid(player.Grid, _selectedCargoIndex);
_schematic.QueueRedraw();
SyncCargoButtons(player.Grid);
if (_garageExpanded)
{
_garageDetails.Text = BuildGarageDetails(player.Grid);
}
// Broadcast Chyron
SyncBroadcastChyron(state.Broadcast);
// Outcome
if (state.Phase == MatchPhase.Victory)
{
_outcomeBanner.Visible = true;
_outcomeLabel.Text = "VICTORY — LAST RIG STANDING\n[R] New Drop";
_outcomeLabel.Modulate = OliveCol;
}
else if (state.Phase == MatchPhase.Defeat)
{
_outcomeBanner.Visible = true;
_outcomeLabel.Text = "CHASSIS DESTROYED\n[R] Redeploy";
_outcomeLabel.Modulate = RedCol;
}
else
{
_outcomeBanner.Visible = false;
}
}
private void BuildTopLeft()
{
var panel = CreateStyledPanel();
panel.AnchorLeft = 0f;
panel.AnchorTop = 0f;
panel.OffsetLeft = 14f;
panel.OffsetTop = 14f;
panel.CustomMinimumSize = new Vector2(360f, 0f);
AddChild(panel);
var vbox = new VBoxContainer { Theme = null };
vbox.AddThemeConstantOverride("separation", 5);
panel.AddChild(vbox);
var headRow = new HBoxContainer();
headRow.AddThemeConstantOverride("separation", 10);
vbox.AddChild(headRow);
_chassisHeader = new Label { Text = "DUNE BUGGY", SizeFlagsHorizontal = Control.SizeFlags.ExpandFill };
_chassisHeader.AddThemeColorOverride("font_color", TextLight);
_chassisHeader.AddThemeFontSizeOverride("font_size", 17);
headRow.AddChild(_chassisHeader);
_planeBadge = new Label { Text = "DORSAL" };
_planeBadge.AddThemeFontSizeOverride("font_size", 13);
headRow.AddChild(_planeBadge);
var hpRow = new HBoxContainer();
hpRow.AddThemeConstantOverride("separation", 8);
vbox.AddChild(hpRow);
_coreHpBar = new ProgressBar
{
ShowPercentage = false,
SizeFlagsHorizontal = Control.SizeFlags.ExpandFill,
CustomMinimumSize = new Vector2(180f, 16f)
};
var barBg = new StyleBoxFlat { BgColor = BgInner, BorderColor = BorderCol };
barBg.SetBorderWidthAll(1);
var barFill = new StyleBoxFlat { BgColor = OliveCol };
_coreHpBar.AddThemeStyleboxOverride("background", barBg);
_coreHpBar.AddThemeStyleboxOverride("fill", barFill);
hpRow.AddChild(_coreHpBar);
_coreHpLabel = new Label { Text = "CORE 100 / 100" };
_coreHpLabel.AddThemeColorOverride("font_color", TextLight);
_coreHpLabel.AddThemeFontSizeOverride("font_size", 13);
hpRow.AddChild(_coreHpLabel);
_statsLabel = new Label { Text = "SPD 0.0 m/s" };
_statsLabel.AddThemeColorOverride("font_color", TextMuted);
_statsLabel.AddThemeFontSizeOverride("font_size", 13);
vbox.AddChild(_statsLabel);
_safeLabel = new Label { Text = "RING SAFE" };
_safeLabel.AddThemeFontSizeOverride("font_size", 13);
vbox.AddChild(_safeLabel);
_promptLabel = new Label { Text = "", Visible = false };
_promptLabel.AddThemeColorOverride("font_color", CyanCol);
_promptLabel.AddThemeFontSizeOverride("font_size", 14);
vbox.AddChild(_promptLabel);
_controlsHint = new Label { Text = "WASD Drive | LMB Fire | E Loot/Swap | Tab Garage | R Reset" };
_controlsHint.AddThemeColorOverride("font_color", TextMuted);
_controlsHint.AddThemeFontSizeOverride("font_size", 11);
vbox.AddChild(_controlsHint);
}
private void BuildTopRightRadar()
{
var panel = CreateStyledPanel();
panel.AnchorLeft = 1f;
panel.AnchorRight = 1f;
panel.AnchorTop = 0f;
panel.OffsetLeft = -246f;
panel.OffsetRight = -14f;
panel.OffsetTop = 14f;
AddChild(panel);
var vbox = new VBoxContainer();
vbox.AddThemeConstantOverride("separation", 4);
panel.AddChild(vbox);
var title = new Label { Text = "GALLERY & ARENA RADAR", HorizontalAlignment = HorizontalAlignment.Center };
title.AddThemeColorOverride("font_color", OrangeCol);
title.AddThemeFontSizeOverride("font_size", 12);
vbox.AddChild(title);
_radar = new RadarWidget
{
CustomMinimumSize = new Vector2(214f, 214f)
};
vbox.AddChild(_radar);
}
private void BuildBottomLeftSchematic()
{
_bottomLeftPanel = CreateStyledPanel();
_bottomLeftPanel.AnchorLeft = 0f;
_bottomLeftPanel.AnchorTop = 1f;
_bottomLeftPanel.AnchorBottom = 1f;
_bottomLeftPanel.GrowVertical = Control.GrowDirection.Begin;
_bottomLeftPanel.OffsetLeft = 14f;
_bottomLeftPanel.OffsetTop = -248f;
_bottomLeftPanel.OffsetBottom = -14f;
AddChild(_bottomLeftPanel);
var hbox = new HBoxContainer();
hbox.AddThemeConstantOverride("separation", 10);
_bottomLeftPanel.AddChild(hbox);
var leftCol = new VBoxContainer();
leftCol.AddThemeConstantOverride("separation", 4);
hbox.AddChild(leftCol);
var title = new Label { Text = "AETHERIA HEAT & HARDPOINTS" };
title.AddThemeColorOverride("font_color", OrangeCol);
title.AddThemeFontSizeOverride("font_size", 12);
leftCol.AddChild(title);
_schematic = new SchematicPanel(OnHardpointClicked)
{
CustomMinimumSize = new Vector2(180f, 190f)
};
leftCol.AddChild(_schematic);
var rightCol = new VBoxContainer { CustomMinimumSize = new Vector2(178f, 0f) };
rightCol.AddThemeConstantOverride("separation", 4);
hbox.AddChild(rightCol);
_cargoHeader = new Label { Text = "SALVAGE CARGO (0/4)" };
_cargoHeader.AddThemeColorOverride("font_color", OliveCol);
_cargoHeader.AddThemeFontSizeOverride("font_size", 12);
rightCol.AddChild(_cargoHeader);
_cargoList = new VBoxContainer();
_cargoList.AddThemeConstantOverride("separation", 3);
rightCol.AddChild(_cargoList);
_garageDetails = new Label { Visible = false, AutowrapMode = TextServer.AutowrapMode.Word };
_garageDetails.AddThemeColorOverride("font_color", TextMuted);
_garageDetails.AddThemeFontSizeOverride("font_size", 11);
_garageDetails.CustomMinimumSize = new Vector2(200f, 0f);
hbox.AddChild(_garageDetails);
}
private void BuildBottomCenterChyron()
{
var panel = CreateStyledPanel();
panel.AnchorLeft = 0.5f;
panel.AnchorRight = 0.5f;
panel.AnchorTop = 1f;
panel.AnchorBottom = 1f;
panel.OffsetLeft = -150f;
panel.OffsetRight = 460f;
panel.OffsetTop = -104f;
panel.OffsetBottom = -14f;
AddChild(panel);
var vbox = new VBoxContainer();
vbox.AddThemeConstantOverride("separation", 3);
panel.AddChild(vbox);
var header = new Label { Text = "IMPERIAL BROADCAST FEED" };
header.AddThemeColorOverride("font_color", GoldCol);
header.AddThemeFontSizeOverride("font_size", 11);
vbox.AddChild(header);
_chyronBox = new VBoxContainer();
_chyronBox.AddThemeConstantOverride("separation", 2);
vbox.AddChild(_chyronBox);
for (int i = 0; i < _chyronLabels.Length; i++)
{
var lbl = new Label
{
Visible = false,
AutowrapMode = TextServer.AutowrapMode.Word
};
lbl.AddThemeFontSizeOverride("font_size", 11);
lbl.AddThemeColorOverride("font_color", TextMuted);
_chyronBox.AddChild(lbl);
_chyronLabels[i] = lbl;
}
}
private void BuildOutcomeBanner()
{
_outcomeBanner = CreateStyledPanel();
_outcomeBanner.AnchorLeft = 0.5f;
_outcomeBanner.AnchorRight = 0.5f;
_outcomeBanner.AnchorTop = 0.28f;
_outcomeBanner.AnchorBottom = 0.28f;
_outcomeBanner.OffsetLeft = -210f;
_outcomeBanner.OffsetRight = 210f;
_outcomeBanner.OffsetTop = -40f;
_outcomeBanner.OffsetBottom = 40f;
_outcomeBanner.Visible = false;
AddChild(_outcomeBanner);
_outcomeLabel = new Label
{
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
_outcomeLabel.AddThemeFontSizeOverride("font_size", 22);
_outcomeBanner.AddChild(_outcomeLabel);
}
private void OnHardpointClicked(string hardpointId)
{
var session = _getSession();
if (session is null) return;
var cargo = session.State.Player.Grid.Cargo;
if (_selectedCargoIndex >= 0 && _selectedCargoIndex < cargo.Count)
{
_onEquipCargo(_selectedCargoIndex, hardpointId);
_selectedCargoIndex = -1;
_lastCargoHash = -1;
}
}
private void SyncCargoButtons(ChassisGrid grid)
{
int hash = grid.Cargo.Count * 97 + (_selectedCargoIndex + 10);
for (int i = 0; i < grid.Cargo.Count; i++)
{
hash = HashCode.Combine(hash, grid.Cargo[i].Id);
}
if (hash == _lastCargoHash) return;
_lastCargoHash = hash;
_cargoHeader.Text = $"SALVAGE CARGO ({grid.Cargo.Count}/{grid.Spec.CargoCapacity})";
for (int i = _cargoList.GetChildCount() - 1; i >= 0; i--)
{
var c = _cargoList.GetChild(i);
_cargoList.RemoveChild(c);
c.QueueFree();
}
if (grid.Cargo.Count == 0)
{
var empty = new Label { Text = "No modules in rack.\nPress [E] near crates." };
empty.AddThemeColorOverride("font_color", TextMuted);
empty.AddThemeFontSizeOverride("font_size", 11);
_cargoList.AddChild(empty);
return;
}
for (int i = 0; i < grid.Cargo.Count; i++)
{
int idx = i;
var mod = grid.Cargo[i];
var row = new HBoxContainer();
row.AddThemeConstantOverride("separation", 4);
var selectBtn = new Button
{
Text = $"{(mod.IsImperial ? "★ " : "")}{mod.Name}",
SizeFlagsHorizontal = Control.SizeFlags.ExpandFill,
Alignment = HorizontalAlignment.Left
};
selectBtn.AddThemeFontSizeOverride("font_size", 11);
if (_selectedCargoIndex == idx)
{
selectBtn.Modulate = OrangeCol;
}
else if (mod.IsImperial)
{
selectBtn.Modulate = GoldCol;
}
selectBtn.Pressed += () =>
{
_selectedCargoIndex = (_selectedCargoIndex == idx) ? -1 : idx;
_lastCargoHash = -1;
};
row.AddChild(selectBtn);
var fitBtn = new Button { Text = "FIT" };
fitBtn.AddThemeFontSizeOverride("font_size", 11);
fitBtn.Pressed += () => AutoFitCargo(grid, idx);
row.AddChild(fitBtn);
_cargoList.AddChild(row);
}
}
private void AutoFitCargo(ChassisGrid grid, int cargoIndex)
{
if (cargoIndex < 0 || cargoIndex >= grid.Cargo.Count) return;
var mod = grid.Cargo[cargoIndex];
for (int i = 0; i < grid.Spec.Hardpoints.Length; i++)
{
var hp = grid.Spec.Hardpoints[i];
if (ChassisGrid.CanFit(hp, mod))
{
_onEquipCargo(cargoIndex, hp.Id);
_selectedCargoIndex = -1;
_lastCargoHash = -1;
return;
}
}
}
private void SyncBroadcastChyron(BroadcastFeed broadcast)
{
var lines = broadcast.Lines;
if (ReferenceEquals(_lastBroadcastFeed, broadcast) && broadcast.Version == _lastChyronVersion)
{
return;
}
_lastBroadcastFeed = broadcast;
_lastChyronVersion = broadcast.Version;
int start = Math.Max(0, lines.Count - _chyronLabels.Length);
int visibleCount = lines.Count - start;
for (int slot = 0; slot < _chyronLabels.Length; slot++)
{
var lbl = _chyronLabels[slot];
if (slot < visibleCount)
{
int lineIdx = start + slot;
var line = lines[lineIdx];
bool isLatest = lineIdx == lines.Count - 1;
lbl.Visible = true;
lbl.Text = $"[{line.Timestamp:00.0}s] {line.Text}";
lbl.AddThemeFontSizeOverride("font_size", isLatest ? 12 : 11);
lbl.AddThemeColorOverride("font_color", isLatest ? TextLight : TextMuted);
}
else
{
lbl.Visible = false;
lbl.Text = "";
}
}
}
private static string BuildInteractionPrompt(MatchState state, ContenderState player)
{
GroundDrop? nearest = null;
float bestD2 = MatchState.PickupRadius * MatchState.PickupRadius;
for (int i = 0; i < state.GroundDrops.Count; i++)
{
var d = state.GroundDrops[i];
if (d.PickedUp) continue;
float dx = d.X - player.X;
float dz = d.Z - player.Z;
float d2 = dx * dx + dz * dz;
if (d2 <= bestD2)
{
bestD2 = d2;
nearest = d;
}
}
if (nearest is null) return "";
if (nearest.Module is { } mod)
{
return $"[E] Loot {(mod.IsImperial ? "IMPERIAL " : "")}{mod.Name}";
}
if (nearest.Chassis is { } ch)
{
return $"[E] Swap to {ch.Name} ({ch.Plane})";
}
return "";
}
private static string BuildGarageDetails(ChassisGrid grid)
{
var lines = new List<string>
{
$"CONDUCTIVITY {grid.Spec.HullConductivity:F1} | THRUST {grid.ComputeDriveThrust():F0} N",
"MOUNTED:"
};
for (int i = 0; i < grid.Mounted.Count; i++)
{
var m = grid.Mounted[i];
lines.Add($"• {m.HardpointId}: {m.Spec.Name} ({m.CurrentTemp:F0}°C, {(m.CurrentPerformance * 100f):F0}%)");
}
return string.Join("\n", lines);
}
private static PanelContainer CreateStyledPanel()
{
var panel = new PanelContainer();
var style = new StyleBoxFlat
{
BgColor = new Color(BgDark.R, BgDark.G, BgDark.B, 0.88f),
BorderColor = BorderCol,
ContentMarginLeft = 10,
ContentMarginRight = 10,
ContentMarginTop = 8,
ContentMarginBottom = 8
};
style.SetBorderWidthAll(2);
style.SetCornerRadiusAll(3);
panel.AddThemeStyleboxOverride("panel", style);
return panel;
}
private sealed partial class RadarWidget : Control
{
private MatchState? _state;
public void SetState(MatchState state) => _state = state;
private static Vector2 WorldToRadar(Vector2 center, float scale, float wx, float wz) =>
// Match chase camera orientation: screen-up = +Z, screen-right = -X.
center + new Vector2(-wx, -wz) * scale;
public override void _Draw()
{
Vector2 center = Size * 0.5f;
float radius = MathF.Min(Size.X, Size.Y) * 0.47f;
float scale = radius / MatchState.ArenaHalfExtent;
DrawCircle(center, radius, BgInner);
DrawArc(center, radius, 0f, MathF.Tau, 48, BorderCol, 1.5f);
if (_state is null) return;
// Safe Circle
float safePx = _state.Spectators.SafeRadius * scale;
DrawArc(center, safePx, 0f, MathF.Tau, 56, OrangeCol, 1.6f);
// Ground drops
for (int i = 0; i < _state.GroundDrops.Count; i++)
{
var d = _state.GroundDrops[i];
if (d.PickedUp) continue;
var p = WorldToRadar(center, scale, d.X, d.Z);
if (d.Chassis is not null)
{
DrawRect(new Rect2(p - new Vector2(2.5f, 2.5f), new Vector2(5f, 5f)), CyanCol);
}
else if (d.Module is { IsImperial: true })
{
DrawCircle(p, 3.0f, GoldCol);
}
}
// Spectator Wall (scaled by RadarSignature) + red target lock lines
var specs = _state.Spectators.Spectators;
for (int i = 0; i < specs.Count; i++)
{
var s = specs[i];
if (!s.Alive) continue;
var p = WorldToRadar(center, scale, s.X, s.Z);
float blipR = 1.6f + s.RadarSignature * 3.1f;
Color col = s.Tier switch
{
SpectatorTier.Tourist => GoldCol,
SpectatorTier.Patron => PatronColor,
_ => RedCol
};
if (s.Stance != SpectatorStance.Neutral && s.TargetContenderId.HasValue)
{
int tid = s.TargetContenderId.Value;
if (tid >= 0 && tid < _state.Contenders.Count && _state.Contenders[tid].Alive)
{
var tgt = _state.Contenders[tid];
var tp = WorldToRadar(center, scale, tgt.X, tgt.Z);
DrawLine(p, tp, new Color(RedCol.R, RedCol.G, RedCol.B, 0.65f), 1.3f);
}
col = RedCol;
}
DrawCircle(p, blipR, col);
}
// Contenders
for (int i = 0; i < _state.Contenders.Count; i++)
{
var c = _state.Contenders[i];
if (!c.Alive) continue;
var p = WorldToRadar(center, scale, c.X, c.Z);
if (c.IsPlayer)
{
var dir = new Vector2(-MathF.Sin(c.Yaw), -MathF.Cos(c.Yaw));
var perp = new Vector2(-dir.Y, dir.X);
var pts = new[]
{
p + dir * 6.5f,
p - dir * 4.5f + perp * 4.2f,
p - dir * 4.5f - perp * 4.2f
};
DrawColoredPolygon(pts, OliveCol);
}
else
{
DrawCircle(p, 3.3f, OrangeCol);
}
}
}
private static readonly Color PatronColor = Color.FromHtml("#4ba8a5");
}
private sealed partial class SchematicPanel : Control
{
private readonly Action<string> _onHardpointClick;
private readonly List<(Rect2 Rect, string HardpointId)> _hitBoxes = new();
private ChassisGrid? _grid;
private int _selectedCargoIndex = -1;
public SchematicPanel(Action<string> onHardpointClick)
{
_onHardpointClick = onHardpointClick;
}
public void SetGrid(ChassisGrid grid, int selectedCargoIndex)
{
_grid = grid;
_selectedCargoIndex = selectedCargoIndex;
}
public override void _GuiInput(InputEvent @event)
{
if (@event is InputEventMouseButton mb && mb.Pressed && mb.ButtonIndex == MouseButton.Left)
{
for (int i = 0; i < _hitBoxes.Count; i++)
{
if (_hitBoxes[i].Rect.HasPoint(mb.Position))
{
_onHardpointClick(_hitBoxes[i].HardpointId);
AcceptEvent();
return;
}
}
}
}
public override void _Draw()
{
_hitBoxes.Clear();
if (_grid is null) return;
var spec = _grid.Spec;
float cellSize = MathF.Min((Size.X - 8f) / spec.Width, (Size.Y - 8f) / spec.Height);
float originX = (Size.X - spec.Width * cellSize) * 0.5f;
float originY = (Size.Y - spec.Height * cellSize) * 0.5f;
for (int y = 0; y < spec.Height; y++)
{
for (int x = 0; x < spec.Width; x++)
{
if (!spec.IsActive(x, y)) continue;
var rect = new Rect2(originX + x * cellSize + 1.5f, originY + y * cellSize + 1.5f, cellSize - 3f, cellSize - 3f);
float temp = _grid.Temperature[x, y];
Color fill = TempToColor(temp);
DrawRect(rect, fill);
// Armor bar at bottom of cell
float maxArm = MathF.Max(1f, _grid.MaxArmor[x, y]);
float armRatio = Math.Clamp(_grid.Armor[x, y] / maxArm, 0f, 1f);
var armRect = new Rect2(rect.Position.X + 2f, rect.End.Y - 5f, (rect.Size.X - 4f) * armRatio, 3f);
DrawRect(armRect, OliveCol);
// Border radiator highlight
Color border = _grid.BorderCells[x, y] ? CyanCol : BorderCol;
DrawRect(rect, border, filled: false, width: _grid.BorderCells[x, y] ? 1.5f : 1.0f);
}
}
// Hardpoints & mounted module temperature badges
ModuleSpec? selectedCargo = _selectedCargoIndex >= 0 && _selectedCargoIndex < _grid.Cargo.Count
? _grid.Cargo[_selectedCargoIndex]
: null;
for (int i = 0; i < spec.Hardpoints.Length; i++)
{
var hp = spec.Hardpoints[i];
var rect = new Rect2(
originX + hp.X * cellSize + 2f,
originY + hp.Y * cellSize + 2f,
hp.Width * cellSize - 4f,
hp.Height * cellSize - 4f);
_hitBoxes.Add((rect, hp.Id));
bool compatible = selectedCargo is not null && ChassisGrid.CanFit(hp, selectedCargo);
Color hpBorder = compatible ? GoldCol : TextLight;
DrawRect(rect, hpBorder, filled: false, width: compatible ? 2.4f : 1.4f);
var mounted = _grid.GetMountedOn(hp.Id);
string tag = mounted is not null
? $"{mounted.Spec.Kind.ToString()[..1]}:{mounted.CurrentTemp:F0}°"
: hp.Kind.ToString()[..3].ToUpperInvariant();
DrawString(
ThemeDB.FallbackFont,
rect.Position + new Vector2(3f, 13f),
tag,
HorizontalAlignment.Left,
rect.Size.X - 4f,
10,
mounted is { Spec.IsImperial: true } ? GoldCol : TextLight);
}
}
private static Color TempToColor(float temp)
{
if (temp < 45f)
{
float t = Math.Clamp((temp - 20f) / 25f, 0f, 1f);
return new Color(0.18f, 0.23f, 0.26f).Lerp(new Color(0.32f, 0.45f, 0.28f), t);
}
if (temp < 95f)
{
float t = Math.Clamp((temp - 45f) / 50f, 0f, 1f);
return new Color(0.32f, 0.45f, 0.28f).Lerp(new Color(0.62f, 0.69f, 0.36f), t);
}
if (temp < 140f)
{
float t = Math.Clamp((temp - 95f) / 45f, 0f, 1f);
return new Color(0.62f, 0.69f, 0.36f).Lerp(OrangeCol, t);
}
float hotT = Math.Clamp((temp - 140f) / 60f, 0f, 1f);
return OrangeCol.Lerp(RedCol, hotT);
}
}
}