92 lines
2.8 KiB
C#
92 lines
2.8 KiB
C#
using System.Diagnostics;
|
|
using Godot;
|
|
|
|
public class HexTile3D : Spatial {
|
|
public class TileTypeInfo {
|
|
public readonly string Name;
|
|
public readonly Color Color;
|
|
public readonly ushort TileTypeMask;
|
|
|
|
public TileTypeInfo(string name, Color color, ushort tileTypeMask) {
|
|
Name = name;
|
|
Color = color;
|
|
TileTypeMask = tileTypeMask;
|
|
}
|
|
}
|
|
|
|
// scene nodes
|
|
private MeshInstance _mesh;
|
|
private StaticBody _staticBody;
|
|
|
|
// signals
|
|
[Signal]
|
|
private delegate void TileClicked(HexTile3D tile3d);
|
|
|
|
[Signal]
|
|
private delegate void TileHovered(HexTile3D tile3d);
|
|
|
|
// other member variables
|
|
private SpatialMaterial _previousMaterial;
|
|
|
|
private readonly HexGrid _hexGrid;
|
|
|
|
public HexCell Cell = new();
|
|
public bool IsMouseOver;
|
|
public MeshInstance Mesh;
|
|
|
|
public Vector2 OffsetCoords {
|
|
get => Cell.OffsetCoords;
|
|
|
|
set {
|
|
Cell.OffsetCoords = value;
|
|
Transform tile3dTransform = Transform;
|
|
Vector2 cellPlaneCoords = _hexGrid.GetHexCenter(Cell);
|
|
tile3dTransform.origin.x = cellPlaneCoords.x;
|
|
tile3dTransform.origin.z = cellPlaneCoords.y;
|
|
Transform = tile3dTransform;
|
|
}
|
|
}
|
|
|
|
private HexTile3D() {
|
|
_hexGrid = new HexGrid();
|
|
}
|
|
|
|
// Called when the node enters the scene tree for the first time.
|
|
public override void _Ready() {
|
|
_mesh = GetNode<MeshInstance>("Mesh");
|
|
_staticBody = GetNode<StaticBody>("StaticBody");
|
|
|
|
_staticBody.Connect("input_event", this, nameof(OnAreaInputEvent));
|
|
_staticBody.Connect("mouse_entered", this, nameof(OnAreaMouseEntered));
|
|
_staticBody.Connect("mouse_exited", this, nameof(OnAreaMouseExited));
|
|
|
|
Mesh = GetNode<MeshInstance>("Mesh");
|
|
Debug.Assert(Mesh != null);
|
|
}
|
|
|
|
|
|
public void OnAreaInputEvent(Node camera, InputEvent inputEvent, Vector3 position, Vector3 normal,
|
|
int shapeIndex) {
|
|
if (IsMouseOver && inputEvent is InputEventMouseButton) {
|
|
InputEventMouseButton mouseButtonEvent = (InputEventMouseButton)inputEvent;
|
|
if (mouseButtonEvent.ButtonIndex == 1 && mouseButtonEvent.Pressed) {
|
|
EmitSignal("TileClicked", this);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void OnAreaMouseEntered() {
|
|
IsMouseOver = true;
|
|
_previousMaterial = (SpatialMaterial)_mesh.MaterialOverride;
|
|
|
|
EmitSignal("TileHovered", this);
|
|
// SpatialMaterial clonedMaterial = (SpatialMaterial)_mesh.GetSurfaceMaterial(0).Duplicate();
|
|
// clonedMaterial.AlbedoColor = new Color(1, 0, 0);
|
|
// _mesh.MaterialOverride = clonedMaterial;
|
|
}
|
|
|
|
public void OnAreaMouseExited() {
|
|
IsMouseOver = false;
|
|
_mesh.MaterialOverride = _previousMaterial;
|
|
}
|
|
} |