Added new nodes to DSL for a wider variety of commands and scripts.
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
using Godot;
|
||||
|
||||
public class WhileNode : ProgramNode
|
||||
{
|
||||
public Item selectedItem;
|
||||
public int amount;
|
||||
public string comparator;
|
||||
public WhileNode()
|
||||
{
|
||||
DisplayText = "While";
|
||||
}
|
||||
public override NodeResult Execute(Robot robot, double delta)
|
||||
{
|
||||
|
||||
if (selectedItem == null)
|
||||
{
|
||||
lastExecutionMessage = "No Item selected";
|
||||
return NodeResult.FAILURE;
|
||||
}
|
||||
|
||||
bool isConditionFulfilled = DetermineCondition();
|
||||
return isConditionFulfilled? NodeResult.SUCCESS : NodeResult.CONDITIONFALSE;
|
||||
}
|
||||
|
||||
private bool DetermineCondition()
|
||||
{
|
||||
int inventoryAmount = GameData.inventory.GetItemAmount(selectedItem.data.Id);
|
||||
switch (comparator)
|
||||
{
|
||||
case "is bigger than":
|
||||
return inventoryAmount > amount;
|
||||
case "is less than":
|
||||
return inventoryAmount < amount;
|
||||
case "is not":
|
||||
return inventoryAmount != amount;
|
||||
case "is less than or equal to":
|
||||
return inventoryAmount <= amount;
|
||||
case "is bigger than or equal to":
|
||||
return inventoryAmount >= amount;
|
||||
default:
|
||||
return inventoryAmount == amount;
|
||||
}
|
||||
}
|
||||
|
||||
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()).Split(":")[0];
|
||||
if (GameData.availableItems.ContainsKey(ItemData.GetIndex(readableItem)))
|
||||
{
|
||||
selectedItem = new Item { data = GameData.availableItems[ItemData.GetIndex(readableItem)] };
|
||||
}
|
||||
amount = (int)valueContainer.GetNode<SpinBox>("./Amount").Value;
|
||||
|
||||
OptionButton comparators = valueContainer.GetNode<OptionButton>("./Comparator");
|
||||
comparator = comparators.GetItemText(comparators.GetSelectedId());
|
||||
}
|
||||
|
||||
public override ProgramNode Duplicate()
|
||||
{
|
||||
WhileNode duplicate = new WhileNode()
|
||||
{
|
||||
selectedItem = selectedItem,
|
||||
amount = amount,
|
||||
comparator = comparator
|
||||
};
|
||||
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 (GameData.availableResearch[item.Research].state != ResearchState.RESEARCHED) continue;
|
||||
options.AddItem(item.GetReadableName());
|
||||
}
|
||||
|
||||
if (selectedItem != null)
|
||||
{
|
||||
for (int i = 0; i < options.ItemCount; i++)
|
||||
{
|
||||
if (ItemData.GetIndex(options.GetItemText(i).Split(":")[0]) == selectedItem.data.Id)
|
||||
{
|
||||
options.Select(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
OptionButton comparators = display.GetNode<OptionButton>("./EditorDisplay/HBoxContainer/Comparator");
|
||||
|
||||
if (comparator != null)
|
||||
{
|
||||
for (int i = 0; i < comparators.ItemCount; i++)
|
||||
{
|
||||
if (comparators.GetItemText(i) == comparator)
|
||||
{
|
||||
comparators.Select(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override string Save()
|
||||
{
|
||||
return $"Name: {DisplayText}, Item: {(selectedItem == null ? "Empty" : selectedItem.data.Id)}, Comparator: {comparator}, Amount: {amount}";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user