Actor.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #pragma once
  2. //========================================================================
  3. // Actor.h - Defines the Actor 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 "ActorComponent.h"
  40. class TiXmlElement;
  41. typedef std::string ActorType;
  42. //---------------------------------------------------------------------------------------------------------------------
  43. // Actor class
  44. // Chapter 6, page 165
  45. //---------------------------------------------------------------------------------------------------------------------
  46. class Actor
  47. {
  48. friend class ActorFactory;
  49. public:
  50. typedef std::map<ComponentId, StrongActorComponentPtr> ActorComponents;
  51. private:
  52. ActorId m_id; // unique id for the actor
  53. ActorComponents m_components; // all components this actor has
  54. ActorType m_type;
  55. // [mrmike] - these were added post press as editor helpers, but will also be great for save game files if we ever make them.
  56. std::string m_resource; // the XML file from which this actor was initialized (considered the "Archetype" file)
  57. public:
  58. explicit Actor(ActorId id);
  59. ~Actor(void);
  60. bool Init(TiXmlElement* pData);
  61. void PostInit(void);
  62. void Destroy(void);
  63. // accessors
  64. ActorId GetId(void) const { return m_id; }
  65. ActorType GetType(void) const { return m_type; }
  66. // template function for retrieving components
  67. template <class ComponentType>
  68. std::weak_ptr<ComponentType> GetComponent(ComponentId id)
  69. {
  70. ActorComponents::iterator findIt = m_components.find(id);
  71. if (findIt != m_components.end())
  72. {
  73. StrongActorComponentPtr pBase(findIt->second);
  74. std::shared_ptr<ComponentType> pSub(std::static_pointer_cast<ComponentType>(pBase)); // cast to subclass version of the pointer
  75. std::weak_ptr<ComponentType> pWeakSub(pSub); // convert strong pointer to weak pointer
  76. return pWeakSub; // return the weak pointer
  77. }
  78. else
  79. {
  80. return std::weak_ptr<ComponentType>();
  81. }
  82. }
  83. template <class ComponentType>
  84. std::weak_ptr<ComponentType> GetComponent(const char *name)
  85. {
  86. ComponentId id = ActorComponent::GetIdFromName(name);
  87. ActorComponents::iterator findIt = m_components.find(id);
  88. if (findIt != m_components.end())
  89. {
  90. StrongActorComponentPtr pBase(findIt->second);
  91. std::shared_ptr<ComponentType> pSub(std::static_pointer_cast<ComponentType>(pBase)); // cast to subclass version of the pointer
  92. std::weak_ptr<ComponentType> pWeakSub(pSub); // convert strong pointer to weak pointer
  93. return pWeakSub; // return the weak pointer
  94. }
  95. else
  96. {
  97. return std::weak_ptr<ComponentType>();
  98. }
  99. }
  100. void AddComponent(StrongActorComponentPtr pComponent);
  101. };