Просмотр исходного кода

Add NetworkRandomComponent

Signed-off-by: puvvadar <[email protected]>
puvvadar 4 лет назад
Родитель
Сommit
cc38a6ebc5

+ 14 - 0
Gem/Code/Source/AutoGen/NetworkRandomComponent.AutoComponent.xml

@@ -0,0 +1,14 @@
+<?xml version="1.0"?>
+
+<Component
+    Name="NetworkRandomComponent" 
+    Namespace="MultiplayerSample" 
+    OverrideComponent="true" 
+    OverrideController="true" 
+    OverrideInclude="Source/Components/NetworkRandomComponent.h"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+
+    <NetworkProperty Type="uint64_t" Init="0" Name="Seed" ReplicateFrom="Authority" ReplicateTo="Client" Container="Object" IsPublic="true" IsRewindable="false" IsPredictable="false" ExposeToEditor="false" ExposeToScript="false" GenerateEventBindings="true" Description="The RNG seed" />
+    <NetworkProperty Type="int" Init="0" Name="Random" ReplicateFrom="Authority" ReplicateTo="Client" Container="Object" IsPublic="true" IsRewindable="true" IsPredictable="true" ExposeToEditor="false" ExposeToScript="false" GenerateEventBindings="true" Description="The random generated from the seed" />
+
+</Component>

+ 88 - 0
Gem/Code/Source/Components/NetworkRandomComponent.cpp

@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) Contributors to the Open 3D Engine Project. For complete copyright and license terms please see the LICENSE at the root of this distribution.
+ *
+ * SPDX-License-Identifier: Apache-2.0 OR MIT
+ *
+ */
+
+#include <Source/Components/NetworkRandomComponent.h>
+
+namespace MultiplayerSample
+{
+    void NetworkRandomComponent::NetworkRandomComponent::Reflect(AZ::ReflectContext* context)
+    {
+        AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
+        if (serializeContext)
+        {
+            serializeContext->Class<NetworkRandomComponent, NetworkRandomComponentBase>()
+                ->Version(1);
+        }
+        NetworkRandomComponentBase::Reflect(context);
+    }
+
+    NetworkRandomComponent::NetworkRandomComponent()
+        : m_seedEventHandler([this](const uint64_t& seed) { OnSeedChangedEvent(seed); })
+    {
+        if (IsNetEntityRoleAuthority())
+        {
+            // Setup seed on authority for proxies to pull
+            AZ::BetterPseudoRandom seedGenerator;
+            uint64_t seed = 0;
+            seedGenerator.GetRandom(seed);
+            static_cast<NetworkRandomComponentController*>(GetController())->SetSeed(seed);
+            m_simpleRng.SetSeed(seed);
+            m_seedInitialized = true;
+        };
+    }
+
+    void NetworkRandomComponent::OnInit()
+    {
+        
+    }
+
+    void NetworkRandomComponent::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
+    {
+        SeedAddEvent(m_seedEventHandler);
+    }
+
+    void NetworkRandomComponent::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
+    {
+        ;
+    }
+
+    void NetworkRandomComponent::RollRandom()
+    {
+        Multiplayer::INetworkTime* networkTime = Multiplayer::GetNetworkTime();
+        // We should not need to roll rands in the past, history should be relied upon instead
+        if (!networkTime->IsTimeRewound())
+        {
+            AZ_Assert(m_seedInitialized, "RNG Seed not initialized");
+            int rand = m_simpleRng.GetRandom();
+            static_cast<NetworkRandomComponentController*>(GetController())->SetRandom(rand);
+        }
+    }
+
+    void NetworkRandomComponent::OnSeedChangedEvent([[maybe_unused]] const uint64_t& seed)
+    {
+        // Proxy hook to set rng seed
+        static_cast<NetworkRandomComponentController*>(GetController())->SetSeed(seed);
+        m_simpleRng.SetSeed(seed);
+        m_seedInitialized = true;
+    }
+
+    NetworkRandomComponentController::NetworkRandomComponentController(NetworkRandomComponent& parent)
+        : NetworkRandomComponentControllerBase(parent)
+    {
+        ;
+    }
+
+    void NetworkRandomComponentController::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
+    {
+        ;
+    }
+
+    void NetworkRandomComponentController::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
+    {
+        ;
+    }
+}

+ 49 - 0
Gem/Code/Source/Components/NetworkRandomComponent.h

@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) Contributors to the Open 3D Engine Project. For complete copyright and license terms please see the LICENSE at the root of this distribution.
+ *
+ * SPDX-License-Identifier: Apache-2.0 OR MIT
+ *
+ */
+
+#pragma once
+
+#include <Source/AutoGen/NetworkRandomComponent.AutoComponent.h>
+
+#include <AzCore/Math/Random.h>
+
+namespace MultiplayerSample
+{
+    class NetworkRandomComponent
+        : public NetworkRandomComponentBase
+    {
+    public:
+        AZ_MULTIPLAYER_COMPONENT(MultiplayerSample::NetworkRandomComponent, s_networkRandomComponentConcreteUuid, MultiplayerSample::NetworkRandomComponentBase);
+
+        static void Reflect(AZ::ReflectContext* context);
+
+        NetworkRandomComponent();
+
+        void OnInit() override;
+        void OnActivate(Multiplayer::EntityIsMigrating entityIsMigrating) override;
+        void OnDeactivate(Multiplayer::EntityIsMigrating entityIsMigrating) override;
+
+        void RollRandom();
+
+    private:
+        void OnSeedChangedEvent(const uint64_t& seed);
+
+        AZ::Event<uint64_t>::Handler m_seedEventHandler;
+        AZ::SimpleLcgRandom m_simpleRng;
+        bool m_seedInitialized = false;
+    };
+
+    class NetworkRandomComponentController
+        : public NetworkRandomComponentControllerBase
+    {
+    public:
+        NetworkRandomComponentController(NetworkRandomComponent& parent);
+
+        void OnActivate(Multiplayer::EntityIsMigrating entityIsMigrating) override;
+        void OnDeactivate(Multiplayer::EntityIsMigrating entityIsMigrating) override;
+    };
+}

+ 3 - 0
Gem/Code/multiplayersample_files.cmake

@@ -11,6 +11,7 @@ set(FILES
     Source/AutoGen/NetworkHealthComponent.AutoComponent.xml
     Source/AutoGen/NetworkHitVolumesComponent.AutoComponent.xml
     Source/AutoGen/NetworkPlayerSpawnerComponent.AutoComponent.xml
+    Source/AutoGen/NetworkRandomComponent.AutoComponent.xml
     Source/AutoGen/NetworkRigidBodyComponent.AutoComponent.xml
     Source/AutoGen/NetworkWeaponsComponent.AutoComponent.xml
     Source/AutoGen/SimplePlayerCameraComponent.AutoComponent.xml
@@ -27,6 +28,8 @@ set(FILES
     Source/Components/NetworkHitVolumesComponent.h
     Source/Components/NetworkPlayerSpawnerComponent.cpp
     Source/Components/NetworkPlayerSpawnerComponent.h
+    Source/Components/NetworkRandomComponent.cpp
+    Source/Components/NetworkRandomComponent.h
     Source/Components/NetworkRigidBodyComponent.cpp
     Source/Components/NetworkRigidBodyComponent.h
     Source/Components/NetworkWeaponsComponent.cpp