Files
Oshima-Game/src/World/GreyboxMap2D.cs
T

118 lines
3.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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(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()
{
QueueRedraw();
}
public void SetMapSize(Vector2I mapSize)
{
MapSize = mapSize;
QueueRedraw();
}
/// <summary>用权威地图数据重建地面。传 null 表示清空。</summary>
public void SetMap(SoloMapDto? map)
{
_grids.Clear();
if (map is not null)
{
_grids.AddRange(map.Grids);
MapSize = new Vector2I(map.Length, map.Width);
}
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 = 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),
center + new Vector2(halfWidth, 0),
center + new Vector2(0, halfHeight),
center + new Vector2(-halfWidth, 0)
];
}
}