Added the ability to look up a Net Entity ID for a given AZ Entity ID

Signed-off-by: pereslav <pereslav@amazon.com>
This commit is contained in:
pereslav
2021-08-19 16:57:32 +01:00
parent f76d09e215
commit 92a46c705e
6 changed files with 38 additions and 1 deletions
@@ -89,6 +89,11 @@ namespace Multiplayer
//! @return the total number of entities tracked by this INetworkEntityManager instance
virtual uint32_t GetEntityCount() const = 0;
//! Returns the Net Entity ID for a given AZ Entity ID.
//! @param entityId the AZ Entity ID
//! @return the Net Entity ID
virtual NetEntityId GetNetEntityIdById(const AZ::EntityId& entityId) const = 0;
//! Adds the provided entity to the internal entity map identified by the provided netEntityId.
//! @param netEntityId the identifier to use for the added entity
//! @param entity the entity to add to the internal entity map
@@ -73,6 +73,11 @@ namespace Multiplayer
return m_networkEntityTracker.Get(netEntityId);
}
NetEntityId NetworkEntityManager::GetNetEntityIdById(const AZ::EntityId& entityId) const
{
return m_networkEntityTracker.Get(entityId);
}
uint32_t NetworkEntityManager::GetEntityCount() const
{
return static_cast<uint32_t>(m_networkEntityTracker.size());
@@ -41,6 +41,7 @@ namespace Multiplayer
MultiplayerComponentRegistry* GetMultiplayerComponentRegistry() override;
HostId GetHostId() const override;
ConstNetworkEntityHandle GetEntity(NetEntityId netEntityId) const override;
NetEntityId GetNetEntityIdById(const AZ::EntityId& entityId) const override;
EntityList CreateEntitiesImmediate(const AzFramework::Spawnable& spawnable, NetEntityRole netEntityRole);
EntityList CreateEntitiesImmediate
@@ -18,6 +18,7 @@ namespace Multiplayer
++m_addChangeDirty;
AZ_Assert(m_entityMap.end() == m_entityMap.find(netEntityId), "Attempting to add the same entity to the entity map multiple times");
m_entityMap[netEntityId] = entity;
m_netEntityIdMap[entity->GetId()] = netEntityId;
}
NetworkEntityHandle NetworkEntityTracker::Get(NetEntityId netEntityId)
@@ -32,6 +33,16 @@ namespace Multiplayer
return ConstNetworkEntityHandle(entity, netEntityId, this);
}
NetEntityId NetworkEntityTracker::Get(const AZ::EntityId& entityId) const
{
auto found = m_netEntityIdMap.find(entityId);
if (found != m_netEntityIdMap.end())
{
return found->second;
}
return Multiplayer::InvalidNetEntityId;
}
bool NetworkEntityTracker::Exists(NetEntityId netEntityId) const
{
return (m_entityMap.find(netEntityId) != m_entityMap.end());
@@ -50,12 +61,22 @@ namespace Multiplayer
void NetworkEntityTracker::erase(NetEntityId netEntityId)
{
++m_deleteChangeDirty;
m_entityMap.erase(netEntityId);
auto found = m_entityMap.find(netEntityId);
if (found != m_entityMap.end())
{
m_netEntityIdMap.erase(found->second->GetId());
m_entityMap.erase(found);
}
}
NetworkEntityTracker::EntityMap::iterator NetworkEntityTracker::erase(EntityMap::iterator iter)
{
++m_deleteChangeDirty;
if (iter != m_entityMap.end())
{
m_netEntityIdMap.erase(iter->second->GetId());
}
return m_entityMap.erase(iter);
}
@@ -22,6 +22,7 @@ namespace Multiplayer
public:
using EntityMap = AZStd::unordered_map<NetEntityId, AZ::Entity*>;
using NetEntityIdMap = AZStd::unordered_map<AZ::EntityId, NetEntityId>;
using iterator = EntityMap::iterator;
using const_iterator = EntityMap::const_iterator;
@@ -36,6 +37,8 @@ namespace Multiplayer
NetworkEntityHandle Get(NetEntityId netEntityId);
ConstNetworkEntityHandle Get(NetEntityId netEntityId) const;
NetEntityId Get(const AZ::EntityId& entityId) const;
//! Returns true if the netEntityId exists.
bool Exists(NetEntityId netEntityId) const;
@@ -74,6 +77,7 @@ namespace Multiplayer
private:
EntityMap m_entityMap;
NetEntityIdMap m_netEntityIdMap;
uint32_t m_deleteChangeDirty = 0;
uint32_t m_addChangeDirty = 0;
};
@@ -48,6 +48,7 @@ namespace Multiplayer
inline void NetworkEntityTracker::clear()
{
m_entityMap.clear();
m_netEntityIdMap.clear();
}
inline uint32_t NetworkEntityTracker::GetChangeDirty(const AZ::Entity* entity) const