Run-and-capture/Assets/Scripts/UI/PlayerUIController.cs
2021-08-30 17:47:26 +03:00

153 lines
4.5 KiB
C#

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;
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<PlayerState>();
_targetingSystem = GetComponent<ActionTargetingSystem>();
_triggerSystem = GetComponent<ActionTriggerSystem>();
_captureController = GetComponent<CaptureController>();
_healthController = GetComponent<HealthController>();
_attackEnergy = GetComponent<AttackEnergyController>();
_targetingSystem.OnFoundTarget += UpdateAttackUI;
_targetingSystem.OnLostTarget += StopUpdateAttackUI;
_playerState.OnCharStateChanged += StartUpdatingProgressUI;
_healthController.OnHealthChanged += UpdateHealthUI;
_attackEnergy.OnAttackEnergyChanged += UpdateEnergyUI;
}
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.Build:
actionTypeText = "Building...";
GetActionProgress += GetCurrentActionProgress;
break;
case CharacterState.TreeAttack:
actionTypeText = "Attack";
GetActionProgress += GetCurrentActionProgress;
break;
default:
return;
}
_progressUI.UpdateActionType(actionTypeText);
StartCoroutine(_progressUICoroutine);
}
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 StopUpdateAttackUI()
{
_attackUI.gameObject.SetActive(false);
}
private void UpdateAttackUI(TileInfo target)
{
Vector3 targetPos = target.tilePosition;
if (targetPos != null)
{
_attackUI.gameObject.SetActive(true);
_attackUI.LookAt(targetPos);
}
}
}