cirnogodot/Scripts/Hud.cs

311 lines
6.7 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;
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; }
[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-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-01-31 13:03:38 +01:00
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-10 17:29:14 +01:00
private Dictionary<string, HudItem> _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
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-01-28 09:17:35 +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();
_playerDead = true;
// ShowMessage("Game Over");
//
// var messageTimer = GetNode<Timer>("MessageTimer");
// await ToSignal(messageTimer, Timer.SignalName.Timeout);
//
// var message = GetNode<Label>("Message");
// message.Text = "Dodge the Creeps!";
// message.Show();
//
// await ToSignal(GetTree().CreateTimer(1.0), SceneTreeTimer.SignalName.Timeout);
// GetNode<Button>("StartButton").Show();
2024-08-27 22:12:47 +02:00
}
2025-03-01 20:50:47 +01:00
public void HideGameOver()
{
}
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;
_gameOverPanel.Hide();
}
2025-01-28 09:17:35 +01:00
_healthLabel.Text = $"{newHealth}/{maxHealth}";
2025-02-11 17:55:50 +01:00
if (_healthBar != null)
{
_healthBar.Value = newHealth;
}
}
public void UpdateShield(float newValue, float maxValue)
{
_shieldLabel.Text = $"{newValue}/{maxValue}";
if (_shieldBar != null)
{
_shieldBar.Value = newValue;
}
2025-01-28 09:17:35 +01: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
2025-02-10 17:29:14 +01:00
public void AddInventoryItem(LootItem item, int currentAmount)
2025-01-31 13:03:38 +01:00
{
2025-02-10 17:29:14 +01:00
if (item.UiType == UiItemType.NoUI) return;
if (!_items.TryGetValue(item.ItemKey, out var item1))
{
var hbox = new HBoxContainer();
_itemsContainer.AddChild(hbox);
var instance = new TextureRect();
instance.Texture = item.InventorySprite;
instance.StretchMode = TextureRect.StretchModeEnum.Keep;
// change transform size?
//var instance = item.HudItemScene.Instantiate<TextureRect>();
2025-02-10 17:29:14 +01:00
//_in.CallDeferred("add_child", instance);
hbox.AddChild(instance);
var hudItem = new HudItem()
{
Item = item,
Container = hbox,
2025-02-11 11:50:45 +01:00
//Amount = currentAmount
2025-02-10 17:29:14 +01:00
};
if (item.UiType == UiItemType.IconText)
{
var label = new Label();
label.Text = currentAmount.ToString();
2025-02-11 11:50:45 +01:00
// if (item.Item == ItemTypes.Weapon && item.WeaponData != null)
// {
// // Show ammo instead of item count
// var ammoItem = _items.GetValueOrDefault(item.WeaponData.AmmoKey);
//
// if (ammoItem != null)
// {
// label.Text = "0";
// }
// else
// {
// label.Text = ammoItem.
// }
//
// }
// else
// {
// label.Text = currentAmount.ToString();
// }
2025-02-11 17:55:50 +01:00
label.LabelSettings = _labelSettings;
2025-02-10 17:29:14 +01:00
hbox.AddChild(label);
hudItem.Label = label;
}
_items.Add(item.ItemKey, hudItem);
}
else
{
if (item.UiType == UiItemType.IconText && item1.Label != null)
{
item1.Label.Text = currentAmount.ToString();
}
}
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-02-11 11:50:45 +01:00
public void RemoveInventoryItem(string itemKey, int currentAmount)
2025-01-31 13:03:38 +01:00
{
2025-02-11 11:50:45 +01:00
if (_items.TryGetValue(itemKey, out var itm))
2025-02-10 17:29:14 +01:00
{
if (currentAmount <= 0)
{
itm.Container.QueueFree();
2025-02-11 11:50:45 +01:00
_items.Remove(itemKey);
2025-02-10 17:29:14 +01:00
}
else
{
2025-02-11 11:50:45 +01:00
if (itm.Item.UiType == UiItemType.IconText)
{
itm.Label.Text = currentAmount.ToString();
}
2025-02-10 17:29:14 +01:00
}
}
else
{
2025-02-11 11:50:45 +01:00
GD.Print($"Tried to remove item {itemKey} but it was not found");
2025-02-10 17:29:14 +01:00
}
// 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-01-31 13:03:38 +01:00
}
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;
SpawnPauseMenu();
};
}
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
}
2024-08-27 22:12:47 +02:00
}