Revamped AzFramework::Scene (#332)

Updated AzFramework::Scene to allow it to serve as the one-stop location for localized singletons. Localized singletons in this case are instance that can only occur once in an environment but multiple times within an application. As an example, this allows settings up a single camera per viewport for instance.

Highlights of changes:

Replaced the original ebuses with interfaces and events for easy of use and performance.
Removed the Entity Context specific code and moved that to new locations within the Entity Context itself.
Allowed basic inheritance. If a subsystem isn't found in a scene the parent can optionally be searched.
Scenes can enter a zombie state and avoid immediately being deleted. This is needed for situations where subsystems can't be destroyed until async calls have been completed.
This commit is contained in:
AMZN-koppersr
2021-04-29 08:50:48 -07:00
committed by GitHub
parent 45fd86c2bf
commit 5e4094b258
42 changed files with 757 additions and 529 deletions
@@ -18,6 +18,8 @@
#include <AzCore/Component/TickBus.h>
#include <AzCore/Debug/Profiler.h>
#include <AzCore/std/containers/stack.h>
#include <AzFramework/Scene/Scene.h>
#include <AzFramework/Scene/SceneSystemInterface.h>
#include "EntityContext.h"
@@ -37,6 +39,30 @@ namespace AzFramework
}
}
AZStd::shared_ptr<Scene> EntityContext::FindContainingScene(const EntityContextId& contextId)
{
auto sceneSystem = SceneSystemInterface::Get();
AZ_Assert(sceneSystem, "Attempting to retrieve the scene containing a entity context before the scene system is available.");
AZStd::shared_ptr<Scene> result;
sceneSystem->IterateActiveScenes([&result, &contextId](const AZStd::shared_ptr<Scene>& scene)
{
EntityContext** entityContext = scene->FindSubsystemInScene<EntityContext::SceneStorageType>();
if (entityContext && (*entityContext)->GetContextId() == contextId)
{
result = scene;
// Result found, returning.
return false;
}
else
{
// No match, continuing to search for containing scene.
return true;
}
});
return result;
}
//=========================================================================
// EntityContext ctor
//=========================================================================
@@ -17,6 +17,7 @@
#include <AzCore/Component/EntityBus.h>
#include <AzCore/Serialization/ObjectStream.h>
#include <AzCore/Serialization/IdUtils.h>
#include <AzCore/std/smart_ptr/shared_ptr.h>
#include <AzFramework/Entity/EntityContextBus.h>
#include <AzFramework/Entity/SliceEntityOwnershipService.h>
@@ -27,7 +28,7 @@ namespace AZ
namespace AzFramework
{
class EntityContext;
class Scene;
/**
* Provides services for a group of entities under the umbrella of a given context.
@@ -47,9 +48,11 @@ namespace AzFramework
, public EntityOwnershipServiceNotificationBus::Handler
{
public:
AZ_TYPE_INFO(EntityContext, "{4F98A6B9-C7B5-450E-8A8A-30EEFC411EF5}");
/// The type used to store entity in AzFramework::Scene.
using SceneStorageType = EntityContext*;
EntityContext(AZ::SerializeContext* serializeContext = nullptr);
EntityContext(const EntityContextId& contextId, AZ::SerializeContext* serializeContext = nullptr);
EntityContext(const EntityContextId& contextId, AZStd::unique_ptr<EntityOwnershipService> entityOwnershipService,
@@ -75,6 +78,7 @@ namespace AzFramework
//////////////////////////////////////////////////////////////////////////
static void Reflect(AZ::ReflectContext* context);
static AZStd::shared_ptr<Scene> FindContainingScene(const EntityContextId& contextId);
protected:
@@ -66,6 +66,8 @@ namespace AzFramework
*/
virtual EntityContextId GetGameEntityContextId() = 0;
virtual EntityContext* GetGameEntityContextInstance() = 0;
/**
* Creates an entity in the game context.
* @param name A name for the new entity.
@@ -51,6 +51,7 @@ namespace AzFramework
//////////////////////////////////////////////////////////////////////////
// GameEntityContextRequestBus
AZ::Uuid GetGameEntityContextId() override { return GetContextId(); }
EntityContext* GetGameEntityContextInstance() override { return this; }
void ResetGameContext() override;
AZ::Entity* CreateGameEntity(const char* name) override;
BehaviorEntity CreateGameEntityForBehaviorContext(const char* name) override;