GameStateCtrl.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Reflection;
  4. using System;
  5. public class GameStateAttribute : AutoRegisterAttribute
  6. {
  7. }
  8. public class GameStateCtrl : Singleton<GameStateCtrl>
  9. {
  10. private StateMachine gameState = new StateMachine();
  11. public override void Init()
  12. {
  13. base.Init();
  14. gameState.RegisterStateByAttributes<GameStateAttribute>(typeof(GameStateAttribute).Assembly);
  15. }
  16. public override void UnInit()
  17. {
  18. gameState.Clear();
  19. gameState = null;
  20. base.UnInit();
  21. }
  22. public void GotoState(string name)
  23. {
  24. string str = string.Format("GameStateCtrl Goto State {0}", name);
  25. DebugHelper.Log(str);
  26. gameState.ChangeState(name);
  27. }
  28. public IState GetCurrentState()
  29. {
  30. return gameState.TopState();
  31. }
  32. public bool isBattleState
  33. {
  34. get { return (gameState.TopState() as BattleState) != null; }
  35. }
  36. public bool isLoadingState
  37. {
  38. get { return (gameState.TopState() as LoadingState) != null; }
  39. }
  40. public bool isLoginState
  41. {
  42. get { return (gameState.TopState() as LoginState) != null; }
  43. }
  44. public string currentStateName
  45. {
  46. get
  47. {
  48. var state = GetCurrentState();
  49. return state != null ? state.name : "unkown state";
  50. }
  51. }
  52. }