From cea20bdaa2f845d00be3529b69b826aca143ec41 Mon Sep 17 00:00:00 2001 From: Eric-Paul Ickhorn Date: Tue, 30 Jan 2024 23:46:47 +0100 Subject: [PATCH] Implemented but didn't test the entity-untagging function and fixed a potentially unwanted behaviour when getting a tag's value. --- core/src-c/entity_tagging.c | 69 +++++++++++++++++++++++++++++++++---- 1 file changed, 63 insertions(+), 6 deletions(-) diff --git a/core/src-c/entity_tagging.c b/core/src-c/entity_tagging.c index 7b69f44..11fe236 100644 --- a/core/src-c/entity_tagging.c +++ b/core/src-c/entity_tagging.c @@ -2,23 +2,29 @@ #include #include -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; - // Find the correct slot if it already exist - u32_t tag_id = mt_get_tag_id(&state->tag_registry, name); usz_t tag_index = 0; while(tag_index < entity->num_tags) { if(entity->tags[tag_index].identifier == tag_id) - return &entity->tags[tag_index]; + return 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) { @@ -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) { + 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); + return tag->data.integer; } 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); return tag->data.real; } 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); return tag->data.string; } 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); return tag->data.pointer; } 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); return tag->data.vec2f; } 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); return tag->data.vec3f; } 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); return tag->data.vec4f; }