- 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
268 lines
12 KiB
C#
268 lines
12 KiB
C#
using Godot;
|
|
using ViaFerrea.Client.App;
|
|
using ViaFerrea.Core.Map;
|
|
using ViaFerrea.Protocol.Client;
|
|
using ViaFerrea.Protocol.Messages;
|
|
|
|
namespace ViaFerrea.Client.Map;
|
|
|
|
/// <summary>
|
|
/// Entità sulla mappa (immagine 04): case con tetti in coppi sulle celle d'abitato, impronte
|
|
/// delle industrie per tipo, stazioni con marciapiedi e fabbricato, depositi, porto e valico;
|
|
/// alberi delle foreste con MultiMeshInstance2D per chunk.
|
|
/// </summary>
|
|
public partial class EntityLayer : Node2D
|
|
{
|
|
private ClientWorld? _world;
|
|
private int _drawnEntities = -1;
|
|
private int _drawnTerrain = -1;
|
|
private int _drawnTrack = -1;
|
|
private float _zoom = 1;
|
|
private readonly Dictionary<(int, int), MultiMeshInstance2D> _forests = [];
|
|
private Texture2D? _treeTexture;
|
|
|
|
public int SelectedStation { get; set; } = -1;
|
|
|
|
public void Bind(ClientWorld world)
|
|
{
|
|
_world = world;
|
|
foreach (var f in _forests.Values) f.QueueFree();
|
|
_forests.Clear();
|
|
_drawnEntities = _drawnTerrain = _drawnTrack = -1;
|
|
}
|
|
|
|
public void SetZoom(float zoom)
|
|
{
|
|
bool redraw = (zoom >= MapScale.OverviewZoom) != (_zoom >= MapScale.OverviewZoom);
|
|
_zoom = zoom;
|
|
if (redraw) QueueRedraw();
|
|
foreach (var f in _forests.Values) f.Visible = zoom >= 0.02f;
|
|
}
|
|
|
|
public void ForestChunk(int cx, int cy)
|
|
{
|
|
var w = _world;
|
|
if (w?.Terrain is null) return;
|
|
_treeTexture ??= TreeTexture();
|
|
var t = w.Terrain;
|
|
var trees = new List<Transform2D>();
|
|
var colors = new List<Color>();
|
|
int x0 = cx * t.ChunkCells, y0 = cy * t.ChunkCells;
|
|
for (int y = y0; y < Math.Min(t.Height, y0 + t.ChunkCells); y++)
|
|
{
|
|
for (int x = x0; x < Math.Min(t.Width, x0 + t.ChunkCells); x++)
|
|
{
|
|
if ((CellType)t.Types[y * t.Width + x] != CellType.Forest) continue;
|
|
uint h = (uint)(x * 73856093 ^ y * 19349663);
|
|
for (int k = 0; k < 5; k++)
|
|
{
|
|
h = h * 1664525 + 1013904223;
|
|
float jx = (h & 0xFFFF) / 65535f, jy = (h >> 16) / 65535f;
|
|
float r = (float)t.CellSizeM * (0.16f + 0.08f * ((h >> 8) & 7) / 7f);
|
|
var pos = new Vector2((float)((x + jx) * t.CellSizeM), (float)((y + jy) * t.CellSizeM));
|
|
trees.Add(new Transform2D(0, new Vector2(r * 2, r * 2), 0, pos));
|
|
colors.Add(Palette.Canopy.Lerp(new Color("7aa65a"), ((h >> 4) & 15) / 15f));
|
|
}
|
|
}
|
|
}
|
|
if (_forests.Remove((cx, cy), out var old)) old.QueueFree();
|
|
if (trees.Count == 0) return;
|
|
var mesh = new MultiMesh
|
|
{
|
|
TransformFormat = MultiMesh.TransformFormatEnum.Transform2D,
|
|
UseColors = true,
|
|
Mesh = new QuadMesh { Size = Vector2.One },
|
|
InstanceCount = trees.Count,
|
|
};
|
|
for (int i = 0; i < trees.Count; i++)
|
|
{
|
|
mesh.SetInstanceTransform2D(i, trees[i]);
|
|
mesh.SetInstanceColor(i, colors[i]);
|
|
}
|
|
var node = new MultiMeshInstance2D { Multimesh = mesh, Texture = _treeTexture, ZIndex = 3 };
|
|
AddChild(node);
|
|
_forests[(cx, cy)] = node;
|
|
}
|
|
|
|
/// <summary>Chioma d'albero: disco con bordo più scuro (immagine 02).</summary>
|
|
private static Texture2D TreeTexture()
|
|
{
|
|
const int n = 64;
|
|
var img = Image.CreateEmpty(n, n, false, Image.Format.Rgba8);
|
|
for (int y = 0; y < n; y++)
|
|
{
|
|
for (int x = 0; x < n; x++)
|
|
{
|
|
float dx = (x + 0.5f) / n * 2 - 1, dy = (y + 0.5f) / n * 2 - 1;
|
|
float d = MathF.Sqrt(dx * dx + dy * dy);
|
|
if (d > 1) { img.SetPixel(x, y, new Color(0, 0, 0, 0)); continue; }
|
|
float light = 1.05f - 0.25f * Math.Clamp(dx + dy + 0.3f, 0, 1) - (d > 0.82f ? 0.25f : 0);
|
|
img.SetPixel(x, y, new Color(light, light, light, 1));
|
|
}
|
|
}
|
|
return ImageTexture.CreateFromImage(img);
|
|
}
|
|
|
|
public override void _Process(double delta)
|
|
{
|
|
if (_world is null) return;
|
|
if (_world.EntityVersion != _drawnEntities || _world.TerrainVersion != _drawnTerrain || _world.TrackVersion != _drawnTrack) QueueRedraw();
|
|
}
|
|
|
|
public override void _Draw()
|
|
{
|
|
var w = _world;
|
|
if (w?.Terrain is null) return;
|
|
_drawnEntities = w.EntityVersion;
|
|
_drawnTerrain = w.TerrainVersion;
|
|
_drawnTrack = w.TrackVersion;
|
|
var t = w.Terrain;
|
|
float cs = (float)t.CellSizeM;
|
|
|
|
// case: blocchi per cella d'abitato, disposizione fissa per cella
|
|
foreach (var town in w.Towns.Values)
|
|
{
|
|
int r = (int)Math.Ceiling(town.RadiusCells) + 1;
|
|
var (tcx, tcy) = t.CellAt(town.Position);
|
|
for (int y = tcy - r; y <= tcy + r; y++)
|
|
{
|
|
for (int x = tcx - r; x <= tcx + r; x++)
|
|
{
|
|
if (x < 0 || y < 0 || x >= t.Width || y >= t.Height || (CellType)t.Types[y * t.Width + x] != CellType.Town) continue;
|
|
DrawHouses(x, y, cs);
|
|
}
|
|
}
|
|
}
|
|
foreach (var ind in w.Industries.Values) DrawIndustry(ind, cs);
|
|
foreach (var hub in w.Hubs.Values) DrawHub(hub, cs);
|
|
foreach (var d in w.Depots.Values) DrawDepot(w, d);
|
|
foreach (var st in w.Stations.Values) DrawStation(w, st);
|
|
}
|
|
|
|
private void DrawHouses(int x, int y, float cs)
|
|
{
|
|
uint h = (uint)(x * 2654435761u ^ y * 40503u);
|
|
int count = 3 + (int)(h % 3);
|
|
for (int k = 0; k < count; k++)
|
|
{
|
|
h = h * 1664525 + 1013904223;
|
|
float fx = (h & 1023) / 1023f * 0.62f + 0.06f, fy = ((h >> 10) & 1023) / 1023f * 0.62f + 0.06f;
|
|
float wpx = cs * (0.18f + ((h >> 20) & 7) * 0.02f), hpx = cs * (0.13f + ((h >> 23) & 7) * 0.015f);
|
|
var rect = new Rect2(x * cs + fx * cs, y * cs + fy * cs, wpx, hpx);
|
|
var roof = ((h >> 26) & 3) switch { 0 => Palette.IndustrialRoof.Lightened(0.15f), 1 => new Color("a8836a"), _ => Palette.TileRoof };
|
|
DrawRect(new Rect2(rect.Position + new Vector2(cs * 0.02f, cs * 0.02f), rect.Size), new Color(0, 0, 0, 0.18f));
|
|
DrawRect(rect, roof);
|
|
DrawLine(rect.Position + new Vector2(0, rect.Size.Y / 2), rect.Position + new Vector2(rect.Size.X, rect.Size.Y / 2), roof.Darkened(0.25f), cs * 0.012f);
|
|
}
|
|
}
|
|
|
|
private void DrawIndustry(IndustryInfo ind, float cs)
|
|
{
|
|
float size = Math.Max(1, ind.FootprintCells) * cs;
|
|
var c = MapScale.V(ind.Position);
|
|
var pad = new Rect2(c - new Vector2(size, size) * 0.45f, new Vector2(size, size) * 0.9f);
|
|
var ground = ind.Closed ? new Color("b8b4a6") : new Color("bdb6a2");
|
|
DrawRect(pad, ground);
|
|
DrawRect(pad, new Color("8f8a7a"), false, cs * 0.03f);
|
|
string type = ind.Type;
|
|
if (type.Contains("mine") || type.Contains("quarry") || type.Contains("well"))
|
|
{
|
|
DrawCircle(c, size * 0.28f, new Color("6b5e4f"));
|
|
DrawCircle(c, size * 0.18f, new Color("3d3630"));
|
|
DrawRect(new Rect2(c + new Vector2(size * 0.12f, -size * 0.38f), new Vector2(size * 0.22f, size * 0.14f)), Palette.IndustrialRoof);
|
|
}
|
|
else if (type.Contains("farm") || type.Contains("ranch") || type.Contains("logging"))
|
|
{
|
|
DrawRect(new Rect2(c - new Vector2(size * 0.3f, size * 0.2f), new Vector2(size * 0.32f, size * 0.22f)), Palette.TileRoof);
|
|
DrawCircle(c + new Vector2(size * 0.2f, -size * 0.1f), size * 0.08f, new Color("9a9a90"));
|
|
}
|
|
else
|
|
{
|
|
// capannoni con lucernari (immagine 04)
|
|
int sheds = Math.Max(2, ind.FootprintCells);
|
|
float sw = size * 0.8f / sheds;
|
|
for (int i = 0; i < sheds; i++)
|
|
{
|
|
var r = new Rect2(pad.Position + new Vector2(size * 0.05f + i * sw, size * 0.08f), new Vector2(sw * 0.92f, size * 0.55f));
|
|
DrawRect(new Rect2(r.Position + new Vector2(cs * 0.04f, cs * 0.04f), r.Size), new Color(0, 0, 0, 0.2f));
|
|
DrawRect(r, Palette.IndustrialRoof);
|
|
DrawLine(r.Position + new Vector2(r.Size.X / 2, 0), r.Position + new Vector2(r.Size.X / 2, r.Size.Y), Palette.IndustrialRoof.Darkened(0.3f), cs * 0.02f);
|
|
}
|
|
DrawCircle(pad.Position + new Vector2(size * 0.78f, size * 0.75f), size * 0.07f, new Color("6e7175"));
|
|
}
|
|
if (ind.Closed) DrawLine(pad.Position, pad.End, Palette.Danger, cs * 0.05f);
|
|
}
|
|
|
|
private void DrawHub(HubInfo hub, float cs)
|
|
{
|
|
var c = MapScale.V(hub.Position);
|
|
if (hub.Kind == "port")
|
|
{
|
|
DrawRect(new Rect2(c - new Vector2(cs * 0.9f, cs * 0.35f), new Vector2(cs * 1.8f, cs * 0.7f)), new Color("9d968a"));
|
|
DrawRect(new Rect2(c + new Vector2(cs * 0.2f, -cs * 0.15f), new Vector2(cs * 1.2f, cs * 0.3f)), new Color("4b5560"));
|
|
}
|
|
else
|
|
{
|
|
DrawRect(new Rect2(c - new Vector2(cs * 0.4f, cs * 0.25f), new Vector2(cs * 0.8f, cs * 0.5f)), Palette.IndustrialRoof);
|
|
for (int i = 0; i < 6; i++)
|
|
DrawLine(c + new Vector2(-cs * 0.6f + i * cs * 0.2f, cs * 0.4f), c + new Vector2(-cs * 0.5f + i * cs * 0.2f, cs * 0.4f), i % 2 == 0 ? Palette.Danger : Colors.White, cs * 0.06f);
|
|
}
|
|
}
|
|
|
|
/// <summary>Direzione del binario che entra nel nodo (per orientare marciapiedi e fabbricati).</summary>
|
|
private static Vector2 NodeDirection(ClientWorld w, int nodeId)
|
|
{
|
|
if (nodeId < 0 || nodeId >= w.Network.Nodes.Count) return Vector2.Right;
|
|
var node = w.Network.Nodes[nodeId];
|
|
foreach (var eid in node.Edges)
|
|
{
|
|
if (!w.Edges.TryGetValue(eid.Value, out var e) || e.S.Length < 2) continue;
|
|
bool atA = e.A == nodeId;
|
|
var p0 = atA ? MapScale.V(e.X[0], e.Y[0]) : MapScale.V(e.X[^1], e.Y[^1]);
|
|
var p1 = atA ? MapScale.V(e.X[1], e.Y[1]) : MapScale.V(e.X[^2], e.Y[^2]);
|
|
var d = p1 - p0;
|
|
if (d.LengthSquared() > 0) return atA ? d.Normalized() : -d.Normalized();
|
|
}
|
|
return Vector2.Right;
|
|
}
|
|
|
|
private void DrawStation(ClientWorld w, StationInfo st)
|
|
{
|
|
float s = MapScale.Symbol(_zoom);
|
|
var c = MapScale.V(st.Position);
|
|
var dir = NodeDirection(w, st.Node);
|
|
var n = new Vector2(-dir.Y, dir.X);
|
|
float len = 60f * Math.Max(1, s * 0.6f);
|
|
float pw = 3.5f * s;
|
|
// marciapiedi ai lati dei binari (immagine 02) e fabbricato con tetto in coppi
|
|
for (int k = 0; k < Math.Min(st.Platforms, 4); k++)
|
|
{
|
|
float side = (k % 2 == 0 ? 1 : -1) * (7f + 6f * (k / 2)) * s;
|
|
DrawLine(c - dir * len / 2 + n * side, c + dir * len / 2 + n * side, new Color("d9d4c6"), pw);
|
|
}
|
|
var b = c + n * 15f * s;
|
|
DrawSetTransform(b, dir.Angle());
|
|
DrawRect(new Rect2(new Vector2(-12, -5.5f) * s + new Vector2(1.5f, 1.5f) * s, new Vector2(24, 11) * s), new Color(0, 0, 0, 0.2f));
|
|
DrawRect(new Rect2(new Vector2(-12, -5.5f) * s, new Vector2(24, 11) * s), Palette.TileRoof);
|
|
DrawLine(new Vector2(-12, 0) * s, new Vector2(12, 0) * s, Palette.TileRoof.Darkened(0.3f), 0.8f * s);
|
|
DrawSetTransform(Vector2.Zero, 0);
|
|
if (st.Id == SelectedStation)
|
|
{
|
|
float radius = (float)(w.Data.Construction.Stations.CatchmentKm * 1000);
|
|
DrawArc(c, radius, 0, MathF.Tau, 96, Palette.Amber, 2f * s);
|
|
}
|
|
}
|
|
|
|
private void DrawDepot(ClientWorld w, DepotInfo d)
|
|
{
|
|
float s = MapScale.Symbol(_zoom);
|
|
var c = MapScale.V(d.Position);
|
|
var dir = NodeDirection(w, d.Node);
|
|
DrawSetTransform(c, dir.Angle());
|
|
DrawRect(new Rect2(new Vector2(-16, -9) * s + new Vector2(2, 2) * s, new Vector2(32, 18) * s), new Color(0, 0, 0, 0.2f));
|
|
DrawRect(new Rect2(new Vector2(-16, -9) * s, new Vector2(32, 18) * s), new Color("5a6670"));
|
|
for (int i = -1; i <= 1; i++) DrawLine(new Vector2(-14, i * 5) * s, new Vector2(14, i * 5) * s, new Color("8a96a0"), 1f * s);
|
|
DrawSetTransform(Vector2.Zero, 0);
|
|
}
|
|
}
|