93 lines
2.9 KiB
C#
93 lines
2.9 KiB
C#
|
|
using System.Collections;
|
||
|
|
using UnityEngine;
|
||
|
|
using UnityEngine.UI;
|
||
|
|
|
||
|
|
public class WeaponHUD : MonoBehaviour
|
||
|
|
{
|
||
|
|
public Text labTitleText;
|
||
|
|
public Text weaponNameText;
|
||
|
|
public Text ammoText;
|
||
|
|
public Text reloadText;
|
||
|
|
public Text hintsText;
|
||
|
|
public Text statsText;
|
||
|
|
public Text messageText;
|
||
|
|
public Text dataDrivenText;
|
||
|
|
|
||
|
|
private Coroutine messageRoutine;
|
||
|
|
|
||
|
|
private void Awake()
|
||
|
|
{
|
||
|
|
SetText(labTitleText, "Лабораторная работа №2 — Data-Driven система оружия");
|
||
|
|
SetText(hintsText, "ЛКМ — стрелять\nR — перезарядка\n1/2/3 или колесо мыши — смена оружия\nWASD + мышь — движение");
|
||
|
|
SetText(dataDrivenText, "Параметры оружия хранятся в ScriptableObject-ассетах в папке Assets/_Data. Для добавления нового оружия достаточно создать новый WeaponData и назначить его объекту Weapon.");
|
||
|
|
SetReloading(false);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void SetWeapon(WeaponData data)
|
||
|
|
{
|
||
|
|
if (data == null)
|
||
|
|
{
|
||
|
|
SetText(weaponNameText, "Weapon: None");
|
||
|
|
UpdateWeaponStats(null);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
SetText(weaponNameText, "Weapon: " + data.weaponName);
|
||
|
|
UpdateWeaponStats(data);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void SetAmmo(int currentAmmo, int reserve)
|
||
|
|
{
|
||
|
|
SetText(ammoText, "Ammo: " + currentAmmo + " / " + reserve);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void SetReloading(bool isReloading)
|
||
|
|
{
|
||
|
|
SetText(reloadText, isReloading ? "Reloading..." : "Ready");
|
||
|
|
}
|
||
|
|
|
||
|
|
public void ShowMessage(string message)
|
||
|
|
{
|
||
|
|
if (messageText == null)
|
||
|
|
return;
|
||
|
|
|
||
|
|
if (messageRoutine != null)
|
||
|
|
StopCoroutine(messageRoutine);
|
||
|
|
|
||
|
|
messageRoutine = StartCoroutine(MessageRoutine(message));
|
||
|
|
}
|
||
|
|
|
||
|
|
public void UpdateWeaponStats(WeaponData data)
|
||
|
|
{
|
||
|
|
if (data == null)
|
||
|
|
{
|
||
|
|
SetText(statsText, "Damage: -\nFire Rate: -\nSpread: -\nRecoil: -\nMagazine: -\nPellets: -");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
string stats =
|
||
|
|
"Damage: " + data.damage.ToString("0.##") +
|
||
|
|
"\nFire Rate: " + data.fireRate.ToString("0.##") + " sec" +
|
||
|
|
"\nSpread: " + data.spread.ToString("0.###") +
|
||
|
|
"\nRecoil: " + data.recoilForce.ToString("0.##") +
|
||
|
|
"\nMagazine: " + data.magazineSize +
|
||
|
|
"\nPellets: " + Mathf.Max(1, data.pelletsPerShot);
|
||
|
|
|
||
|
|
SetText(statsText, stats);
|
||
|
|
}
|
||
|
|
|
||
|
|
private IEnumerator MessageRoutine(string message)
|
||
|
|
{
|
||
|
|
SetText(messageText, message);
|
||
|
|
yield return new WaitForSeconds(2f);
|
||
|
|
SetText(messageText, string.Empty);
|
||
|
|
messageRoutine = null;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static void SetText(Text target, string value)
|
||
|
|
{
|
||
|
|
if (target != null)
|
||
|
|
target.text = value;
|
||
|
|
}
|
||
|
|
}
|