supervisor-simulator/scripts/TestMap.cs
2026-01-11 23:57:24 +08:00

45 lines
1.1 KiB
C#

using Godot;
using System;
/// <summary>
/// 测试地图层
/// </summary>
public partial class TestMap : TileMapLayer
{
/// <summary>
/// 上一次高亮的单元格。用于避免不必要的更新。
/// </summary>
private Vector2I _lastHighlightCell;
private readonly Vector2I _highlightTileCoord = new(0, 5);
private readonly Vector2I _vector2INegOne = new(-1, -1);
// 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)
{
Vector2 mousePos = GetLocalMousePosition();
Vector2I cell = LocalToMap(mousePos);
if (cell != _lastHighlightCell)
{
if (_lastHighlightCell != _vector2INegOne)
{
EraseCell(_lastHighlightCell);
}
_lastHighlightCell = cell;
SetCell(cell, 0, _highlightTileCoord);
}
}
}