Events.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. #pragma once
  2. //========================================================================
  3. // Events.h : defines common game events
  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 "EventManager.h"
  40. #include "../GameCode4/GameCode.h"
  41. #include "../LUAScripting/ScriptEvent.h"
  42. #include "../Graphics3D/Scene.h"
  43. //---------------------------------------------------------------------------------------------------------------------
  44. // EvtData_New_Actor - This event is sent out when an actor is *actually* created.
  45. //---------------------------------------------------------------------------------------------------------------------
  46. class EvtData_New_Actor : public BaseEventData
  47. {
  48. ActorId m_actorId;
  49. GameViewId m_viewId;
  50. public:
  51. static const EventType sk_EventType;
  52. EvtData_New_Actor(void)
  53. {
  54. m_actorId = INVALID_ACTOR_ID;
  55. m_viewId = gc_InvalidGameViewId;
  56. }
  57. explicit EvtData_New_Actor(ActorId actorId, GameViewId viewId = gc_InvalidGameViewId)
  58. : m_actorId(actorId),
  59. m_viewId(viewId)
  60. {
  61. }
  62. virtual void VDeserialize(std::istrstream& in)
  63. {
  64. in >> m_actorId;
  65. in >> m_viewId;
  66. }
  67. virtual const EventType& VGetEventType(void) const
  68. {
  69. return sk_EventType;
  70. }
  71. virtual IEventDataPtr VCopy(void) const
  72. {
  73. return IEventDataPtr(GCC_NEW EvtData_New_Actor(m_actorId, m_viewId));
  74. }
  75. virtual void VSerialize(std::ostream& out) const
  76. {
  77. out << m_actorId << " ";
  78. out << m_viewId << " ";
  79. }
  80. virtual const char* GetName(void) const
  81. {
  82. return "EvtData_New_Actor";
  83. }
  84. const ActorId GetActorId(void) const
  85. {
  86. return m_actorId;
  87. }
  88. GameViewId GetViewId(void) const
  89. {
  90. return m_viewId;
  91. }
  92. };
  93. //---------------------------------------------------------------------------------------------------------------------
  94. // EvtData_Destroy_Actor - sent when actors are destroyed
  95. //---------------------------------------------------------------------------------------------------------------------
  96. class EvtData_Destroy_Actor : public BaseEventData
  97. {
  98. ActorId m_id;
  99. public:
  100. static const EventType sk_EventType;
  101. explicit EvtData_Destroy_Actor(ActorId id = INVALID_ACTOR_ID)
  102. : m_id(id)
  103. {
  104. //
  105. }
  106. virtual const EventType& VGetEventType(void) const
  107. {
  108. return sk_EventType;
  109. }
  110. virtual IEventDataPtr VCopy(void) const
  111. {
  112. return IEventDataPtr(GCC_NEW EvtData_Destroy_Actor(m_id));
  113. }
  114. virtual void VSerialize(std::ostrstream &out) const
  115. {
  116. out << m_id;
  117. }
  118. virtual void VDeserialize(std::istrstream& in)
  119. {
  120. in >> m_id;
  121. }
  122. virtual const char* GetName(void) const
  123. {
  124. return "EvtData_Destroy_Actor";
  125. }
  126. ActorId GetId(void) const { return m_id; }
  127. };
  128. //---------------------------------------------------------------------------------------------------------------------
  129. // EvtData_New_Render_Component - This event is sent out when an actor is *actually* created.
  130. //---------------------------------------------------------------------------------------------------------------------
  131. class EvtData_New_Render_Component : public BaseEventData
  132. {
  133. ActorId m_actorId;
  134. std::shared_ptr<SceneNode> m_pSceneNode;
  135. public:
  136. static const EventType sk_EventType;
  137. EvtData_New_Render_Component(void)
  138. {
  139. m_actorId = INVALID_ACTOR_ID;
  140. }
  141. explicit EvtData_New_Render_Component(ActorId actorId, std::shared_ptr<SceneNode> pSceneNode)
  142. : m_actorId(actorId),
  143. m_pSceneNode(pSceneNode)
  144. {
  145. }
  146. virtual void VSerialize(std::ostrstream& out) const
  147. {
  148. GCC_ERROR(GetName() + std::string(" should not be serialzied!"));
  149. }
  150. virtual void VDeserialize(std::istrstream& in)
  151. {
  152. GCC_ERROR(GetName() + std::string(" should not be serialzied!"));
  153. }
  154. virtual const EventType& VGetEventType(void) const
  155. {
  156. return sk_EventType;
  157. }
  158. virtual IEventDataPtr VCopy(void) const
  159. {
  160. return IEventDataPtr(GCC_NEW EvtData_New_Render_Component(m_actorId, m_pSceneNode));
  161. }
  162. virtual const char* GetName(void) const
  163. {
  164. return "EvtData_New_Render_Component";
  165. }
  166. const ActorId GetActorId(void) const
  167. {
  168. return m_actorId;
  169. }
  170. std::shared_ptr<SceneNode> GetSceneNode(void) const
  171. {
  172. return m_pSceneNode;
  173. }
  174. };
  175. //---------------------------------------------------------------------------------------------------------------------
  176. // EvtData_Modified_Render_Component - This event is sent out when a render component is changed
  177. // NOTE: This class is not described in the book!
  178. //---------------------------------------------------------------------------------------------------------------------
  179. class EvtData_Modified_Render_Component : public BaseEventData
  180. {
  181. ActorId m_id;
  182. public:
  183. static const EventType sk_EventType;
  184. virtual const EventType& VGetEventType(void) const
  185. {
  186. return sk_EventType;
  187. }
  188. EvtData_Modified_Render_Component(void)
  189. {
  190. m_id = INVALID_ACTOR_ID;
  191. }
  192. EvtData_Modified_Render_Component(ActorId id)
  193. : m_id(id)
  194. {
  195. }
  196. virtual void VSerialize(std::ostrstream &out) const
  197. {
  198. out << m_id;
  199. }
  200. virtual void VDeserialize(std::istrstream& in)
  201. {
  202. in >> m_id;
  203. }
  204. virtual IEventDataPtr VCopy() const
  205. {
  206. return IEventDataPtr(GCC_NEW EvtData_Modified_Render_Component(m_id));
  207. }
  208. virtual const char* GetName(void) const
  209. {
  210. return "EvtData_Modified_Render_Component";
  211. }
  212. ActorId GetActorId(void) const
  213. {
  214. return m_id;
  215. }
  216. };
  217. //---------------------------------------------------------------------------------------------------------------------
  218. // EvtData_Environment_Loaded - this event is sent when a new game is started
  219. //---------------------------------------------------------------------------------------------------------------------
  220. class EvtData_Environment_Loaded : public BaseEventData
  221. {
  222. public:
  223. static const EventType sk_EventType;
  224. EvtData_Environment_Loaded(void) { }
  225. virtual const EventType& VGetEventType(void) const { return sk_EventType; }
  226. virtual IEventDataPtr VCopy(void) const
  227. { return IEventDataPtr( GCC_NEW EvtData_Environment_Loaded( ) ); }
  228. virtual const char* GetName(void) const { return "EvtData_Environment_Loaded"; }
  229. };
  230. //---------------------------------------------------------------------------------------------------------------------
  231. // EvtData_Environment_Loaded - this event is sent when a client has loaded its environment
  232. // This is special because we only want this event to go from client to server, and stop there. The
  233. // EvtData_Environment_Loaded is received by server and proxy logics alike. Thy to do this with just the above
  234. // event and you'll get into an endless loop of the EvtData_Environment_Loaded event making infinite round trips
  235. // from client to server.
  236. //
  237. // FUTURE_WORK: It would be an interesting idea to add a "Private" type of event that is addressed only to a specific
  238. // listener. Of course, that might be a really dumb idea too - someone will have to try it!
  239. //---------------------------------------------------------------------------------------------------------------------
  240. class EvtData_Remote_Environment_Loaded : public BaseEventData
  241. {
  242. public:
  243. static const EventType sk_EventType;
  244. EvtData_Remote_Environment_Loaded(void) { }
  245. virtual const EventType& VGetEventType(void) const { return sk_EventType; }
  246. virtual IEventDataPtr VCopy(void) const
  247. { return IEventDataPtr( GCC_NEW EvtData_Remote_Environment_Loaded( ) ); }
  248. virtual const char* GetName(void) const { return "EvtData_Remote_Environment_Loaded"; }
  249. };
  250. //---------------------------------------------------------------------------------------------------------------------
  251. // EvtData_Request_Start_Game - this is sent by the authoritative game logic to all views so they will load a game level.
  252. //---------------------------------------------------------------------------------------------------------------------
  253. class EvtData_Request_Start_Game : public BaseEventData
  254. {
  255. public:
  256. static const EventType sk_EventType;
  257. EvtData_Request_Start_Game(void) { }
  258. virtual const EventType& VGetEventType(void) const
  259. {
  260. return sk_EventType;
  261. }
  262. virtual IEventDataPtr VCopy() const
  263. {
  264. return IEventDataPtr( GCC_NEW EvtData_Request_Start_Game( ) );
  265. }
  266. virtual const char* GetName(void) const
  267. {
  268. return "EvtData_Request_Start_Game";
  269. }
  270. };
  271. //---------------------------------------------------------------------------------------------------------------------
  272. // EvtData_Update_Tick - sent by the game logic each game tick
  273. //---------------------------------------------------------------------------------------------------------------------
  274. class EvtData_Update_Tick : public BaseEventData
  275. {
  276. int m_DeltaMilliseconds;
  277. public:
  278. static const EventType sk_EventType;
  279. explicit EvtData_Update_Tick( const int deltaMilliseconds )
  280. : m_DeltaMilliseconds( deltaMilliseconds )
  281. {
  282. }
  283. virtual const EventType& VGetEventType( void ) const
  284. {
  285. return sk_EventType;
  286. }
  287. virtual IEventDataPtr VCopy() const
  288. {
  289. return IEventDataPtr (GCC_NEW EvtData_Update_Tick ( m_DeltaMilliseconds ) );
  290. }
  291. virtual void Serialize( std::ostrstream & out )
  292. {
  293. GCC_ERROR("You should not be serializing update ticks!");
  294. }
  295. virtual const char* GetName(void) const
  296. {
  297. return "EvtData_Update_Tick";
  298. }
  299. };
  300. //---------------------------------------------------------------------------------------------------------------------
  301. // class EvtData_Request_New_Actor
  302. // This event is sent by a server asking Client proxy logics to create new actors from their local resources.
  303. // It can be sent from script or via code.
  304. // This event is also sent from the server game logic to client logics AFTER it has created a new actor. The logics will allow follow suit to stay in sync.
  305. //---------------------------------------------------------------------------------------------------------------------
  306. class EvtData_Request_New_Actor : public BaseEventData
  307. {
  308. std::string m_actorResource;
  309. bool m_hasInitialTransform;
  310. glm::mat4 m_initialTransform;
  311. ActorId m_serverActorId;
  312. GameViewId m_viewId;
  313. public:
  314. static const EventType sk_EventType;
  315. EvtData_Request_New_Actor()
  316. {
  317. m_actorResource = "";
  318. m_hasInitialTransform = false;
  319. m_initialTransform = glm::mat4(1.0f);
  320. m_serverActorId = -1;
  321. m_viewId = gc_InvalidGameViewId;
  322. }
  323. explicit EvtData_Request_New_Actor(const std::string &actorResource, std::optional<const glm::mat4> initialTransform=std::nullopt, const ActorId serverActorId = INVALID_ACTOR_ID, const GameViewId viewId = gc_InvalidGameViewId)
  324. {
  325. m_actorResource = actorResource;
  326. if (initialTransform)
  327. {
  328. m_hasInitialTransform = true;
  329. m_initialTransform = *initialTransform;
  330. }
  331. else
  332. m_hasInitialTransform = false;
  333. m_serverActorId = serverActorId;
  334. m_viewId = viewId;
  335. }
  336. virtual const EventType& VGetEventType(void) const
  337. {
  338. return sk_EventType;
  339. }
  340. virtual void VDeserialize(std::istrstream & in)
  341. {
  342. in >> m_actorResource;
  343. in >> m_hasInitialTransform;
  344. if (m_hasInitialTransform)
  345. {
  346. for (int i=0; i<4; i++)
  347. {
  348. for (int j=0; j<4; ++j)
  349. {
  350. in >> m_initialTransform[i][j];
  351. }
  352. }
  353. }
  354. in >> m_serverActorId;
  355. in >> m_viewId;
  356. }
  357. virtual IEventDataPtr VCopy() const
  358. {
  359. return IEventDataPtr (GCC_NEW EvtData_Request_New_Actor(m_actorResource, (m_hasInitialTransform) ? std::optional(m_initialTransform) : std::nullopt, m_serverActorId, m_viewId));
  360. }
  361. virtual void VSerialize( std::ostrstream & out ) const
  362. {
  363. out << m_actorResource << " ";
  364. out << m_hasInitialTransform << " ";
  365. if (m_hasInitialTransform)
  366. {
  367. for (int i=0; i<4; ++i)
  368. {
  369. for (int j=0; j<4; ++j)
  370. {
  371. out << m_initialTransform[i][j];
  372. }
  373. }
  374. }
  375. }
  376. virtual const char* GetName(void) const { return "EvtData_Request_New_Actor"; }
  377. const std::string &GetActorResource(void) const { return m_actorResource; }
  378. const glm::mat4 *GetInitialTransform(void) const { return (m_hasInitialTransform) ? &m_initialTransform : NULL; }
  379. const ActorId GetServerActorId(void) const { return m_serverActorId; }
  380. GameViewId GetViewId(void) const { return m_viewId; }
  381. };
  382. //---------------------------------------------------------------------------------------------------------------------
  383. // EvtData_PlaySound - sent by any system wishing for a HumanView to play a sound
  384. //---------------------------------------------------------------------------------------------------------------------
  385. class EvtData_PlaySound : public ScriptEvent
  386. {
  387. std::string m_soundResource;
  388. public:
  389. static const EventType sk_EventType;
  390. EvtData_PlaySound(void) { }
  391. EvtData_PlaySound(const std::string& soundResource)
  392. : m_soundResource(soundResource)
  393. {
  394. }
  395. virtual const EventType& VGetEventType(void) const
  396. {
  397. return sk_EventType;
  398. }
  399. virtual IEventDataPtr VCopy() const
  400. {
  401. return IEventDataPtr(GCC_NEW EvtData_PlaySound(m_soundResource));
  402. }
  403. virtual void VSerialize(std::ostrstream& out) const
  404. {
  405. out << m_soundResource;
  406. }
  407. virtual void VDeserialize(std::istrstream& in)
  408. {
  409. in >> m_soundResource;
  410. }
  411. const std::string& GetResource(void) const
  412. {
  413. return m_soundResource;
  414. }
  415. virtual const char* GetName(void) const
  416. {
  417. return "EvtData_PlaySound";
  418. }
  419. virtual bool VBuildEventFromScript(void);
  420. EXPORT_FOR_SCRIPT_EVENT(EvtData_PlaySound);
  421. };