UI提升,进入能跑阶段

This commit is contained in:
2026-09-24 22:55:46 +08:00
parent afb343aa49
commit 5f1be65512
47 changed files with 4645 additions and 354 deletions
+80 -38
View File
@@ -1,12 +1,34 @@
using OSM.Client.Contracts;
namespace OSM.Client.World;
/// <summary>
/// 灰盒地面渲染。
/// <para><b>只画服务器地图里真实存在的格子</b>:坐标、高度、颜色全部来自 <c>MapDto</c>,
/// 因此不再假设「地图是从 (0,0) 起的稠密矩形」,也不会画出协议里根本不存在的掩体
/// (<c>GridDto</c> 只有 <c>Id/X/Y/Z/Color/Characters</c>,掩体规则见实施计划 §10,本阶段不包含)。</para>
/// </summary>
public partial class GreyboxMap2D : Node2D
{
/// <summary>
/// 服务器格子颜色的默认值,对应 FunGame.Core 里 <c>Grid.Color</c> 的初始值
/// <c>System.Drawing.Color.Gray</c>。全图都是它说明地图作者没给任何格子指定颜色。
/// </summary>
private const string ServerDefaultGridColor = "Gray";
[Export]
public Vector2I MapSize { get; set; } = new(16, 12);
[Export]
public Vector2 CellSize { get; set; } = new(128, 64);
public Vector2 CellSize { get; set; } = new(144, 72);
[Export(PropertyHint.Range, "0,8,0.25")]
public float CellGapPixels { get; set; } = 2.0f;
private readonly List<SoloGridDto> _grids = [];
/// <summary>当前实际参与绘制的格子(自检用:画的就是服务器给的那批)。</summary>
public IReadOnlyList<SoloGridDto> DrawnGrids => _grids;
public override void _Ready()
{
@@ -19,33 +41,71 @@ public partial class GreyboxMap2D : Node2D
QueueRedraw();
}
public override void _Draw()
/// <summary>用权威地图数据重建地面。传 null 表示清空。</summary>
public void SetMap(SoloMapDto? map)
{
for (var y = 0; y < MapSize.Y; y++)
_grids.Clear();
if (map is not null)
{
for (var x = 0; x < MapSize.X; x++)
{
var center = GridProjector.GridToScreen(new Vector2I(x, y), CellSize.X, CellSize.Y);
var points = Diamond(center);
var checker = (x + y) % 2 == 0;
var fill = checker
? new Color("273746")
: new Color("22313f");
DrawColoredPolygon(points, fill);
}
_grids.AddRange(map.Grids);
MapSize = new Vector2I(map.Length, map.Width);
}
DrawCover(new Vector2I(5, 4), new Color("526777"));
DrawCover(new Vector2I(6, 4), new Color("526777"));
DrawCover(new Vector2I(10, 7), new Color("705d54"));
DrawCover(new Vector2I(11, 7), new Color("705d54"));
QueueRedraw();
}
public override void _Draw()
{
foreach (var grid in _grids)
{
var points = Diamond(CellCenter(grid));
var checker = (grid.X + grid.Y) % 2 == 0;
DrawColoredPolygon(points, checker ? new Color("273746") : new Color("22313f"));
// 服务器显式指定过颜色时叠一层半透明着色;默认 Gray 视为「未指定」,
// 保留灰盒棋盘配色(否则当前每张图都会被刷成整片灰)。
if (TryParseExplicitColor(grid.Color, out var tint))
{
DrawColoredPolygon(points, tint with { A = 0.45f });
}
}
}
private Vector2 CellCenter(SoloGridDto grid)
{
// 用服务器的 Z 抬升格心:单位位置本来就用了 Z(见 BattleScreen 的 GridToScreen 调用),
// 地面若忽略 Z,多层地图上地面与棋子就会错位。
return GridProjector.GridToScreen(
new Vector2I(grid.X, grid.Y),
CellSize.X,
CellSize.Y,
grid.Z);
}
private static bool TryParseExplicitColor(string? serverColor, out Color color)
{
color = Colors.White;
if (string.IsNullOrWhiteSpace(serverColor)
|| string.Equals(serverColor, ServerDefaultGridColor, StringComparison.OrdinalIgnoreCase))
{
return false;
}
// Godot 的命名色表不保证覆盖 System.Drawing 的全部名字;认不出来就当没指定。
var parsed = Color.FromString(serverColor, Colors.Transparent);
if (parsed.A <= 0.001f)
{
return false;
}
color = parsed;
return true;
}
private Vector2[] Diamond(Vector2 center)
{
var halfWidth = CellSize.X * 0.5f;
var halfHeight = CellSize.Y * 0.5f;
var halfWidth = Math.Max(1.0f, CellSize.X * 0.5f - CellGapPixels);
var halfHeight = Math.Max(1.0f, CellSize.Y * 0.5f - CellGapPixels);
return
[
center + new Vector2(0, -halfHeight),
@@ -54,22 +114,4 @@ public partial class GreyboxMap2D : Node2D
center + new Vector2(-halfWidth, 0)
];
}
private void DrawCover(Vector2I grid, Color color)
{
var center = GridProjector.GridToScreen(grid, CellSize.X, CellSize.Y);
var top = center + new Vector2(0, -28);
var basePoints = Diamond(center);
var topPoints = Diamond(top);
DrawColoredPolygon(
[
basePoints[1], basePoints[2], topPoints[2], topPoints[1]
], color.Darkened(0.25f));
DrawColoredPolygon(
[
basePoints[2], basePoints[3], topPoints[3], topPoints[2]
], color.Darkened(0.4f));
DrawColoredPolygon(topPoints, color);
}
}