Files
RuinAdventurer/Scripts/UI/Robots/RobotList.cs
T
2026-05-11 19:37:28 +02:00

105 lines
2.5 KiB
C#

using Godot;
public partial class RobotList : PanelContainer
{
[Export] VBoxContainer robotList;
[Export] OptionButton selectableRobots;
[Export] Button spawnRobot;
[Signal]
public delegate void OnRobotJumpToEventHandler(Robot robot);
[Signal]
public delegate void OnRobotFollowEventHandler(Robot robot);
private PackedScene robotDisplayPrefab;
private string spawnId = "";
public override void _Ready()
{
robotDisplayPrefab = ResourceLoader.LoadRobotDisplay();
}
public override void _Notification(int id)
{
if (id == NotificationVisibilityChanged)
{
if (Visible)
{
ReloadRobots();
ReloadSelectableRobots();
}
}
}
public void ReloadRobots()
{
foreach (Node node in robotList.GetChildren())
{
robotList.RemoveChild(node);
node.QueueFree();
}
RobotDisplay display;
foreach (Robot robotObject in GameData.robots)
{
display = robotDisplayPrefab.Instantiate<RobotDisplay>();
display.robot = robotObject;
display.listItem.Text = robotObject.Name;
display.OnRobotJumpTo += (robot) =>
{
EmitSignal(SignalName.OnRobotJumpTo, robot);
Visible = false;
};
display.OnRobotFollow += (robot) =>
{
EmitSignal(SignalName.OnRobotFollow, robot);
Visible = false;
};
robotList.AddChild(display);
}
}
public void ReloadSelectableRobots()
{
selectableRobots.Clear();
if(GameData.robots.Count >= GameData.maxRobotCount)
{
selectableRobots.AddItem("You can't have more robots currently!");
selectableRobots.Disabled = true;
spawnRobot.Disabled = true;
return;
}
selectableRobots.Disabled = false;
spawnRobot.Disabled = false;
selectableRobots.AddItem("Select robot...");
foreach (Item item in GameData.inventory.items)
{
if (GameData.robotStats.RobotTypes.ContainsKey(item.data.Id))
{
selectableRobots.AddItem(item.data.GetReadableName());
}
}
}
public void SpawnRobot()
{
if(spawnId.Length <= 0) return;
GameData.inventory.RemoveItem(spawnId, 1);
Robot robot = ResourceLoader.LoadRobotPrefab().Instantiate<Robot>();
robot.Name = $"Robot #{GameData.robots.Count}";
robot.Position = GameData.map[0].tiles[0, 0].Position;
robot.robotType = spawnId;
GetNode("/root/Main/World").AddChild(robot);
GameData.robots.Add(robot);
spawnId = "";
ReloadRobots();
ReloadSelectableRobots();
}
public void OnRobotSelect(int index)
{
//Selected option is the "please select..." option
if(index == 0) return;
spawnId = ItemData.GetIndex(selectableRobots.GetItemText(index));
}
}