Added ability to delete nodes from editor, added complete node load and save for the DSL
This commit is contained in:
@@ -4,11 +4,17 @@ using System.Collections.Generic;
|
||||
|
||||
public partial class CodingWindow : PanelContainer
|
||||
{
|
||||
//General
|
||||
Robot robot;
|
||||
//Scripting
|
||||
[Export] VBoxContainer codeBlocks;
|
||||
[Export] VBoxContainer editorWindow;
|
||||
public Dictionary<ProgramNode, PackedScene> DSLNodes;
|
||||
Robot robot;
|
||||
|
||||
[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()
|
||||
@@ -20,10 +26,46 @@ public partial class CodingWindow : PanelContainer
|
||||
// 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();
|
||||
}
|
||||
|
||||
//Move, Harvest, Craft
|
||||
public void GenerateCodingBlocks()
|
||||
{
|
||||
NodeDisplay nodeDisplay;
|
||||
@@ -39,6 +81,10 @@ public partial class CodingWindow : PanelContainer
|
||||
editorDisplay.node = node;
|
||||
editorWindow.AddChild(editorDisplay);
|
||||
editorDisplay.ShowEditorDisplay();
|
||||
editorDisplay.OnDeleteNode += () =>
|
||||
{
|
||||
editorWindow.RemoveChild(editorDisplay);
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -50,6 +96,7 @@ public partial class CodingWindow : PanelContainer
|
||||
editorWindow.RemoveChild(node);
|
||||
node.QueueFree();
|
||||
}
|
||||
scriptName.Text = "";
|
||||
}
|
||||
|
||||
public void CompileProgram()
|
||||
@@ -62,15 +109,52 @@ public partial class CodingWindow : PanelContainer
|
||||
nodes.Add(editorWindow.GetChild<NodeDisplay>(i).node.Duplicate());
|
||||
if (i != 0)
|
||||
{
|
||||
nodes[i-1].nextNode = nodes[i];
|
||||
nodes[i - 1].nextNode = nodes[i];
|
||||
}
|
||||
}
|
||||
|
||||
if(nodes.Count > 0) robot.SetupExecution(nodes);
|
||||
if (nodes.Count > 0) robot.SetupExecution(nodes);
|
||||
}
|
||||
|
||||
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,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Godot;
|
||||
|
||||
public partial class NodeDisplay : PanelContainer
|
||||
@@ -7,6 +8,9 @@ public partial class NodeDisplay : PanelContainer
|
||||
[Export] public Button listDisplay;
|
||||
public ProgramNode node;
|
||||
|
||||
[Signal]
|
||||
public delegate void OnDeleteNodeEventHandler();
|
||||
|
||||
public void SetNode(ProgramNode node)
|
||||
{
|
||||
this.node = node;
|
||||
@@ -19,7 +23,7 @@ public partial class NodeDisplay : PanelContainer
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void ShowListDisplay()
|
||||
@@ -33,4 +37,94 @@ public partial class NodeDisplay : PanelContainer
|
||||
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,75 +1,92 @@
|
||||
using System.Linq;
|
||||
using Godot;
|
||||
|
||||
public class CraftNode : ProgramNode
|
||||
{
|
||||
Item selectedItem;
|
||||
int amount;
|
||||
public CraftNode()
|
||||
{
|
||||
DisplayText = "Craft";
|
||||
}
|
||||
public override NodeResult Execute(Robot robot, double delta)
|
||||
{
|
||||
if (selectedItem == null)
|
||||
{
|
||||
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";
|
||||
return NodeResult.FAILURE;
|
||||
}
|
||||
public Item selectedItem;
|
||||
public int amount;
|
||||
public CraftNode()
|
||||
{
|
||||
DisplayText = "Craft";
|
||||
}
|
||||
public override NodeResult Execute(Robot robot, double delta)
|
||||
{
|
||||
if (selectedItem == null)
|
||||
{
|
||||
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";
|
||||
return NodeResult.FAILURE;
|
||||
}
|
||||
|
||||
switch (selectedItem.Craft(amount, delta))
|
||||
{
|
||||
case CraftingResult.FAILED:
|
||||
lastExecutionMessage = "Not enough space to add item to inventory";
|
||||
return NodeResult.FAILURE;
|
||||
case CraftingResult.FINISHED:
|
||||
return NodeResult.SUCCESS;
|
||||
}
|
||||
|
||||
return NodeResult.RUNNING;
|
||||
}
|
||||
switch (selectedItem.Craft(amount, delta))
|
||||
{
|
||||
case CraftingResult.FAILED:
|
||||
lastExecutionMessage = "Not enough space to add item to inventory";
|
||||
return NodeResult.FAILURE;
|
||||
case CraftingResult.FINISHED:
|
||||
return NodeResult.SUCCESS;
|
||||
}
|
||||
|
||||
public override void ReadParameters(NodeDisplay display)
|
||||
{
|
||||
HBoxContainer valueContainer = display.GetNode<HBoxContainer>("./EditorDisplay/HBoxContainer/");
|
||||
OptionButton items = valueContainer.GetNode<OptionButton>("./Item");
|
||||
string readableItem = items.GetItemText(items.GetSelectedId());
|
||||
if (GameData.availableItems.ContainsKey(ItemData.GetIndex(readableItem)))
|
||||
{
|
||||
selectedItem = new Item { data = GameData.availableItems[ItemData.GetIndex(readableItem)]};
|
||||
}
|
||||
amount = (int)valueContainer.GetNode<SpinBox>("./Amount").Value;
|
||||
}
|
||||
return NodeResult.RUNNING;
|
||||
}
|
||||
|
||||
public override ProgramNode Duplicate()
|
||||
{
|
||||
CraftNode duplicate = new CraftNode()
|
||||
{
|
||||
selectedItem = selectedItem,
|
||||
amount = amount
|
||||
};
|
||||
return duplicate;
|
||||
}
|
||||
public override void ReadParameters(NodeDisplay display)
|
||||
{
|
||||
HBoxContainer valueContainer = display.GetNode<HBoxContainer>("./EditorDisplay/HBoxContainer/");
|
||||
OptionButton items = valueContainer.GetNode<OptionButton>("./Item");
|
||||
string readableItem = items.GetItemText(items.GetSelectedId());
|
||||
if (GameData.availableItems.ContainsKey(ItemData.GetIndex(readableItem)))
|
||||
{
|
||||
selectedItem = new Item { data = GameData.availableItems[ItemData.GetIndex(readableItem)] };
|
||||
}
|
||||
amount = (int)valueContainer.GetNode<SpinBox>("./Amount").Value;
|
||||
}
|
||||
|
||||
public override void Setup(NodeDisplay display)
|
||||
{
|
||||
OptionButton options = display.GetNode<OptionButton>("./EditorDisplay/HBoxContainer/Item");
|
||||
options.AddItem("Select item...");
|
||||
foreach (ItemData item in GameData.availableItems.Values)
|
||||
{
|
||||
if (item.Inputs.Count > 0)
|
||||
{
|
||||
options.AddItem(item.GetReadableName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public override ProgramNode Duplicate()
|
||||
{
|
||||
CraftNode duplicate = new CraftNode()
|
||||
{
|
||||
selectedItem = selectedItem,
|
||||
amount = amount
|
||||
};
|
||||
return duplicate;
|
||||
}
|
||||
|
||||
public override void Setup(NodeDisplay display)
|
||||
{
|
||||
OptionButton options = display.GetNode<OptionButton>("./EditorDisplay/HBoxContainer/Item");
|
||||
options.AddItem("Select item...");
|
||||
foreach (ItemData item in GameData.availableItems.Values)
|
||||
{
|
||||
if (item.Inputs.Count > 0)
|
||||
{
|
||||
options.AddItem(item.GetReadableName());
|
||||
}
|
||||
}
|
||||
if (selectedItem != null)
|
||||
{
|
||||
for (int i = 0; i < options.ItemCount; i++)
|
||||
{
|
||||
if (ItemData.GetIndex(options.GetItemText(i)) == selectedItem.data.Id)
|
||||
{
|
||||
options.Select(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override string Save()
|
||||
{
|
||||
return $"Name: {DisplayText}, Item: {(selectedItem == null ? "Empty" : selectedItem.data.Id)}, Amount: {amount}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,4 +92,9 @@ public class ExploreNode : ProgramNode
|
||||
{
|
||||
//Currently does nothing
|
||||
}
|
||||
|
||||
public override string Save()
|
||||
{
|
||||
return $"Name: {DisplayText}";
|
||||
}
|
||||
}
|
||||
@@ -49,4 +49,9 @@ public class HarvestNode : ProgramNode
|
||||
{
|
||||
//Currently does nothing
|
||||
}
|
||||
|
||||
public override string Save()
|
||||
{
|
||||
return $"Name: {DisplayText}";
|
||||
}
|
||||
}
|
||||
@@ -77,4 +77,9 @@ public class MoveNode : ProgramNode
|
||||
{
|
||||
//Currently does nothing
|
||||
}
|
||||
|
||||
public override string Save()
|
||||
{
|
||||
return $"Name: {DisplayText}, Position: ({targetPosition.X}|{targetPosition.Y}|{targetPosition.Z})";
|
||||
}
|
||||
}
|
||||
@@ -10,4 +10,5 @@ public abstract class ProgramNode
|
||||
public abstract NodeResult Execute(Robot robot, double delta);
|
||||
public abstract void ReadParameters(NodeDisplay display);
|
||||
public abstract ProgramNode Duplicate();
|
||||
public abstract string Save();
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using System.Collections.Generic;
|
||||
using Godot;
|
||||
|
||||
public class FileHandler()
|
||||
{
|
||||
public static void CreateScriptDirectory()
|
||||
{
|
||||
DirAccess.MakeDirRecursiveAbsolute("user://scripts");
|
||||
}
|
||||
|
||||
public static void SaveProgram(string filename, string content)
|
||||
{
|
||||
CreateScriptDirectory();
|
||||
string path = $"user://scripts/{filename}.dsl";
|
||||
|
||||
FileAccess file = FileAccess.Open(path, FileAccess.ModeFlags.Write);
|
||||
file.StoreString(content);
|
||||
}
|
||||
|
||||
public static List<string> LoadProgramNames()
|
||||
{
|
||||
CreateScriptDirectory();
|
||||
List<string> programs = new List<string>();
|
||||
|
||||
DirAccess dir = DirAccess.Open("user://scripts");
|
||||
if (dir == null)
|
||||
return programs;
|
||||
|
||||
dir.ListDirBegin();
|
||||
while (true)
|
||||
{
|
||||
string fileName = dir.GetNext();
|
||||
if (fileName == "")
|
||||
break;
|
||||
|
||||
if (!dir.CurrentIsDir() && fileName.EndsWith(".dsl"))
|
||||
{
|
||||
programs.Add(fileName.Replace(".dsl", ""));
|
||||
}
|
||||
}
|
||||
dir.ListDirEnd();
|
||||
|
||||
return programs;
|
||||
}
|
||||
|
||||
public static string LoadProgram(string name)
|
||||
{
|
||||
CreateScriptDirectory();
|
||||
string path = $"user://scripts/{name}.dsl";
|
||||
|
||||
if (!FileAccess.FileExists(path))
|
||||
return "";
|
||||
|
||||
FileAccess file = FileAccess.Open(path, FileAccess.ModeFlags.Read);
|
||||
return file.GetAsText();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://y6p1ybasi0c2
|
||||
@@ -18,7 +18,7 @@ public partial class GameData
|
||||
public static float robotSpeed = 20f;
|
||||
public static float tileWidth = 6;
|
||||
public static float tileHeight = 4;
|
||||
public static Dictionary<string, ItemData> availableItems = ResourceLoader.LoadItems();
|
||||
public static SortedDictionary<string, ItemData> availableItems = ResourceLoader.LoadItems();
|
||||
|
||||
//--- PLAYER ADJUSTABLE VALUES ---
|
||||
//Color used in primary objects (e.g. Robots)
|
||||
|
||||
@@ -85,13 +85,13 @@ public partial class ResourceLoader
|
||||
return symbols;
|
||||
}
|
||||
|
||||
public static Dictionary<string, ItemData> LoadItems()
|
||||
public static SortedDictionary<string, ItemData> LoadItems()
|
||||
{
|
||||
|
||||
FileAccess file = FileAccess.Open("res://Assets/Recipes.json", FileAccess.ModeFlags.Read);
|
||||
string json = file.GetAsText();
|
||||
|
||||
Dictionary<string, ItemData> result = new();
|
||||
SortedDictionary<string, ItemData> result = new();
|
||||
|
||||
List<ItemData> items = JsonSerializer.Deserialize<List<ItemData>>(json);
|
||||
foreach (ItemData item in items)
|
||||
|
||||
@@ -26,6 +26,7 @@ public partial class UIHandler : Control
|
||||
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
DisplayStats();
|
||||
robotList.OnRobotJumpTo += (robot) =>
|
||||
{
|
||||
if(receivedRobotJumpSignal) return;
|
||||
@@ -35,11 +36,17 @@ public partial class UIHandler : Control
|
||||
OpenUIElement(codingWindow);
|
||||
};
|
||||
|
||||
//Enable user to write in input fields
|
||||
Control focused = GetViewport().GuiGetFocusOwner();
|
||||
|
||||
if (focused is LineEdit || focused is TextEdit)
|
||||
return;
|
||||
|
||||
if (Input.IsActionJustPressed("map")) HandleMapButton();
|
||||
if (Input.IsActionJustPressed("menu")) HandleMenuButton();
|
||||
if (Input.IsActionJustPressed("robot_list")) HandleRobotListButton();
|
||||
if (Input.IsActionJustPressed("inventory")) HandleInventoryButton();
|
||||
DisplayStats();
|
||||
|
||||
}
|
||||
|
||||
public void HandleMenuButton()
|
||||
|
||||
Reference in New Issue
Block a user