Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
4.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #ifndef GAMEMANAGER_H
  8. #define GAMEMANAGER_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "tier1/utlvector.h"
  13. //-----------------------------------------------------------------------------
  14. // State we are in
  15. //-----------------------------------------------------------------------------
  16. enum LevelState_t
  17. {
  18. NOT_IN_LEVEL = 0,
  19. LOADING_LEVEL,
  20. IN_LEVEL,
  21. SHUTTING_DOWN_LEVEL,
  22. };
  23. //-----------------------------------------------------------------------------
  24. // State we are in
  25. //-----------------------------------------------------------------------------
  26. enum LevelRetVal_t
  27. {
  28. FAILED = 0,
  29. MORE_WORK,
  30. FINISHED,
  31. };
  32. //-----------------------------------------------------------------------------
  33. // Tick interval
  34. //-----------------------------------------------------------------------------
  35. #define TICK_INTERVAL 0.015f
  36. //-----------------------------------------------------------------------------
  37. // Game managers are singleton objects in the game code responsible for various tasks
  38. // The order in which the server systems appear in this list are the
  39. // order in which they are initialized and updated. They are shut down in
  40. // reverse order from which they are initialized.
  41. //-----------------------------------------------------------------------------
  42. abstract_class IGameManager
  43. {
  44. public:
  45. // GameManagers are expected to implement these methods.
  46. virtual bool Init() = 0;
  47. virtual LevelRetVal_t LevelInit( bool bFirstCall ) = 0;
  48. virtual void Update( ) = 0;
  49. virtual LevelRetVal_t LevelShutdown( bool bFirstCall ) = 0;
  50. virtual void Shutdown() = 0;
  51. // Called during game save
  52. virtual void OnSave() = 0;
  53. // Called during game restore
  54. virtual void OnRestore() = 0;
  55. // This this game manager involved in I/O or simulation?
  56. virtual bool PerformsSimulation() = 0;
  57. // Add, remove game managers
  58. static void Add( IGameManager* pSys );
  59. static void Remove( IGameManager* pSys );
  60. static void RemoveAll( );
  61. // Init, shutdown game managers
  62. static bool InitAllManagers();
  63. static void ShutdownAllManagers();
  64. // Start, stop running game managers
  65. static void Start ();
  66. static void Stop ();
  67. static int FrameNumber();
  68. // Used in simulation
  69. static float CurrentSimulationTime();
  70. static float SimulationDeltaTime();
  71. // Used in rendering
  72. static float CurrentTime();
  73. static float DeltaTime();
  74. // Start loading a level
  75. static void StartNewLevel();
  76. static void ShutdownLevel();
  77. static LevelState_t GetLevelState();
  78. protected:
  79. // Updates the state machine related to loading levels
  80. static void UpdateLevelStateMachine();
  81. virtual ~IGameManager() {}
  82. typedef LevelRetVal_t (IGameManager::*GameManagerLevelFunc_t)( bool bFirstCall );
  83. typedef bool (IGameManager::*GameManagerInitFunc_t)();
  84. typedef void (IGameManager::*GameManagerFunc_t)();
  85. // Used to invoke a method of all added game managers in order
  86. static void InvokeMethod( GameManagerFunc_t f );
  87. static void InvokeMethodReverseOrder( GameManagerFunc_t f );
  88. static bool InvokeMethod( GameManagerInitFunc_t f );
  89. static LevelRetVal_t InvokeLevelMethod( GameManagerLevelFunc_t f, bool bFirstCall );
  90. static LevelRetVal_t InvokeLevelMethodReverseOrder( GameManagerLevelFunc_t f, bool bFirstCall );
  91. static bool m_bLevelShutdownRequested;
  92. static bool m_bLevelStartRequested;
  93. static bool m_bStopRequested;
  94. static CUtlVector< IGameManager* > m_GameManagers;
  95. static bool m_bIsRunning;
  96. static bool m_bIsInitialized;
  97. static int m_nFrameNumber;
  98. static float m_flCurrentTime;
  99. static float m_flLastTime;
  100. static LevelState_t m_LevelState;
  101. };
  102. //-----------------------------------------------------------------------------
  103. // Default decorator base-class for IGameManager
  104. //-----------------------------------------------------------------------------
  105. template< class BaseClass = IGameManager >
  106. class CGameManager : public BaseClass
  107. {
  108. public:
  109. virtual ~CGameManager();
  110. // GameManagers are expected to implement these methods.
  111. // NOTE: If Init or LevelInit fail, they are expected to call
  112. virtual bool Init() { return true; }
  113. virtual LevelRetVal_t LevelInit( bool bFirstCall ) { return FINISHED; }
  114. virtual void Update( ) {}
  115. virtual LevelRetVal_t LevelShutdown( bool bFirstCall ) { return FINISHED; }
  116. virtual void Shutdown() {}
  117. virtual void OnSave() {}
  118. virtual void OnRestore() {}
  119. virtual bool PerformsSimulation() { return false; }
  120. };
  121. //-----------------------------------------------------------------------------
  122. // Automatically remove the game system if it gets deleted.
  123. //-----------------------------------------------------------------------------
  124. template< class BaseClass >
  125. inline CGameManager< BaseClass >::~CGameManager()
  126. {
  127. Remove( this );
  128. }
  129. #endif // GAMEMANAGER_H