Files
RuinAdventurer/Scripts/UI/Common/UIHandler.cs
T

196 lines
4.3 KiB
C#

using Godot;
public partial class UIHandler : Control
{
[Export] CodingWindow codingWindow;
[Export] RobotList robotList;
[Export] Camera3d mainCam;
[Export] Map map;
[Export] RichTextLabel FPS;
[Export] RichTextLabel RAM;
[Export] PanelContainer options;
[Export] Control uiContent;
[Export] PanelContainer menu;
[Export] PanelContainer inventory;
[Export] ResearchList researchList;
[Export] TextureRect robotAlarm;
[Export] RichTextLabel energyLabel;
[Export] RichTextLabel waterLabel;
[Export] RichTextLabel hungerLabel;
[Export] RichTextLabel survivalStatus;
[Export] RichTextLabel currentLayer;
[Export] RichTextLabel deepestLayer;
[Export] Button unlockLayer;
[Export] PanelContainer gameOver;
private bool receivedRobotJumpSignal = false;
private bool receivedRobotFollowSignal = false;
public override void _Ready()
{
UIStyle.Apply(this);
robotList.OnRobotJumpTo += OnRobotJumpTo;
robotList.OnRobotFollow += OnRobotFollow;
}
public override void _ExitTree()
{
robotList.OnRobotJumpTo -= OnRobotJumpTo;
robotList.OnRobotFollow -= OnRobotFollow;
}
public override void _Process(double delta)
{
DisplayStats();
DisplayRobotAlarm();
if (IsTextInputFocused()) return;
if (Input.IsActionJustPressed("map")) HandleMapButton();
if (Input.IsActionJustPressed("menu")) HandleMenuButton();
if (Input.IsActionJustPressed("robot_list")) HandleRobotListButton();
if (Input.IsActionJustPressed("inventory")) HandleInventoryButton();
if (Input.IsActionJustPressed("research")) HandleResearchButton();
}
private bool IsTextInputFocused()
{
Control focused = GetViewport().GuiGetFocusOwner();
return focused is LineEdit || focused is TextEdit;
}
public void HandleMenuButton()
{
if (GameData.survival.isDead) return;
OpenUIElement(menu);
GameData.isPaused = menu.Visible || options.Visible;
}
public void HandleMenu()
{
HandleMenuButton();
}
public void ShowOptions()
{
if (GameData.survival.isDead) return;
menu.Hide();
OpenUIElement(options);
GameData.isPaused = options.Visible;
}
public void HandleMapButton()
{
if (GameData.survival.isDead) return;
OpenUIElement(map);
if (map.Visible) map.ShowMap();
}
public void HandleRobotListButton()
{
if (GameData.survival.isDead) return;
receivedRobotFollowSignal = false;
receivedRobotJumpSignal = false;
OpenUIElement(robotList);
}
public void HandleInventoryButton()
{
if (GameData.survival.isDead) return;
OpenUIElement(inventory);
}
public void HandleResearchButton()
{
if (GameData.survival.isDead) return;
OpenUIElement(researchList);
if (researchList.Visible) researchList.SetupGraph();
}
public void ExitGame()
{
GetTree().ChangeSceneToFile("res://Scenes/MainMenu.tscn");
}
public void SaveGame()
{
SaveGameManager.SaveGame();
}
public void LoadGame()
{
if (!SaveGameManager.SaveExists()) return;
GameData.loadSaveOnStart = true;
GetTree().ChangeSceneToFile("res://Scenes/Game.tscn");
}
public void OpenUIElement(Control element)
{
SoundManager.PlayButton();
element.Visible = !element.Visible;
HideUIElements(element);
}
private void HideUIElements(Control element)
{
foreach (PanelContainer child in uiContent.GetChildren())
{
if (child == element) continue;
child.Visible = false;
}
if (element != menu && element != options)
{
GameData.isPaused = false;
}
}
private void OnRobotJumpTo(Robot robot)
{
if (receivedRobotJumpSignal) return;
receivedRobotJumpSignal = true;
mainCam.Position = new Vector3(robot.Position.X, mainCam.Position.Y, robot.Position.Z + 3f);
codingWindow.SetRobot(robot);
OpenUIElement(codingWindow);
}
private void OnRobotFollow(Robot robot)
{
if (receivedRobotFollowSignal) return;
receivedRobotFollowSignal = true;
mainCam.Follow(robot);
}
public void UnlockLayer()
{
if (GameData.inventory.CanCraft(GameData.map[GameData.lowestLayer].gateIngredients, 1))
{
int openedLayer = GameData.lowestLayer;
foreach (Ingredient ingredient in GameData.map[GameData.lowestLayer].gateIngredients)
{
GameData.inventory.RemoveItem(ingredient.Item, ingredient.Amount);
}
GameData.lowestLayer++;
World world = GetNodeOrNull<World>("/root/Main/World");
if (world != null)
{
world.OpenGate(openedLayer);
}
if (GameData.lowestLayer == GameData.ruinSize)
{
gameOver.Show();
}
}
}
}