half-earth/half-earth/scripts/GridCursor.cs
2022-12-07 00:13:11 +00:00

52 lines
1.5 KiB
C#

using Godot;
using System;
public class GridCursor : Sprite
{
[Export]
public NodePath Grid { get; set; }
private WorldGrid _grid;
private ShaderMaterial _material;
private const string T = "t";
public override void _Ready()
{
_grid = GetNode<WorldGrid>(Grid);
_material = (ShaderMaterial)Material;
}
public override void _Input(InputEvent @event)
{
base._Input(@event);
if (@event is InputEventMouseMotion mouseMoveEvent)
{
var pos = mouseMoveEvent.Position;
_grid.GetGridPos(pos, out var x, out var y);
this.Visible = _grid.IsInBounds(x, y);
var position = new Vector2(x + .5f, y + .5f) * _grid.CellSize;
this.Position = position;
}
else if (@event is InputEventMouseButton mouseButtonEvent)
{
switch ((ButtonList)mouseButtonEvent.ButtonIndex)
{
case ButtonList.Left:
if (mouseButtonEvent.Pressed)
{
var pos = mouseButtonEvent.Position;
_grid.GetGridPos(pos, out var x, out var y);
_grid.ToggleTileHighlight(x, y);
_material.SetShaderParam(T, 1f);
}
else
{
_material.SetShaderParam(T, 0f);
}
break;
}
}
}
}