supervisor-simulator/scripts/TestMap.cs
2026-01-18 20:05:23 +08:00

38 lines
1.2 KiB
C#

using Godot;
/// <summary>
/// 测试地图层
/// </summary>
public partial class TestMap : TileMapLayer {
private readonly Vector2I _highlightTileCoord = new(0, 5);
private readonly Vector2I _vector2INegOne = new(-1, -1);
/// <summary>
/// 上一次高亮的单元格。用于避免不必要的更新。
/// </summary>
private Vector2I _lastHighlightCell;
// Called when the node enters the scene tree for the first time.
/// <summary>
/// 场景加载完成时调用
/// </summary>
public override void _Ready() {
// 初始化最后高亮单元格为无效值。
_lastHighlightCell = _vector2INegOne;
}
/// <summary>
/// 每帧更新。
/// </summary>
/// <param name="delta">距离上一帧的时间间隔。</param>
public override void _Process(double delta) {
var mousePos = GetLocalMousePosition();
var cell = LocalToMap(mousePos);
if (cell != _lastHighlightCell) {
if (_lastHighlightCell != _vector2INegOne) EraseCell(_lastHighlightCell);
_lastHighlightCell = cell;
SetCell(cell, 0, _highlightTileCoord);
}
}
}