feat: replaced mirror_get_converter with a more direct mirror_get_typeclass

This commit is contained in:
Sara 2024-01-11 23:51:53 +01:00
parent 1a3c4c9676
commit 760d9f2879
3 changed files with 13 additions and 11 deletions

View file

@ -1,10 +1,10 @@
#include "mirror.h" #include "mirror.h"
void* mirror_get_converter(void* self, IMirror const* tc, const char* typeclass) { const void* mirror_get_typeclass(void* self, IMirror const* tc, const char* typeclass) {
uintptr_t target_hash = strhash(typeclass); uintptr_t target_hash = strhash(typeclass);
list_foreach(MirroredTypeclass*, class, tc->get_typeclasses(self)) { list_foreach(MirroredTypeclass*, class, tc->get_typeclasses(self)) {
if(target_hash == class->name_hash && strcmp(typeclass, class->typeclass_name) == 0) if(target_hash == class->name_hash && strcmp(typeclass, class->typeclass_name) == 0)
return class->wrap; return class->typeclass;
} }
return NULL; return NULL;
} }

View file

@ -23,7 +23,7 @@ typedef struct {
typedef struct { typedef struct {
const char* typeclass_name; const char* typeclass_name;
uintptr_t name_hash; uintptr_t name_hash;
void* (*const wrap)(void*); const void* typeclass;
} MirroredTypeclass; } MirroredTypeclass;
static inline int mirror_is_typeid(const Mirror* mirror, typeid id) { static inline int mirror_is_typeid(const Mirror* mirror, typeid id) {
@ -36,7 +36,7 @@ static inline int mirror_eq(const Mirror* lhs, const Mirror* rhs) {
return lhs->tc->get_typeid(lhs->data) == rhs->tc->get_typeid(rhs->data); return lhs->tc->get_typeid(lhs->data) == rhs->tc->get_typeid(rhs->data);
} }
extern void* mirror_get_converter(void* data, IMirror const* tc, const char* typeclass); extern const void* mirror_get_typeclass(void* data, IMirror const* tc, const char* typeclass);
#define MIRROR_TRY_WRAP(Into_, Mirror_, Typeclass_){\ #define MIRROR_TRY_WRAP(Into_, Mirror_, Typeclass_){\
MirroredTypeclassWrapFunc fn_ = mirror_get_typeclass(Mirror_, #Typeclass_);\ MirroredTypeclassWrapFunc fn_ = mirror_get_typeclass(Mirror_, #Typeclass_);\
@ -85,13 +85,15 @@ List* T##_get_typeclasses(T* self) {\
if(!init_flag) {\ if(!init_flag) {\
init_flag = 1,\ init_flag = 1,\
typeclasses = list_init(sizeof(MirroredTypeclass));\ typeclasses = list_init(sizeof(MirroredTypeclass));\
MirroredTypeclass tc;\
#define REFLECT_TYPECLASS(T, TypeClass_)\ #define REFLECT_TYPECLASS(T, TypeClass_)\
list_add(&typeclasses, &(MirroredTypeclass){\ tc = (MirroredTypeclass){\
.name_hash = strhash(#TypeClass_),\ .name_hash = strhash(#TypeClass_),\
.typeclass_name = #TypeClass_,\ .typeclass_name = #TypeClass_,\
.wrap = (void*(*const)(void*)) T##_as_##TypeClass_\ .typeclass = (void*)T##_as_##TypeClass_(NULL).tc\
}); };\
list_add(&typeclasses, &tc);
#define END_REFLECT(T)\ #define END_REFLECT(T)\
}\ }\

View file

@ -1,4 +1,5 @@
#include "PlayerStates.h" #include "PlayerStates.h"
#include "mirror.h"
#include "physics_world.h" #include "physics_world.h"
#include "Damagable.h" #include "Damagable.h"
@ -59,14 +60,13 @@ void PlayerAttackTrigger(Player* self) {
MakeVector(0.1f, 0.06f), PHYSICS_LAYER_COMBAT, self->rigidbody); MakeVector(0.1f, 0.06f), PHYSICS_LAYER_COMBAT, self->rigidbody);
if(found != NULL) { if(found != NULL) {
PhysicsEntity entity = collider_get_owner(found); PhysicsEntity entity = collider_get_owner(found);
Damagable(*const as_damagable)(void*) = mirror_get_converter(entity.data, entity.mirror, "Damagable"); const IDamagable* damagable = mirror_get_typeclass(entity.data, entity.mirror, "Damagable");
if(as_damagable) { if(damagable) {
Damagable damagable = as_damagable(entity.data);
DamageEventData data = { DamageEventData data = {
.damageAmount = 1, .damageAmount = 1,
.origin = self->transform.position .origin = self->transform.position
}; };
damagable.tc->damage(entity.data, &data); damagable->damage(entity.data, &data);
} }
} }
++self->animationTriggers; ++self->animationTriggers;