Advertisement
SuperMeatBoy

MovementAction extension

Aug 1st, 2023 (edited)
803
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.84 KB | None | 0 0
  1. namespace HexGame.Actions
  2. {
  3.     [Serializable]
  4.     public class Movement : MovementAction
  5.     {
  6.         [Inject] private IUnit _unit;
  7.         [Inject] private BoardView _view;
  8.         [Inject] private Board.Board _board;
  9.  
  10.         [SerializeField] private float _duration;
  11.         [SerializeField] private bool _activate;
  12.         [SerializeField] private bool _triggerMoveEffects;
  13.         [SerializeField] private bool _smoothAnimation;
  14.  
  15.         public override async UniTask Execute(Offset position, CancellationToken cancellationToken)
  16.         {
  17.             _board.SetStateChangedFlag();
  18.             if(_triggerMoveEffects) await _board.InvokeLifeCycleTrigger(Trigger.OnPreMove, cancellationToken);
  19.  
  20.             var pointView = _view.GetNodeView(position);
  21.             var node = _board.Nodes.First(node => node.Position == position);
  22.             if (_smoothAnimation)
  23.             {
  24.                 _view.UnitView.transform.DOMove(pointView.transform.position, _duration)
  25.                     .ToUniTask(cancellationToken: cancellationToken)
  26.                     .Forget();
  27.             }
  28.             else
  29.             {
  30.                 //instant "teleportation"
  31.                 _view.UnitView.transform.position = pointView.transform.position;
  32.             }
  33.            
  34.             //check if position has changed?
  35.             _unit.Position = position;
  36.  
  37.             var exhaust = _activate && !node.HasState(NodeStateFlags.Exhausted);
  38.  
  39.             if (exhaust) await _board.ChangeStateAsync(node, NodeState.Exhausted, cancellationToken);
  40.  
  41.             if (exhaust && node.TryGet(Trigger.OnActivation, out var action))
  42.             {
  43.                 await action.Execute(cancellationToken);
  44.             }
  45.  
  46.             if(_triggerMoveEffects) await _board.InvokeLifeCycleTrigger(Trigger.OnPostMove, cancellationToken);
  47.         }
  48.     }
  49. }
Tags: NetCrawl
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement