Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3. using System.Collections.Generic;
  4.  
  5. public class GameStateManager : Manager<GameStateManager>
  6. {
  7. private State _currentState = null;
  8. private Stack<State> m_pActiveStates;
  9. private Dictionary<string, Type> registeredStates;
  10.  
  11. State temp;
  12.  
  13. public State CurrentState
  14. {
  15. get
  16. {
  17. return _currentState;
  18. }
  19. }
  20.  
  21. private GameStateManager()
  22. {
  23. m_pActiveStates = new Stack<State>();
  24. }
  25.  
  26. protected override void Terminate()
  27. {
  28.  
  29. }
  30.  
  31. public void Update()
  32. {
  33. float deltaTime = Time.deltaTime;
  34. foreach (State state in m_pActiveStates)
  35. {
  36. if (m_pActiveStates.Count > 0)
  37. {
  38. state.Process.Invoke(deltaTime);
  39. if (state.IsBlocking)
  40. {
  41. break;
  42. }
  43. }
  44. break;
  45. }
  46. }
  47.  
  48. public State StateExists(string a_stateName)
  49. {
  50. foreach (State state in m_pActiveStates)
  51. {
  52. string pName = state.StateName;
  53. if (pName != null && pName == a_stateName)
  54. {
  55. return state;
  56. }
  57. }
  58. return null;
  59. }
  60.  
  61. public bool EnterState(string a_stateName)
  62. {
  63. State pState = StateExists(a_stateName);
  64. if (pState != null)
  65. {
  66. PopToState(pState);
  67. return true;
  68. }
  69. else
  70. {
  71. if (registeredStates.ContainsKey(a_stateName))
  72. {
  73. State nextState = Activator.CreateInstance(registeredStates[a_stateName], a_stateName) as State;
  74. PushState(nextState);
  75. return true;
  76. }
  77. }
  78. return false;
  79. }
  80.  
  81. private void PopToState(State a_state)
  82. {
  83. temp = m_pActiveStates.Peek();
  84. while (m_pActiveStates.Count != 0 && m_pActiveStates.Peek() != a_state)
  85. {
  86. m_pActiveStates.Pop();
  87. }
  88. _currentState = m_pActiveStates.Peek();
  89. temp.Process.Invoke(0.0f);
  90. temp.Process.Invoke(0.0f);
  91. temp = null;
  92. }
  93.  
  94. private void PushState(State a_state)
  95. {
  96. if(m_pActiveStates.Count != 0)
  97. {
  98. temp = m_pActiveStates.Peek();
  99. }
  100. m_pActiveStates.Push(a_state);
  101. _currentState = m_pActiveStates.Peek();
  102. if(temp != null)
  103. {
  104. temp.Process.Invoke(0.0f);
  105. temp.Process.Invoke(0.0f);
  106. temp = null;
  107. }
  108. }
  109.  
  110. private void PopState()
  111. {
  112. m_pActiveStates.Pop();
  113. _currentState = m_pActiveStates.Peek();
  114. }
  115.  
  116. public void RegisterState<T>(string a_stateName)
  117. where T : State
  118. {
  119. if (registeredStates == null)
  120. {
  121. registeredStates = new Dictionary<string, Type>();
  122. }
  123. registeredStates.Add(a_stateName, typeof(T));
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement