Advertisement
BlackWindX

LevelLoopState (SRP)

Dec 16th, 2023
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.64 KB | None | 0 0
  1. using CodeBase.Gameplay.BaseBlock;
  2. using CodeBase.Gameplay.HoistingRopeLogic;
  3. using CodeBase.Gameplay.Obstacle;
  4. using CodeBase.Gameplay.Services;
  5. using CodeBase.Gameplay.Services.BlockBind;
  6. using CodeBase.Gameplay.Services.BlockMiss;
  7. using CodeBase.Gameplay.Services.BlockReleaseTimer;
  8. using CodeBase.Gameplay.Services.Collision;
  9. using CodeBase.Gameplay.Services.Combo;
  10. using CodeBase.Gameplay.Stability;
  11. using CodeBase.Infrastructure.States;
  12. using UnityEngine.InputSystem;
  13.  
  14. namespace CodeBase.Gameplay.States
  15. {
  16.     public class LevelLoopState : ILevelState, IState
  17.     {
  18.         private readonly LevelStateMachine _stateMachine;
  19.         private readonly BlockBinder _blockBinder;
  20.         private readonly ReleaseTimerController _releaseTimerController;
  21.         private readonly StabilityController _stabilityController;
  22.         private readonly CollisionObserver _collisionObserver;
  23.         private readonly HoistingRope _hoistingRope;
  24.         private readonly MissChecker _missChecker;
  25.         private readonly ObstacleValidator _obstacleValidator;
  26.         private readonly LandingController _landingController;
  27.         private readonly OffsetChecker _offsetChecker;
  28.         private readonly ComboChecker _comboChecker;
  29.         private readonly ReleasedBlockTracker _releasedBlockTracker;
  30.  
  31.         private InputActions.GameplayActions _gameplayInput;
  32.  
  33.         public LevelLoopState(
  34.             LevelStateMachine stateMachine,
  35.             InputActions inputActions,
  36.             BlockBinder blockBinder,
  37.             ReleaseTimerController releaseTimerController,
  38.             StabilityController stabilityController,
  39.             CollisionObserver collisionObserver,
  40.             HoistingRope hoistingRope,
  41.             MissChecker missChecker,
  42.             ObstacleValidator obstacleValidator,
  43.             LandingController landingController,
  44.             OffsetChecker offsetChecker,
  45.             ComboChecker comboChecker,
  46.             ReleasedBlockTracker releasedBlockTracker
  47.         )
  48.         {
  49.             _stateMachine = stateMachine;
  50.             _gameplayInput = inputActions.Gameplay;
  51.             _blockBinder = blockBinder;
  52.             _releaseTimerController = releaseTimerController;
  53.             _stabilityController = stabilityController;
  54.             _collisionObserver = collisionObserver;
  55.  
  56.             _hoistingRope = hoistingRope;
  57.             _missChecker = missChecker;
  58.             _obstacleValidator = obstacleValidator;
  59.             _landingController = landingController;
  60.             _offsetChecker = offsetChecker;
  61.             _comboChecker = comboChecker;
  62.             _releasedBlockTracker = releasedBlockTracker;
  63.         }
  64.  
  65.         public void Enter()
  66.         {
  67.             _hoistingRope.OnReleased += OnBlockReleased;
  68.             _collisionObserver.OnCollision += OnBlockCollision;
  69.             _missChecker.OnMiss += OnBlockMissed;
  70.  
  71.             EnableInput();
  72.         }
  73.  
  74.         public void Exit()
  75.         {
  76.             _hoistingRope.OnReleased -= OnBlockReleased;
  77.             _collisionObserver.OnCollision -= OnBlockCollision;
  78.             _missChecker.OnMiss -= OnBlockMissed;
  79.  
  80.             DisableInput();
  81.         }
  82.  
  83.         private void OnBlockReleased(Block block) =>
  84.             _releasedBlockTracker.StartTracking(block);
  85.  
  86.         private void OnBlockCollision(Block block, IObstacle obstacle)
  87.         {
  88.             _releasedBlockTracker.StopTracking();
  89.             _releaseTimerController.Stop();
  90.             HandleBlockCollision(block, obstacle);
  91.         }
  92.  
  93.         private void HandleBlockCollision(Block block, IObstacle obstacle)
  94.         {
  95.             bool isCorrectObstacle = _obstacleValidator.IsCorrectObstacle(obstacle);
  96.             OffsetCheckerResult offset = _offsetChecker.IsPermissibleOffset(obstacle, block.transform.position);
  97.  
  98.             if (!isCorrectObstacle || !offset.isPermissible)
  99.             {
  100.                 FailGameByCrash(block, offset);
  101.                 return;
  102.             }
  103.  
  104.             LandBlock(block, offset);
  105.            
  106.             if (!_stabilityController.IsStable)
  107.                 FailGameByTowerCollapse();
  108.             else
  109.                 NextLoopStep();
  110.         }
  111.  
  112.         private void FailGameByCrash(Block block, OffsetCheckerResult offset)
  113.         {
  114.             _landingController.LandWithCrash(block, offset.direction);
  115.             SwitchToFailState();
  116.         }
  117.  
  118.         private void LandBlock(Block block, OffsetCheckerResult offset)
  119.         {
  120.             bool isCombo = _comboChecker.CheckCombo(offset.percent);
  121.  
  122.             _landingController.Land(block, isCombo);
  123.             _stabilityController.Change(offset.percent, isCombo);
  124.         }
  125.  
  126.         private void FailGameByTowerCollapse() =>
  127.             SwitchToFailState();
  128.  
  129.         private void NextLoopStep()
  130.         {
  131.             _blockBinder.BindNext();
  132.             _releaseTimerController.Start();
  133.         }
  134.  
  135.         private void SwitchToFailState() =>
  136.             _stateMachine.Enter<LevelFailState>();
  137.  
  138.         private void OnBlockMissed() =>
  139.             SwitchToFailState();
  140.  
  141.         private void EnableInput()
  142.         {
  143.             _gameplayInput.Tap.performed += OnPlayerTap;
  144.             _gameplayInput.Enable();
  145.         }
  146.  
  147.         private void DisableInput()
  148.         {
  149.             _gameplayInput.Tap.performed -= OnPlayerTap;
  150.             _gameplayInput.Disable();
  151.         }
  152.  
  153.         private void OnPlayerTap(InputAction.CallbackContext ctx)
  154.         {
  155.             if (IsPlayerInputBlocked())
  156.                 return;
  157.            
  158.             _blockBinder.Unbind();
  159.         }
  160.  
  161.         private bool IsPlayerInputBlocked() =>
  162.             !_blockBinder.IsCanUnbind;
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement