Advertisement
sergezhu

Untitled

Dec 16th, 2020
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.80 KB | None | 0 0
  1. public class Spawner : MonoBehaviour
  2. {
  3.     [SerializeField] private GameObject[] _templates;
  4.     [SerializeField] private float _spawnDelay;
  5.  
  6.     [SerializeField] private Obstacle _obstacleTemplate;
  7.     [Range(0, 100)]
  8.     [SerializeField] private int _obstacleChance;
  9.  
  10.     private void Start()
  11.     {
  12.         StartCoroutine(Spawn());
  13.     }
  14.  
  15.     private IEnumerator Spawn()
  16.     {
  17.         while (true)
  18.         {
  19.             if(Random.Range(0, 100) >= _obstacleChance)
  20.             {
  21.                 int foodNumber = Random.Range(0, _templates.Length);
  22.                 Instantiate(_templates[foodNumber], transform.position, _templates[foodNumber].transform.rotation);
  23.             }
  24.             else
  25.             {
  26.                 Instantiate(_obstacleTemplate, transform.position, _obstacleTemplate.transform.rotation);
  27.             }
  28.  
  29.             yield return new WaitForSeconds(_spawnDelay);
  30.         }
  31.     }
  32. }
  33. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  34.  
  35. public class Obstacle : MonoBehaviour
  36. {
  37.     private Slicer _slicer;
  38.  
  39.     private void Awake()
  40.     {
  41.         _slicer = FindObjectOfType<Slicer>();
  42.     }
  43.     private void OnTriggerEnter(Collider other)
  44.     {
  45.         if(other.TryGetComponent(out BzKnife knife))
  46.         {
  47.             _slicer.StartStun();
  48.         }
  49.     }
  50. }
  51. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  52.  
  53. public class Collector : MonoBehaviour
  54. {
  55.     private int _collectedScores = 0;
  56.  
  57.     public event UnityAction<int> ScoresChanged;
  58.  
  59.     private void OnTriggerEnter(Collider other)
  60.     {
  61.         if(other.TryGetComponent(out Fruit fruit))
  62.         {
  63.  
  64.             if (fruit.IsSliced)
  65.             {
  66.                 _collectedScores += fruit.RewardScore;
  67.                 ScoresChanged?.Invoke(_collectedScores);
  68.             }
  69.             else
  70.             {
  71.                 Debug.Log(other.gameObject.name +  " no sliced");
  72.             }
  73.            
  74.  
  75.             Destroy(other.gameObject);
  76.         }
  77.     }
  78. }
  79. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  80.  
  81. public class ScoresView : MonoBehaviour
  82. {
  83.     [SerializeField] private Collector _collector;
  84.     [SerializeField] private Text _scoresText;
  85.     [SerializeField] private float _changingSpeed;
  86.  
  87.     private int _currentScores = 0;
  88.     private int _targetScores = 0;
  89.  
  90.     private void OnEnable()
  91.     {
  92.         _collector.ScoresChanged += OnScoresChanged;
  93.     }
  94.     private void OnDisable()
  95.     {
  96.         _collector.ScoresChanged -= OnScoresChanged;
  97.     }
  98.  
  99.     private void OnScoresChanged(int value)
  100.     {
  101.         _targetScores = value;
  102.         Debug.Log("targetScores " + value);
  103.     }
  104.  
  105.     private void Update()
  106.     {
  107.         if(_currentScores != _targetScores)
  108.         {
  109.             _currentScores = Mathf.RoundToInt((float)_currentScores + (float)(_targetScores - _currentScores) * _changingSpeed * Time.deltaTime);
  110.             _scoresText.text = _currentScores.ToString();
  111.         }
  112.     }
  113. }
  114. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  115.  
  116. public class Fruit : MonoBehaviour, IBzObjectSlicedEvent
  117. {
  118.     [SerializeField] private int _rewardScore;
  119.  
  120.     public bool IsSliced { get; private set; } = false;
  121.  
  122.     public int RewardScore => _rewardScore;
  123.  
  124.     public void ObjectSliced(GameObject original, GameObject resutlNeg, GameObject resultPos)
  125.     {
  126.         //Debug.Log("original " + original + "   resutlNeg " + resutlNeg + "   resultPos " + resultPos);
  127.         IsSliced = true;
  128.     }
  129. }
  130. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  131.  
  132. public class Slicer : MonoBehaviour
  133. {
  134.     [SerializeField] private GameObject _blade;
  135.     [SerializeField] private float _duration;
  136.     [SerializeField] private float _offsetY;
  137.  
  138.     [Header("Stun")]
  139.     [SerializeField] private float _stunDuration;
  140.     [SerializeField] private float _stunPower;
  141.  
  142.     private BzKnife _knife;
  143.     private Transform _slicerTransform;
  144.     private Transform _bladeTransform;
  145.     private float _time;
  146.  
  147.     private Vector3 _initPosition;
  148.     private bool _isStunned = false;
  149.     private float _stunnedTime = 0;
  150.  
  151.     private void Start()
  152.     {
  153.         _knife = _blade.GetComponentInChildren<BzKnife>();
  154.         _slicerTransform = _blade.transform.parent;
  155.         _bladeTransform = _blade.transform;
  156.         _initPosition = _slicerTransform.position;
  157.         _time = _duration;
  158.     }
  159.  
  160.     private void Update()
  161.     {
  162.         _time += Time.deltaTime;
  163.  
  164.         if(_isStunned)
  165.         {
  166.             _stunnedTime += Time.deltaTime;
  167.  
  168.             if(_stunnedTime >= _stunDuration)
  169.             {
  170.                 StopStun();
  171.             }
  172.         }
  173.  
  174.         if (_time > _duration && _isStunned == false)
  175.         {
  176.             if (Input.GetMouseButton(0))
  177.             {
  178.                 _knife.BeginNewSlice();  
  179.  
  180.                 _blade.transform.parent.DOMoveY(_slicerTransform.position.y - _offsetY, _duration / 2f).SetLoops(2, LoopType.Yoyo).SetEase(Ease.InCubic);
  181.             }
  182.  
  183.             _time = 0;
  184.         }    
  185.     }
  186.  
  187.     public void StartStun()
  188.     {
  189.         if (_isStunned == true) return;
  190.        
  191.         _isStunned = true;
  192.         _stunnedTime = 0;
  193.  
  194.         _bladeTransform.DOLocalMoveX(_bladeTransform.localPosition.x - _stunPower, _duration * 0.2f).SetLoops(6, LoopType.Yoyo).SetEase(Ease.InOutCubic);
  195.         _slicerTransform.DOMoveY(_initPosition.y, _duration * 2).SetEase(Ease.OutCubic);
  196.     }
  197.  
  198.     public void StopStun()
  199.     {
  200.         if (_isStunned == false) return;
  201.  
  202.         _isStunned = false;
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement