Pro_Unit

TargetingStateSwitcher

Jul 22nd, 2021 (edited)
362
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using AnimalSimulator.Gameplay;
  2. using AnimalSimulator.Gameplay.Input;
  3. using AnimalSimulator.Installers;
  4.  
  5. using UniRx;
  6.  
  7. using Zenject;
  8.  
  9. using IState = AnimalSimulator.AI.States.IState;
  10.  
  11. namespace AnimalSimulator.Targeting
  12. {
  13.     public class TargetingStateSwitcher : ITickable, IInitializable
  14.  
  15.     {
  16.         private readonly GameControl _control;
  17.         private readonly TargetAnimalFinder _targetAnimalFinder;
  18.  
  19.         public readonly Subject<Unit> OnFreeTarget = new Subject<Unit>();
  20.         public readonly Subject<Animal> OnLockTarget = new Subject<Animal>();
  21.  
  22.         public bool Targeting { get; private set; }
  23.  
  24.         public TargetingStateSwitcher(GameControl control, TargetAnimalFinder targetAnimalFinder)
  25.         {
  26.             _control = control;
  27.             _targetAnimalFinder = targetAnimalFinder;
  28.         }
  29.  
  30.         public void Initialize() =>
  31.             FreeState();
  32.  
  33.         public void Tick()
  34.         {
  35.             if (_control.Camera.LockTarget.triggered)
  36.                 ToggleTargetingState();
  37.         }
  38.  
  39.         private void ToggleTargetingState()
  40.         {
  41.             Targeting = !Targeting;
  42.  
  43.             if (Targeting)
  44.             {
  45.                 Animal potentialTarget = _targetAnimalFinder.PotentialTarget.Value;
  46.  
  47.                 if (potentialTarget == null)
  48.                 {
  49.                     Targeting = false;
  50.                     return;
  51.                 }
  52.  
  53.                 OnLockTarget.OnNext(potentialTarget);
  54.             }
  55.             else
  56.             {
  57.                 FreeState();
  58.             }
  59.         }
  60.  
  61.         private void FreeState() =>
  62.             OnFreeTarget.OnNext(Unit.Default);
  63.  
  64.         public void DisableLock()
  65.         {
  66.             Targeting = false;
  67.             FreeState();
  68.         }
  69.     }
  70. }
RAW Paste Data