- 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
146 lines
5.3 KiB
C#
146 lines
5.3 KiB
C#
using Godot;
|
|
using ViaFerrea.Client.Net;
|
|
|
|
namespace ViaFerrea.Client.Map;
|
|
|
|
/// <summary>
|
|
/// Vista della mappa: Node2D con Camera2D e i livelli (terreno, opere e binari, entità, treni,
|
|
/// strumenti). Rotella = zoom attorno al cursore, tasto destro o centrale = spostamento,
|
|
/// WASD/frecce = spostamento da tastiera. Coordinate del mondo in metri (1 unità = 1 m).
|
|
/// </summary>
|
|
public partial class MapView : Node2D
|
|
{
|
|
public const float MinZoom = 0.008f;
|
|
public const float MaxZoom = 6f;
|
|
|
|
public Camera2D Camera { get; } = new();
|
|
public TerrainLayer Terrain { get; } = new() { ZIndex = 0 };
|
|
public TrackLayer Track { get; } = new() { ZIndex = 2 };
|
|
public EntityLayer Entities { get; } = new() { ZIndex = 1 };
|
|
public TrainLayer Trains { get; } = new() { ZIndex = 5 };
|
|
public ToolOverlay Tools { get; } = new() { ZIndex = 8 };
|
|
|
|
/// <summary>Clic sinistro sulla mappa (posizione nel mondo).</summary>
|
|
public event Action<Vector2>? Clicked;
|
|
/// <summary>Clic destro senza trascinamento (annulla lo strumento).</summary>
|
|
public event Action? Cancelled;
|
|
/// <summary>Spostamento del mouse sulla mappa (posizione nel mondo).</summary>
|
|
public event Action<Vector2>? Hovered;
|
|
|
|
private bool _dragging;
|
|
private Vector2 _dragDistance;
|
|
private float _lastZoom = -1;
|
|
|
|
public override void _Ready()
|
|
{
|
|
AddChild(Terrain);
|
|
AddChild(Entities);
|
|
AddChild(Track);
|
|
AddChild(Trains);
|
|
AddChild(Tools);
|
|
AddChild(Camera);
|
|
Camera.Enabled = true;
|
|
Camera.MakeCurrent();
|
|
}
|
|
|
|
public void Bind(GameSession session)
|
|
{
|
|
Terrain.Bind(session.World);
|
|
Track.Bind(session.World);
|
|
Entities.Bind(session.World);
|
|
Trains.Bind(session);
|
|
Tools.Bind(session.World);
|
|
var t = session.World.Summary!;
|
|
Camera.Position = new Vector2((float)(t.WidthCells * t.CellSizeM / 2), (float)(t.HeightCells * t.CellSizeM / 2));
|
|
float fit = (float)(GetViewportRect().Size.X / (t.WidthCells * t.CellSizeM));
|
|
SetZoom(Math.Clamp(fit * 2.5f, MinZoom, MaxZoom));
|
|
}
|
|
|
|
public float Zoom => Camera.Zoom.X;
|
|
|
|
public void SetZoom(float zoom)
|
|
{
|
|
zoom = Math.Clamp(zoom, MinZoom, MaxZoom);
|
|
Camera.Zoom = new Vector2(zoom, zoom);
|
|
}
|
|
|
|
public void CenterOn(Vector2 world) => Camera.Position = world;
|
|
|
|
/// <summary>Rettangolo del mondo visibile.</summary>
|
|
public Rect2 VisibleRect()
|
|
{
|
|
var size = GetViewportRect().Size / Zoom;
|
|
return new Rect2(Camera.GetScreenCenterPosition() - size / 2, size);
|
|
}
|
|
|
|
public override void _Process(double delta)
|
|
{
|
|
var move = Vector2.Zero;
|
|
if (Input.IsKeyPressed(Key.W) || Input.IsKeyPressed(Key.Up)) move.Y -= 1;
|
|
if (Input.IsKeyPressed(Key.S) || Input.IsKeyPressed(Key.Down)) move.Y += 1;
|
|
if (Input.IsKeyPressed(Key.A) || Input.IsKeyPressed(Key.Left)) move.X -= 1;
|
|
if (Input.IsKeyPressed(Key.D) || Input.IsKeyPressed(Key.Right)) move.X += 1;
|
|
if (move != Vector2.Zero && GetViewport().GuiGetFocusOwner() is not LineEdit)
|
|
Camera.Position += move.Normalized() * (float)delta * 700f / Zoom;
|
|
if (Math.Abs(Zoom - _lastZoom) > 1e-6)
|
|
{
|
|
_lastZoom = Zoom;
|
|
Terrain.SetZoom(Zoom);
|
|
Track.SetZoom(Zoom);
|
|
Entities.SetZoom(Zoom);
|
|
Trains.SetZoom(Zoom);
|
|
Tools.SetZoom(Zoom);
|
|
}
|
|
}
|
|
|
|
public override void _UnhandledInput(InputEvent e)
|
|
{
|
|
switch (e)
|
|
{
|
|
case InputEventMouseButton { ButtonIndex: MouseButton.WheelUp, Pressed: true } wheel:
|
|
ZoomAt(wheel.Position, 1.15f);
|
|
GetViewport().SetInputAsHandled();
|
|
break;
|
|
case InputEventMouseButton { ButtonIndex: MouseButton.WheelDown, Pressed: true } wheel:
|
|
ZoomAt(wheel.Position, 1 / 1.15f);
|
|
GetViewport().SetInputAsHandled();
|
|
break;
|
|
case InputEventMouseButton { ButtonIndex: MouseButton.Right or MouseButton.Middle } b:
|
|
if (b.Pressed)
|
|
{
|
|
_dragging = true;
|
|
_dragDistance = Vector2.Zero;
|
|
}
|
|
else
|
|
{
|
|
_dragging = false;
|
|
if (b.ButtonIndex == MouseButton.Right && _dragDistance.Length() < 4) Cancelled?.Invoke();
|
|
}
|
|
GetViewport().SetInputAsHandled();
|
|
break;
|
|
case InputEventMouseButton { ButtonIndex: MouseButton.Left, Pressed: true }:
|
|
Clicked?.Invoke(GetGlobalMousePosition());
|
|
GetViewport().SetInputAsHandled();
|
|
break;
|
|
case InputEventMouseMotion motion:
|
|
if (_dragging)
|
|
{
|
|
Camera.Position -= motion.Relative / Zoom;
|
|
_dragDistance += motion.Relative;
|
|
}
|
|
Hovered?.Invoke(GetGlobalMousePosition());
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void ZoomAt(Vector2 screen, float factor)
|
|
{
|
|
var before = GetCanvasTransform().AffineInverse() * screen;
|
|
SetZoom(Zoom * factor);
|
|
// la camera si sposta perché il punto sotto il cursore resti fermo
|
|
var size = GetViewportRect().Size;
|
|
var offset = (screen - size / 2) / Zoom;
|
|
Camera.Position = before - offset;
|
|
}
|
|
}
|