CmGameObjectManager.h 2.7 KB

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