Implemented but didn't test the entity-untagging function and fixed a potentially unwanted behaviour when getting a tag's value.

This commit is contained in:
Eric-Paul Ickhorn 2024-01-30 23:46:47 +01:00
parent 2fa120c024
commit cea20bdaa2
Signed by: epickh
GPG Key ID: F5EBBE013924D95F
1 changed files with 63 additions and 6 deletions

View File

@ -2,23 +2,29 @@
#include <state.h> #include <state.h>
#include <stdlib.h> #include <stdlib.h>
MtTag * mt_get_entity_tag_slot(MtEntity *entity, char *name) i16_t mt_get_entity_tag_slot_index(MtEntity *entity, const char *name)
{ {
MtState *state = entity->parent_state; MtState *state = entity->parent_state;
// Find the correct slot if it already exist
u32_t tag_id = mt_get_tag_id(&state->tag_registry, name); u32_t tag_id = mt_get_tag_id(&state->tag_registry, name);
usz_t tag_index = 0; usz_t tag_index = 0;
while(tag_index < entity->num_tags) while(tag_index < entity->num_tags)
{ {
if(entity->tags[tag_index].identifier == tag_id) if(entity->tags[tag_index].identifier == tag_id)
return &entity->tags[tag_index]; return tag_index;
++tag_index; ++tag_index;
} }
return -1;
}
// Create a new slot if needed MtTag * mt_get_entity_tag_slot(MtEntity *entity, char *name)
{
i16_t tag_slot_index = mt_get_entity_tag_slot_index(entity, name);
if(tag_slot_index >= 0)
return &entity->tags[tag_slot_index];
// A new slot is needed
if(entity->num_tags >= entity->tags_capacity) if(entity->num_tags >= entity->tags_capacity)
{ {
@ -74,46 +80,97 @@ void mt_tag_vec4(MtEntity *entity, char *name, rr_vec4f_s vector)
void mt_untag(MtEntity *entity, char *name); void mt_untag(MtEntity *entity, char *name)
{
i64_t tag_slot_index = mt_get_entity_tag_slot_index(entity, name);
entity->tags[tag_slot_index] = entity->tags[entity->num_tags-1];
--entity->num_tags;
}
i64_t mt_get_i64_tag(MtEntity *entity, char *name) i64_t mt_get_i64_tag(MtEntity *entity, char *name)
{ {
i16_t slot_index = mt_get_entity_tag_slot_index(entity, name);
if(slot_index < 0)
return 0;
MtTag *tag = mt_get_entity_tag_slot(entity, name); MtTag *tag = mt_get_entity_tag_slot(entity, name);
return tag->data.integer; return tag->data.integer;
} }
f64_t mt_get_f64_tag(MtEntity *entity, char *name) f64_t mt_get_f64_tag(MtEntity *entity, char *name)
{ {
i16_t slot_index = mt_get_entity_tag_slot_index(entity, name);
if(slot_index < 0)
return 0.0f;
MtTag *tag = mt_get_entity_tag_slot(entity, name); MtTag *tag = mt_get_entity_tag_slot(entity, name);
return tag->data.real; return tag->data.real;
} }
char * mt_get_str_tag(MtEntity *entity, char *name) char * mt_get_str_tag(MtEntity *entity, char *name)
{ {
i16_t slot_index = mt_get_entity_tag_slot_index(entity, name);
if(slot_index < 0)
return NULL;
MtTag *tag = mt_get_entity_tag_slot(entity, name); MtTag *tag = mt_get_entity_tag_slot(entity, name);
return tag->data.string; return tag->data.string;
} }
void * mt_get_ptr_tag(MtEntity *entity, char *name) void * mt_get_ptr_tag(MtEntity *entity, char *name)
{ {
i16_t slot_index = mt_get_entity_tag_slot_index(entity, name);
if(slot_index < 0)
return NULL;
MtTag *tag = mt_get_entity_tag_slot(entity, name); MtTag *tag = mt_get_entity_tag_slot(entity, name);
return tag->data.pointer; return tag->data.pointer;
} }
rr_vec2f_s mt_get_vec2_tag(MtEntity *entity, char *name) rr_vec2f_s mt_get_vec2_tag(MtEntity *entity, char *name)
{ {
i16_t slot_index = mt_get_entity_tag_slot_index(entity, name);
if(slot_index < 0)
{
rr_vec2f_s err_vector;
err_vector.x = 0.0f;
err_vector.y = 0.0f;
return err_vector;
}
MtTag *tag = mt_get_entity_tag_slot(entity, name); MtTag *tag = mt_get_entity_tag_slot(entity, name);
return tag->data.vec2f; return tag->data.vec2f;
} }
rr_vec3f_s mt_get_vec3_tag(MtEntity *entity, char *name) rr_vec3f_s mt_get_vec3_tag(MtEntity *entity, char *name)
{ {
i16_t slot_index = mt_get_entity_tag_slot_index(entity, name);
if(slot_index < 0)
{
rr_vec3f_s err_vector;
err_vector.x = 0.0f;
err_vector.y = 0.0f;
err_vector.z = 0.0f;
return err_vector;
}
MtTag *tag = mt_get_entity_tag_slot(entity, name); MtTag *tag = mt_get_entity_tag_slot(entity, name);
return tag->data.vec3f; return tag->data.vec3f;
} }
rr_vec4f_s mt_get_vec4_tag(MtEntity *entity, char *name) rr_vec4f_s mt_get_vec4_tag(MtEntity *entity, char *name)
{ {
i16_t slot_index = mt_get_entity_tag_slot_index(entity, name);
if(slot_index < 0)
{
rr_vec4f_s err_vector;
err_vector.x = 0.0f;
err_vector.y = 0.0f;
err_vector.z = 0.0f;
err_vector.w = 0.0f;
return err_vector;
}
MtTag *tag = mt_get_entity_tag_slot(entity, name); MtTag *tag = mt_get_entity_tag_slot(entity, name);
return tag->data.vec4f; return tag->data.vec4f;
} }