| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- using UnityEngine;
- using UnityEngine.UI;
- using DamageNumbersPro;
- using System.Collections;
- public class CharacterUIController : MonoBehaviour
- {
- public Slider hpSlider;
- public Slider fatigueSlider;
- public Slider expSlider;
- public GameObject skillPanel;
- public Transform skillListContainer;
- public GameObject skillButtonPrefab;
-
- public GameObject inventoryPanel; // Panel displaying character inventory
- public InventoryUIController inventoryUIController; // Reference to inventory UI controller
- public Image characterIcon;
- public Button attackButton;
- public Button repeatActionButton;
- public Button openSkillsButton;
- public Button openInventoryButton; // Button to open character inventory
- public Image cooldownOverlay;
- public FirstPersonWeapon assignedWeapon;
- public CharacterInGroup character;
- public CharacterInGroup Character => character;
- public DamageNumber damageUIPrefab; // à assigner dans l’inspector (UI)
- public RectTransform damageSpawnPoint;
- public void Setup(CharacterInGroup chara, FirstPersonWeapon weapon)
- {
- character = chara;
- assignedWeapon = weapon;
-
- // Initialisation des sliders
- if (hpSlider != null)
- {
- hpSlider.maxValue = character.maxHP;
- hpSlider.value = character.currentHP;
- }
- if (fatigueSlider != null)
- {
- fatigueSlider.maxValue = character.maxFatigue;
- fatigueSlider.value = character.currentFatigue;
- }
- if (expSlider != null)
- {
- expSlider.maxValue = 100;
- expSlider.value = character.experience;
- }
- if (attackButton != null)
- attackButton.onClick.AddListener(OnAttack);
- if (repeatActionButton != null)
- repeatActionButton.onClick.AddListener(OnRepeat);
- if (openSkillsButton != null)
- openSkillsButton.onClick.AddListener(ToggleSkillPanel);
- if (openInventoryButton != null)
- openInventoryButton.onClick.AddListener(OnOpenInventory);
- UIUpdater.Instance?.Register(character, this);
- PopulateSkillList();
- if (cooldownOverlay != null)
- {
- cooldownOverlay.gameObject.SetActive(false);
- }
- }
- public void ShowDamageOnCard(int amount)
- {
- if (damageUIPrefab != null && damageSpawnPoint != null)
- {
- damageUIPrefab.SpawnGUI(damageSpawnPoint, Vector2.zero, amount);
- }
- }
- void PopulateSkillList()
- {
- foreach (Transform child in skillListContainer)
- Destroy(child.gameObject);
- foreach (var skill in character.learnedSkills)
- {
- SkillData data = skill.GetData();
- if (data == null) continue;
- var btnObj = Instantiate(skillButtonPrefab, skillListContainer);
- btnObj.transform.Find("Image").GetComponent<Image>().sprite = data.icon;
- Button btn = btnObj.GetComponent<Button>();
- if (btn != null)
- {
- // Capture la compétence associée
- SkillData capturedSkill = data;
- btn.onClick.AddListener(() => OnSkillButtonClicked(capturedSkill));
- }
- }
- }
- void OnAttack()
- {
- if (character.currentFatigue <= 0)
- {
- if (Random.value < 0.8f)
- {
- DisableButtonsTemporarily(5f);
- return;
- }
- }
-
- if (Random.value < 0.3f && character.currentFatigue > 0)
- {
- character.currentFatigue = Mathf.Max(0, character.currentFatigue - 5);
- UIUpdater.Instance?.UpdateCharacterFatigue(character);
- }
-
- character.lastAction = ActionType.Attack;
- assignedWeapon?.TriggerAttack();
- DisableButtonsTemporarily(5f);
- }
- void OnSkillButtonClicked(SkillData skillData)
- {
- int staminaCost = 15;
- if (character.currentFatigue < staminaCost)
- {
- return;
- }
- character.currentFatigue = Mathf.Max(0, character.currentFatigue - staminaCost);
- UIUpdater.Instance?.UpdateCharacterFatigue(character);
- if (skillData.skillName == "Boule de Feu")
- {
- FindFirstObjectByType<SpellCastingManager>()?.StartFireballCasting(skillData.icon.texture);
- character.lastAction = ActionType.UseSkill;
- character.lastSkillName = skillData.skillName;
- }
-
- DisableButtonsTemporarily(5f);
- }
-
- void OnRepeat()
- {
- switch (character.lastAction)
- {
- case ActionType.Attack:
- OnAttack();
- return;
- case ActionType.UseSkill:
- var learnedSkill = character.learnedSkills.Find(s => s.GetData()?.skillName == character.lastSkillName);
- if (learnedSkill != null)
- {
- var skillData = learnedSkill.GetData();
- if (skillData != null)
- {
- OnSkillButtonClicked(skillData);
- return;
- }
- }
- break;
- case ActionType.Heal:
- break;
- default:
- break;
- }
- DisableButtonsTemporarily(5f);
- }
- void ToggleSkillPanel()
- {
- if (skillPanel != null)
- skillPanel.SetActive(!skillPanel.activeSelf);
- }
-
- void OnOpenInventory()
- {
- if (inventoryPanel != null)
- {
- inventoryPanel.SetActive(!inventoryPanel.activeSelf);
-
- if (inventoryPanel.activeSelf && inventoryUIController != null)
- {
- inventoryUIController.Bind(character);
- }
- }
- }
- public void UpdateHPBar()
- {
- if (hpSlider == null || character == null) return;
-
- hpSlider.maxValue = character.maxHP;
- hpSlider.value = character.currentHP;
-
- UnityEngine.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(hpSlider.GetComponent<RectTransform>());
- }
- public void UpdateFatigueBar()
- {
- if (fatigueSlider == null || character == null) return;
-
- fatigueSlider.maxValue = character.maxFatigue;
- fatigueSlider.value = character.currentFatigue;
- UnityEngine.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(fatigueSlider.GetComponent<RectTransform>());
- }
- public void UpdateExpBar()
- {
- if (expSlider == null || character == null) return;
-
- expSlider.value = character.experience;
- UnityEngine.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(expSlider.GetComponent<RectTransform>());
- }
- public void DisableButtonsTemporarily(float duration)
- {
- StartCoroutine(CooldownRoutine(duration));
- }
- private IEnumerator CooldownRoutine(float duration)
- {
- cooldownOverlay.gameObject.SetActive(true);
- SetButtonsInteractable(false);
- if (cooldownOverlay) cooldownOverlay.fillAmount = 1f;
- float elapsed = 0f;
- while (elapsed < duration)
- {
- elapsed += Time.deltaTime;
- float fill = Mathf.Clamp01(1f - elapsed / duration);
- if (cooldownOverlay) cooldownOverlay.fillAmount = fill;
- yield return null;
- }
- SetButtonsInteractable(true);
- if (cooldownOverlay) cooldownOverlay.fillAmount = 0f;
- }
- private void SetButtonsInteractable(bool interactable)
- {
- if (attackButton != null)
- attackButton.interactable = interactable;
- if (repeatActionButton != null)
- repeatActionButton.interactable = interactable;
- if (openSkillsButton != null)
- openSkillsButton.interactable = interactable;
- // Note: Inventory button should remain accessible during cooldown
-
- var fadedColor = interactable ? Color.white : new Color(1f, 1f, 1f, 0.5f);
- if (attackButton != null)
- attackButton.image.color = fadedColor;
- if (repeatActionButton != null)
- repeatActionButton.image.color = fadedColor;
- if (openSkillsButton != null)
- openSkillsButton.image.color = fadedColor;
- }
- public void HandleCharacterDeath()
- {
- string characterName = character.characterName;
-
- TeamCohesionManager cohesionManager = FindFirstObjectByType<TeamCohesionManager>();
- if (cohesionManager != null)
- {
- cohesionManager.groupMembers.Remove(character);
- }
-
- UIUpdater.Instance?.Unregister(characterName);
-
- Destroy(gameObject);
-
- if (cohesionManager != null && cohesionManager.groupMembers.Count == 0)
- {
- var gameManagerObj = GameObject.Find("GameManager");
- if (gameManagerObj != null)
- {
- gameManagerObj.SendMessage("OnPartyWiped", SendMessageOptions.DontRequireReceiver);
- }
- }
- }
- }
|