BaseGameLogic.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #pragma once
  2. //========================================================================
  3. // BaseGameLogic.h : Defines the Base Game Logic class
  4. //
  5. // Part of the GameCode4 Application
  6. //
  7. // GameCode4 is the sample application that encapsulates much of the source code
  8. // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David
  9. // "Rez" Graham, published by Charles River Media.
  10. // ISBN-10: 1133776574 | ISBN-13: 978-1133776574
  11. //
  12. // If this source code has found it's way to you, and you think it has helped you
  13. // in any way, do the authors a favor and buy a new copy of the book - there are
  14. // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com
  15. // by clicking here:
  16. // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller=
  17. //
  18. // There's a companion web site at http://www.mcshaffry.com/GameCode/
  19. //
  20. // The source code is managed and maintained through Google Code:
  21. // http://code.google.com/p/gamecode4/
  22. //
  23. // (c) Copyright 2012 Michael L. McShaffry and David Graham
  24. //
  25. // This program is free software; you can redistribute it and/or
  26. // modify it under the terms of the GNU Lesser GPL v3
  27. // as published by the Free Software Foundation.
  28. //
  29. // This program is distributed in the hope that it will be useful,
  30. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  32. // http://www.gnu.org/licenses/lgpl-3.0.txt for more details.
  33. //
  34. // You should have received a copy of the GNU Lesser GPL v3
  35. // along with this program; if not, write to the Free Software
  36. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  37. //
  38. //========================================================================
  39. #include "interfaces.h"
  40. #include "../Utilities/Math.h"
  41. #include "../Actors/Actor.h"
  42. #include "../Actors/ActorFactory.h"
  43. #include <vector>
  44. class LevelManager;
  45. enum BaseGameState
  46. {
  47. BGS_Invalid,
  48. BGS_Initializing,
  49. BGS_MainMenu,
  50. BGS_WaitingForPlayers,
  51. BGS_LoadingGameEnvironment,
  52. BGS_WaitingForPlayersToLoadEnvironment,
  53. BGS_SpawningPlayersActors,
  54. BGS_Running
  55. };
  56. typedef std::map<ActorId, StrongActorPtr> ActorMap;
  57. typedef std::string Level;
  58. class LevelManager
  59. {
  60. public:
  61. const std::vector<Level> &GetLevels() const { return m_Levels; }
  62. const int GetCurrentLevel() const { return m_CurrentLevel; }
  63. bool Initialize(std::vector<std::string> &levels);
  64. protected:
  65. std::vector<Level> m_Levels;
  66. int m_CurrentLevel;
  67. };
  68. class BaseGameLogic : public IGameLogic
  69. {
  70. friend class GameCodeApp; // This is only to gain access to the view list
  71. protected:
  72. float m_Lifetime; //indicates how long this game has been in session
  73. BaseGameState m_State; // game state: loading, running, etc.
  74. ActorMap m_actors;
  75. int m_ExpectedPlayers; // how many local human players
  76. int m_ExpectedRemotePlayers; // expected remote human players
  77. int m_ExpectedAI; // how many AI players
  78. int m_HumanPlayersAttached;
  79. int m_AIPlayersAttached;
  80. int m_HumanGamesLoaded;
  81. GameViewList m_gameViews; // views that are attached to our game
  82. ActorFactory* m_pActorFactory;
  83. bool m_bProxy; // set if this is a proxy game logic, not a real one
  84. bool m_RenderDiagnostics; // Are we rendering diagnostics?
  85. LevelManager* m_pLevelManager; // Manages loading and chaining levels
  86. public:
  87. BaseGameLogic();
  88. virtual ~BaseGameLogic();
  89. bool Init(void);
  90. void SetProxy(bool isProxy)
  91. {
  92. m_bProxy = isProxy;
  93. }
  94. const bool IsProxy() const { return m_bProxy; }
  95. // [mrmike] CanRunLua() is a bit of a hack - but I can't have Lua scripts running on the clients. They should belong to the logic.
  96. // FUTURE WORK - Perhaps the scripts can have a marker or even a special place in the resource file for any scripts that can run on remote clients
  97. const bool CanRunLua() const { return !IsProxy() || GetState()!=BGS_Running; }
  98. virtual void VAddView(std::shared_ptr<IGameView> pView, ActorId actorId=INVALID_ACTOR_ID);
  99. virtual void VRemoveView(std::shared_ptr<IGameView> pView);
  100. virtual StrongActorPtr VCreateActor(
  101. const std::string &actorResource,
  102. TiXmlElement *overrides,
  103. std::optional<const glm::mat4> initialTransform=std::nullopt,
  104. const ActorId serversActorId=INVALID_ACTOR_ID // [rez] note: don't store this strong pointer outside of this class
  105. );
  106. virtual void VDestroyActor(const ActorId actorId);
  107. virtual WeakActorPtr VGetActor(const ActorId actorId);
  108. virtual void VModifyActor(const ActorId actorId, TiXmlElement *override);
  109. // Level management
  110. const LevelManager* GetLevelManager() { return m_pLevelManager; }
  111. virtual bool VLoadGame(const char* levelResource) override; // [rez] Subclasses shouldn't override this function; use VLoadGameDelegate() instead
  112. virtual void VSetProxy();
  113. // Logic Update
  114. virtual void VOnUpdate(float time, float elapsedTime);
  115. // Changing Game Logic State
  116. virtual void VChangeState(BaseGameState newState);
  117. const BaseGameState GetState() const { return m_State; }
  118. // Render Diagnostics
  119. void ToggleRenderDiagnostics() { m_RenderDiagnostics = !m_RenderDiagnostics; }
  120. virtual void VRenderDiagnostics();
  121. protected:
  122. virtual ActorFactory* VCreateActorFactory(void);
  123. // [rez] Override this function to do any game-specific loading.
  124. virtual bool VLoadGameDelegate(TiXmlElement* pLevelData) { return true; }
  125. };