CmGameObjectManager.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmModule.h"
  4. #include "CmGameObject.h"
  5. namespace CamelotFramework
  6. {
  7. class CM_EXPORT GameObjectManager : public Module<GameObjectManager>
  8. {
  9. public:
  10. GameObjectManager();
  11. ~GameObjectManager();
  12. GameObjectHandleBase registerObject(const std::shared_ptr<GameObject>& object);
  13. void unregisterObject(const GameObjectHandleBase& object);
  14. GameObjectHandleBase getObject(UINT64 id) const;
  15. bool objectExists(UINT64 id) const;
  16. /************************************************************************/
  17. /* DESERIALIZATION */
  18. /************************************************************************/
  19. // Note: GameObjects need a bit of special handling when it comes to deserialization,
  20. // which is what this part of the code is used for. It performs two main actions:
  21. // - 1. Resolves all GameObjectHandles on deserialization
  22. // - We can't just resolve them as we go because during deserialization not all objects
  23. // have necessarily been created.
  24. // - 2. Maps serialized IDs to actual in-engine ids.
  25. /**
  26. * @brief Needs to be called whenever GameObject deserialization starts. Must be followed
  27. * by endDeserialization call.
  28. */
  29. void startDeserialization();
  30. /**
  31. * @brief Needs to be called whenever GameObject deserialization ends. Must be preceded
  32. * by startDeserialization call.
  33. */
  34. void endDeserialization();
  35. /**
  36. * @brief Returns true if GameObject deserialization is currently in progress.
  37. */
  38. bool isGameObjectDeserializationActive() const { return mIsDeserializationActive; }
  39. /**
  40. * @brief Registers an id that was deserialized, and has been remapped to
  41. * an actual in-engine ID. This will be used when resolving GameObjectHandles
  42. * (since they might point to the invalid deserialized id).
  43. */
  44. void registerDeserializedId(UINT64 deserializedId, UINT64 actualId);
  45. /**
  46. * @brief Queues the specified handle and resolves it when deserialization ends.
  47. */
  48. void registerUnresolvedHandle(const GameObjectHandleBase& object);
  49. /**
  50. * @brief Registers a callback that will be triggered when GameObject serialization ends.
  51. */
  52. void registerOnDeserializationEndCallback(std::function<void()> callback);
  53. private:
  54. UINT64 mNextAvailableID; // 0 is not a valid ID
  55. Map<UINT64, GameObjectHandleBase>::type mObjects;
  56. GameObject* mActiveDeserializedObject;
  57. bool mIsDeserializationActive;
  58. Map<UINT64, UINT64>::type mIdMapping;
  59. Vector<GameObjectHandleBase>::type mUnresolvedHandles;
  60. Vector<std::function<void()>>::type mEndCallbacks;
  61. };
  62. }