Skip to content

Commit

Permalink
v0.1.27 (dev)
Browse files Browse the repository at this point in the history
  • Loading branch information
valkyrienyanko committed Feb 4, 2022
1 parent d952a8e commit f6d6c85
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 17 deletions.
40 changes: 39 additions & 1 deletion Scripts/UI/UITechTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class UITechTree : Control
private bool Drag { get; set; }
private Vector2 ScreenStartPos = Vector2.Zero;
private Vector2 PrevCameraPos = Vector2.Zero;
private bool Draw;

public async override void _Ready()
{
Expand All @@ -25,14 +26,51 @@ public async override void _Ready()
UITechViewport.Camera2D.ResetSmoothing(); // otherwise you will see camera move to center
UITechViewport.Camera2D.Position = RectPosition + RectSize / 2;

UITechTreeResearch.Init(this);
UITechTreeResearch.Content = this;
UITechTreeResearch.Init();

Draw = true;
Update();
}

public override void _Draw()
{
if (!Draw)
return;

// UITechTree lines between nodes will be drawn here
var firstNodeInTechCategory = UITechTreeResearch.TechTreeData[0].StartingResearchNodes[0];
var firstNode = UITechTreeResearch.ResearchData[firstNodeInTechCategory];

DrawLinesForChildren(firstNodeInTechCategory);
}

private void DrawLinesForChildren(ResearchType type)
{
var researchData = UITechTreeResearch.ResearchData;

var node = researchData[type];

var children = node.Children;

if (children == null)
return;

var centerMiddle = researchData[children[0]].CenterPosition - new Vector2(100, 0);
//DrawLine(node.CenterPosition, centerMiddle); // red line

for (int i = 0; i < children.Length; i++)
{
//DrawLine(centerMiddle + new Vector2(0, i * 125), researchData[children[i]].CenterPosition); // green lines


DrawLine(node.CenterPosition, researchData[children[i]].CenterPosition);
DrawLinesForChildren(children[i]);
}
}

private void DrawLine(Vector2 from, Vector2 to) => DrawLine(from, to, Colors.White, 5, true);

public override void _PhysicsProcess(float delta)
{
var cam = UITechViewport.Camera2D;
Expand Down
65 changes: 49 additions & 16 deletions Scripts/UI/UITechTreeResearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,27 @@ public class UITechTreeResearch

private static PackedScene Research = ResourceLoader.Load<PackedScene>("res://Scenes/Prefabs/Research.tscn");

private static Dictionary<ResearchType, Research> ResearchData = new Dictionary<ResearchType, Research>(){
public static Dictionary<ResearchType, Research> ResearchData = new Dictionary<ResearchType, Research>(){
{ ResearchType.A, new Research {
Children = new ResearchType[] {
ResearchType.B,
ResearchType.C,
ResearchType.D
}
}},
{ ResearchType.B, new Research {}},
{ ResearchType.B, new Research {
Children = new ResearchType[] {
ResearchType.E,
ResearchType.F
}
}},
{ ResearchType.C, new Research {}},
{ ResearchType.D, new Research {}}
{ ResearchType.D, new Research {}},
{ ResearchType.E, new Research {}},
{ ResearchType.F, new Research {}}
};

private static TechTree[] TechTreeData = new TechTree[] {
public static TechTree[] TechTreeData = new TechTree[] {
new TechTree {
Type = TechTreeType.Wood,
StartingResearchNodes = new ResearchType[] {
Expand All @@ -32,31 +39,56 @@ public class UITechTreeResearch
}
};

public static void Init(Control content)
private static int ChildSpacingHorizontal = 200;
private static int ChildSpacingVertical = 125;
public static Control Content;
public static int ResearchNodeWidth = 100;
public static int ResearchNodeHeight = 100;

public static void Init()
{
// This is where the first research is placed on the tech tree
ResearchStartPos = new Vector2(content.RectSize.x / 2 - 50, content.RectSize.y / 2 - 50);
ResearchStartPos = new Vector2(Content.RectSize.x / 2 - ResearchNodeWidth / 2, Content.RectSize.y / 2 - ResearchNodeHeight / 2);

var firstNodeInTechCategory = TechTreeData[0].StartingResearchNodes[0];
var firstNode = ResearchData[firstNodeInTechCategory];

firstNode.Position = ResearchStartPos;
AddNode(firstNodeInTechCategory);

var firstTechType = TechTreeData[0].StartingResearchNodes[0];
ResearchData[firstTechType].Position = ResearchStartPos;
AddNode(content, firstTechType);
AddChildrenNodes(firstNodeInTechCategory);
}

public static void AddChildrenNodes(ResearchType type)
{
var children = ResearchData[type].Children; // parent children
var position = ResearchData[type].Position; // parent position

if (children == null) // if parent has no children do nothing
return;

for (int i = 0; i < ResearchData[firstTechType].Children.Length; i++)
// calculate vertical center offset
var verticalCenterOffset = ((ChildSpacingVertical * children.Length) / 2) - ChildSpacingVertical / 2;

// add each child
for (int i = 0; i < children.Length; i++)
{
var horizontalColumnSpacing = 200;
var verticalChildrenSpacing = 200;
ResearchData[ResearchData[firstTechType].Children[i]].Position = ResearchData[firstTechType].Position + new Vector2(horizontalColumnSpacing, verticalChildrenSpacing * i);
AddNode(content, ResearchData[firstTechType].Children[i]);
var childPos = new Vector2(ChildSpacingHorizontal, (ChildSpacingVertical * i) - verticalCenterOffset);

ResearchData[children[i]].Position = position + childPos;
AddNode(children[i]);

AddChildrenNodes(children[i]);
}
}

public static void AddNode(Control content, ResearchType researchType)
public static void AddNode(ResearchType researchType)
{
var researchInstance = Research.Instance<UIResearch>();
researchInstance.SetPosition(ResearchData[researchType].Position);
researchInstance.Init(Enum.GetName(typeof(ResearchType), researchType));

content.AddChild(researchInstance);
Content.AddChild(researchInstance);
}
}

Expand All @@ -70,6 +102,7 @@ public class Research
{
public Vector2 Position { get; set; }
public ResearchType[] Children { get; set; }
public Vector2 CenterPosition => Position + new Vector2(UITechTreeResearch.ResearchNodeWidth / 2, UITechTreeResearch.ResearchNodeHeight / 2);
}

public enum TechTreeType
Expand Down

0 comments on commit f6d6c85

Please sign in to comment.