GodotComponentTest/scenes/HexTile3D.cs

93 lines
2.7 KiB
C#
Raw Normal View History

using System.Diagnostics;
2022-12-03 21:58:28 +01:00
using Godot;
2023-11-18 22:32:57 +01:00
public class HexTile3D : Spatial {
public enum TileType {
Undefined,
2022-12-03 21:58:28 +01:00
Sand,
Grass,
DeepGrass
}
2023-11-18 22:32:57 +01:00
public static TileType[] ValidTileTypes = { TileType.Sand, TileType.Grass, TileType.DeepGrass };
2022-12-03 21:58:28 +01:00
// scene nodes
private MeshInstance _mesh;
private StaticBody _staticBody;
2022-12-03 21:58:28 +01:00
// signals
[Signal]
2023-11-18 22:32:57 +01:00
private delegate void TileClicked(HexTile3D tile3d);
2022-12-03 21:58:28 +01:00
[Signal]
2023-11-18 22:32:57 +01:00
private delegate void TileHovered(HexTile3D tile3d);
2022-12-03 21:58:28 +01:00
// other member variables
private SpatialMaterial _previousMaterial;
2023-11-18 22:32:57 +01:00
private readonly HexGrid _hexGrid;
2023-11-18 22:32:57 +01:00
public HexCell Cell = new();
public bool IsMouseOver;
public MeshInstance Mesh;
2022-12-03 21:58:28 +01:00
2023-11-18 22:32:57 +01:00
public Vector2 OffsetCoords {
get => Cell.OffsetCoords;
2023-11-18 22:32:57 +01:00
set {
Cell.OffsetCoords = value;
Transform tile3dTransform = Transform;
Vector2 cellPlaneCoords = _hexGrid.GetHexCenter(Cell);
tile3dTransform.origin.x = cellPlaneCoords.x;
tile3dTransform.origin.z = cellPlaneCoords.y;
Transform = tile3dTransform;
}
}
2023-08-28 13:59:39 +02:00
public TileType Type { get; set; }
2022-12-03 21:58:28 +01:00
2023-11-18 22:32:57 +01:00
private HexTile3D() {
_hexGrid = new HexGrid();
}
2022-12-03 21:58:28 +01:00
// Called when the node enters the scene tree for the first time.
2023-11-18 22:32:57 +01:00
public override void _Ready() {
2022-12-03 21:58:28 +01:00
_mesh = GetNode<MeshInstance>("Mesh");
_staticBody = GetNode<StaticBody>("StaticBody");
2022-12-03 21:58:28 +01:00
_staticBody.Connect("input_event", this, nameof(OnAreaInputEvent));
_staticBody.Connect("mouse_entered", this, nameof(OnAreaMouseEntered));
_staticBody.Connect("mouse_exited", this, nameof(OnAreaMouseExited));
2022-12-03 21:58:28 +01:00
2023-11-18 22:32:57 +01:00
Mesh = GetNode<MeshInstance>("Mesh");
Debug.Assert(Mesh != null);
2023-11-18 22:32:57 +01:00
Type = TileType.Undefined;
2022-12-03 21:58:28 +01:00
}
public void OnAreaInputEvent(Node camera, InputEvent inputEvent, Vector3 position, Vector3 normal,
2023-11-18 22:32:57 +01:00
int shapeIndex) {
if (IsMouseOver && inputEvent is InputEventMouseButton) {
2022-12-03 21:58:28 +01:00
InputEventMouseButton mouseButtonEvent = (InputEventMouseButton)inputEvent;
2023-11-18 22:32:57 +01:00
if (mouseButtonEvent.ButtonIndex == 1 && mouseButtonEvent.Pressed) {
2023-01-04 22:49:00 +01:00
EmitSignal("TileClicked", this);
2022-12-03 21:58:28 +01:00
}
}
}
2023-11-18 22:32:57 +01:00
public void OnAreaMouseEntered() {
2022-12-03 21:58:28 +01:00
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;
2022-12-03 21:58:28 +01:00
}
2023-11-18 22:32:57 +01:00
public void OnAreaMouseExited() {
2022-12-03 21:58:28 +01:00
IsMouseOver = false;
_mesh.MaterialOverride = _previousMaterial;
}
}