Advertisement
Pro_Unit

Stamina

Feb 5th, 2023
1,122
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.67 KB | None | 0 0
  1. using Game.SaveStorages;
  2. using Game.SaveStorages.Paths;
  3. using Game.Tools.Extensions;
  4. using MalbersAnimations.Controller;
  5. using UnityEngine;
  6.  
  7. namespace Game.Stats.Stamina
  8. {
  9.     public sealed class Stamina : MonoBehaviour
  10.     {
  11.         [SerializeField, Min(1)] private int _default = 100;
  12.         [SerializeField, Min(1)] private int _degenerate = 2;
  13.         [SerializeField, Min(1)] private int _regenerate = 1;
  14.         [SerializeField] private Bar _view;
  15.         [SerializeField] private MAnimal _animal;
  16.  
  17.         private ISaveStorage<int> _maxValueStorage;
  18.         private Stat _stat;
  19.  
  20.         private void Awake()
  21.         {
  22.             _maxValueStorage = new BinaryStorage<int>(new Path(nameof(Stamina)));
  23.  
  24.             _stat = Stat();
  25.         }
  26.  
  27.         private void Update()
  28.         {
  29.             if (_animal.MovementDetected)
  30.             {
  31.                 if (_animal.Sprint)
  32.                     _stat.Subtract(_degenerate);
  33.             }
  34.             else
  35.             {
  36.                 _stat.Add(_regenerate);
  37.             }
  38.         }
  39.  
  40.         private Stat Stat()
  41.         {
  42.             int max = _maxValueStorage.HasSave()
  43.                 ? _maxValueStorage.Load()
  44.                 : _default;
  45.  
  46.             IStatChanged statEmpty = new StatEmpty(new StatEmptyCallback(DisableSprint));
  47.             IStatChanged statNotEmpty = new StatNotEmpty(new StatNotEmptyCallback(EnableSprint));
  48.             IStatChanged textView = new StatChangedCallback(_view.Visualize);
  49.             IStatChanged[] changes = { statEmpty, statNotEmpty, textView };
  50.             IStatChanged statChanged = new CompositeStatChanged(changes);
  51.  
  52.             return new Stat(max, statChanged);
  53.         }
  54.  
  55.         private void DisableSprint() => _animal.UseSprint = false;
  56.  
  57.         private void EnableSprint() => _animal.UseSprint = true;
  58.  
  59.         public void IncreaseMaxValue(int value)
  60.         {
  61.             value.ThrowExceptionIfLessThanZero();
  62.  
  63.             _maxValueStorage.Save(value);
  64.  
  65.             _stat = Stat();
  66.         }
  67.     }
  68. }
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement