282 lines
11 KiB
C#
282 lines
11 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using Spaceshooter.Player;
|
|
using Spaceshooter.Managers;
|
|
using Spaceshooter.Weapons;
|
|
|
|
namespace Spaceshooter.UI
|
|
{
|
|
public class UIManager : MonoBehaviour
|
|
{
|
|
public static UIManager Instance { get; private set; }
|
|
|
|
[Header("State Panels")]
|
|
[SerializeField] private GameObject panelMainMenu;
|
|
[SerializeField] private GameObject panelHUD;
|
|
[SerializeField] private GameObject panelPause;
|
|
[SerializeField] private GameObject panelStationShop;
|
|
[SerializeField] private GameObject panelGameOver;
|
|
|
|
[Header("HUD - Health & Scrap")]
|
|
[SerializeField] private Image healthBarFill;
|
|
[SerializeField] private Text healthText;
|
|
[SerializeField] private Text scrapText;
|
|
[SerializeField] private Image progressFill;
|
|
[SerializeField] private Button pauseButton;
|
|
|
|
[Header("HUD - 3 Weapon Slots")]
|
|
[SerializeField] private List<WeaponSlotUI> weaponSlotUIs = new List<WeaponSlotUI>();
|
|
|
|
[Header("Shop UI")]
|
|
[SerializeField] private Text shopScrapText;
|
|
[SerializeField] private Button repairHullButton;
|
|
[SerializeField] private Text repairHullText;
|
|
[SerializeField] private Button buyLaserButton;
|
|
[SerializeField] private Text buyLaserText;
|
|
[SerializeField] private Button buyPlasmaButton;
|
|
[SerializeField] private Text buyPlasmaText;
|
|
[SerializeField] private Button continueShopButton;
|
|
|
|
[Header("Menu & Game Over Buttons")]
|
|
[SerializeField] private Button startButton;
|
|
[SerializeField] private Button resumeButton;
|
|
[SerializeField] private Button restartPauseButton;
|
|
[SerializeField] private Button menuPauseButton;
|
|
[SerializeField] private Button restartGameOverButton;
|
|
[SerializeField] private Button menuGameOverButton;
|
|
|
|
[System.Serializable]
|
|
public class WeaponSlotUI
|
|
{
|
|
public GameObject root;
|
|
public Text nameText;
|
|
public Image borderHighlight;
|
|
public Image cooldownOverlay;
|
|
public Button clickButton;
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance != null && Instance != this)
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
Instance = this;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
SetupButtonListeners();
|
|
|
|
if (PlayerShip3D.Instance != null)
|
|
{
|
|
PlayerShip3D.Instance.OnStatsChanged += RefreshHUD;
|
|
PlayerShip3D.Instance.OnWeaponSlotChanged += RefreshWeaponSlots;
|
|
}
|
|
|
|
RefreshHUD();
|
|
RefreshWeaponSlots();
|
|
}
|
|
|
|
private void SetupButtonListeners()
|
|
{
|
|
if (startButton != null) startButton.onClick.AddListener(() => GameManager.Instance.StartGame());
|
|
if (pauseButton != null) pauseButton.onClick.AddListener(() => GameManager.Instance.TogglePause());
|
|
if (resumeButton != null) resumeButton.onClick.AddListener(() => GameManager.Instance.ResumeGame());
|
|
if (restartPauseButton != null) restartPauseButton.onClick.AddListener(() => GameManager.Instance.RestartGame());
|
|
if (menuPauseButton != null) menuPauseButton.onClick.AddListener(() => GameManager.Instance.ReturnToMainMenu());
|
|
if (restartGameOverButton != null) restartGameOverButton.onClick.AddListener(() => GameManager.Instance.RestartGame());
|
|
if (menuGameOverButton != null) menuGameOverButton.onClick.AddListener(() => GameManager.Instance.ReturnToMainMenu());
|
|
|
|
// Shop buttons
|
|
if (continueShopButton != null) continueShopButton.onClick.AddListener(() => GameManager.Instance.ContinueAfterShop());
|
|
|
|
if (repairHullButton != null)
|
|
{
|
|
repairHullButton.onClick.AddListener(() =>
|
|
{
|
|
if (PlayerShip3D.Instance != null && PlayerShip3D.Instance.RepairHull(40f, 25))
|
|
{
|
|
RefreshShopUI();
|
|
}
|
|
});
|
|
}
|
|
|
|
if (buyLaserButton != null)
|
|
{
|
|
buyLaserButton.onClick.AddListener(() =>
|
|
{
|
|
WeaponData laser = WeaponData.CreateDefault(WeaponType.RapidLaser);
|
|
if (PlayerShip3D.Instance != null && PlayerShip3D.Instance.BuyWeapon(laser))
|
|
{
|
|
RefreshShopUI();
|
|
}
|
|
});
|
|
}
|
|
|
|
if (buyPlasmaButton != null)
|
|
{
|
|
buyPlasmaButton.onClick.AddListener(() =>
|
|
{
|
|
WeaponData plasma = WeaponData.CreateDefault(WeaponType.HeavyPlasma);
|
|
if (PlayerShip3D.Instance != null && PlayerShip3D.Instance.BuyWeapon(plasma))
|
|
{
|
|
RefreshShopUI();
|
|
}
|
|
});
|
|
}
|
|
|
|
// Weapon slot click listeners
|
|
for (int i = 0; i < weaponSlotUIs.Count; i++)
|
|
{
|
|
int slotIndex = i;
|
|
if (weaponSlotUIs[i].clickButton != null)
|
|
{
|
|
weaponSlotUIs[i].clickButton.onClick.AddListener(() =>
|
|
{
|
|
if (PlayerShip3D.Instance != null)
|
|
{
|
|
PlayerShip3D.Instance.SetActiveSlot(slotIndex);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
// Update progress bar
|
|
if (progressFill != null && GameManager.Instance != null)
|
|
{
|
|
progressFill.fillAmount = GameManager.Instance.ProgressNormalized;
|
|
}
|
|
|
|
// Update cooldown overlays on weapon slots
|
|
if (PlayerShip3D.Instance != null)
|
|
{
|
|
var slots = PlayerShip3D.Instance.WeaponSlots;
|
|
for (int i = 0; i < weaponSlotUIs.Count && i < slots.Count; i++)
|
|
{
|
|
if (weaponSlotUIs[i].cooldownOverlay != null)
|
|
{
|
|
weaponSlotUIs[i].cooldownOverlay.fillAmount = slots[i].CooldownProgress;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void UpdatePanels(GameState state)
|
|
{
|
|
if (panelMainMenu != null) panelMainMenu.SetActive(state == GameState.MainMenu);
|
|
if (panelHUD != null) panelHUD.SetActive(state == GameState.Playing || state == GameState.Paused || state == GameState.StationDocked);
|
|
if (panelPause != null) panelPause.SetActive(state == GameState.Paused);
|
|
if (panelStationShop != null)
|
|
{
|
|
panelStationShop.SetActive(state == GameState.StationDocked);
|
|
if (state == GameState.StationDocked) RefreshShopUI();
|
|
}
|
|
if (panelGameOver != null) panelGameOver.SetActive(state == GameState.GameOver);
|
|
|
|
RefreshHUD();
|
|
RefreshWeaponSlots();
|
|
}
|
|
|
|
public void RefreshHUD()
|
|
{
|
|
if (PlayerShip3D.Instance == null) return;
|
|
|
|
float curHp = PlayerShip3D.Instance.CurrentHealth;
|
|
float maxHp = PlayerShip3D.Instance.MaxHealth;
|
|
|
|
if (healthBarFill != null)
|
|
{
|
|
healthBarFill.fillAmount = maxHp > 0 ? (curHp / maxHp) : 0f;
|
|
}
|
|
|
|
if (healthText != null)
|
|
{
|
|
healthText.text = $"HP: {Mathf.CeilToInt(curHp)} / {Mathf.CeilToInt(maxHp)}";
|
|
}
|
|
|
|
if (scrapText != null)
|
|
{
|
|
scrapText.text = $"ORE: {PlayerShip3D.Instance.ScrapCount}";
|
|
}
|
|
}
|
|
|
|
public void RefreshWeaponSlots()
|
|
{
|
|
if (PlayerShip3D.Instance == null) return;
|
|
|
|
var slots = PlayerShip3D.Instance.WeaponSlots;
|
|
int activeIndex = PlayerShip3D.Instance.ActiveSlotIndex;
|
|
|
|
for (int i = 0; i < weaponSlotUIs.Count; i++)
|
|
{
|
|
if (i < slots.Count && slots[i].IsOccupied)
|
|
{
|
|
// Show slot
|
|
if (weaponSlotUIs[i].root != null) weaponSlotUIs[i].root.SetActive(true);
|
|
if (weaponSlotUIs[i].nameText != null)
|
|
{
|
|
weaponSlotUIs[i].nameText.text = $"[{i + 1}] {slots[i].weaponData.weaponName}";
|
|
}
|
|
|
|
// Highlight active
|
|
if (weaponSlotUIs[i].borderHighlight != null)
|
|
{
|
|
weaponSlotUIs[i].borderHighlight.color = (i == activeIndex) ? new Color(0.2f, 1f, 0.4f, 1f) : new Color(0.3f, 0.3f, 0.3f, 0.5f);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// If none selected / empty, hide it completely!
|
|
if (weaponSlotUIs[i].root != null) weaponSlotUIs[i].root.SetActive(false);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void RefreshShopUI()
|
|
{
|
|
if (PlayerShip3D.Instance == null) return;
|
|
|
|
int scrap = PlayerShip3D.Instance.ScrapCount;
|
|
if (shopScrapText != null)
|
|
{
|
|
shopScrapText.text = $"Available Ore: {scrap}";
|
|
}
|
|
|
|
bool needsRepair = PlayerShip3D.Instance.CurrentHealth < PlayerShip3D.Instance.MaxHealth;
|
|
if (repairHullButton != null)
|
|
{
|
|
repairHullButton.interactable = needsRepair && scrap >= 25;
|
|
}
|
|
if (repairHullText != null)
|
|
{
|
|
repairHullText.text = needsRepair ? "Repair Hull (+40 HP) - 25 Ore" : "Hull at Max Health";
|
|
}
|
|
|
|
// Check weapon ownership
|
|
bool ownsLaser = HasWeapon(WeaponType.RapidLaser);
|
|
if (buyLaserButton != null) buyLaserButton.interactable = !ownsLaser && scrap >= 30;
|
|
if (buyLaserText != null) buyLaserText.text = ownsLaser ? "Rapid Laser (Owned)" : "Buy Rapid Laser - 30 Ore";
|
|
|
|
bool ownsPlasma = HasWeapon(WeaponType.HeavyPlasma);
|
|
if (buyPlasmaButton != null) buyPlasmaButton.interactable = !ownsPlasma && scrap >= 60;
|
|
if (buyPlasmaText != null) buyPlasmaText.text = ownsPlasma ? "Heavy Plasma (Owned)" : "Buy Heavy Plasma - 60 Ore";
|
|
}
|
|
|
|
private bool HasWeapon(WeaponType type)
|
|
{
|
|
if (PlayerShip3D.Instance == null) return false;
|
|
foreach (var slot in PlayerShip3D.Instance.WeaponSlots)
|
|
{
|
|
if (slot.IsOccupied && slot.weaponData.type == type) return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
}
|