supervisor-simulator/scripts/TestMap.cs
2024-12-06 17:14:41 +08:00

41 lines
1009 B
C#

using Godot;
using System;
public partial class TestMap : TileMapLayer
{
/// <summary>
/// The last cell that was highlighted. Used to avoid unnecessary updates.
/// </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.
public override void _Ready()
{
// Initialize the lastHighlightCell to an invalid value.
lastHighlightCell = vector2INegOne;
}
/// <summary>
/// Called every frame.
/// </summary>
/// <param name="delta">the elapsed time since the previous frame.</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);
}
}
}