-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDemo3D.cs
177 lines (144 loc) · 6.28 KB
/
Demo3D.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using Godot;
using IanByrne.HexTiles;
using System.Collections.Generic;
public class Demo3D : Spatial
{
private CameraGimbal _camera;
private CSGPolygon _highlightedCell;
private Label _mouseCoords;
private Label _cellCoords;
private PackedScene _cellScene;
private Button _modeButton;
private RigidBody _player;
[Export]
public HexGrid HexGrid;
[Export]
public int GridRadius = 5;
public override void _Ready()
{
base._Ready();
_camera = GetNode<CameraGimbal>("../MainCamera");
_highlightedCell = GetNode<CSGPolygon>("HighlightedCell");
_mouseCoords = GetNode<Label>("../HUD/Coordinates/MouseValue");
_cellCoords = GetNode<Label>("../HUD/Coordinates/CellValue");
_modeButton = GetNode<Button>("../HUD/Controls/ModeValue");
_player = GetNode<RigidBody>("../Player");
_cellScene = GD.Load<PackedScene>("res://HexCell3D.tscn");
_modeButton.Pressed = HexGrid.Mode == HexMode.FLAT;
_modeButton.Text = HexGrid.Mode.ToString();
DrawGrid(GridRadius);
}
public override void _UnhandledInput(InputEvent @event)
{
base._UnhandledInput(@event);
if (@event is InputEventMouse mouse)
{
var spaceState = GetWorld().DirectSpaceState;
var rayOrigin = _camera.Camera.ProjectRayOrigin(mouse.Position);
var rayEnd = _camera.Camera.ProjectRayNormal(mouse.Position) * 2000;
var intersection = spaceState.IntersectRay(rayOrigin, rayEnd);
if (intersection.Contains("position"))
{
var relativePosV3 = Transform.AffineInverse().Xform((Vector3)intersection["position"]);
var relativePos = new Vector2(relativePosV3.x, relativePosV3.z);
var cell = HexGrid.GetCellAtPixel(relativePos);
if (@event is InputEventMouseMotion && !_camera.Clicking)
{
if (_mouseCoords != null)
_mouseCoords.Text = relativePos.ToString();
if (_cellCoords != null && cell.HasValue)
_cellCoords.Text = cell.Value.Index.ToString() + " " + cell.Value.CubeCoordinates.ToString();
if (_highlightedCell != null && cell.HasValue)
{
var planePos = HexGrid.GetPixelAtCell(cell.Value);
_highlightedCell.Translation = new Vector3(planePos.x, 0, planePos.y);
}
}
if (@event is InputEventMouseButton mouseButton && cell.HasValue)
{
if (mouseButton.ButtonIndex == (int)ButtonList.Right && mouseButton.Pressed)
{
// Toggle obstacle at selected hex
// Undraw old cell
foreach (CSGPolygon 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);
}
if (mouseButton.ButtonIndex == (int)ButtonList.Left && mouseButton.Pressed)
{
// Move player to selected hex
var path = HexGrid.GetAStarPath(new HexCell(), cell.Value);
//// Draw ring?
//var cells = HexGrid.GetSpiral(cell.Value, 3);
// Undraw old cells
DrawGrid(GridRadius);
foreach (var vec2 in path)
{
var thisCell = new HexCell(vec2);
foreach (CSGPolygon polygon in GetTree().GetNodesInGroup(thisCell.CubeCoordinates.ToString()))
polygon.QueueFree();
var colour = new Color(255, 0, 0);
// Add new cell
HexGrid.SetCell(thisCell);
DrawCell(thisCell, colour);
}
}
}
}
}
}
private void DrawGrid(int radius)
{
foreach (CSGPolygon polygon in GetTree().GetNodesInGroup("cells"))
polygon.QueueFree();
if (HexGrid.Mode == HexMode.POINTY)
_highlightedCell.RotationDegrees = new Vector3(90, 30, 0);
else
_highlightedCell.RotationDegrees = new Vector3(90, 0, 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 position = HexGrid.GetPixelAtCell(cell);
var material = new SpatialMaterial();
material.AlbedoColor = colour;
var node = _cellScene.Instance<CSGPolygon>();
node.Translation = new Vector3(position.x, 0, position.y);
node.Material = material;
node.Scale = new Vector3(0.009f, 0.009f, 0.009f);
node.AddToGroup("cells");
node.AddToGroup(cell.CubeCoordinates.ToString());
if (HexGrid.Mode == HexMode.POINTY)
node.RotationDegrees = new Vector3(90, 30, 0);
else
node.RotationDegrees = new Vector3(90, 0, 0);
AddChild(node);
}
private void OnModeToggled(bool toggle)
{
HexGrid.Mode = toggle ? HexMode.FLAT : HexMode.POINTY;
_modeButton.Text = HexGrid.Mode.ToString();
DrawGrid(GridRadius);
}
}