NetworkEntityTracker.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #include <Source/NetworkEntity/NetworkEntityTracker.h>
  9. #include <Multiplayer/NetworkEntity/NetworkEntityHandle.h>
  10. #include <Multiplayer/Components/NetBindComponent.h>
  11. #include <AzCore/Console/IConsole.h>
  12. #include <AzCore/Console/ILogger.h>
  13. namespace Multiplayer
  14. {
  15. void NetworkEntityTracker::Add(NetEntityId netEntityId, AZ::Entity* entity)
  16. {
  17. ++m_addChangeDirty;
  18. AZ_Assert(m_entityMap.end() == m_entityMap.find(netEntityId), "Attempting to add the same entity to the entity map multiple times");
  19. m_entityMap[netEntityId] = entity;
  20. m_netEntityIdMap[entity->GetId()] = netEntityId;
  21. }
  22. void NetworkEntityTracker::RegisterNetBindComponent(AZ::Entity* entity, NetBindComponent* component)
  23. {
  24. m_netBindingMap[entity] = component;
  25. }
  26. void NetworkEntityTracker::UnregisterNetBindComponent(NetBindComponent* component)
  27. {
  28. m_netBindingMap.erase(component->GetEntity());
  29. }
  30. NetworkEntityHandle NetworkEntityTracker::Get(NetEntityId netEntityId)
  31. {
  32. AZ::Entity* entity = GetRaw(netEntityId);
  33. return NetworkEntityHandle(entity, this);
  34. }
  35. ConstNetworkEntityHandle NetworkEntityTracker::Get(NetEntityId netEntityId) const
  36. {
  37. AZ::Entity* entity = GetRaw(netEntityId);
  38. return ConstNetworkEntityHandle(entity, this);
  39. }
  40. NetEntityId NetworkEntityTracker::Get(const AZ::EntityId& entityId) const
  41. {
  42. auto found = m_netEntityIdMap.find(entityId);
  43. if (found != m_netEntityIdMap.end())
  44. {
  45. return found->second;
  46. }
  47. return Multiplayer::InvalidNetEntityId;
  48. }
  49. bool NetworkEntityTracker::Exists(NetEntityId netEntityId) const
  50. {
  51. return (m_entityMap.find(netEntityId) != m_entityMap.end());
  52. }
  53. AZ::Entity* NetworkEntityTracker::GetRaw(NetEntityId netEntityId) const
  54. {
  55. auto found = m_entityMap.find(netEntityId);
  56. if (found != m_entityMap.end())
  57. {
  58. return found->second;
  59. }
  60. return nullptr;
  61. }
  62. void NetworkEntityTracker::erase(NetEntityId netEntityId)
  63. {
  64. ++m_deleteChangeDirty;
  65. auto found = m_entityMap.find(netEntityId);
  66. if (found != m_entityMap.end())
  67. {
  68. m_netEntityIdMap.erase(found->second->GetId());
  69. m_entityMap.erase(found);
  70. }
  71. }
  72. NetworkEntityTracker::EntityMap::iterator NetworkEntityTracker::erase(EntityMap::iterator iter)
  73. {
  74. ++m_deleteChangeDirty;
  75. if (iter != m_entityMap.end())
  76. {
  77. m_netEntityIdMap.erase(iter->second->GetId());
  78. }
  79. return m_entityMap.erase(iter);
  80. }
  81. AZ::Entity *NetworkEntityTracker::Move(EntityMap::iterator iter)
  82. {
  83. AZ::Entity *ptr = iter->second;
  84. erase(iter);
  85. return ptr;
  86. }
  87. }