diff --git a/Assets/Images/Sources/TrashSymbol.aseprite b/Assets/Images/Sources/TrashSymbol.aseprite new file mode 100644 index 0000000..5057545 Binary files /dev/null and b/Assets/Images/Sources/TrashSymbol.aseprite differ diff --git a/Assets/Images/TrashSymbol.png b/Assets/Images/TrashSymbol.png new file mode 100644 index 0000000..b3163e2 Binary files /dev/null and b/Assets/Images/TrashSymbol.png differ diff --git a/Assets/Images/TrashSymbol.png.import b/Assets/Images/TrashSymbol.png.import new file mode 100644 index 0000000..74c8606 --- /dev/null +++ b/Assets/Images/TrashSymbol.png.import @@ -0,0 +1,40 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://wq8yc0u0ee33" +path="res://.godot/imported/TrashSymbol.png-3f91ec9cd1f5cf96bc959424d5164ff1.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://Assets/Images/TrashSymbol.png" +dest_files=["res://.godot/imported/TrashSymbol.png-3f91ec9cd1f5cf96bc959424d5164ff1.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/uastc_level=0 +compress/rdo_quality_loss=0.0 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/channel_remap/red=0 +process/channel_remap/green=1 +process/channel_remap/blue=2 +process/channel_remap/alpha=3 +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 diff --git a/Prefabs/DSL/CraftNode.tscn b/Prefabs/DSL/CraftNode.tscn index 65684d5..c3b5495 100644 --- a/Prefabs/DSL/CraftNode.tscn +++ b/Prefabs/DSL/CraftNode.tscn @@ -1,6 +1,7 @@ [gd_scene format=3 uid="uid://cinn18bl736rk"] [ext_resource type="Script" uid="uid://b6kxwmuhmruul" path="res://Scripts/DSL/NodeDisplay.cs" id="1_qemp1"] +[ext_resource type="Texture2D" uid="uid://wq8yc0u0ee33" path="res://Assets/Images/TrashSymbol.png" id="2_loic7"] [node name="Craft" type="PanelContainer" unique_id=247502695 node_paths=PackedStringArray("editorDisplay", "listDisplay")] anchors_preset = 14 @@ -43,6 +44,16 @@ rounded = true alignment = 1 prefix = "x" -[node name="ListDisplay" type="Button" parent="." unique_id=200731038] +[node name="TextureButton" type="TextureButton" parent="EditorDisplay/HBoxContainer" unique_id=479938935] layout_mode = 2 +size_flags_horizontal = 3 +size_flags_stretch_ratio = 0.2 +texture_normal = ExtResource("2_loic7") + +[node name="ListDisplay" type="Button" parent="." unique_id=200731038] +visible = false +layout_mode = 2 +tooltip_text = "Crafts the amount of the selected item." text = "Craft" + +[connection signal="pressed" from="EditorDisplay/HBoxContainer/TextureButton" to="." method="DeleteNodePressed"] diff --git a/Prefabs/DSL/ExploreNode.tscn b/Prefabs/DSL/ExploreNode.tscn index 38b99c9..ba0aff6 100644 --- a/Prefabs/DSL/ExploreNode.tscn +++ b/Prefabs/DSL/ExploreNode.tscn @@ -1,6 +1,7 @@ [gd_scene format=3 uid="uid://dit4u45jegwv0"] [ext_resource type="Script" uid="uid://b6kxwmuhmruul" path="res://Scripts/DSL/NodeDisplay.cs" id="1_3kgh4"] +[ext_resource type="Texture2D" uid="uid://wq8yc0u0ee33" path="res://Assets/Images/TrashSymbol.png" id="2_6eg2n"] [node name="Explore" type="PanelContainer" unique_id=1474470717 node_paths=PackedStringArray("editorDisplay", "listDisplay")] anchors_preset = 14 @@ -24,12 +25,22 @@ alignment = 1 [node name="Flavour" type="RichTextLabel" parent="EditorDisplay/HBoxContainer" unique_id=1497013575] layout_mode = 2 +size_flags_horizontal = 3 text = "Randomly explore" fit_content = true autowrap_mode = 0 horizontal_alignment = 1 vertical_alignment = 1 +[node name="TextureButton" type="TextureButton" parent="EditorDisplay/HBoxContainer" unique_id=57412049] +layout_mode = 2 +size_flags_horizontal = 3 +size_flags_stretch_ratio = 0.2 +texture_normal = ExtResource("2_6eg2n") + [node name="ListDisplay" type="Button" parent="." unique_id=347942160] layout_mode = 2 +tooltip_text = "Randomly explores the available ruin" text = "Explore" + +[connection signal="pressed" from="EditorDisplay/HBoxContainer/TextureButton" to="." method="DeleteNodePressed"] diff --git a/Prefabs/DSL/HarvestNode.tscn b/Prefabs/DSL/HarvestNode.tscn index dbc4705..06103c7 100644 --- a/Prefabs/DSL/HarvestNode.tscn +++ b/Prefabs/DSL/HarvestNode.tscn @@ -1,6 +1,7 @@ [gd_scene format=3 uid="uid://com0ou37wj2xo"] [ext_resource type="Script" uid="uid://b6kxwmuhmruul" path="res://Scripts/DSL/NodeDisplay.cs" id="1_ve3v1"] +[ext_resource type="Texture2D" uid="uid://wq8yc0u0ee33" path="res://Assets/Images/TrashSymbol.png" id="2_u1say"] [node name="Harvest" type="PanelContainer" unique_id=1323721153 node_paths=PackedStringArray("editorDisplay", "listDisplay")] anchors_preset = 14 @@ -25,13 +26,21 @@ alignment = 1 [node name="Flavour" type="RichTextLabel" parent="EditorDisplay/HBoxContainer" unique_id=1393328640] layout_mode = 2 size_flags_horizontal = 3 -size_flags_stretch_ratio = 0.2 -text = "Harvest: " +text = "Harvest" fit_content = true autowrap_mode = 0 horizontal_alignment = 1 vertical_alignment = 1 +[node name="TextureButton" type="TextureButton" parent="EditorDisplay/HBoxContainer" unique_id=661274351] +layout_mode = 2 +size_flags_horizontal = 3 +size_flags_stretch_ratio = 0.2 +texture_normal = ExtResource("2_u1say") + [node name="ListDisplay" type="Button" parent="." unique_id=884865258] layout_mode = 2 +tooltip_text = "Harvests the resource from the tile the robot is currently standing on." text = "Harvest" + +[connection signal="pressed" from="EditorDisplay/HBoxContainer/TextureButton" to="." method="DeleteNodePressed"] diff --git a/Prefabs/DSL/MoveNode.tscn b/Prefabs/DSL/MoveNode.tscn index ca57d02..e6885a9 100644 --- a/Prefabs/DSL/MoveNode.tscn +++ b/Prefabs/DSL/MoveNode.tscn @@ -1,6 +1,7 @@ [gd_scene format=3 uid="uid://by0khq5dmxjvm"] [ext_resource type="Script" uid="uid://b6kxwmuhmruul" path="res://Scripts/DSL/NodeDisplay.cs" id="1_mexpj"] +[ext_resource type="Texture2D" uid="uid://wq8yc0u0ee33" path="res://Assets/Images/TrashSymbol.png" id="2_5ujwv"] [node name="Move" type="PanelContainer" unique_id=1474470717 node_paths=PackedStringArray("editorDisplay", "listDisplay")] anchors_preset = 14 @@ -24,6 +25,7 @@ alignment = 1 [node name="Flavour" type="RichTextLabel" parent="EditorDisplay/HBoxContainer" unique_id=1497013575] layout_mode = 2 +size_flags_horizontal = 3 text = "Move to: " fit_content = true autowrap_mode = 0 @@ -32,22 +34,34 @@ vertical_alignment = 1 [node name="CoordinateX" type="SpinBox" parent="EditorDisplay/HBoxContainer" unique_id=485214217] layout_mode = 2 +size_flags_horizontal = 3 rounded = true alignment = 1 prefix = "X" [node name="CoordinateY" type="SpinBox" parent="EditorDisplay/HBoxContainer" unique_id=724940645] layout_mode = 2 +size_flags_horizontal = 3 rounded = true alignment = 1 prefix = "Y" [node name="CoordinateZ" type="SpinBox" parent="EditorDisplay/HBoxContainer" unique_id=578943647] layout_mode = 2 +size_flags_horizontal = 3 rounded = true alignment = 1 prefix = "Z" +[node name="TextureButton" type="TextureButton" parent="EditorDisplay/HBoxContainer" unique_id=753598237] +layout_mode = 2 +size_flags_horizontal = 3 +size_flags_stretch_ratio = 0.2 +texture_normal = ExtResource("2_5ujwv") + [node name="ListDisplay" type="Button" parent="." unique_id=347942160] layout_mode = 2 +tooltip_text = "Moves the robot to the selected coordinate" text = "Move" + +[connection signal="pressed" from="EditorDisplay/HBoxContainer/TextureButton" to="." method="DeleteNodePressed"] diff --git a/Scenes/Game.tscn b/Scenes/Game.tscn index ff7abb0..5f38a53 100644 --- a/Scenes/Game.tscn +++ b/Scenes/Game.tscn @@ -140,7 +140,7 @@ vertical_alignment = 1 layout_mode = 2 size_flags_vertical = 3 -[node name="CodingWindow" type="PanelContainer" parent="CanvasLayer/UIHandler/MainUI/Content" unique_id=1576652491 node_paths=PackedStringArray("codeBlocks", "editorWindow")] +[node name="CodingWindow" type="PanelContainer" parent="CanvasLayer/UIHandler/MainUI/Content" unique_id=1576652491 node_paths=PackedStringArray("codeBlocks", "editorWindow", "availableScripts", "scriptName", "nameInput")] visible = false layout_mode = 1 anchors_preset = 11 @@ -154,6 +154,9 @@ theme_override_styles/panel = SubResource("StyleBoxFlat_7lihs") script = ExtResource("6_7lihs") codeBlocks = NodePath("VBoxContainer/Scripting/CodeBlocks/VBoxContainer") editorWindow = NodePath("VBoxContainer/Scripting/EditorWindow/CodeContainer/VBoxContainer") +availableScripts = NodePath("VBoxContainer/Scripting/EditorWindow/Load") +scriptName = NodePath("VBoxContainer/Scripting/EditorWindow/Saving/ScriptName") +nameInput = NodePath("VBoxContainer/Renaming/LineEdit") [node name="VBoxContainer" type="VBoxContainer" parent="CanvasLayer/UIHandler/MainUI/Content/CodingWindow" unique_id=582741975] layout_mode = 2 @@ -254,10 +257,9 @@ layout_mode = 2 size_flags_horizontal = 3 text = "Save" -[node name="Load" type="Button" parent="CanvasLayer/UIHandler/MainUI/Content/CodingWindow/VBoxContainer/Scripting/EditorWindow" unique_id=2064954816] +[node name="Load" type="OptionButton" parent="CanvasLayer/UIHandler/MainUI/Content/CodingWindow/VBoxContainer/Scripting/EditorWindow" unique_id=970393437] layout_mode = 2 size_flags_horizontal = 3 -text = "Load script" [node name="Close" type="Button" parent="CanvasLayer/UIHandler/MainUI/Content/CodingWindow/VBoxContainer" unique_id=200541741] layout_mode = 2 @@ -430,8 +432,12 @@ layout_mode = 2 tooltip_text = "Menu (ESC)" texture_normal = ExtResource("12_3so38") +[connection signal="pressed" from="CanvasLayer/UIHandler/MainUI/Content/CodingWindow/VBoxContainer/Renaming/Button" to="CanvasLayer/UIHandler/MainUI/Content/CodingWindow" method="SaveRobotName"] [connection signal="button_up" from="CanvasLayer/UIHandler/MainUI/Content/CodingWindow/VBoxContainer/Scripting/EditorWindow/Buttons/Clear" to="CanvasLayer/UIHandler/MainUI/Content/CodingWindow" method="ClearWindow"] [connection signal="button_up" from="CanvasLayer/UIHandler/MainUI/Content/CodingWindow/VBoxContainer/Scripting/EditorWindow/Buttons/Compile" to="CanvasLayer/UIHandler/MainUI/Content/CodingWindow" method="CompileProgram"] +[connection signal="pressed" from="CanvasLayer/UIHandler/MainUI/Content/CodingWindow/VBoxContainer/Scripting/EditorWindow/Saving/Save" to="CanvasLayer/UIHandler/MainUI/Content/CodingWindow" method="SaveProgram"] +[connection signal="item_selected" from="CanvasLayer/UIHandler/MainUI/Content/CodingWindow/VBoxContainer/Scripting/EditorWindow/Load" to="CanvasLayer/UIHandler/MainUI/Content/CodingWindow" method="LoadProgram"] +[connection signal="pressed" from="CanvasLayer/UIHandler/MainUI/Content/CodingWindow/VBoxContainer/Close" to="CanvasLayer/UIHandler/MainUI/Content/CodingWindow" method="CloseWindow"] [connection signal="pressed" from="CanvasLayer/UIHandler/MainUI/Content/Menu/VBoxContainer/Button" to="CanvasLayer/UIHandler" method="HandleMenu"] [connection signal="pressed" from="CanvasLayer/UIHandler/MainUI/Content/Menu/VBoxContainer/Button2" to="CanvasLayer/UIHandler" method="ShowOptions"] [connection signal="pressed" from="CanvasLayer/UIHandler/MainUI/Content/Menu/VBoxContainer/Button3" to="CanvasLayer/UIHandler" method="ExitGame"] diff --git a/Scripts/DSL/CodingWindow.cs b/Scripts/DSL/CodingWindow.cs index 3c2bb5e..86b5418 100644 --- a/Scripts/DSL/CodingWindow.cs +++ b/Scripts/DSL/CodingWindow.cs @@ -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 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 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(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(i).node.ReadParameters(editorWindow.GetChild(i)); + result += editorWindow.GetChild(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(); + } } diff --git a/Scripts/DSL/NodeDisplay.cs b/Scripts/DSL/NodeDisplay.cs index dc57b66..3765893 100644 --- a/Scripts/DSL/NodeDisplay.cs +++ b/Scripts/DSL/NodeDisplay.cs @@ -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 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(); + result.node = program; + result.LoadMove(nodeSanitized); + break; + case "harvest": + program = new HarvestNode(); + result = prefab.Instantiate(); + result.node = program; + result.LoadHarvest(nodeSanitized); + break; + case "explore": + program = new ExploreNode(); + result = prefab.Instantiate(); + result.node = program; + result.LoadExplore(nodeSanitized); + break; + case "craft": + program = new CraftNode(); + result = prefab.Instantiate(); + 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("./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("./CoordinateX").Value = posX; + valueContainer.GetNode("./CoordinateY").Value = posY; + valueContainer.GetNode("./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("./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("./Amount").Value = int.Parse(amountString); + } } diff --git a/Scripts/DSL/Nodes/CraftNode.cs b/Scripts/DSL/Nodes/CraftNode.cs index 7949e97..0e6e501 100644 --- a/Scripts/DSL/Nodes/CraftNode.cs +++ b/Scripts/DSL/Nodes/CraftNode.cs @@ -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("./EditorDisplay/HBoxContainer/"); - OptionButton items = valueContainer.GetNode("./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("./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("./EditorDisplay/HBoxContainer/"); + OptionButton items = valueContainer.GetNode("./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("./Amount").Value; + } - public override void Setup(NodeDisplay display) - { - OptionButton options = display.GetNode("./EditorDisplay/HBoxContainer/Item"); - options.AddItem("Select item..."); - foreach (ItemData item in GameData.availableItems.Values) - { - if (item.Inputs.Count > 0) - { - options.AddItem(item.GetReadableName()); - } - } - } -} \ No newline at end of file + public override ProgramNode Duplicate() + { + CraftNode duplicate = new CraftNode() + { + selectedItem = selectedItem, + amount = amount + }; + return duplicate; + } + + public override void Setup(NodeDisplay display) + { + OptionButton options = display.GetNode("./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}"; + } +} diff --git a/Scripts/DSL/Nodes/ExploreNode.cs b/Scripts/DSL/Nodes/ExploreNode.cs index a4d6235..2b5f1da 100644 --- a/Scripts/DSL/Nodes/ExploreNode.cs +++ b/Scripts/DSL/Nodes/ExploreNode.cs @@ -92,4 +92,9 @@ public class ExploreNode : ProgramNode { //Currently does nothing } + + public override string Save() + { + return $"Name: {DisplayText}"; + } } \ No newline at end of file diff --git a/Scripts/DSL/Nodes/HarvestNode.cs b/Scripts/DSL/Nodes/HarvestNode.cs index 76bd852..8082ced 100644 --- a/Scripts/DSL/Nodes/HarvestNode.cs +++ b/Scripts/DSL/Nodes/HarvestNode.cs @@ -49,4 +49,9 @@ public class HarvestNode : ProgramNode { //Currently does nothing } + + public override string Save() + { + return $"Name: {DisplayText}"; + } } \ No newline at end of file diff --git a/Scripts/DSL/Nodes/MoveNode.cs b/Scripts/DSL/Nodes/MoveNode.cs index 6e683d3..97ab613 100644 --- a/Scripts/DSL/Nodes/MoveNode.cs +++ b/Scripts/DSL/Nodes/MoveNode.cs @@ -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})"; + } } \ No newline at end of file diff --git a/Scripts/DSL/Nodes/ProgramNode.cs b/Scripts/DSL/Nodes/ProgramNode.cs index 5cef70b..091c1e7 100644 --- a/Scripts/DSL/Nodes/ProgramNode.cs +++ b/Scripts/DSL/Nodes/ProgramNode.cs @@ -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(); } \ No newline at end of file diff --git a/Scripts/Helpers/FileHandler.cs b/Scripts/Helpers/FileHandler.cs new file mode 100644 index 0000000..befb9ce --- /dev/null +++ b/Scripts/Helpers/FileHandler.cs @@ -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 LoadProgramNames() + { + CreateScriptDirectory(); + List programs = new List(); + + 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(); + } +} \ No newline at end of file diff --git a/Scripts/Helpers/FileHandler.cs.uid b/Scripts/Helpers/FileHandler.cs.uid new file mode 100644 index 0000000..42726cd --- /dev/null +++ b/Scripts/Helpers/FileHandler.cs.uid @@ -0,0 +1 @@ +uid://y6p1ybasi0c2 diff --git a/Scripts/Helpers/GameData.cs b/Scripts/Helpers/GameData.cs index bb7be84..dccc216 100644 --- a/Scripts/Helpers/GameData.cs +++ b/Scripts/Helpers/GameData.cs @@ -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 availableItems = ResourceLoader.LoadItems(); + public static SortedDictionary availableItems = ResourceLoader.LoadItems(); //--- PLAYER ADJUSTABLE VALUES --- //Color used in primary objects (e.g. Robots) diff --git a/Scripts/Helpers/ResourceLoader.cs b/Scripts/Helpers/ResourceLoader.cs index 4a881e1..2bed49c 100644 --- a/Scripts/Helpers/ResourceLoader.cs +++ b/Scripts/Helpers/ResourceLoader.cs @@ -85,13 +85,13 @@ public partial class ResourceLoader return symbols; } - public static Dictionary LoadItems() + public static SortedDictionary LoadItems() { FileAccess file = FileAccess.Open("res://Assets/Recipes.json", FileAccess.ModeFlags.Read); string json = file.GetAsText(); - Dictionary result = new(); + SortedDictionary result = new(); List items = JsonSerializer.Deserialize>(json); foreach (ItemData item in items) diff --git a/Scripts/Helpers/UIHandler.cs b/Scripts/Helpers/UIHandler.cs index 69c52b4..0508db1 100644 --- a/Scripts/Helpers/UIHandler.cs +++ b/Scripts/Helpers/UIHandler.cs @@ -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()