Files
o3de/Gems/NvCloth/Code/Source/System/SystemComponent.h
T
2021-03-08 14:30:57 -08:00

98 lines
3.6 KiB
C++

/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#pragma once
#include <AzCore/Component/Component.h>
#include <AzCore/Component/TickBus.h>
#include <NvCloth/IClothSystem.h>
#include <NvCloth/ICloth.h>
#include <NvCloth/ISolver.h>
#include <System/Factory.h>
#include <System/Solver.h>
#include <System/Fabric.h>
#include <System/Cloth.h>
namespace NvCloth
{
//! Implementation of the IClothSystem interface.
//!
//! This class has the responsibility to initialize and tear down NvCloth library.
//! It owns all Solvers, Cloths and Fabrics, and it manages their creation and destruction.
//! It's also the responsible for updating (on Physics Tick) all the solvers that are not flagged as "user simulated".
class SystemComponent
: public AZ::Component
, protected IClothSystem
, protected AZ::TickBus::Handler
{
public:
AZ_COMPONENT(SystemComponent, "{89DF5C48-64AC-4B8E-9E61-0D4C7A7B5491}");
static void Reflect(AZ::ReflectContext* context);
static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
static void InitializeNvClothLibrary();
static void TearDownNvClothLibrary();
//! Returns true when there is no error reported.
static bool CheckLastClothError();
//! Resets the last error reported by NvCloth.
static void ResetLastClothError();
protected:
// AZ::Component overrides ...
void Activate() override;
void Deactivate() override;
// IClothSystem overrides ...
ISolver* FindOrCreateSolver(const AZStd::string& name) override;
void DestroySolver(ISolver*& solver) override;
ISolver* GetSolver(const AZStd::string& name) override;
ICloth* CreateCloth(
const AZStd::vector<SimParticleFormat>& initialParticles,
const FabricCookedData& fabricCookedData) override;
void DestroyCloth(ICloth*& cloth) override;
ICloth* GetCloth(ClothId clothId) override;
bool AddCloth(ICloth* cloth, const AZStd::string& solverName = DefaultSolverName) override;
void RemoveCloth(ICloth* cloth) override;
// AZ::TickBus::Handler overrides ...
void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
int GetTickOrder() override;
private:
void InitializeSystem();
void DestroySystem();
FabricId FindOrCreateFabric(const FabricCookedData& fabricCookedData);
void DestroyFabric(FabricId fabricId);
// Factory that creates all the solvers, fabric and cloths.
AZStd::unique_ptr<Factory> m_factory;
// List of all the solvers created.
AZStd::vector<AZStd::unique_ptr<Solver>> m_solvers;
// List of all the fabrics created.
AZStd::unordered_map<FabricId, AZStd::unique_ptr<Fabric>> m_fabrics;
// List of all the cloths created.
AZStd::unordered_map<ClothId, AZStd::unique_ptr<Cloth>> m_cloths;
};
} // namespace NvCloth