Dieton

PlayerBaseState

May 18th, 2023
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.90 KB | Gaming | 0 0
  1. //Warning script dosnt work
  2. public abstract class PlayerBaseState
  3. {
  4.     private bool _isRootState = false;
  5.     private PlayerStateMachine _ctx;
  6.     private PlayerStateFactory _factory;
  7.     private PlayerBaseState _currentSubState;
  8.     private PlayerBaseState _currentSuperState;
  9.  
  10.     protected bool IsRootState { set { _isRootState = value; } }
  11.     protected PlayerStateMachine Ctx { get { return _ctx; } }
  12.     protected PlayerStateFactory Factory { get { return _factory; } }
  13.  
  14.     public PlayerBaseState(PlayerStateMachine currentContext, PlayerStateFactory playerStateFactory) {
  15.         _ctx = currentContext;
  16.         _factory = playerStateFactory;
  17.     }
  18.  
  19.     public abstract void EnterState();
  20.  
  21.     public abstract void UpdateState();
  22.  
  23.     public abstract void ExitState();
  24.  
  25.     public abstract void CheckSwitchStates();
  26.  
  27.     public abstract void InitializeSubState();
  28.  
  29.     public void UpdateStates() {
  30.         UpdateState();
  31.         if (_currentSubState != null) {
  32.             _currentSubState.UpdateStates();
  33.         }
  34.     }
  35.     /*
  36.     public void ExitStates() {
  37.         ExitState();
  38.         if (_currentSubState != null) {
  39.             _currentSubState.ExitStates();
  40.         }
  41.     }
  42.     */
  43.     protected void SwitchState(PlayerBaseState newState) {
  44.         // current state exits state
  45.         ExitState();
  46.  
  47.         // new stste enters state
  48.         newState.EnterState();
  49.  
  50.         if (_isRootState){
  51.             // switch current state of context
  52.             _ctx.CurrentState = newState;
  53.         }else if (_currentSuperState != null) {
  54.             _currentSuperState.SetSubState(newState);
  55.         }
  56.     }
  57.  
  58.     protected void SetSuperState(PlayerBaseState newSuperState) {
  59.         _currentSuperState = newSuperState;
  60.     }
  61.  
  62.     protected void SetSubState(PlayerBaseState newSubState) {
  63.         _currentSubState = newSubState;
  64.         newSubState.SetSuperState(this);
  65.     }
  66. }
  67.  
Tags: Unity
Add Comment
Please, Sign In to add comment