164 lines
4.0 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class MissileAimView : MonoBehaviour
{
[SerializeField] private MissileAim aimSource;
//[SerializeField] private TextMeshProUGUI noTargetLabel;
[SerializeField] private CanvasGroup reloadGroup;
[SerializeField] private Image reloadBar;
[SerializeField] private TextMeshProUGUI statusLabel;
[SerializeField] private Color NoTargetColor = Color.red;
[SerializeField] private Color HasTargetColor = Color.green;
[SerializeField] private Color ClearColor = Color.clear;
//[SerializeField] private TextMeshProUGUI ammoLabel;
private Transform lastTarget = null;
private EnemyShip lastEnemySheep;
private MissileWeapon weapon;
private bool initialized = false;
private bool reloadAnimationEnabled = false;
private bool currentTargetOutlined = false;
public void Initialilze()
{
if (G.WeaponModel.Weapons[1].TryGetComponent(out MissileWeapon missile))
{
weapon = missile;
initialized = true;
}
else
{
Debug.Log("Не найдено ракетное оружие");
initialized = false;
}
RefreshAmmo(weapon.Ammo);
weapon.AmmoChanged += RefreshAmmo;
}
private void Update()
{
if (!initialized) return;
Refresh();
}
private void Refresh()
{
if (weapon.ReloadPercent < 1 && !reloadAnimationEnabled)
{
EnableReloadAnimation();
}
if (reloadAnimationEnabled && weapon.ReloadPercent >= 1)
{
DisableReloadAnimation();
}
Transform target = aimSource.Target;
bool hasTarget = target != null;
bool targetChanged = target != lastTarget;
bool canSetOutline = !reloadAnimationEnabled;
if (targetChanged)
{
currentTargetOutlined = false;
var ship = GetNewShip(target);
lastEnemySheep?.HideOutline();
lastEnemySheep = ship;
lastTarget = target;
}
if(!currentTargetOutlined && canSetOutline)
{
lastEnemySheep?.ShowOutline();
currentTargetOutlined = true;
}
if (reloadAnimationEnabled)
{
statusLabel.color = ClearColor;
}
else
{
if (hasTarget)
{
statusLabel.color = HasTargetColor;
statusLabel.text = "Цель захвачена";
}
else
{
statusLabel.color = NoTargetColor;
statusLabel.text = "Нет цели";
}
}
}
private EnemyShip GetNewShip(Transform target)
{
if (target == null) return null;
return target.GetComponent<EnemyShip>();
}
private void OnEnable()
{
reloadGroup.alpha = 0;
}
private void OnDisable()
{
lastEnemySheep?.HideOutline();
lastEnemySheep = null;
lastTarget = null;
reloadAnimationEnabled = false;
}
private void OnDestroy()
{
if (!initialized) return;
weapon.AmmoAdded -= RefreshAmmo;
}
private void EnableReloadAnimation()
{
StopAllCoroutines();
reloadAnimationEnabled = true;
StartCoroutine(ReloadAnimation());
}
private void DisableReloadAnimation()
{
reloadAnimationEnabled = false;
}
IEnumerator ReloadAnimation()
{
while (reloadAnimationEnabled)
{
reloadGroup.alpha = Mathf.Sin(Time.time * 5) / 2 + 0.5f;
reloadBar.fillAmount = weapon.ReloadPercent;
yield return null;
}
while (reloadGroup.alpha > 0)
{
reloadGroup.alpha -= Time.deltaTime;
}
}
private void RefreshAmmo(int ammo)
{
lastEnemySheep?.HideOutline();
currentTargetOutlined = false;
//ammoLabel.text = $"Ракеты: {ammo}";
}
}