101 lines
2.7 KiB
C
101 lines
2.7 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>
|
|
|
|
typedef struct Entity Entity;
|
|
struct Entity
|
|
{
|
|
EntityID ID;
|
|
const Class* derived_kind;
|
|
DerivedType derived;
|
|
Vector4 position;
|
|
bool8 is_dirty;
|
|
};
|
|
DECLARE_CLASS(Entity)
|
|
|
|
// Can cast to this to have the offset of base for any entity
|
|
typedef struct EntityTemplate EntityTemplate;
|
|
struct EntityTemplate
|
|
{
|
|
DECLARE_ENTITY()
|
|
};
|
|
|
|
static inline EntityID make_entity_id(uint8 user_id, uint64 local_counter)
|
|
{
|
|
Assert(user_id != kInvalid_user_id);
|
|
Assert((local_counter & kEntityID_user_id_mask) == kInvalid_entity_id);
|
|
|
|
EntityID result = ((EntityID)user_id << kEntityID_user_id_shift) | (local_counter & kEntityID_user_id_mask);
|
|
return result;
|
|
}
|
|
|
|
static inline uint8 get_user_id(EntityID entity_id)
|
|
{
|
|
return (uint8)((entity_id & kEntityID_user_id_mask) >> kEntityID_user_id_shift);
|
|
}
|
|
|
|
static inline uint64 get_local_counter(EntityID entity_id)
|
|
{
|
|
return (uint64)entity_id & ~kEntityID_user_id_mask;
|
|
}
|
|
|
|
static inline bool8 is_entity_id_valid(EntityID entity_id)
|
|
{
|
|
bool8 result = (entity_id != kInvalid_entity_id);
|
|
return result;
|
|
}
|
|
|
|
static inline bool8 IsEntityA(const Entity* entity, const Class* kind)
|
|
{
|
|
Assert(entity != NULL);
|
|
return entity->derived_kind == kind;
|
|
}
|
|
|
|
#define IsEntityOfType(entity_ptr, EntityType) IsEntityA((entity_ptr), ClassKind(EntityType))
|
|
|
|
static inline void* make_entity_by_class(EntityManager* manager, const Class* kind, float x, float y, float z)
|
|
{
|
|
Assert(manager != NULL);
|
|
Assert(kind != NULL);
|
|
|
|
Entity* base_ptr = allocate_entity(manager, kind);
|
|
Assert(base_ptr != NULL);
|
|
|
|
base_ptr->ID = g_entity_manager_id++;
|
|
|
|
base_ptr->position.x = x;
|
|
base_ptr->position.y = y;
|
|
base_ptr->position.z = z;
|
|
base_ptr->position.w = 1.0f;
|
|
|
|
return base_ptr->derived;
|
|
}
|
|
|
|
#define MakeEntity(EntityType, manager_ptr, x, y, z) \
|
|
((EntityType*)make_entity_by_class((manager_ptr), ClassKind(EntityType), (x), (y), (z)))
|
|
|
|
static inline void* down_cast_entity(Entity* entity, const Class* kind)
|
|
{
|
|
Assert(entity != NULL);
|
|
Assert(IsEntityA(entity, kind));
|
|
return entity->derived;
|
|
}
|
|
|
|
#define DownCastEntity(EntityType, entity_ptr) ((EntityType*)down_cast_entity((entity_ptr), ClassKind(EntityType)))
|
|
|
|
void serialize_entity(Archive* ar, Entity* entity);
|
|
|
|
JULIET_API uint8 get_local_user_id(void);
|
|
|
|
JULIET_API String format_entity_id(Arena* arena, EntityID id);
|
|
|
|
JULIET_API bool8 parse_entity_id(String str, EntityID* outId);
|
|
|
|
const Class* find_class_by_name(String name);
|
|
|
|
const Class* resolve_entity_class(uint8 kind, uint32 crc);
|