/
Lexan97
/
Cowbeat
Обзор
Документация
Войти
/
Lexan97
/
Cowbeat
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Assets/Scripts/Player/RevolverController.cs
169 строк
5 KB
Lexa97
Final Commit gor present
26 май 2026, 23:40
26 май 2026, 23:40
70e9bdb
Код
Авторство
О чём код?
using UnityEngine; using UnityEngine.UI; // Обязательно для работы с Image using System.Collections; using TMPro; public class RevolverController : MonoBehaviour { [Header("Dependencies")] public AimController aimController; public Transform camTransform; public Image muzzleFlashImage; // Теперь это Image из Canvas public GameObject damageTextPrefab; public Animator playerAnimator; [Header("Weapon Settings")] public int baseDamage = 25; public int minDamageOffset = 3; public int maxDamageOffset = 4; public float range = 100f; public LayerMask shootableLayers; [Header("Fire Rate Settings")] public float fireRateDelay = 0.5f; private float lastShootTime; [Header("Ammo & System")] public int maxDrumAmmo = 6; public int currentDrumAmmo; public int totalInventoryAmmo = 24; public float reloadTime = 2.0f; public TextMeshProUGUI ammoTextUI; public GameObject reloadingTextUI; private bool isReloading = false; private Coroutine reloadCoroutine; void Awake() { if (reloadingTextUI != null) reloadingTextUI.SetActive(false); // Скрываем картинку при старте if (muzzleFlashImage != null) SetFlashAlpha(0f); } void Start() { currentDrumAmmo = maxDrumAmmo; UpdateAmmoUI(); if (playerAnimator == null) playerAnimator = GetComponentInParent<Animator>(); } void OnDisable() { ForceResetReload(); } void Update() { UpdateAmmoUI(); if (ShopManager.IsShopOpen || !PlayerInventory.Instance.hasRevolver) return; if (aimController == null || !aimController.isAiming) { if (isReloading) ForceResetReload(); return; } if (Input.GetMouseButtonDown(0) && !isReloading) { if (currentDrumAmmo > 0) { if (Time.time >= lastShootTime + fireRateDelay) { Shoot(); } } } if (Input.GetKeyDown(KeyCode.R) && !isReloading && currentDrumAmmo < maxDrumAmmo && totalInventoryAmmo > 0) { reloadCoroutine = StartCoroutine(ReloadRoutine()); } } void Shoot() { lastShootTime = Time.time; currentDrumAmmo--; // Запускаем эффект картинки if (muzzleFlashImage != null) StartCoroutine(FlashEffectRoutine()); if (playerAnimator != null && playerAnimator.runtimeAnimatorController != null) { playerAnimator.SetTrigger("Shoot"); } if (Physics.Raycast(camTransform.position, camTransform.forward, out RaycastHit hit, range, shootableLayers)) { EnemyHealth enemy = hit.collider.GetComponent<EnemyHealth>(); if (enemy != null) { int finalDamage = baseDamage + Random.Range(-minDamageOffset, maxDamageOffset + 1); enemy.TakeDamage(finalDamage, Random.value > 0.5f, false); } } } // Корутина для плавного затухания IEnumerator FlashEffectRoutine() { float duration = 0.1f; // Как быстро она исчезает SetFlashAlpha(1f); // Мгновенно включаем yield return new WaitForSeconds(0.05f); // Маленькая задержка перед угасанием float elapsed = 0f; while (elapsed < duration) { elapsed += Time.deltaTime; float alpha = Mathf.Lerp(1f, 0f, elapsed / duration); SetFlashAlpha(alpha); yield return null; } SetFlashAlpha(0f); } void SetFlashAlpha(float alpha) { Color c = muzzleFlashImage.color; c.a = alpha; muzzleFlashImage.color = c; } IEnumerator ReloadRoutine() { isReloading = true; if (reloadingTextUI != null) reloadingTextUI.SetActive(true); yield return new WaitForSeconds(reloadTime); int bulletsToLoad = Mathf.Min(maxDrumAmmo - currentDrumAmmo, totalInventoryAmmo); currentDrumAmmo += bulletsToLoad; totalInventoryAmmo -= bulletsToLoad; isReloading = false; reloadCoroutine = null; if (reloadingTextUI != null) reloadingTextUI.SetActive(false); } private void ForceResetReload() { if (reloadCoroutine != null) { StopCoroutine(reloadCoroutine); reloadCoroutine = null; } isReloading = false; if (reloadingTextUI != null) reloadingTextUI.SetActive(false); } private void UpdateAmmoUI() { if (ammoTextUI != null) ammoTextUI.text = $"{currentDrumAmmo} / {totalInventoryAmmo}"; } }