NetworkEntityTracker.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <Multiplayer/MultiplayerTypes.h>
  10. #include <Multiplayer/NetworkEntity/NetworkEntityHandle.h>
  11. #include <AzCore/std/containers/unordered_map.h>
  12. #include <AzCore/Component/Entity.h>
  13. namespace Multiplayer
  14. {
  15. class NetBindComponent;
  16. //! @class NetworkEntityTracker
  17. //! @brief This class allows entity netEntityIds to be looked up.
  18. class NetworkEntityTracker
  19. {
  20. public:
  21. using EntityMap = AZStd::unordered_map<NetEntityId, AZ::Entity*>;
  22. using NetEntityIdMap = AZStd::unordered_map<AZ::EntityId, NetEntityId>;
  23. using NetBindingMap = AZStd::unordered_map<AZ::Entity*, NetBindComponent*>;
  24. using iterator = EntityMap::iterator;
  25. using const_iterator = EntityMap::const_iterator;
  26. NetworkEntityTracker() = default;
  27. //! Adds a networked entity to the tracker.
  28. //! @param netEntityId the networkId of the entity to add
  29. //! @param entity pointer to the entity corresponding to the networkId
  30. void Add(NetEntityId netEntityId, AZ::Entity* entity);
  31. //! Registers a new NetBindComponent with the NetworkEntityTracker.
  32. //! @param entity pointer to the entity we are registering the NetBindComponent for
  33. //! @param component pointer to the NetBindComponent being registered
  34. void RegisterNetBindComponent(AZ::Entity* entity, NetBindComponent* component);
  35. //! Unregisters a NetBindComponent from the NetworkEntityTracker.
  36. //! @param component pointer to the NetBindComponent being removed
  37. void UnregisterNetBindComponent(NetBindComponent* component);
  38. //! Returns an entity handle which can validate entity existence.
  39. NetworkEntityHandle Get(NetEntityId netEntityId);
  40. ConstNetworkEntityHandle Get(NetEntityId netEntityId) const;
  41. //! Returns Net Entity ID for a given AZ Entity ID.
  42. NetEntityId Get(const AZ::EntityId& entityId) const;
  43. //! Returns true if the netEntityId exists.
  44. bool Exists(NetEntityId netEntityId) const;
  45. //! Get a raw pointer of an entity.
  46. AZ::Entity *GetRaw(NetEntityId netEntityId) const;
  47. //! Moves the given iterator out of the entity holder and returns the ptr.
  48. AZ::Entity *Move(EntityMap::iterator iter);
  49. //! Retrieves the NetBindComponent for the provided AZ::Entity, nullptr if the entity does not have netbinding.
  50. //! @param entity pointer to the entity to retrieve the NetBindComponent for
  51. //! @return pointer to the entities NetBindComponent, or nullptr if the entity doesn't exist or does not have netbinding
  52. NetBindComponent* GetNetBindComponent(AZ::Entity* rawEntity) const;
  53. //! Container overloads
  54. //!@{
  55. iterator begin();
  56. const_iterator begin() const;
  57. iterator end();
  58. const_iterator end() const;
  59. iterator find(NetEntityId netEntityId);
  60. const_iterator find(NetEntityId netEntityId) const;
  61. void erase(NetEntityId netEntityId);
  62. iterator erase(EntityMap::iterator iter);
  63. AZStd::size_t size() const;
  64. void clear();
  65. //! @}
  66. //! Dirty tracking optimizations to avoid unnecessary hash lookups.
  67. //! There are two counts, one for adds and one for deletes
  68. //! If an entity is nullptr, check adds to check to see if our entity was added again
  69. //! If an entity is not nullptr, check removes which reminds us to see if the entity no longer exists
  70. //! Passing in the entity into this helper assists in retrieving the correct count, so we do not need to store both counts inside each handle
  71. uint32_t GetChangeDirty(const AZ::Entity* entity) const;
  72. uint32_t GetDeleteChangeDirty() const;
  73. uint32_t GetAddChangeDirty() const;
  74. //! Prevent copying and heap allocation.
  75. AZ_DISABLE_COPY_MOVE(NetworkEntityTracker);
  76. private:
  77. EntityMap m_entityMap;
  78. NetEntityIdMap m_netEntityIdMap;
  79. NetBindingMap m_netBindingMap;
  80. uint32_t m_deleteChangeDirty = 0;
  81. uint32_t m_addChangeDirty = 0;
  82. };
  83. }
  84. #include "Source/NetworkEntity/NetworkEntityTracker.inl"