Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Spawner : MonoBehaviour
- {
- [SerializeField] private GameObject[] _templates;
- [SerializeField] private float _spawnDelay;
- [SerializeField] private Obstacle _obstacleTemplate;
- [Range(0, 100)]
- [SerializeField] private int _obstacleChance;
- private void Start()
- {
- StartCoroutine(Spawn());
- }
- private IEnumerator Spawn()
- {
- while (true)
- {
- if(Random.Range(0, 100) >= _obstacleChance)
- {
- int foodNumber = Random.Range(0, _templates.Length);
- Instantiate(_templates[foodNumber], transform.position, _templates[foodNumber].transform.rotation);
- }
- else
- {
- Instantiate(_obstacleTemplate, transform.position, _obstacleTemplate.transform.rotation);
- }
- yield return new WaitForSeconds(_spawnDelay);
- }
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public class Obstacle : MonoBehaviour
- {
- private Slicer _slicer;
- private void Awake()
- {
- _slicer = FindObjectOfType<Slicer>();
- }
- private void OnTriggerEnter(Collider other)
- {
- if(other.TryGetComponent(out BzKnife knife))
- {
- _slicer.StartStun();
- }
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public class Collector : MonoBehaviour
- {
- private int _collectedScores = 0;
- public event UnityAction<int> ScoresChanged;
- private void OnTriggerEnter(Collider other)
- {
- if(other.TryGetComponent(out Fruit fruit))
- {
- if (fruit.IsSliced)
- {
- _collectedScores += fruit.RewardScore;
- ScoresChanged?.Invoke(_collectedScores);
- }
- else
- {
- Debug.Log(other.gameObject.name + " no sliced");
- }
- Destroy(other.gameObject);
- }
- }
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public class ScoresView : MonoBehaviour
- {
- [SerializeField] private Collector _collector;
- [SerializeField] private Text _scoresText;
- [SerializeField] private float _changingSpeed;
- private int _currentScores = 0;
- private int _targetScores = 0;
- private void OnEnable()
- {
- _collector.ScoresChanged += OnScoresChanged;
- }
- private void OnDisable()
- {
- _collector.ScoresChanged -= OnScoresChanged;
- }
- private void OnScoresChanged(int value)
- {
- _targetScores = value;
- Debug.Log("targetScores " + value);
- }
- private void Update()
- {
- if(_currentScores != _targetScores)
- {
- _currentScores = Mathf.RoundToInt((float)_currentScores + (float)(_targetScores - _currentScores) * _changingSpeed * Time.deltaTime);
- _scoresText.text = _currentScores.ToString();
- }
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public class Fruit : MonoBehaviour, IBzObjectSlicedEvent
- {
- [SerializeField] private int _rewardScore;
- public bool IsSliced { get; private set; } = false;
- public int RewardScore => _rewardScore;
- public void ObjectSliced(GameObject original, GameObject resutlNeg, GameObject resultPos)
- {
- //Debug.Log("original " + original + " resutlNeg " + resutlNeg + " resultPos " + resultPos);
- IsSliced = true;
- }
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public class Slicer : MonoBehaviour
- {
- [SerializeField] private GameObject _blade;
- [SerializeField] private float _duration;
- [SerializeField] private float _offsetY;
- [Header("Stun")]
- [SerializeField] private float _stunDuration;
- [SerializeField] private float _stunPower;
- private BzKnife _knife;
- private Transform _slicerTransform;
- private Transform _bladeTransform;
- private float _time;
- private Vector3 _initPosition;
- private bool _isStunned = false;
- private float _stunnedTime = 0;
- private void Start()
- {
- _knife = _blade.GetComponentInChildren<BzKnife>();
- _slicerTransform = _blade.transform.parent;
- _bladeTransform = _blade.transform;
- _initPosition = _slicerTransform.position;
- _time = _duration;
- }
- private void Update()
- {
- _time += Time.deltaTime;
- if(_isStunned)
- {
- _stunnedTime += Time.deltaTime;
- if(_stunnedTime >= _stunDuration)
- {
- StopStun();
- }
- }
- if (_time > _duration && _isStunned == false)
- {
- if (Input.GetMouseButton(0))
- {
- _knife.BeginNewSlice();
- _blade.transform.parent.DOMoveY(_slicerTransform.position.y - _offsetY, _duration / 2f).SetLoops(2, LoopType.Yoyo).SetEase(Ease.InCubic);
- }
- _time = 0;
- }
- }
- public void StartStun()
- {
- if (_isStunned == true) return;
- _isStunned = true;
- _stunnedTime = 0;
- _bladeTransform.DOLocalMoveX(_bladeTransform.localPosition.x - _stunPower, _duration * 0.2f).SetLoops(6, LoopType.Yoyo).SetEase(Ease.InOutCubic);
- _slicerTransform.DOMoveY(_initPosition.y, _duration * 2).SetEase(Ease.OutCubic);
- }
- public void StopStun()
- {
- if (_isStunned == false) return;
- _isStunned = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement