using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerUIController : MonoBehaviour { public float updateRate = 0.01f; [SerializeField] private Transform _attackUI; [SerializeField] private UI_Progress _progressUI; [SerializeField] private UI_Quantity _healthUI; [SerializeField] private UI_Quantity _attackEnergyUI; [SerializeField] private UI_SuperJump _superJumpUI; private PlayerState _playerState; private ActionTargetingSystem _targetingSystem; private ActionTriggerSystem _triggerSystem; private CaptureController _captureController; private HealthController _healthController; private AttackEnergyController _attackEnergy; private IEnumerator _progressUICoroutine; private delegate float GetProgress(); private GetProgress GetActionProgress; private void Awake() { _playerState = GetComponent(); _targetingSystem = GetComponent(); _triggerSystem = GetComponent(); _captureController = GetComponent(); _healthController = GetComponent(); _attackEnergy = GetComponent(); _targetingSystem.OnFoundTarget += UpdateActionUI; _targetingSystem.OnLostTarget += StopUpdateActionUI; _playerState.OnCharStateChanged += StartUpdatingProgressUI; _healthController.OnHealthChanged += UpdateHealthUI; _attackEnergy.OnAttackEnergyChanged += UpdateEnergyUI; } private void UpdateActionUI(TileInfo target, ActionType actionType) { switch (actionType) { case ActionType.SuperJump: _superJumpUI.UpdateUI(target); break; default: UpdateAttackUI(target); break; } } private void UpdateAttackUI(TileInfo target) { Vector3 targetPos = target.tilePosition; if (targetPos != null) { _attackUI.gameObject.SetActive(true); _attackUI.LookAt(targetPos); } } private void UpdateEnergyUI(float curEnergy, float maxEnergy) { _attackEnergyUI.UpdateBar(curEnergy, maxEnergy); } private void UpdateHealthUI(float curHealth, float maxHealth) { _healthUI.UpdateBar(curHealth, maxHealth); } private void StartUpdatingProgressUI(CharacterState newState) { //Debug.Log("UPDATING UI"); GetActionProgress = null; _progressUI.gameObject.SetActive(true); _progressUICoroutine = UpdateProgressBar(updateRate); string actionTypeText = ""; switch (newState) { /*case CharacterState.Attack: actionTypeText = "Attack"; GetActionProgress += GetCurrentActionProgress; break;*/ case CharacterState.Capture: actionTypeText = "Capturing..."; GetActionProgress += GetCaptureProgress; break; case CharacterState.Move: StopUpdateProgressUI(); return; case CharacterState.Idle: StopUpdateProgressUI(); return; case CharacterState.Action: actionTypeText = GetActionText(_playerState); GetActionProgress += GetCurrentActionProgress; break; /*case CharacterState.Build: actionTypeText = "Building..."; GetActionProgress += GetCurrentActionProgress; break; case CharacterState.TreeAttack: actionTypeText = "Attack"; GetActionProgress += GetCurrentActionProgress; break;*/ default: return; } _progressUI.UpdateActionType(actionTypeText); StartCoroutine(_progressUICoroutine); } private string GetActionText(PlayerState player) { var charSubState = player.currentAction.actionType; string result = ""; switch (charSubState) { case ActionType.Attack: result = "Attack"; break; case ActionType.Build: result = "Build..."; break; case ActionType.TreeAttack: result = "TreeAttack"; break; case ActionType.SuperJump: result = "SuperJump"; break; } return result; } private void StopUpdateProgressUI() { _progressUI.StopUpdateUI(); if (_progressUICoroutine != null) { StopCoroutine(_progressUICoroutine); _progressUICoroutine = null; } _progressUI.gameObject.SetActive(false); } private float GetCurrentActionProgress() { return _triggerSystem.GetActionProgress(); } private float GetCaptureProgress() { return _captureController.GetCaptureProgress(); } private IEnumerator UpdateProgressBar(float updateRate) { float currentActionProgress = 0f; while (true) { if (GetActionProgress != null) { currentActionProgress = GetActionProgress.Invoke(); } _progressUI.UpdateUI(currentActionProgress); yield return new WaitForSeconds(updateRate); } } private void StopUpdateActionUI() { _attackUI.gameObject.SetActive(false); _superJumpUI.StopUpdateUI() ; } }