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() { ClearRobotList(); foreach (Robot robotObject in GameData.robots) { robotList.AddChild(CreateRobotDisplay(robotObject)); } } private void ClearRobotList() { foreach (Node node in robotList.GetChildren()) { robotList.RemoveChild(node); node.QueueFree(); } } private RobotDisplay CreateRobotDisplay(Robot robotObject) { RobotDisplay display = robotDisplayPrefab.Instantiate(); display.robot = robotObject; display.listItem.Text = robotObject.Name; display.OnRobotJumpTo += HandleRobotJumpTo; display.OnRobotFollow += HandleRobotFollow; return display; } private void HandleRobotJumpTo(Robot robot) { EmitSignal(SignalName.OnRobotJumpTo, robot); Visible = false; } private void HandleRobotFollow(Robot robot) { EmitSignal(SignalName.OnRobotFollow, robot); Visible = false; } 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.Position = GameData.map[0].tiles[0, 0].Position; robot.robotType = spawnId; GetNode("/root/Main/World").AddChild(robot); robot.Name = $"Robot #{GameData.robots.Count}"; GameData.robots.Add(robot); spawnId = ""; ReloadRobots(); ReloadSelectableRobots(); } public void OnRobotSelect(int index) { if (index == 0) return; spawnId = ItemData.GetIndex(selectableRobots.GetItemText(index)); } }