using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System.Collections.Generic;
using System.Collections;
namespace BulletHellTemplate
{
///
/// Manages the user interface during gameplay, including player stats, skills, and input handling for both mobile and PC platforms.
///
public class UIGameplay : MonoBehaviour
{
#region Mobile Controls
[Header("Mobile Controls")]
[Tooltip("Reference to the Joystick for mobile movement")]
public Joystick joystick; // Reference to the Joystick for mobile movement
[Header("Mobile Skill Joysticks")]
[Tooltip("Array to hold skill joysticks for mobile")]
public SkillJoystick[] skillJoysticks; // Array to hold skill joysticks for mobile
#endregion
#region PC Controls
[Header("PC Controls")]
[Tooltip("Reference to the PC Input Controller")]
public PCInputController pcInputController; // Reference to the PC Input Controller
[Tooltip("Reference to the PC Skill Controller")]
public PCSkillController pcSkillController; // Reference to the PC Skill Controller
#endregion
[Header("Skill Icons")]
[Tooltip("Array to hold skill images")]
public SkillImage[] skillImages;
[Header("Final Boss")]
[Tooltip("Final boss message GameObject")]
public GameObject finalBossMessage;
private MonsterEntity finalBossEntity;
private bool showBossHP;
[Tooltip("Time to close the final boss message")]
public float timeToCloseMessage = 3f;
[Header("Gameplay Rules Info")]
[Tooltip("Timer text")]
public TextMeshProUGUI timer;
[Tooltip("Monsters killed text")]
public TextMeshProUGUI monstersKilled;
[Tooltip("Gold gain text")]
public TextMeshProUGUI goldGain;
[Tooltip("Insufficient MP text")]
public TextMeshProUGUI insufficientMPText;
[Tooltip("Pause menu GameObject")]
public GameObject pauseMenu;
public Image minimapImage;
[Header("Upgrade Infos")]
[Tooltip("Perk entry prefab")]
public PerkEntry perkEntryPrefab;
[Tooltip("Perk container transform")]
public Transform perkContainer;
public Button openUpgradesButton;
public GameObject updateAvailable;
[Header("Skill Perk Images")]
[Tooltip("Array to hold skill perk images")]
public SkillsPerkImage[] skillPerkImages; // Array to hold skill perk images
[Header("Stat Perk Images")]
[Tooltip("Array to hold stat perk images")]
public StatPerkImage[] statPerkImages; // Array to hold stat perk images
[Header("Boss HP Bar")]
[Tooltip("Boss HP bar Image")]
public Image bossHpBar;
[Tooltip("Boss HP container GameObject")]
public GameObject bossHpContainer;
[Tooltip("Timer container GameObject")]
public GameObject timerContainer;
[Header("End Game Screen")]
[Tooltip("End game screen GameObject")]
public GameObject endGameScreen;
[Tooltip("End game message text")]
public TextMeshProUGUI endGameMessage;
[Tooltip("Victory objects to activate")]
public GameObject[] VictoryObjects;
[Tooltip("Defeat objects to activate")]
public GameObject[] DefeatObjects;
[Tooltip("Victory audio clip")]
public AudioClip VictoryAudio;
[Tooltip("Defeat audio clip")]
public AudioClip DefeatAudio;
[Header("Locked Perk Sprites")]
[Tooltip("Sprite for locked skills")]
public Sprite lockedSkillSprite;
[Tooltip("Sprite for locked stats")]
public Sprite lockedStatSprite;
[Tooltip("Sprite for unlocked slots")]
public Sprite unlockedSprite;
[Header("UI Character Info")]
public Image characterIcon;
[Tooltip("Health bar Image")]
public Image hpBar;
[Tooltip("Mana bar Image")]
public Image mpBar;
[Tooltip("Experience bar Image")]
public Image xpBar;
[Tooltip("Current text")]
public TextMeshProUGUI hpCurrent;
public TextMeshProUGUI energyCurrent;
public TextMeshProUGUI xpCurrent;
[Tooltip("Level text")]
public TextMeshProUGUI level;
[Tooltip("Health text")]
public TextMeshProUGUI hpText;
[Tooltip("Health regeneration text")]
public TextMeshProUGUI hpRegenText;
[Tooltip("Health leech text")]
public TextMeshProUGUI hpLeechText;
[Tooltip("Mana text")]
public TextMeshProUGUI mpText;
[Tooltip("Mana regeneration text")]
public TextMeshProUGUI mpRegenText;
[Tooltip("Damage text")]
public TextMeshProUGUI damageText;
[Tooltip("Attack speed text")]
public TextMeshProUGUI attackSpeedText;
[Tooltip("Cooldown reduction text")]
public TextMeshProUGUI cooldownReductionText;
[Tooltip("Critical rate text")]
public TextMeshProUGUI criticalRateText;
[Tooltip("Critical damage multiplier text")]
public TextMeshProUGUI criticalDamageMultiplierText;
[Tooltip("Defense text")]
public TextMeshProUGUI defenseText;
[Tooltip("Shield text")]
public TextMeshProUGUI shieldText;
[Tooltip("Move speed text")]
public TextMeshProUGUI moveSpeedText;
[Tooltip("Collect range text")]
public TextMeshProUGUI collectRangeText;
private CharacterEntity characterEntity;
private float currentHp;
private bool isUpdatingHpBar = false;
private int upgradeAmount = 0;
public static UIGameplay Singleton { get; private set; } // Singleton instance
private void Awake()
{
// Implement singleton pattern
if (Singleton == null)
{
Singleton = this;
}
else
{
Destroy(gameObject);
}
}
private void Start()
{
StartCoroutine(InitializeUpgradeButton());
StartCoroutine(BlinkGameObjectWhileUpgrading(updateAvailable));
}
///
/// Attempts to initialize the upgrade button when GameplayManager is ready.
///
private IEnumerator InitializeUpgradeButton()
{
// Keep trying until GameplayManager is available
while (GameplayManager.Singleton == null)
{
yield return null; // Wait for the next frame
}
// Check the upgrade mode and set up the button accordingly
if (openUpgradesButton != null && GameplayManager.Singleton.upgradeMode == GameplayManager.UpgradeMode.UpgradeOnButtonClick)
{
openUpgradesButton.interactable = false;
openUpgradesButton.onClick.AddListener(OnClickToChoicePowerUp);
}
else if (openUpgradesButton != null)
{
openUpgradesButton.gameObject.SetActive(false);
}
}
void Update()
{
if (characterEntity != null)
{
// Mobile movement using joystick
if (joystick != null)
{
Vector3 direction = new Vector3(joystick.Horizontal, 0, joystick.Vertical);
characterEntity.Move(direction); // Pass the joystick direction
}
UpdateUI(); // Update the UI with the latest character stats
}
// Update timer and other UI elements
timer.text = GameplayManager.Singleton.GetFormattedTime();
monstersKilled.text = GameplayManager.Singleton.GetMonstersKilled().ToString();
goldGain.text = GameplayManager.Singleton.GetGainGold().ToString();
// Update skill cooldowns
UpdateSkillCooldowns();
if (characterEntity != null)
{
// Mobile aiming with skill joysticks
if (skillJoysticks != null)
{
foreach (var skillJoystick in skillJoysticks)
{
if (skillJoystick != null)
{
Vector3 direction = new Vector3(skillJoystick.Horizontal, 0, skillJoystick.Vertical);
characterEntity.UpdateDirectionalAim(new Vector2(direction.x, direction.z),skillJoystick.skillIndex);
}
}
}
}
if (showBossHP)
{
if (finalBossEntity != null)
{
bossHpBar.fillAmount = finalBossEntity.GetCurrentHP() / finalBossEntity.GetMaxHP();
}
else
{
bossHpBar.fillAmount = 0;
}
}
}
///
/// Shows the final boss message and triggers camera shake.
///
public void ShowFinalBossMessage()
{
if (finalBossMessage != null)
{
finalBossMessage.SetActive(true);
StartCoroutine(HideFinalBossMessageAfterDelay(timeToCloseMessage));
if (TopDownCameraController.Singleton != null)
{
TopDownCameraController.Singleton.TriggerCameraShake();
}
}
}
private IEnumerator HideFinalBossMessageAfterDelay(float delay)
{
yield return new WaitForSeconds(delay);
finalBossMessage.SetActive(false);
timerContainer.SetActive(false);
showBossHP = true;
bossHpContainer.SetActive(true);
}
///
/// Displays the power-up choices when leveling up.
///
public void OnLevelUpChoicePowerUp()
{
foreach (Transform child in perkContainer)
{
Destroy(child.gameObject);
}
List