Files
Juliet/Game/Entity/Entity.h
T

115 lines
3.0 KiB
C++

#pragma once
#include <Core/Common/CoreUtils.h>
#include <Core/Common/EnumUtils.h>
#include <Core/Math/Vector.h>
#include <Core/Memory/Allocator.h>
#include <Core/Memory/MemoryArena.h>
#include <Engine/Class.h>
#define DECLARE_ENTITY() \
Entity* base; \
static Class* kind;
#define DEFINE_ENTITY_VERSIONED(entity, version, serialize_fct) \
constexpr Class entityKind##entity = MakeClass(ConstString(#entity), (uint8)Entity_Type::entity, (version), \
&classKindEntity, sizeof(entity), alignof(entity), (serialize_fct)); \
Class* entity::kind = const_cast<Class*>(&entityKind##entity);
struct EntityManager;
using DerivedType = void*;
using EntityID = uint64_t;
struct Entity final
{
static Class* kind;
EntityID ID = 0;
Class* derived_kind = nullptr;
DerivedType derived = nullptr;
Vector4 position = {};
};
struct Inert
{
DECLARE_ENTITY()
index_t MeshInstance = indexMax;
};
// clang-format off
#define ENTITY_TYPE_LIST(X) X(Inert),
#define AS_ENUM(name) name
enum class Entity_Type : uint8
{
ENTITY_TYPE_LIST(AS_ENUM)
Count
};
#undef AS_ENUM
#define AS_STR(name) #name
inline const char* kEntity_type_names[] = {
ENTITY_TYPE_LIST(AS_STR)
"Count"
};
#undef AS_STR
#define AS_CLASS(name) name::kind
inline Class* kEntity_type_class_ptr[]
{
ENTITY_TYPE_LIST(AS_CLASS)
nullptr
};
#undef AS_CLASS
#define ENTITY(kind) ToUnderlying(Entity_Type::kind)
// clang-format on
// Can reinterpret cast to this to have the offset of Base and Kind for any entity
struct entity_template
{
DECLARE_ENTITY();
};
//
template <typename EntityType>
concept EntityConcept = requires(EntityType entity) {
{ EntityType::kind } -> std::convertible_to<const Class*>;
requires std::same_as<decltype(entity.base), Entity*>;
};
template <typename EntityType>
requires EntityConcept<EntityType>
[[nodiscard]] bool IsA(const Entity* entity)
{
Assert(entity != nullptr);
return entity->derived_kind == EntityType::kind;
}
template <typename EntityType>
requires EntityConcept<EntityType>
[[nodiscard]] EntityType* MakeEntity(EntityManager& manager, float x, float y, float z)
{
EntityType result;
Entity base;
base.position.x = x;
base.position.y = y;
base.position.z = z;
base.position.w = 1.0f;
base.derived_kind = EntityType::kind;
return (EntityType*)RegisterEntity(manager, &base, &result);
}
template <typename EntityType>
requires EntityConcept<EntityType>
[[nodiscard]] EntityType* DownCast(Entity* entity)
{
Assert(entity != nullptr);
Assert(IsA<EntityType>(entity));
return static_cast<EntityType*>(entity->derived);
}
void serialize(Archive& ar, NonNullPtr<Entity> entity);