/
KirRo
/
Test_1
Обзор
Документация
Войти
/
KirRo
/
Test_1
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Assets/_Project/Scripts/MoneySpawner.cs
166 строк
4 KB
KirRo
Правки. Разделена логика игрока, камеры, UI, Object Pool. Ожидание сервисов ввода и аудио
30 июл 2026, 22:37
30 июл 2026, 22:37
d3abbab
Код
Авторство
О чём код?
using System.Collections.Generic; using UnityEngine; public class MoneySpawner : MonoBehaviour { public delegate void MoneyCollectedHandler(int value); public event MoneyCollectedHandler MoneyCollected; public delegate void SpawningCompletedHandler(); public event SpawningCompletedHandler SpawningCompleted; [SerializeField] private MoneyObjectPool _moneyObjectPool; [SerializeField] private Transform[] _spawnPoints; [SerializeField] private int _maxCollectibles = 10; [SerializeField] private int _totalSpawnCount = 15; [SerializeField] private float _spawnInterval = 2f; [SerializeField] private float _moneyBagSpawnChance = 0.15f; [SerializeField] private AudioClip _spawnSound; private readonly Dictionary<Transform, CollectibleMoney> _spawnedCollectiblesByPoint = new(); private readonly Dictionary<CollectibleMoney, Transform> _spawnPointByCollectible = new(); private readonly List<Transform> _freeSpawnPoints = new(); private int _spawnedTotalCount; private float _spawnTimer; private bool _isCompleted; private void Update() { CompleteSpawningIfNeeded(); UpdateSpawnTimer(); } private void UpdateSpawnTimer() { if (_isCompleted || _spawnedTotalCount >= _totalSpawnCount) { return; } if (_spawnedCollectiblesByPoint.Count >= _maxCollectibles) { return; } _spawnTimer += Time.deltaTime; if (_spawnTimer < _spawnInterval) { return; } if (!TrySpawnCollectible()) { return; } _spawnTimer = 0f; } private bool TrySpawnCollectible() { var spawnPoint = GetRandomFreeSpawnPoint(); if (spawnPoint == null) { return false; } var collectibleMoney = GetRandomCollectible(spawnPoint.position, spawnPoint.rotation); if (collectibleMoney == null) { return false; } collectibleMoney.Collected += OnCollectibleCollected; _spawnedCollectiblesByPoint.Add(spawnPoint, collectibleMoney); _spawnPointByCollectible.Add(collectibleMoney, spawnPoint); _spawnedTotalCount++; PlaySpawnSound(collectibleMoney.transform.position); return true; } private Transform GetRandomFreeSpawnPoint() { _freeSpawnPoints.Clear(); foreach (var spawnPoint in _spawnPoints) { if (spawnPoint == null || _spawnedCollectiblesByPoint.ContainsKey(spawnPoint)) { continue; } _freeSpawnPoints.Add(spawnPoint); } if (_freeSpawnPoints.Count == 0) { return null; } var randomIndex = UnityEngine.Random.Range(0, _freeSpawnPoints.Count); return _freeSpawnPoints[randomIndex]; } private CollectibleMoney GetRandomCollectible(Vector3 position, Quaternion rotation) { var shouldSpawnMoneyBag = UnityEngine.Random.value < _moneyBagSpawnChance; if (shouldSpawnMoneyBag) { var moneyBag = _moneyObjectPool.GetMoneyBag(position, rotation); if (moneyBag != null) { return moneyBag; } } return _moneyObjectPool.GetCashPack(position, rotation); } private void OnCollectibleCollected(CollectibleMoney money, int value) { money.Collected -= OnCollectibleCollected; if (_spawnPointByCollectible.TryGetValue(money, out var spawnPoint)) { _spawnedCollectiblesByPoint.Remove(spawnPoint); _spawnPointByCollectible.Remove(money); } _moneyObjectPool.Return(money); MoneyCollected?.Invoke(value); } private void PlaySpawnSound(Vector3 position) { if (_spawnSound == null) { return; } AudioSource.PlayClipAtPoint(_spawnSound, position); } private void CompleteSpawningIfNeeded() { if (_isCompleted) { return; } if (_spawnedTotalCount < _totalSpawnCount || _spawnedCollectiblesByPoint.Count > 0) { return; } _isCompleted = true; SpawningCompleted?.Invoke(); } }