Files
Seele/src/Engine/Scene/Scene.h
T

70 lines
3.5 KiB
C++
Raw Normal View History

2020-05-05 01:51:13 +02:00
#pragma once
2025-04-06 09:57:47 +02:00
#include "MinimalEngine.h"
2024-06-09 12:20:04 +02:00
#include "Graphics/Graphics.h"
#include "Physics/PhysicsSystem.h"
#include <entt/entt.hpp>
namespace Seele {
2021-04-01 16:40:14 +02:00
DECLARE_REF(Material)
DECLARE_REF(Entity)
2026-04-15 16:56:19 +02:00
DECLARE_REF(FluidScene)
DECLARE_REF(LightEnvironment)
2024-06-09 12:20:04 +02:00
class Scene {
public:
2020-09-19 14:36:50 +02:00
Scene(Gfx::PGraphics graphics);
2020-04-15 02:03:56 +02:00
~Scene();
2024-07-18 11:45:56 +02:00
void bakeLighting();
2023-01-21 18:43:21 +01:00
void update(float deltaTime);
2024-06-09 12:20:04 +02:00
entt::entity createEntity() { return registry.create(); }
// Out-of-line (Scene.cpp): the bodies need the complete Entity type (they call
// getIdentifier() on roster entries). Scene.h only forward-declares Entity
// (DECLARE_REF) to keep the header light and to avoid a circular include -
// Entity.h includes Scene.h, so Scene.h must not pull Actor/Entity.h back in.
void destroyEntity(entt::entity identifier);
// The scene is the single source of truth for which entities exist. The editor's
// inspector enumerates this roster (non-owning views) to build its entity list - the
// whole point of "the scene owns the entity". Populated as entities are created and
// pruned as they are destroyed; never points at a live handle that outlives its record.
const Array<PEntity>& getEntities() const { return entities; }
// Internal: called by Entity's ctor/dtor to keep the roster in lock-step with the
// registry. Not part of the public "create/destroy" surface (use createEntity /
// destroyEntity for that).
void registerEntity(entt::entity identifier, PEntity self) { entities.add(self); }
// Out-of-line (Scene.cpp): calls getIdentifier() on roster entries -> needs complete Entity.
void unregisterEntity(entt::entity identifier);
2024-06-09 12:20:04 +02:00
template <typename Component, typename... Args> Component& attachComponent(entt::entity entity, Args&&... args) {
2023-01-21 18:43:21 +01:00
return registry.emplace<Component>(entity, std::forward<Args>(args)...);
}
2024-06-09 12:20:04 +02:00
template <typename Component> Component& accessComponent(entt::entity entity) { return registry.get<Component>(entity); }
template <typename Component> const Component& accessComponent(entt::entity entity) const { return registry.get<Component>(entity); }
template <typename... Component, typename Func>
void view(Func func)
requires std::is_invocable_v<Func, Component&...> || std::is_invocable_v<Func, entt::entity, Component&...>
2023-01-29 18:58:59 +01:00
{
2023-10-24 15:01:09 +02:00
registry.view<Component...>().each(func);
2023-01-29 18:58:59 +01:00
}
2024-06-09 12:20:04 +02:00
template <typename Component> auto constructCallback() { return registry.on_construct<Component>(); }
template <typename Component> auto destroyCallback() { return registry.on_destroy<Component>(); }
2023-10-26 18:37:29 +02:00
PLightEnvironment getLightEnvironment() { return lightEnv; }
2026-04-12 20:49:02 +02:00
PFluidScene getFluidScene() { return fluidScene; }
2023-01-29 18:58:59 +01:00
Gfx::PGraphics getGraphics() const { return graphics; }
entt::registry registry;
2024-06-09 12:20:04 +02:00
private:
2023-11-06 14:47:21 +01:00
Gfx::PGraphics graphics;
// The roster: the scene's single source of truth for which entities currently exist.
// Non-owning views (P* = RefPtr, a plain pointer) - the *owning* O* (OwningPtr) held
// by the code that created each actor still owns the C++ object; the roster only tracks
// existence so tooling (the editor's inspector) can enumerate entities in handle space.
// Kept in lock-step by Entity's ctor (registerEntity) and dtor (routed through
// destroyEntity -> unregisterEntity), so it never points at a dead record.
Array<PEntity> entities;
2023-11-01 23:12:30 +01:00
OLightEnvironment lightEnv;
2026-04-12 20:49:02 +02:00
OFluidScene fluidScene;
2023-01-21 18:43:21 +01:00
PhysicsSystem physics;
2024-07-18 11:45:56 +02:00
Array<Gfx::OTexture2D> lightMaps;
2020-04-15 02:03:56 +02:00
};
DEFINE_REF(Scene)
2020-05-05 01:51:13 +02:00
} // namespace Seele