-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathUITechTreeResearch.cs
363 lines (320 loc) · 10.7 KB
/
UITechTreeResearch.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
using Godot;
using System;
using System.Linq;
using System.Collections.Generic;
namespace Client.UI
{
// The word 'Node' is used a lot here to refer to 'Tech Research Nodes' placed in the tech tree, please do not confuse this with Godots nodes.
public partial class UITechTreeResearch
{
// Research tech tree node prefab
private static PackedScene Research = ResourceLoader.Load<PackedScene>("res://Scenes/Prefabs/Research.tscn");
// Tech tree data
public static Dictionary<ResearchType, Research> ResearchData = new Dictionary<ResearchType, Research>(){
{ ResearchType.A, new Research {
Unlocks = new ResearchType[] {
ResearchType.B,
ResearchType.I,
ResearchType.J,
ResearchType.L,
ResearchType.M
}
}},
{ ResearchType.B, new Research {
Unlocks = new ResearchType[] {
ResearchType.C,
ResearchType.W,
ResearchType.D,
ResearchType.E,
ResearchType.F
}
}},
{ ResearchType.C, new Research {
Unlocks = new ResearchType[] {
ResearchType.U
}
}},
{ ResearchType.D, new Research {
Unlocks = new ResearchType[] {
ResearchType.G,
ResearchType.H,
ResearchType.P,
ResearchType.Q
}
}},
{ ResearchType.E, new Research {
Unlocks = new ResearchType[] {
ResearchType.R,
ResearchType.S
}
}},
{ ResearchType.F, new Research {
Unlocks = new ResearchType[] {
ResearchType.T
}
}},
{ ResearchType.G, new Research {
Unlocks = new ResearchType[] {
ResearchType.AA
}
}},
{ ResearchType.H, new Research {
}},
{ ResearchType.I, new Research {
Unlocks = new ResearchType[] {
ResearchType.V
}
}},
{ ResearchType.J, new Research {
}},
{ ResearchType.K, new Research {
}},
{ ResearchType.L, new Research {
}},
{ ResearchType.M, new Research {
}},
{ ResearchType.N, new Research {
}},
{ ResearchType.O, new Research {
}},
{ ResearchType.P, new Research {
}},
{ ResearchType.Q, new Research {
}},
{ ResearchType.R, new Research {
}},
{ ResearchType.S, new Research {
}},
{ ResearchType.T, new Research {
}},
{ ResearchType.U, new Research {
}},
{ ResearchType.V, new Research {
}},
{ ResearchType.W, new Research {
Unlocks = new ResearchType[] {
ResearchType.X
}
}},
{ ResearchType.X, new Research {
Unlocks = new ResearchType[] {
ResearchType.Y,
ResearchType.Z
}
}},
{ ResearchType.Y, new Research {
}},
{ ResearchType.Z, new Research {
}},
{ ResearchType.AA, new Research {
}}
};
// There will be multiple tech trees
public static TechTree[] TechTreeData = new TechTree[] {
new TechTree {
Type = TechTreeType.Wood,
StartingResearchNodes = new ResearchType[] {
ResearchType.A
}
}
};
private const int SPACING_H = 200;
private const int SPACING_V = 100;
private static int MaxDepth { get; set; }
private static Dictionary<int, List<ResearchType>> Nodes = new Dictionary<int, List<ResearchType>>();
public static void Init()
{
// Calculate depth for all nodes and calculate MaxDepth
SetupNode(ResearchType.A, null);
// Sort nodes by column aka depth (nodes with depth = 0 are not in any column)
SortNodesByDepth();
// Position the nodes
PositionNodes();
// Enlarge the canvas if needed
ResizeCanvas();
// Offset nodes to be centered at start
OffsetNodes();
// Create the nodes
CreateNodes();
}
private static void CreateNodes()
{
// Create nodes
foreach (var pair in ResearchData)
// nodes with depth 0 have no parents or children and are therefore not connected to any nodes in the tech tree
if (pair.Value.Depth != 0)
CreateNode(pair.Key);
}
private static void ResizeCanvas()
{
// Get max XY
float xMax = 0, yMax = 0;
foreach (var pair in ResearchData)
if (pair.Value.Depth != 0)
{
var pos = ResearchData[pair.Key].Position;
if (pos.X > xMax)
xMax = pos.X;
if (pos.Y > yMax)
yMax = pos.Y;
}
// Set tech tree canvas minsize to size of tech tree
if (yMax > UITechTree.Instance.CustomMinimumSize.Y / 2)
UITechTree.Instance.CustomMinimumSize = new Vector2(yMax * 2, yMax * 2); // seems to work just fine
}
private static void OffsetNodes()
{
// Offset all nodes to center starting node at center left
var startPos = new Vector2(SPACING_H, UITechTree.Instance.CustomMinimumSize.Y / 2 - UIResearch.BoxSize.Y / 2);
var startingNode = Nodes[1][0];
var offset = startPos - ResearchData[startingNode].Position;
foreach (var pair in ResearchData)
if (pair.Value.Depth != 0)
ResearchData[pair.Key].Position += offset;
}
private static void SortNodesByDepth()
{
for (int i = 0; i <= MaxDepth; i++)
{
Nodes[i] = new List<ResearchType>();
}
foreach (var pair in ResearchData)
{
var depth = pair.Value.Depth;
Nodes[depth].Add(pair.Key);
}
}
private static void PositionNodes()
{
// Step 1
// Place nodes at MaxDepth
{
var y = 0;
foreach (var node in Nodes[MaxDepth])
{
ResearchData[node].Position = new Vector2(MaxDepth * SPACING_H, y * SPACING_V);
y++;
//CreateNode(node);
}
}
for (int j = 0; j < MaxDepth - 1; j++)
{
// Step 2a
// Determine positions of nodes at MaxDepth - 1 based off positions of nodes at MaxDepth
// Nodes that were not positioned yet
var nodesWithNoChildren = new List<ResearchType>();
var yMax = 0f;
foreach (var node in Nodes[MaxDepth - 1 - j])
{
var data = ResearchData[node];
var yPos = 0f;
if (data.Unlocks == null)
{
// No children
nodesWithNoChildren.Add(node);
}
else
{
// Find weighted middle y pos based off children
for (int i = 0; i < data.Unlocks.Length; i++)
{
yPos += ResearchData[data.Unlocks[i]].Position.Y;
}
yPos /= data.Unlocks.Length;
data.Position = new Vector2((MaxDepth - 1) * SPACING_H - j * SPACING_H, yPos);
// Determine largest Y pos
if (yPos > yMax)
yMax = yPos;
}
}
// Step 2b
for (int i = 0; i < nodesWithNoChildren.Count; i++)
{
var data = ResearchData[nodesWithNoChildren[i]];
data.Position = new Vector2((MaxDepth - 1) * SPACING_H - j * SPACING_H, yMax + (i + 1) * SPACING_V);
}
}
}
public static void CreateNode(ResearchType type)
{
var researchInstance = Research.Instantiate<UIResearch>();
var name = Enum.GetName(typeof(ResearchType), type);
var data = ResearchData[type];
researchInstance.Position = data.Position;
researchInstance.Init(name);
UITechTree.Instance.AddChild(researchInstance);
}
private static void SetupNode(ResearchType type, ResearchType? parent, int depth = 1)
{
var data = ResearchData[type];
data.Depth = depth;
if (parent != null)
data.Requirements.Add(parent);
var unlocks = data.Unlocks;
if (unlocks == null)
{
if (depth > MaxDepth)
MaxDepth = depth;
return;
}
for (int i = 0; i < unlocks.Length; i++)
{
SetupNode(unlocks[i], type, depth + 1);
}
}
}
public struct TechTree
{
public TechTreeType Type { get; set; }
public ResearchType[] StartingResearchNodes { get; set; }
}
public partial class Research
{
public Vector2 Position { get; set; }
public Vector2 CenterPosition => Position + UIResearch.BoxSize / 2;
public List<ResearchType?> Requirements = new List<ResearchType?>();
public ResearchType[] Unlocks { get; set; }
public int Depth { get; set; }
}
public enum TechTreeType
{
Wood
}
public enum ResearchType
{
A,
B,
C,
D,
E,
F,
G,
H,
I,
J,
K,
L,
M,
N,
O,
P,
Q,
R,
S,
T,
U,
V,
W,
X,
Y,
Z,
AA,
AB,
AC,
AD,
AE,
AF,
AG,
AH
}
}