Files
Seele/src/Engine/Actor/Entity.h
T

52 lines
2.5 KiB
C++
Raw Normal View History

#pragma once
#include "MinimalEngine.h"
#include "Scene/Scene.h"
2024-06-09 12:20:04 +02:00
#include <entt/entt.hpp>
#include <string>
#include <sstream>
2024-06-09 12:20:04 +02:00
namespace Seele {
// An entity describes a part of a scene
// It is just a wrapper the ID of a registry
2024-06-09 12:20:04 +02:00
class Entity {
public:
Entity(PScene scene);
virtual ~Entity();
// True if this entity currently has the given component attached. Pure handle-space
// (no entt::entity crosses the boundary) - the building block the editor's component
// inspector uses to ask "what is attached to this OEntity?" without ever touching the
// registry directly. Uses the entt try_get idiom: it returns a pointer to the component
// storage the entity holds, or null if the component is not attached - so the check is
// null-safe (no assertion) and works for any displayable component type.
template <typename Component> bool hasComponent() const { return scene->registry.try_get<Component>(identifier) != nullptr; }
// The entt record this handle wraps. Exposed (not as the old raw `getId()`, but scoped
// and documented as internal) so the *scene* can keep its roster in lock-step with the
// registry during Entity ctor/dtor. The editor never calls this - it works entirely in
// handle space via hasComponent/accessComponent.
entt::entity getIdentifier() const { return identifier; }
// A short, stable, human-facing label for tooling (the inspector's row text). Formatted
// *inside* the engine so the editor never has to name the entt::entity type at all -
// it just asks "what should I display for this OEntity?" and gets a string. This is the
// "display the ID of an entity" requirement satisfied in pure handle space: the numeric
// id is rendered to text here and never leaks out as an entt type.
std::string getLabel() const {
std::ostringstream oss;
oss << "#" << static_cast<uint32>(identifier);
return oss.str();
}
2024-06-09 12:20:04 +02:00
template <typename Component, typename... Args> Component& attachComponent(Args&&... args) {
2023-01-21 18:43:21 +01:00
return scene->attachComponent<Component>(identifier, std::forward<Args>(args)...);
}
2024-06-09 12:20:04 +02:00
template <typename Component> Component& accessComponent() { return scene->accessComponent<Component>(identifier); }
template <typename Component> const Component& accessComponent() const { return scene->accessComponent<Component>(identifier); }
protected:
PScene scene;
entt::entity identifier;
};
DEFINE_REF(Entity)
} // namespace Seele