Files
Rail_Enterprise/client/ViaFerrea.Client/scripts/Hud/Minimap.cs
T
Claude 9a4ada6d9d F3: client Godot minimo, replay dei comandi e collaudi automatici
- Client Godot 4.7 .NET (Godot.NET.Sdk 4.7.2): menu, collegamento, partita
  locale con server figlio; mappa con terreno (fasce, ombreggiatura, rive
  curve con shader, curve di livello), opere, entità, alberi, treni
  interpolati con fumo; strumenti binari con profilo, stazione, deposito,
  segnali, demolizione; pannelli treni, linee, mercato, finanze, schede;
  barra superiore, minimappa, notiziario; italiano e inglese.
- Pilota automatico del client e scripts/client-autopilot.sh: partita locale,
  linea con ponte, treni in moto e schermate (docs/screenshots).
- Registro dei comandi del server (--record) e viaferrea-tools replay.
- PROTOCOL.md normativo, ADR 0009, ARCHITECTURE, STATE e README aggiornati.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0191R7vXgtbAvSV4V8tj8uzL
2026-10-02 00:47:42 +00:00

105 lines
3.9 KiB
C#

using Godot;
using ViaFerrea.Client.App;
using ViaFerrea.Client.Map;
using ViaFerrea.Core.Map;
using ViaFerrea.Protocol.Client;
namespace ViaFerrea.Client.Hud;
/// <summary>Minimappa: terreno ridotto, linee, città e rettangolo della vista; un clic centra la mappa.</summary>
public partial class Minimap : Control
{
private ClientWorld? _world;
private MapView? _map;
private ImageTexture? _texture;
private int _terrainVersion = -1;
public void Bind(ClientWorld world, MapView map)
{
_world = world;
_map = map;
_terrainVersion = -1;
CustomMinimumSize = new Vector2(0, 220);
MouseFilter = MouseFilterEnum.Stop;
}
public override void _Process(double delta)
{
if (_world?.Terrain is { } t && _world.TerrainVersion != _terrainVersion && Engine.GetProcessFrames() % 30 == 0)
{
_terrainVersion = _world.TerrainVersion;
int step = Math.Max(1, t.Width / 256);
int w = t.Width / step, h = t.Height / step;
var data = new byte[w * h * 4];
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
var type = (CellType)t.Types[y * step * t.Width + x * step];
var c = type switch
{
CellType.Sea => Palette.Sea,
CellType.Water => Palette.Water,
CellType.Forest => Palette.Undergrowth,
CellType.Town => Palette.TileRoof.Lightened(0.3f),
CellType.Mountain or CellType.Rock => Palette.Highland,
CellType.Hill => Palette.Hill,
_ => Palette.Plain,
};
int i = (y * w + x) * 4;
data[i] = (byte)(c.R * 255);
data[i + 1] = (byte)(c.G * 255);
data[i + 2] = (byte)(c.B * 255);
data[i + 3] = 255;
}
}
_texture = ImageTexture.CreateFromImage(Image.CreateFromData(w, h, false, Image.Format.Rgba8, data));
}
QueueRedraw();
}
private Rect2 MapRect()
{
var t = _world!.Terrain!;
float aspect = (float)t.Width / t.Height;
float w = Size.X, h = Size.X / aspect;
if (h > Size.Y)
{
h = Size.Y;
w = h * aspect;
}
return new Rect2((Size.X - w) / 2, (Size.Y - h) / 2, w, h);
}
public override void _Draw()
{
DrawStyleBox(UiTheme.Box(Palette.Inner, Palette.Border, 8, 0), new Rect2(Vector2.Zero, Size));
if (_world?.Terrain is not { } t || _texture is null || _map is null) return;
var r = MapRect();
DrawTextureRect(_texture, r, false);
float k = r.Size.X / (float)(t.Width * t.CellSizeM);
Vector2 P(Vector2 world) => r.Position + world * k;
foreach (var e in _world.Edges.Values)
{
if (e.S.Length < 2) continue;
DrawLine(P(MapScale.V(e.X[0], e.Y[0])), P(MapScale.V(e.X[^1], e.Y[^1])), new Color("2a2f2c"), 2);
}
foreach (var town in _world.Towns.Values) DrawCircle(P(MapScale.V(town.Position)), 3.5f, new Color("8a6a55"));
var view = _map.VisibleRect();
DrawRect(new Rect2(P(view.Position), view.Size * k), Palette.Amber, false, 2);
}
public override void _GuiInput(InputEvent e)
{
if (_world?.Terrain is not { } t || _map is null) return;
if (e is InputEventMouseButton { ButtonIndex: MouseButton.Left, Pressed: true } b || e is InputEventMouseMotion { ButtonMask: MouseButtonMask.Left })
{
var pos = e is InputEventMouse m ? m.Position : Vector2.Zero;
var r = MapRect();
float k = r.Size.X / (float)(t.Width * t.CellSizeM);
_map.CenterOn((pos - r.Position) / k);
AcceptEvent();
}
}
}