Entity migrations now totally functional again, plus some fixes to network rigid bodies to make them work properly as they migrate around

Signed-off-by: kberg-amzn <karlberg@amazon.com>
This commit is contained in:
kberg-amzn
2021-09-30 22:53:51 -07:00
parent 02bc89cd92
commit 8d993494f6
27 changed files with 277 additions and 155 deletions
@@ -13,6 +13,7 @@
#include <Multiplayer/NetworkEntity/NetworkEntityRpcMessage.h>
#include <Multiplayer/NetworkEntity/NetworkEntityUpdateMessage.h>
#include <Multiplayer/NetworkInput/NetworkInput.h>
#include <Source/NetworkEntity/NetworkEntityTracker.h>
#include <AzCore/Console/IConsole.h>
#include <AzCore/Console/ILogger.h>
#include <AzCore/Interface/Interface.h>
@@ -58,7 +59,7 @@ namespace Multiplayer
return false;
}
NetBindComponent* netBindComponent = entity-> FindComponent<NetBindComponent>();
NetBindComponent* netBindComponent = GetNetworkEntityTracker()->GetNetBindComponent(entity);
if (!netBindComponent)
{
AZ_Warning( "NetBindComponent", false, "NetBindComponent IsNetEntityRoleAuthority failed. Entity '%s' (id: %s) is missing a NetBindComponent, make sure this entity contains a component which derives from NetBindComponent.", entity->GetName().c_str(), id.ToString().c_str())
@@ -75,7 +76,7 @@ namespace Multiplayer
return false;
}
NetBindComponent* netBindComponent = entity->FindComponent<NetBindComponent>();
NetBindComponent* netBindComponent = GetNetworkEntityTracker()->GetNetBindComponent(entity);
if (!netBindComponent)
{
AZ_Warning("NetBindComponent", false, "NetBindComponent IsNetEntityRoleAutonomous failed. Entity '%s' (id: %s) is missing a NetBindComponent, make sure this entity contains a component which derives from NetBindComponent.", entity->GetName().c_str(), id.ToString().c_str())
@@ -92,7 +93,7 @@ namespace Multiplayer
return false;
}
NetBindComponent* netBindComponent = entity->FindComponent<NetBindComponent>();
NetBindComponent* netBindComponent = GetNetworkEntityTracker()->GetNetBindComponent(entity);
if (!netBindComponent)
{
AZ_Warning("NetBindComponent", false, "NetBindComponent IsNetEntityRoleClient failed. Entity '%s' (id: %s) is missing a NetBindComponent, make sure this entity contains a component which derives from NetBindComponent.", entity->GetName().c_str(), id.ToString().c_str())
@@ -109,7 +110,7 @@ namespace Multiplayer
return false;
}
NetBindComponent* netBindComponent = entity->FindComponent<NetBindComponent>();
NetBindComponent* netBindComponent = GetNetworkEntityTracker()->GetNetBindComponent(entity);
if (!netBindComponent)
{
AZ_Warning("NetBindComponent", false, "NetBindComponent IsNetEntityRoleServer failed. Entity '%s' (id: %s) is missing a NetBindComponent, make sure this entity contains a component which derives from NetBindComponent.", entity->GetName().c_str(), id.ToString().c_str())
@@ -171,6 +172,8 @@ namespace Multiplayer
{
GetNetworkEntityManager()->NotifyControllersDeactivated(m_netEntityHandle, EntityIsMigrating::False);
}
GetNetworkEntityTracker()->UnregisterNetBindComponent(this);
}
NetEntityRole NetBindComponent::GetNetEntityRole() const
@@ -502,6 +505,9 @@ namespace Multiplayer
m_netEntityId = netEntityId;
m_netEntityRole = netEntityRole;
m_prefabEntityId = prefabEntityId;
GetNetworkEntityTracker()->RegisterNetBindComponent(entity, this);
m_netEntityHandle = GetNetworkEntityManager()->AddEntityToEntityMap(m_netEntityId, entity);
for (AZ::Component* component : entity->GetComponents())
@@ -57,16 +57,13 @@ namespace Multiplayer
NetworkRigidBodyRequestBus::Handler::BusConnect(GetEntityId());
GetNetBindComponent()->AddEntitySyncRewindEventHandler(m_syncRewindHandler);
GetEntity()->FindComponent<AzFramework::TransformComponent>()->BindTransformChangedEventHandler(m_transformChangedHandler);
GetEntity()->GetTransform()->BindTransformChangedEventHandler(m_transformChangedHandler);
m_physicsRigidBodyComponent =
Physics::RigidBodyRequestBus::FindFirstHandler(GetEntity()->GetId());
AZ_Assert(m_physicsRigidBodyComponent, "PhysX Rigid Body Component is required on entity %s", GetEntity()->GetName().c_str());
if (!HasController())
{
m_physicsRigidBodyComponent->SetKinematic(true);
}
// By default we're kinematic, activating a controller will allow us to simulate
m_physicsRigidBodyComponent->SetKinematic(true);
}
void NetworkRigidBodyComponent::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
@@ -121,18 +118,26 @@ namespace Multiplayer
NetworkRigidBodyComponentController::NetworkRigidBodyComponentController(NetworkRigidBodyComponent& parent)
: NetworkRigidBodyComponentControllerBase(parent)
, m_transformChangedHandler([this](const AZ::Transform&, const AZ::Transform&) { OnTransformUpdate(); })
{
;
}
void NetworkRigidBodyComponentController::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
{
;
GetParent().m_physicsRigidBodyComponent->SetKinematic(false);
if (IsAuthority())
{
AzPhysics::RigidBody* rigidBody = GetParent().m_physicsRigidBodyComponent->GetRigidBody();
rigidBody->SetLinearVelocity(GetLinearVelocity());
rigidBody->SetAngularVelocity(GetAngularVelocity());
GetEntity()->GetTransform()->BindTransformChangedEventHandler(m_transformChangedHandler);
}
}
void NetworkRigidBodyComponentController::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
{
;
GetParent().m_physicsRigidBodyComponent->SetKinematic(true);
}
void NetworkRigidBodyComponentController::HandleSendApplyImpulse
@@ -145,4 +150,11 @@ namespace Multiplayer
AzPhysics::RigidBody* rigidBody = GetParent().m_physicsRigidBodyComponent->GetRigidBody();
rigidBody->ApplyLinearImpulseAtWorldPoint(impulse, worldPoint);
}
void NetworkRigidBodyComponentController::OnTransformUpdate()
{
AzPhysics::RigidBody* rigidBody = GetParent().m_physicsRigidBodyComponent->GetRigidBody();
SetLinearVelocity(rigidBody->GetLinearVelocity());
SetAngularVelocity(rigidBody->GetAngularVelocity());
}
} // namespace Multiplayer