72 lines
1.8 KiB
C++
72 lines
1.8 KiB
C++
#pragma once
|
|
|
|
#include <Core/Common/CoreUtils.h>
|
|
#include <Core/Math/Vector.h>
|
|
#include <Engine/Class.h>
|
|
#include <Entity/entity_common.h>
|
|
#include <Entity/EntityManager.h>
|
|
|
|
struct Entity final
|
|
{
|
|
DECLARE_CLASS()
|
|
|
|
EntityID ID = 0;
|
|
const Class* derived_kind = nullptr;
|
|
DerivedType derived = nullptr;
|
|
Vector4 position = {};
|
|
bool is_dirty = false;
|
|
};
|
|
|
|
// Can reinterpret cast to this to have the offset of Base and Kind for any entity
|
|
struct EntityTemplate
|
|
{
|
|
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)
|
|
{
|
|
Entity* base_ptr = allocate_entity(manager, EntityType::kind);
|
|
Assert(base_ptr);
|
|
|
|
base_ptr->ID = EntityManager::ID++;
|
|
|
|
base_ptr->position.x = x;
|
|
base_ptr->position.y = y;
|
|
base_ptr->position.z = z;
|
|
base_ptr->position.w = 1.0f;
|
|
|
|
return (EntityType*)base_ptr->derived;
|
|
}
|
|
|
|
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);
|
|
|
|
[[nodiscard]] const Class* find_class_by_name(String name);
|
|
|
|
[[nodiscard]] const Class* resolve_entity_class(uint8 kind, uint32 crc);
|