cirnogodot/Scripts/Hud.cs

404 lines
8.6 KiB
C#
Raw Normal View History

2024-08-27 22:12:47 +02:00
using Godot;
using System;
2025-02-10 17:29:14 +01:00
using System.Collections.Generic;
using System.Linq;
2025-02-11 11:50:45 +01:00
using Cirno.Scripts;
2025-01-31 13:03:38 +01:00
using Cirno.Scripts.Resources;
2025-02-19 15:45:21 +01:00
using Cirno.Scripts.UI;
using Cirno.Scripts.Utils;
2024-08-27 22:12:47 +02:00
public partial class Hud : CanvasLayer
{
2025-03-01 20:50:47 +01:00
public static Hud Instance { get; private set; }
2024-08-27 22:12:47 +02:00
[Signal]
public delegate void StartGameEventHandler();
2025-01-28 09:17:35 +01:00
2025-01-31 13:03:38 +01:00
//private Label _healthLabel;
2024-08-27 22:12:47 +02:00
2025-01-30 08:34:09 +01:00
[Export]
public PackedScene SelectorScene { get; set; }
2025-03-05 18:55:30 +01:00
[Export]
public PackedScene WeaponContainerTemplate { get; private set; }
2025-03-06 15:03:14 +01:00
[Export]
public PackedScene ItemNotificationTemplate { get; private set; }
2025-01-30 08:34:09 +01:00
2025-03-06 15:03:14 +01:00
[Export] public float ItemsNotificationTimeout { get; private set; } = 3f;
2025-01-30 08:34:09 +01:00
[Export]
private Node2D _selector;
2025-01-31 13:03:38 +01:00
[Export] private Label _healthLabel;
2025-02-11 17:55:50 +01:00
[Export] private ProgressBar _healthBar;
[Export] private Label _shieldLabel;
[Export] private ProgressBar _shieldBar;
2025-03-31 18:28:33 +02:00
[Export] private Label _motivationLabel;
2025-02-11 17:55:50 +01:00
2025-01-31 13:03:38 +01:00
[Export] private Container _itemsContainer;
2025-02-11 17:55:50 +01:00
[Export] private LabelSettings _labelSettings;
2025-02-20 16:12:53 +01:00
[Export] private Container _gameOverPanel;
2025-04-01 11:23:37 +02:00
[Export] private Container _fairyTerminatedPanel;
[Export] private Container _hudInfoPanel;
[Export] public Container NotificationsContainer { get; private set; }
[Export] public Container WeaponContainer { get; private set; }
2025-02-19 15:45:21 +01:00
[ExportGroup("Pause Menu")] [Export]
public Control PauseMenuContainer;
[ExportGroup("Pause Menu")] [Export]
public PackedScene PauseMenuScene;
[ExportGroup("Debug Menu")]
[Export]
public PackedScene DebugMenuTemplate { get; set; }
[ExportGroup("Debug Menu")]
[Export]
public Control DebugMenuHolder { get; set; }
2025-02-19 15:45:21 +01:00
2025-03-05 18:55:30 +01:00
private Dictionary<string, WeaponAmmoCounter> _items = new();
2025-02-12 18:16:16 +01:00
2025-02-19 15:45:21 +01:00
private PauseMenu _pauseMenu;
2025-02-20 16:12:53 +01:00
2025-03-01 20:50:47 +01:00
private bool _playerDead = false; // useless
private WeaponAmmoCounter _weapon;
2025-02-19 15:45:21 +01:00
2025-01-28 09:17:35 +01:00
public override void _Ready()
{
2025-03-01 20:50:47 +01:00
Instance = this;
2025-01-28 09:17:35 +01:00
// Assuming the HUD has a Label node named "HealthLabel"
2025-01-31 13:03:38 +01:00
//_healthLabel = GetNode<Label>("HealthLabel");
2025-02-20 16:12:53 +01:00
_gameOverPanel.Hide();
2025-03-06 15:03:14 +01:00
InventoryManager.Instance.ItemAdded += OnItemAdded;
2025-01-28 09:17:35 +01:00
}
2025-03-06 15:03:14 +01:00
private void OnItemAdded(LootItem item, int currentamount)
{
var instance = ItemNotificationTemplate.Instantiate<ItemNotification>();
NotificationsContainer.CallDeferred("add_child", instance);
2025-03-06 15:03:14 +01:00
instance.Init(item, currentamount, ItemsNotificationTimeout);
if (item.UiType.HasAnyFlag(UiItemType.Ammo | UiItemType.Energy))
{
AddWeapon(item);
return;
}
if (item.UiType.HasAnyFlag(UiItemType.Icon | UiItemType.Count))
{
AddInventoryItem(item, currentamount);
}
2025-03-06 15:03:14 +01:00
}
2024-08-27 22:12:47 +02:00
public void ShowMessage(string text)
{
var message = GetNode<Label>("Message");
message.Text = text;
message.Show();
GetNode<Timer>("MessageTimer").Start();
}
2025-02-20 16:12:53 +01:00
public void ShowGameOver()
2024-08-27 22:12:47 +02:00
{
2025-02-20 16:12:53 +01:00
_gameOverPanel.Show();
2025-04-01 11:23:37 +02:00
HideHud();
2025-02-20 16:12:53 +01:00
_playerDead = true;
2024-08-27 22:12:47 +02:00
}
2025-03-01 20:50:47 +01:00
public void HideGameOver()
{
2025-03-02 11:58:30 +01:00
_gameOverPanel.Hide();
2025-04-01 11:23:37 +02:00
ShowHud();
2025-03-02 11:58:30 +01:00
_playerDead = false;
2025-03-01 20:50:47 +01:00
}
2025-04-01 11:23:37 +02:00
public void ShowTerminated()
{
_fairyTerminatedPanel.Show();
HideHud();
_playerDead = true;
}
public void HideTerminated()
{
_fairyTerminatedPanel.Hide();
ShowHud();
_playerDead = false;
}
public void ShowHud()
{
_hudInfoPanel.Show();
}
public void HideHud()
{
_hudInfoPanel.Hide();
}
2025-01-28 09:17:35 +01:00
public void UpdateHealth(float newHealth, float maxHealth)
{
2025-02-20 16:12:53 +01:00
// Hide game over if player is no longer dead
if (_playerDead && newHealth > 0)
{
_playerDead = false;
2025-04-01 11:23:37 +02:00
_fairyTerminatedPanel.Hide();
2025-02-20 16:12:53 +01:00
}
2025-04-09 09:30:49 +02:00
_healthLabel.Text = $"{newHealth:N0}/{maxHealth:N0}";
2025-02-11 17:55:50 +01:00
if (_healthBar != null)
{
_healthBar.Value = newHealth;
}
}
public void UpdateShield(float newValue, float maxValue)
{
2025-04-09 09:30:49 +02:00
_shieldLabel.Text = $"{newValue:N0}/{maxValue:N0}";
2025-02-11 17:55:50 +01:00
if (_shieldBar != null)
{
_shieldBar.Value = newValue;
}
2025-01-28 09:17:35 +01:00
}
2025-03-31 18:28:33 +02:00
public void UpdateMotivation(float newValue, float maxValue)
{
2025-04-09 09:30:49 +02:00
_motivationLabel.Text = $"{newValue:N0}%";
2025-03-31 18:28:33 +02:00
}
2025-01-30 08:34:09 +01:00
public void UpdateInteractable(Interactable interactable) {
GD.Print($"Interactable ${interactable.Name} entered in HUD");
//_selector.Position = _selector.tolo
}
2025-01-31 13:03:38 +01:00
private void AddWeapon(LootItem item)
{
_weapon?.Delete();
var instance = WeaponContainerTemplate.Instantiate<WeaponAmmoCounter>();
WeaponContainer.CallDeferred("add_child", instance);
instance.Init(item);
_weapon = instance;
}
2025-02-10 17:29:14 +01:00
public void AddInventoryItem(LootItem item, int currentAmount)
2025-01-31 13:03:38 +01:00
{
if (item.UiType == 0) return;
2025-02-10 17:29:14 +01:00
if (!_items.TryGetValue(item.ItemKey, out var item1))
{
2025-03-05 18:55:30 +01:00
var instance = WeaponContainerTemplate.Instantiate<WeaponAmmoCounter>();
2025-03-05 18:55:30 +01:00
_itemsContainer.CallDeferred("add_child", instance);
2025-02-10 17:29:14 +01:00
2025-03-05 18:55:30 +01:00
// var hbox = new HBoxContainer();
// _itemsContainer.AddChild(hbox);
//
// var instance = new TextureRect();
// instance.Texture = item.InventorySprite;
// instance.StretchMode = TextureRect.StretchModeEnum.Keep;
// hbox.AddChild(instance);
2025-02-10 17:29:14 +01:00
2025-03-05 18:55:30 +01:00
// var hudItem = new HudItem()
// {
// Item = item,
// Container = hbox,
// //Amount = currentAmount
// };
// if (item.UiType == UiItemType.IconText)
// {
// var label = new Label();
// label.Text = currentAmount.ToString();
//
//
// label.LabelSettings = _labelSettings;
// hbox.AddChild(label);
//
// hudItem.Label = label;
// }
_items.Add(item.ItemKey, instance);
2025-02-10 17:29:14 +01:00
2025-03-05 18:55:30 +01:00
instance.Init(item);
2025-02-10 17:29:14 +01:00
}
else
{
2025-03-05 18:55:30 +01:00
// nothing
// if (item.UiType == UiItemType.IconText && item1.Label != null)
// {
// item1.Label.Text = currentAmount.ToString();
// }
2025-02-10 17:29:14 +01:00
}
2025-01-31 13:03:38 +01:00
2025-02-10 17:29:14 +01:00
// TextureRect texture = new TextureRect();
// texture.Texture = item.InventorySprite;
//
// texture.ExpandMode = TextureRect.ExpandModeEnum.KeepSize;
// texture.StretchMode = TextureRect.StretchModeEnum.Keep;
//
// _itemsContainer.AddChild(texture);
2025-01-31 13:03:38 +01:00
}
2025-02-19 15:45:21 +01:00
public void OnGameStateChanged(GameState state)
{
switch (state)
{
case GameState.Paused:
SpawnPauseMenu();
break;
2025-03-01 20:50:47 +01:00
case GameState.Controlling:
case GameState.Menu:
2025-02-19 15:45:21 +01:00
case GameState.Playing:
case GameState.Dialogue:
2025-03-01 20:50:47 +01:00
default:
2025-02-19 15:45:21 +01:00
ClearPauseMenu();
break;
}
}
private void SpawnPauseMenu()
{
if (PauseMenuContainer is null || PauseMenuScene is null) return;
PauseMenuContainer.Visible = true;
_pauseMenu = PauseMenuScene.Instantiate<PauseMenu>();
PauseMenuContainer.CallDeferred("add_child", _pauseMenu);
_pauseMenu.SpawnDebugMenu += SpawnDebugMenu;
}
private void ClearPauseMenu()
{
if (PauseMenuContainer is null) return;
if (_pauseMenu is not null)
{
_pauseMenu.SpawnDebugMenu -= SpawnDebugMenu;
}
PauseMenuContainer.Visible = false;
var children = PauseMenuContainer.GetChildren();
foreach (var child in children)
{
child.QueueFree();
}
}
2025-03-05 18:55:30 +01:00
// public void RemoveInventoryItem(string itemKey, int currentAmount)
// {
// if (_items.TryGetValue(itemKey, out var itm))
// {
// if (currentAmount <= 0)
// {
// itm.Container.QueueFree();
// _items.Remove(itemKey);
// }
// else
// {
// if (itm.Item.UiType == UiItemType.IconText)
// {
// itm.Label.Text = currentAmount.ToString();
// }
// }
// }
// else
// {
// GD.Print($"Tried to remove item {itemKey} but it was not found");
// }
//
// // var containerItem = _items.FirstOrDefault(x => x.Item == item);
// // if (containerItem == null)
// // {
// // GD.Print($"Tried to remove item {item.ItemName} but it was not found");
// // return;
// // }
// //
// // containerItem.Container.QueueFree();
//
// }
2025-02-10 17:29:14 +01:00
2025-02-19 15:45:21 +01:00
private void SpawnDebugMenu()
{
ClearPauseMenu();
if (DebugMenuTemplate is null || DebugMenuHolder is null) return;
DebugMenuHolder.Visible = true;
var children = DebugMenuHolder.GetChildren();
foreach (var child in children)
{
child.QueueFree();
}
var menu = DebugMenuTemplate.Instantiate<DebugMenu>();
DebugMenuHolder.CallDeferred("add_child", menu);
2025-02-25 21:21:07 +01:00
menu.MenuClosed += () =>
2025-02-19 15:45:21 +01:00
{
DebugMenuHolder.Visible = false;
2025-05-20 17:46:35 +02:00
if (GameManager.Instance.GameState is GameState.Paused)
{
SpawnPauseMenu();
}
2025-02-19 15:45:21 +01:00
};
}
2025-02-10 17:29:14 +01:00
public class HudItem
{
public LootItem Item { get; set; }
public HBoxContainer Container { get; set; }
public Label Label { get; set; }
2025-02-11 11:50:45 +01:00
//public int Amount { get; set; }
2025-02-10 17:29:14 +01:00
}
2025-03-05 18:55:30 +01:00
public void EquipWeapon(string itemKey)
{
if (_items.TryGetValue(itemKey, out var localItem))
{
// It's already in the hud, abort
GD.Print($"Item with key {itemKey} is already in hud, aborting.");
return;
}
if (!InventoryManager.Instance.TryGetItem(itemKey, out var item))
{
GD.Print($"Item with key {itemKey} not found in inventory when equipping in hud.");
return;
}
// Clear all items
// foreach (var hudItem in _items)
// {
// hudItem.Value.Delete();
// }
// _items.Clear();
2025-03-05 18:55:30 +01:00
AddWeapon(item.Item);
//AddInventoryItem(item.Item, item.Count);
2025-03-05 18:55:30 +01:00
}
2025-03-31 18:28:33 +02:00
2024-08-27 22:12:47 +02:00
}