FSM.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "FSM.h"
  11. #include "State.h"
  12. #include "RakAssert.h"
  13. FSM::FSM()
  14. {
  15. }
  16. FSM::~FSM()
  17. {
  18. Clear();
  19. }
  20. void FSM::Clear(void)
  21. {
  22. unsigned i;
  23. if (stateHistory.Size())
  24. stateHistory[stateHistory.Size()-1]->OnLeave(this, true);
  25. for (i=0; i < stateHistory.Size(); i++)
  26. stateHistory[i]->FSMRemoveRef(this);
  27. stateHistory.Clear(false, _FILE_AND_LINE_);
  28. }
  29. State *FSM::CurrentState(void) const
  30. {
  31. if (stateHistory.Size()==0)
  32. return 0;
  33. return stateHistory[stateHistory.Size()-1];
  34. }
  35. State *FSM::GetState(int index) const
  36. {
  37. RakAssert(index>=0 && index < (int) stateHistory.Size());
  38. return stateHistory[(unsigned) index];
  39. }
  40. int FSM::GetStateIndex(State *state) const
  41. {
  42. return (int) stateHistory.GetIndexOf(state);
  43. }
  44. int FSM::GetStateHistorySize(void) const
  45. {
  46. return stateHistory.Size();
  47. }
  48. void FSM::RemoveState(const int index)
  49. {
  50. RakAssert(index>=0 && index < (int) stateHistory.Size());
  51. if (index==stateHistory.Size()-1)
  52. stateHistory[index]->OnLeave(this, true);
  53. stateHistory[index]->FSMRemoveRef(this);
  54. stateHistory.RemoveAtIndex((const unsigned int)index);
  55. if (index==stateHistory.Size())
  56. stateHistory[stateHistory.Size()-1]->OnEnter(this, false);
  57. }
  58. void FSM::AddState(State *state)
  59. {
  60. if (stateHistory.Size())
  61. stateHistory[stateHistory.Size()-1]->OnLeave(this, false);
  62. state->FSMAddRef(this);
  63. state->OnEnter(this, true);
  64. stateHistory.Insert(state, _FILE_AND_LINE_ );
  65. }
  66. void FSM::ReplaceState(const int index, State *state)
  67. {
  68. RakAssert(index>=0 && index < (int) stateHistory.Size());
  69. if (state!=stateHistory[index])
  70. {
  71. if (index==stateHistory.Size()-1)
  72. stateHistory[index]->OnLeave(this, true);
  73. stateHistory[index]->FSMRemoveRef(this);
  74. state->FSMAddRef(this);
  75. if (index==stateHistory.Size()-1)
  76. state->OnEnter(this, true);
  77. stateHistory[index]=state;
  78. }
  79. }
  80. void FSM::SetPriorState(const int index)
  81. {
  82. RakAssert(index>=0 && index < (int) stateHistory.Size());
  83. stateHistory[stateHistory.Size()-1]->OnLeave(this, true);
  84. for (unsigned i=stateHistory.Size()-1; i > (unsigned) index; --i)
  85. {
  86. stateHistory[i]->FSMRemoveRef(this);
  87. stateHistory.RemoveFromEnd();
  88. }
  89. stateHistory[index]->OnEnter(this, false);
  90. }
粤ICP备19079148号