Cleaned up project with better structure.
This commit is contained in:
@@ -1,161 +0,0 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public partial class CodingWindow : PanelContainer
|
||||
{
|
||||
//General
|
||||
Robot robot;
|
||||
//Scripting
|
||||
[Export] VBoxContainer codeBlocks;
|
||||
[Export] VBoxContainer editorWindow;
|
||||
public Dictionary<ProgramNode, PackedScene> DSLNodes;
|
||||
[Export] OptionButton availableScripts;
|
||||
[Export] LineEdit scriptName;
|
||||
|
||||
//Renaming
|
||||
[Export] LineEdit nameInput;
|
||||
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
public override void _Ready()
|
||||
{
|
||||
DSLNodes = ResourceLoader.LoadDSLNodes();
|
||||
GenerateCodingBlocks();
|
||||
}
|
||||
|
||||
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override void _Notification(int id)
|
||||
{
|
||||
if (id == NotificationVisibilityChanged)
|
||||
{
|
||||
if (Visible) LoadWindow();
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadWindow()
|
||||
{
|
||||
nameInput.Text = robot.Name;
|
||||
SetupScriptOptions();
|
||||
ClearWindow();
|
||||
}
|
||||
|
||||
private void SetupScriptOptions()
|
||||
{
|
||||
availableScripts.Clear();
|
||||
availableScripts.AddItem("Select script to load...");
|
||||
List<string> scripts = FileHandler.LoadProgramNames();
|
||||
scripts.Sort((a, b) => a.CompareTo(b));
|
||||
foreach (string script in scripts)
|
||||
{
|
||||
availableScripts.AddItem(script);
|
||||
}
|
||||
}
|
||||
|
||||
public void SaveRobotName()
|
||||
{
|
||||
robot.Name = nameInput.Text;
|
||||
}
|
||||
|
||||
public void CloseWindow()
|
||||
{
|
||||
Hide();
|
||||
}
|
||||
|
||||
public void GenerateCodingBlocks()
|
||||
{
|
||||
NodeDisplay nodeDisplay;
|
||||
foreach (ProgramNode node in DSLNodes.Keys)
|
||||
{
|
||||
nodeDisplay = DSLNodes[node].Instantiate<NodeDisplay>();
|
||||
nodeDisplay.node = node;
|
||||
codeBlocks.AddChild(nodeDisplay);
|
||||
nodeDisplay.ShowListDisplay();
|
||||
nodeDisplay.listDisplay.Pressed += () =>
|
||||
{
|
||||
NodeDisplay editorDisplay = DSLNodes[node].Instantiate<NodeDisplay>();
|
||||
editorDisplay.node = node;
|
||||
editorWindow.AddChild(editorDisplay);
|
||||
editorDisplay.ShowEditorDisplay();
|
||||
editorDisplay.OnDeleteNode += () =>
|
||||
{
|
||||
editorWindow.RemoveChild(editorDisplay);
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public void ClearWindow()
|
||||
{
|
||||
foreach (Node node in editorWindow.GetChildren())
|
||||
{
|
||||
editorWindow.RemoveChild(node);
|
||||
node.QueueFree();
|
||||
}
|
||||
scriptName.Text = "";
|
||||
}
|
||||
|
||||
public void CompileProgram()
|
||||
{
|
||||
List<ProgramNode> nodes = new List<ProgramNode>();
|
||||
|
||||
for (int i = 0; i < editorWindow.GetChildCount(); i++)
|
||||
{
|
||||
editorWindow.GetChild<NodeDisplay>(i).node.ReadParameters(editorWindow.GetChild<NodeDisplay>(i));
|
||||
nodes.Add(editorWindow.GetChild<NodeDisplay>(i).node.Duplicate());
|
||||
if (i != 0)
|
||||
{
|
||||
nodes[i - 1].nextNode = nodes[i];
|
||||
}
|
||||
}
|
||||
|
||||
if (nodes.Count > 0) robot.SetupExecution(nodes);
|
||||
robot.currentProgram = scriptName.Text.Length <= 0 ? $"Script{availableScripts.ItemCount}" : scriptName.Text;
|
||||
}
|
||||
|
||||
public void SetRobot(Robot robot)
|
||||
{
|
||||
this.robot = robot;
|
||||
}
|
||||
|
||||
public void LoadProgram(int index)
|
||||
{
|
||||
ClearWindow();
|
||||
string scriptContent = FileHandler.LoadProgram(availableScripts.GetItemText(index));
|
||||
string[] nodes = scriptContent.Split(";");
|
||||
foreach (string node in nodes)
|
||||
{
|
||||
NodeDisplay nodeDisplay = NodeDisplay.Load(node, DSLNodes);
|
||||
if (nodeDisplay != null)
|
||||
{
|
||||
editorWindow.AddChild(nodeDisplay);
|
||||
nodeDisplay.ShowEditorDisplay();
|
||||
nodeDisplay.OnDeleteNode += () =>
|
||||
{
|
||||
editorWindow.RemoveChild(nodeDisplay);
|
||||
};
|
||||
}
|
||||
}
|
||||
scriptName.Text = availableScripts.GetItemText(index);
|
||||
availableScripts.Select(0);
|
||||
}
|
||||
|
||||
public void SaveProgram()
|
||||
{
|
||||
string result = "";
|
||||
for (int i = 0; i < editorWindow.GetChildCount(); i++)
|
||||
{
|
||||
editorWindow.GetChild<NodeDisplay>(i).node.ReadParameters(editorWindow.GetChild<NodeDisplay>(i));
|
||||
result += editorWindow.GetChild<NodeDisplay>(i).node.Save();
|
||||
result += ";\r\n";
|
||||
}
|
||||
if (result.Length <= 0) return;
|
||||
string filename = scriptName.Text.Length <= 0 ? $"Script{availableScripts.ItemCount}" : scriptName.Text;
|
||||
FileHandler.SaveProgram(filename, result);
|
||||
SetupScriptOptions();
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
uid://bsd6n6b06a4pe
|
||||
@@ -1,130 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Godot;
|
||||
|
||||
public partial class NodeDisplay : PanelContainer
|
||||
{
|
||||
[Export] PanelContainer editorDisplay;
|
||||
[Export] public Button listDisplay;
|
||||
public ProgramNode node;
|
||||
|
||||
[Signal]
|
||||
public delegate void OnDeleteNodeEventHandler();
|
||||
|
||||
public void SetNode(ProgramNode node)
|
||||
{
|
||||
this.node = node;
|
||||
}
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
node.Setup(this);
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void ShowListDisplay()
|
||||
{
|
||||
editorDisplay.Visible = false;
|
||||
listDisplay.Visible = true;
|
||||
}
|
||||
|
||||
public void ShowEditorDisplay()
|
||||
{
|
||||
editorDisplay.Visible = true;
|
||||
listDisplay.Visible = false;
|
||||
}
|
||||
|
||||
public void DeleteNodePressed()
|
||||
{
|
||||
EmitSignal(SignalName.OnDeleteNode);
|
||||
}
|
||||
|
||||
public static NodeDisplay Load(string content, Dictionary<ProgramNode, PackedScene> DSLNodes)
|
||||
{
|
||||
NodeDisplay result = null;
|
||||
ProgramNode program;
|
||||
string nodeName;
|
||||
string nodeSanitized;
|
||||
PackedScene prefab = null;
|
||||
nodeSanitized = content.Replace("\r\n", "");
|
||||
nodeName = nodeSanitized.Split(",")[0].Replace("Name: ", "").ToLower();
|
||||
foreach (ProgramNode programNode in DSLNodes.Keys)
|
||||
{
|
||||
if (programNode.DisplayText.ToLower() == nodeName)
|
||||
{
|
||||
prefab = DSLNodes[programNode];
|
||||
break;
|
||||
}
|
||||
}
|
||||
switch (nodeName)
|
||||
{
|
||||
case "move":
|
||||
program = new MoveNode();
|
||||
result = prefab.Instantiate<NodeDisplay>();
|
||||
result.node = program;
|
||||
result.LoadMove(nodeSanitized);
|
||||
break;
|
||||
case "harvest":
|
||||
program = new HarvestNode();
|
||||
result = prefab.Instantiate<NodeDisplay>();
|
||||
result.node = program;
|
||||
result.LoadHarvest(nodeSanitized);
|
||||
break;
|
||||
case "explore":
|
||||
program = new ExploreNode();
|
||||
result = prefab.Instantiate<NodeDisplay>();
|
||||
result.node = program;
|
||||
result.LoadExplore(nodeSanitized);
|
||||
break;
|
||||
case "craft":
|
||||
program = new CraftNode();
|
||||
result = prefab.Instantiate<NodeDisplay>();
|
||||
result.node = program;
|
||||
result.LoadCraft(nodeSanitized);
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void LoadHarvest(string content)
|
||||
{
|
||||
//Currently does nothing
|
||||
}
|
||||
|
||||
public void LoadMove(string content)
|
||||
{
|
||||
HBoxContainer valueContainer = GetNode<HBoxContainer>("./EditorDisplay/HBoxContainer/");
|
||||
string[] parts = content.Split(",");
|
||||
string positionValues = parts[1].Replace("Position: ", "").Replace("(", "").Replace(")", "");
|
||||
int posX = int.Parse(positionValues.Split("|")[0]);
|
||||
int posY = int.Parse(positionValues.Split("|")[1]);
|
||||
int posZ = int.Parse(positionValues.Split("|")[2]);
|
||||
valueContainer.GetNode<SpinBox>("./CoordinateX").Value = posX;
|
||||
valueContainer.GetNode<SpinBox>("./CoordinateY").Value = posY;
|
||||
valueContainer.GetNode<SpinBox>("./CoordinateZ").Value = posZ;
|
||||
|
||||
(node as MoveNode).targetPosition = new Vector3I(posX, posY, posZ);
|
||||
}
|
||||
|
||||
public void LoadExplore(string content)
|
||||
{
|
||||
//Currently does nothing
|
||||
}
|
||||
|
||||
public void LoadCraft(string content)
|
||||
{
|
||||
HBoxContainer valueContainer = GetNode<HBoxContainer>("./EditorDisplay/HBoxContainer/");
|
||||
string[] parts = content.Split(",");
|
||||
string itemString = parts[1].Replace("Item: ", "").Replace(" ", "");
|
||||
if (itemString.ToLower() != "empty")
|
||||
{
|
||||
(node as CraftNode).selectedItem = new Item { data = GameData.availableItems[itemString] };
|
||||
}
|
||||
string amountString = parts[2].Replace("Amount: ", "");
|
||||
valueContainer.GetNode<SpinBox>("./Amount").Value = int.Parse(amountString);
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
uid://b6kxwmuhmruul
|
||||
@@ -1,4 +1,3 @@
|
||||
using System.Linq;
|
||||
using Godot;
|
||||
|
||||
public class CraftNode : ProgramNode
|
||||
@@ -16,11 +15,13 @@ public class CraftNode : ProgramNode
|
||||
lastExecutionMessage = "No Item selected";
|
||||
return NodeResult.FAILURE;
|
||||
}
|
||||
|
||||
if (amount <= 0)
|
||||
{
|
||||
lastExecutionMessage = "Amount has to be atleast 1";
|
||||
return NodeResult.FAILURE;
|
||||
}
|
||||
|
||||
if (!GameData.inventory.CanCraft(selectedItem.data.Inputs, amount))
|
||||
{
|
||||
lastExecutionMessage = "Not enough items to craft this";
|
||||
@@ -67,12 +68,13 @@ public class CraftNode : ProgramNode
|
||||
options.AddItem("Select item...");
|
||||
foreach (ItemData item in GameData.availableItems.Values)
|
||||
{
|
||||
if(GameData.availableResearch[item.Research].state != ResearchState.RESEARCHED) continue;
|
||||
if (GameData.availableResearch[item.Research].state != ResearchState.RESEARCHED) continue;
|
||||
if (item.Inputs.Count > 0)
|
||||
{
|
||||
options.AddItem(item.GetCraftingDisplay());
|
||||
}
|
||||
}
|
||||
|
||||
if (selectedItem != null)
|
||||
{
|
||||
for (int i = 0; i < options.ItemCount; i++)
|
||||
|
||||
@@ -16,9 +16,10 @@ public class ExploreNode : ProgramNode
|
||||
if (pathPoints == null)
|
||||
{
|
||||
int safetyCounter = 0;
|
||||
int layerRange = Math.Max(GameData.lowestLayer, 1);
|
||||
while (true)
|
||||
{
|
||||
targetPosition = new Vector3I(GameData.rand.Next(GameData.layerSize), GameData.rand.Next(GameData.lowestLayer), GameData.rand.Next(GameData.layerSize));
|
||||
targetPosition = new Vector3I(GameData.rand.Next(GameData.layerSize), GameData.rand.Next(layerRange), GameData.rand.Next(GameData.layerSize));
|
||||
if (!GameData.map[targetPosition.Y].tiles[targetPosition.X, targetPosition.Z].wasVisited) break;
|
||||
safetyCounter++;
|
||||
if (safetyCounter > Math.Pow(GameData.layerSize, 2) * 2)
|
||||
@@ -29,7 +30,7 @@ public class ExploreNode : ProgramNode
|
||||
}
|
||||
}
|
||||
|
||||
pathPoints ??= [.. Pathfinding.GetPath(Pathfinding.GetClosestStartPoint(robot.Position), targetPosition)];
|
||||
pathPoints ??= new List<Vector3>(Pathfinding.GetPath(Pathfinding.GetClosestStartPoint(robot.Position), targetPosition));
|
||||
|
||||
if (pathPoints.Count <= 0)
|
||||
{
|
||||
@@ -50,6 +51,7 @@ public class ExploreNode : ProgramNode
|
||||
{
|
||||
tile.VisitTile();
|
||||
}
|
||||
|
||||
pathPoints.Remove(pathPoints[0]);
|
||||
if (pathPoints.Count <= 0)
|
||||
{
|
||||
@@ -84,16 +86,14 @@ public class ExploreNode : ProgramNode
|
||||
|
||||
public override void ReadParameters(NodeDisplay display)
|
||||
{
|
||||
//Currently does nothing
|
||||
}
|
||||
|
||||
public override void Setup(NodeDisplay display)
|
||||
{
|
||||
//Currently does nothing
|
||||
}
|
||||
|
||||
public override string Save()
|
||||
{
|
||||
return $"Name: {DisplayText}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,6 @@ public class HarvestNode : ProgramNode
|
||||
|
||||
public override void ReadParameters(NodeDisplay display)
|
||||
{
|
||||
//Currently does nothing
|
||||
}
|
||||
|
||||
public override ProgramNode Duplicate()
|
||||
@@ -47,11 +46,10 @@ public class HarvestNode : ProgramNode
|
||||
|
||||
public override void Setup(NodeDisplay display)
|
||||
{
|
||||
//Currently does nothing
|
||||
}
|
||||
|
||||
public override string Save()
|
||||
{
|
||||
return $"Name: {DisplayText}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Godot;
|
||||
|
||||
@@ -13,12 +12,14 @@ public class MoveNode : ProgramNode
|
||||
}
|
||||
public override NodeResult Execute(Robot robot, double delta)
|
||||
{
|
||||
pathPoints ??= [.. Pathfinding.GetPath(Pathfinding.GetClosestStartPoint(robot.Position), targetPosition)];
|
||||
pathPoints ??= new List<Vector3>(Pathfinding.GetPath(Pathfinding.GetClosestStartPoint(robot.Position), targetPosition));
|
||||
|
||||
if (pathPoints.Count <= 0)
|
||||
{
|
||||
lastExecutionMessage = "No path available";
|
||||
return NodeResult.FAILURE;
|
||||
}
|
||||
|
||||
startPosition = robot.Position;
|
||||
Vector3 target = pathPoints[0] - startPosition;
|
||||
float distance = target.Length();
|
||||
@@ -32,6 +33,7 @@ public class MoveNode : ProgramNode
|
||||
{
|
||||
tile.VisitTile();
|
||||
}
|
||||
|
||||
pathPoints.Remove(pathPoints[0]);
|
||||
if (pathPoints.Count <= 0)
|
||||
{
|
||||
@@ -74,11 +76,10 @@ public class MoveNode : ProgramNode
|
||||
|
||||
public override void Setup(NodeDisplay display)
|
||||
{
|
||||
//Currently does nothing
|
||||
}
|
||||
|
||||
public override string Save()
|
||||
{
|
||||
return $"Name: {DisplayText}, Position: ({targetPosition.X}|{targetPosition.Y}|{targetPosition.Z})";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user