FSMMgr.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. local FSMMgr = class("FSMMgr")
  2. function FSMMgr:ctor()
  3. self.currentState = nil
  4. self.fakeState = nil
  5. self.stateDic = {}
  6. end
  7. function FSMMgr:RegisterStates()
  8. for k,v in pairs(Enum.StateEnum) do
  9. local state = require(k):new(v.name, v.stateId)
  10. self:AddState(state)
  11. end
  12. end
  13. function FSMMgr:GetCurrentState()
  14. return self.currentState
  15. end
  16. function FSMMgr:AddState(state)
  17. self.stateDic[state.stateId] = state
  18. end
  19. function FSMMgr:ChangeState(stateId, ...)
  20. local newState = self.stateDic[stateId]
  21. Log("FSMMgr:ChangeState "..newState:GetStateName())
  22. if self.fakeState then
  23. self.fakeState:Exit()
  24. self.fakeState = nil
  25. end
  26. if not newState:IsFake() and self.currentState then
  27. self.currentState:Exit()
  28. end
  29. if newState:IsFake() then
  30. self.fakeState = newState
  31. if self.currentState.FakeExit then
  32. self.currentState:FakeExit();
  33. end
  34. else
  35. self.currentState = newState
  36. end
  37. newState:Enter(...)
  38. end
  39. function FSMMgr:GetState(stateId)
  40. local newState = self.stateDic[stateId]
  41. return newState
  42. end
  43. function FSMMgr:Update()
  44. if self.fakeState then
  45. self.fakeState:Update()
  46. end
  47. if self.currentState then
  48. self.currentState:Update()
  49. end
  50. end
  51. function FSMMgr:Destroy()
  52. self.currentState = nil
  53. self.fakeState = nil
  54. self.stateDic = {}
  55. if tolua.getpeer(self) ~= nil then
  56. tolua.setpeer(self, nil)
  57. end
  58. end
  59. function FSMMgr:IsLoginState()
  60. return self.currentState.stateId == Enum.StateEnum.LoginState.stateId;
  61. end
  62. function FSMMgr:IsLoadingState()
  63. return self.currentState.stateId == Enum.StateEnum.LoadingState.stateId;
  64. end
  65. function FSMMgr:IsBattleState()
  66. return self.currentState.stateId == Enum.StateEnum.BattleState.stateId;
  67. end
  68. function FSMMgr:IsGuildLobbyState()
  69. return self.currentState.stateId == Enum.StateEnum.GuildLobbyState.stateId;
  70. end
  71. function FSMMgr:IsIdleState()
  72. return self.currentState.stateId == Enum.StateEnum.IdleState.stateId;
  73. end
  74. function FSMMgr:IsDojoState()
  75. return self.currentState.stateId == Enum.StateEnum.DojoState.stateId;
  76. end
  77. function FSMMgr:IsBossBattleState()
  78. return self.currentState.stateId == Enum.StateEnum.BossBattleState.stateId
  79. end
  80. function FSMMgr:IsCreateRoleState()
  81. return self.currentState.stateId == Enum.StateEnum.CreateRoleState.stateId
  82. end
  83. function FSMMgr:IsTimeBattleState()
  84. return self.currentState.stateId == Enum.StateEnum.TimeBattleState.stateId
  85. end
  86. function FSMMgr:IsFightingState()
  87. return self:IsBattleState() or self:IsDojoState() or self:IsBossBattleState() or self:IsTimeBattleState()
  88. end
  89. return FSMMgr