Przeglądaj źródła

Clear counter on physics deinitialization

Michał Pełka 2 lat temu
rodzic
commit
3b0dfb6163

+ 26 - 0
Project/Gem/Source/FruitStorage/FruitStorageComponent.cpp

@@ -17,14 +17,34 @@
 
 namespace AppleKraken
 {
+
+    FruitStorageComponent::FruitStorageComponent()
+    {
+
+        m_postSimulateHandler = AzPhysics::SystemEvents::OnPostsimulateEvent::Handler(
+        [this](float deltaTime)
+        {
+            AZ_Printf("FruitStorageComponent", "FruitStorageComponentClear\n");
+            AppleKraken::FruitStorageComponent::ResetCounter();
+            m_postSimulateHandler.Disconnect();
+        });
+    }
+
     void FruitStorageComponent::Activate()
     {
         FruitStorageRequestsBus::Handler::BusConnect(GetEntityId());
         AZ::TickBus::Handler::BusConnect();
+        auto* physicsSystem = AZ::Interface<AzPhysics::SystemInterface>::Get();
+        if (physicsSystem)
+        {
+            physicsSystem->RegisterPostSimulateEvent(m_postSimulateHandler);
+        }
+
     }
 
     void FruitStorageComponent::Deactivate()
     {
+
         FruitStorageRequestsBus::Handler::BusDisconnect(GetEntityId());
     }
 
@@ -180,6 +200,12 @@ namespace AppleKraken
         displayNumberOfApples(m_allApplesGathered);
         AZ::TickBus::Handler::BusDisconnect();
     }
+
+    void FruitStorageComponent::ResetCounter(){
+        FruitStorageComponent::m_allApplesGathered.store(0);
+    }
+
     const AZStd::string FruitStorageComponent::kAppleGatheredElementName{"AppleGatheredElement"};
     AZStd::atomic_int32_t FruitStorageComponent::m_allApplesGathered {0};
+
 } // namespace AppleKraken

+ 5 - 1
Project/Gem/Source/FruitStorage/FruitStorageComponent.h

@@ -8,6 +8,7 @@
 #pragma once
 
 #include "FruitStorageBus.h"
+#include <AzFramework/Physics/PhysicsSystem.h>
 #include <AzCore/Component/Component.h>
 #include <AzFramework/AzFrameworkModule.h>
 #include <AzFramework/Spawnable/SpawnableEntitiesInterface.h>
@@ -24,7 +25,7 @@ namespace AppleKraken
         AZ_COMPONENT(FruitStorageComponent, "{9AC0B456-9C29-4EDD-AD25-6FAA57D253C5}", AZ::Component, FruitStorageRequestsBus::Handler);
 
         // AZ::Component interface implementation.
-        FruitStorageComponent() = default;
+        FruitStorageComponent();
         ~FruitStorageComponent() = default;
         void Activate() override;
         void Deactivate() override;
@@ -34,6 +35,8 @@ namespace AppleKraken
         uint32_t GetCurrentStorageAppleCount() const override;
         void AddApple(const Tags& tags = {}) override;
 
+        static void ResetCounter();
+
     private:
         void SpawnCrate();
         void PreSpawn(AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableEntityContainerView);
@@ -49,5 +52,6 @@ namespace AppleKraken
         ApplesGatheredByTag m_tagsStored;
         AZ::EntityId m_ui_entity;
         const static AZStd::string kAppleGatheredElementName;
+        AzPhysics::SystemEvents::OnPostsimulateEvent ::Handler m_postSimulateHandler;
     };
 } // namespace AppleKraken