-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDemo2D.cs
131 lines (106 loc) · 3.97 KB
/
Demo2D.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using Godot;
using IanByrne.HexTiles;
using System;
using System.Collections.Generic;
public class Demo2D : Node2D
{
private Polygon2D _highlightedCell;
private Label _mouseCoords;
private Label _cellCoords;
private PackedScene _cellScene;
private Button _modeButton;
[Export]
public HexGrid HexGrid;
[Export]
public int GridRadius = 5;
public override void _Ready()
{
base._Ready();
_highlightedCell = GetNode<Polygon2D>("HighlightedCell");
_mouseCoords = GetNode<Label>("../HUD/Coordinates/MouseValue");
_cellCoords = GetNode<Label>("../HUD/Coordinates/CellValue");
_modeButton = GetNode<Button>("../HUD/Controls/ModeValue");
_cellScene = GD.Load<PackedScene>("res://HexCell2D.tscn");
_modeButton.Pressed = HexGrid.Mode == HexMode.FLAT;
_modeButton.Text = HexGrid.Mode.ToString();
DrawGrid(GridRadius);
}
public override void _UnhandledInput(InputEvent @event)
{
base._UnhandledInput(@event);
var relativePos = Transform.AffineInverse().Multiply(GetGlobalMousePosition());
var cell = HexGrid.GetCellAtPixel(relativePos);
if (@event is InputEventMouseMotion)
{
if (_mouseCoords != null)
_mouseCoords.Text = relativePos.ToString();
if (_cellCoords != null && cell.HasValue)
_cellCoords.Text = cell.Value.CubeCoordinates.ToString();
if (_highlightedCell != null && cell.HasValue)
_highlightedCell.Position = HexGrid.GetPixelAtCell(cell.Value);
}
if (@event is InputEventMouseButton mouseButton && mouseButton.ButtonIndex == (int)ButtonList.Right && mouseButton.Pressed)
{
// Toggle obstacle at selected hex
if (cell.HasValue)
{
// Undraw old cell
foreach (Polygon2D polygon in GetTree().GetNodesInGroup(cell.Value.CubeCoordinates.ToString()))
polygon.QueueFree();
var newCell = cell.Value;
newCell.MovementCost = newCell.MovementCost == 0 ? -1 : 0;
var colour = newCell.MovementCost == -1 ? new Color(255, 255, 0) : new Color(0, 255, 0);
// Add new cell
HexGrid.SetCell(newCell);
DrawCell(newCell, colour);
}
}
}
private void DrawGrid(int radius)
{
foreach (Polygon2D polygon in GetTree().GetNodesInGroup("cells"))
polygon.QueueFree();
if (HexGrid.Mode == HexMode.POINTY)
_highlightedCell.RotationDegrees = 30;
else
_highlightedCell.RotationDegrees = 0;
var cells = new Dictionary<Vector2, HexCell>();
for (int x = -radius; x <= radius; ++x)
{
for (int y = -radius; y <= radius; ++y)
{
for (int z = -radius; z <= radius; ++z)
{
if (x + y + z == 0)
{
var cell = new HexCell(x, y, z);
cells.Add(cell.AxialCoordinates, cell);
DrawCell(cell, new Color(0, 255, 0));
}
}
}
}
HexGrid.SetCells(cells);
}
private void DrawCell(HexCell cell, Color colour)
{
var node = _cellScene.Instance<Polygon2D>();
node.Position = HexGrid.GetPixelAtCell(cell);
node.ZIndex = -1;
node.Color = colour;
node.Scale = new Vector2(0.9f, 0.9f);
node.AddToGroup("cells");
node.AddToGroup(cell.CubeCoordinates.ToString());
if (HexGrid.Mode == HexMode.POINTY)
node.RotationDegrees = 30;
else
node.RotationDegrees = 0;
AddChild(node);
}
private void OnModeToggled(bool toggle)
{
HexGrid.Mode = toggle ? HexMode.FLAT : HexMode.POINTY;
_modeButton.Text = HexGrid.Mode.ToString();
DrawGrid(GridRadius);
}
}