TeapotWars.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //========================================================================
  2. // TeapotWars.cpp : source file for the sample game
  3. //
  4. // Part of the GameCode4 Application
  5. //
  6. // GameCode4 is the sample application that encapsulates much of the source code
  7. // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David
  8. // "Rez" Graham, published by Charles River Media.
  9. // ISBN-10: 1133776574 | ISBN-13: 978-1133776574
  10. //
  11. // If this source code has found it's way to you, and you think it has helped you
  12. // in any way, do the authors a favor and buy a new copy of the book - there are
  13. // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com
  14. // by clicking here:
  15. // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller=
  16. //
  17. // There's a companion web site at http://www.mcshaffry.com/GameCode/
  18. //
  19. // The source code is managed and maintained through Google Code:
  20. // http://code.google.com/p/gamecode4/
  21. //
  22. // (c) Copyright 2012 Michael L. McShaffry and David Graham
  23. //
  24. // This program is free software; you can redistribute it and/or
  25. // modify it under the terms of the GNU Lesser GPL v3
  26. // as published by the Free Software Foundation.
  27. //
  28. // This program is distributed in the hope that it will be useful,
  29. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  31. // http://www.gnu.org/licenses/lgpl-3.0.txt for more details.
  32. //
  33. // You should have received a copy of the GNU Lesser GPL v3
  34. // along with this program; if not, write to the Free Software
  35. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  36. //
  37. //========================================================================
  38. #include "Unix/TeapotWarsStd.h"
  39. #include "../GCC4/GameCode4/GameCode.h"
  40. #include "../GCC4/EventManager/EventManagerImpl.h"
  41. #include "../GCC4/Utilities/String.h"
  42. #include "Unix/TeapotWars.h"
  43. #include "TeapotWarsView.h"
  44. #include "TeapotEvents.h"
  45. TeapotWarsApp g_TeapotWarsApp;
  46. //========================================================================
  47. // wWinMain - Defines the entry point to your application (Chapter 5 - page 121)
  48. //
  49. // In the book _tWinMain goes straight into the initialization sequence. Here,
  50. // the GameCode4() free function handles it. This allows the GameCode4 function
  51. // to live in a library, separating the game engine from game specific code,
  52. // in this case TeapotWars.
  53. //
  54. //========================================================================
  55. int main(int argc, char **argv)
  56. {
  57. return GameCode4(argc, argv);
  58. }
  59. //========================================================================
  60. //
  61. // TeapotWarsApp Implementation - Chapter 21, page 722
  62. //
  63. //========================================================================
  64. //
  65. // TeapotWarsApp::VCreateGameAndView
  66. //
  67. BaseGameLogic *TeapotWarsApp::VCreateGameAndView()
  68. {
  69. m_pGame = GCC_NEW TeapotWarsLogic();
  70. m_pGame->Init();
  71. std::shared_ptr<IGameView> menuView(GCC_NEW MainMenuView());
  72. m_pGame->VAddView(menuView);
  73. return m_pGame;
  74. }
  75. //========================================================================
  76. //
  77. // TeapotWarsLogic Implementation - Chapter 21, page 725
  78. //
  79. //========================================================================
  80. //
  81. // TeapotWarsLogic::TeapotWarsLogic
  82. //
  83. TeapotWarsLogic::TeapotWarsLogic()
  84. {
  85. // m_pPhysics.reset(CreateGamePhysics());
  86. RegisterAllDelegates();
  87. }
  88. //
  89. // TeapotWarsLogic::~TeapotWarsLogic
  90. //
  91. TeapotWarsLogic::~TeapotWarsLogic()
  92. {
  93. RemoveAllDelegates();
  94. // DestroyAllNetworkEventForwarders();
  95. }
  96. //
  97. // TeapotWarsLogic::VChangeState
  98. //
  99. void TeapotWarsLogic::VChangeState(BaseGameState newState)
  100. {
  101. BaseGameLogic::VChangeState(newState);
  102. switch(newState)
  103. {
  104. case BGS_WaitingForPlayers:
  105. {
  106. // spawn all local players (should only be one, though we might support more in the future)
  107. GCC_ASSERT(m_ExpectedPlayers == 1);
  108. for (int i = 0; i < m_ExpectedPlayers; ++i)
  109. {
  110. std::shared_ptr<IGameView> playersView(GCC_NEW TeapotWarsHumanView(g_pApp->m_Renderer));
  111. VAddView(playersView);
  112. if (m_bProxy)
  113. {
  114. // if we are a remote player, all we have to do is spawn our view - the server will do the rest.
  115. return;
  116. }
  117. }
  118. // // spawn all remote player's views on the game
  119. // for (int i = 0; i < m_ExpectedRemotePlayers; ++i)
  120. // {
  121. // std::shared_ptr<IGameView> remoteGameView(GCC_NEW NetworkGameView);
  122. // VAddView(remoteGameView);
  123. // }
  124. // // spawn all AI's views on the game
  125. // for (int i = 0; i < m_ExpectedAI; ++i)
  126. // {
  127. // std::shared_ptr<IGameView> aiView(GCC_NEW AITeapotView(m_pPathingGraph));
  128. // VAddView(aiView);
  129. // }
  130. break;
  131. }
  132. case BGS_SpawningPlayersActors:
  133. {
  134. if (m_bProxy)
  135. {
  136. // only the server needs to do this
  137. return;
  138. }
  139. for (auto it = m_gameViews.begin(); it != m_gameViews.end(); ++it)
  140. {
  141. std::shared_ptr<IGameView> pView = *it;
  142. if (pView->VGetType() == GameView_Human)
  143. {
  144. StrongActorPtr pActor = VCreateActor("actors/player_teapot.xml", NULL);
  145. if (pActor)
  146. {
  147. std::shared_ptr<EvtData_New_Actor> pNewActorEvent(GCC_NEW EvtData_New_Actor(pActor->GetId(), pView->VGetId()));
  148. IEventManager::Get()->VTriggerEvent(pNewActorEvent); // [rez] This needs to happen asap because the constructor function for Lua (which is called in through VCreateActor()) queues an event that expects this event to have been handled
  149. }
  150. }
  151. }
  152. }
  153. }
  154. }
  155. //
  156. // TeapotWarsLogic::VAddView
  157. //
  158. void TeapotWarsLogic::VAddView(std::shared_ptr<IGameView> pView, ActorId actor)
  159. {
  160. BaseGameLogic::VAddView(pView, actor);
  161. // This is commented out because while the view is created and waiting, the player has NOT attached yet.
  162. // if (dynamic_pointer_cast<NetworkGameView>(pView))
  163. // {
  164. // m_HumanPlayersAttached++;
  165. // }
  166. if (dynamic_pointer_cast<TeapotWarsHumanView>(pView))
  167. {
  168. m_HumanPlayersAttached++;
  169. }
  170. // else if (dynamic_pointer_cast<AITeapotView>(pView))
  171. // {
  172. // m_AIPlayersAttached++;
  173. // }
  174. }
  175. void TeapotWarsLogic::RegisterAllDelegates(void)
  176. {
  177. // FUTURE WORK: Lots of these functions are ok to go into the base game logic!
  178. IEventManager* pGlobalEventManager = IEventManager::Get();
  179. // pGlobalEventManager->VAddListener(MakeDelegate(this, &TeapotWarsLogic::RemoteClientDelegate), EvtData_Remote_Client::sk_EventType);
  180. // pGlobalEventManager->VAddListener(MakeDelegate(this, &TeapotWarsLogic::MoveActorDelegate), EvtData_Move_Actor::sk_EventType);
  181. pGlobalEventManager->VAddListener(MakeDelegate(this, &TeapotWarsLogic::RequestStartGameDelegate), EvtData_Request_Start_Game::sk_EventType);
  182. // pGlobalEventManager->VAddListener(MakeDelegate(this, &TeapotWarsLogic::NetworkPlayerActorAssignmentDelegate), EvtData_Network_Player_Actor_Assignment::sk_EventType);
  183. pGlobalEventManager->VAddListener(MakeDelegate(this, &TeapotWarsLogic::EnvironmentLoadedDelegate), EvtData_Environment_Loaded::sk_EventType);
  184. // pGlobalEventManager->VAddListener(MakeDelegate(this, &TeapotWarsLogic::EnvironmentLoadedDelegate), EvtData_Remote_Environment_Loaded::sk_EventType);
  185. // FUTURE WORK: Only these belong in TeapotWars.
  186. // pGlobalEventManager->VAddListener(MakeDelegate(this, &TeapotWarsLogic::StartThrustDelegate), EvtData_StartThrust::sk_EventType);
  187. // pGlobalEventManager->VAddListener(MakeDelegate(this, &TeapotWarsLogic::EndThrustDelegate), EvtData_EndThrust::sk_EventType);
  188. // pGlobalEventManager->VAddListener(MakeDelegate(this, &TeapotWarsLogic::StartSteerDelegate), EvtData_StartSteer::sk_EventType);
  189. // pGlobalEventManager->VAddListener(MakeDelegate(this, &TeapotWarsLogic::EndSteerDelegate), EvtData_EndSteer::sk_EventType);
  190. pGlobalEventManager->VAddListener(MakeDelegate(this, &TeapotWarsLogic::TestScriptDelegate), EvtData_ScriptEventTest_FromLua::sk_EventType);
  191. }
  192. void TeapotWarsLogic::RemoveAllDelegates(void)
  193. {
  194. // FUTURE WORK: See the note in RegisterDelegates above....
  195. IEventManager* pGlobalEventManager = IEventManager::Get();
  196. // pGlobalEventManager->VRemoveListener(MakeDelegate(this, &TeapotWarsLogic::RemoteClientDelegate), EvtData_Remote_Client::sk_EventType);
  197. // pGlobalEventManager->VRemoveListener(MakeDelegate(this, &TeapotWarsLogic::MoveActorDelegate), EvtData_Move_Actor::sk_EventType);
  198. pGlobalEventManager->VRemoveListener(MakeDelegate(this, &TeapotWarsLogic::RequestStartGameDelegate), EvtData_Request_Start_Game::sk_EventType);
  199. // pGlobalEventManager->VRemoveListener(MakeDelegate(this, &TeapotWarsLogic::NetworkPlayerActorAssignmentDelegate), EvtData_Network_Player_Actor_Assignment::sk_EventType);
  200. pGlobalEventManager->VRemoveListener(MakeDelegate(this, &TeapotWarsLogic::EnvironmentLoadedDelegate), EvtData_Environment_Loaded::sk_EventType);
  201. // pGlobalEventManager->VRemoveListener(MakeDelegate(this, &TeapotWarsLogic::EnvironmentLoadedDelegate), EvtData_Remote_Environment_Loaded::sk_EventType);
  202. // if (m_bProxy)
  203. // {
  204. // pGlobalEventManager->VRemoveListener(MakeDelegate(this, &TeapotWarsLogic::RequestNewActorDelegate), EvtData_Request_New_Actor::sk_EventType);
  205. // }
  206. // FUTURE WORK: These belong in teapot wars!
  207. // pGlobalEventManager->VRemoveListener(MakeDelegate(this, &TeapotWarsLogic::StartThrustDelegate), EvtData_StartThrust::sk_EventType);
  208. // pGlobalEventManager->VRemoveListener(MakeDelegate(this, &TeapotWarsLogic::EndThrustDelegate), EvtData_EndThrust::sk_EventType);
  209. // pGlobalEventManager->VRemoveListener(MakeDelegate(this, &TeapotWarsLogic::StartSteerDelegate), EvtData_StartSteer::sk_EventType);
  210. // pGlobalEventManager->VRemoveListener(MakeDelegate(this, &TeapotWarsLogic::EndSteerDelegate), EvtData_EndSteer::sk_EventType);
  211. pGlobalEventManager->VRemoveListener(MakeDelegate(this, &TeapotWarsLogic::TestScriptDelegate), EvtData_ScriptEventTest_FromLua::sk_EventType);
  212. }
  213. void TeapotWarsLogic::RequestStartGameDelegate(IEventDataPtr pEventData)
  214. {
  215. VChangeState(BGS_WaitingForPlayers);
  216. }
  217. void TeapotWarsLogic::EnvironmentLoadedDelegate(IEventDataPtr pEventData)
  218. {
  219. ++m_HumanGamesLoaded;
  220. }
  221. void TeapotWarsLogic::TestScriptDelegate(IEventDataPtr pEventData)
  222. {
  223. std::shared_ptr<EvtData_ScriptEventTest_FromLua> pCastEventData = static_pointer_cast<EvtData_ScriptEventTest_FromLua>(pEventData);
  224. GCC_LOG("Lua", "Event received in C++ from Lua: " + ToStr(pCastEventData->GetNum()));
  225. }
  226. bool TeapotWarsLogic::VLoadGameDelegate(TiXmlElement* pLevelData)
  227. {
  228. RegisterTeapotScriptEvents();
  229. return true;
  230. }