--- a/src/behavior.c Sat Aug 16 22:59:49 2025 +0200 +++ b/src/behavior.c Mon Aug 18 23:11:50 2025 +0200 @@ -31,7 +31,7 @@ #include "ascension/util.h" #include "ascension/error.h" -#include <cx/array_list.h> +#include <cx/hash_map.h> #include <cx/tree.h> #include <assert.h> @@ -45,17 +45,20 @@ } } -static CxList *asc_behavior_new_list(void) { - CxList *list = cxArrayListCreate(NULL, NULL, sizeof(AscBehavior), 4); - cxDefineDestructor(list, asc_behavior_destroy); - return list; +static CxMap *asc_behavior_new_map(void) { + CxMap *map = cxHashMapCreateSimple(sizeof(AscBehavior)); + cxDefineDestructor(map, asc_behavior_destroy); + return map; } AscBehavior *asc_behavior_add_(AscSceneNode *node, struct asc_behavior_create_args args) { if (node->behaviors == NULL) { - node->behaviors = asc_behavior_new_list(); + node->behaviors = asc_behavior_new_map(); } - AscBehavior *behavior = cxListEmplace(node->behaviors); + cxmutstr name = args.name == NULL + ? asc_util_gen_name("behavior") + : cx_mutstr(strdup(args.name)); + AscBehavior *behavior = cxMapEmplace(node->behaviors, name); assert(behavior != NULL); behavior->enabled = true; behavior->killed = false; @@ -65,7 +68,7 @@ behavior->data = args.data; behavior->interval = args.interval; behavior->last_execution = 0; - asc_behavior_name(behavior, args.name); + behavior->name = name; asc_dprintf("Create behavior: %"CX_PRIstr, CX_SFMT(behavior->name)); return behavior; } @@ -84,16 +87,6 @@ behavior->last_execution = asc_context.total_nanos; } -void asc_behavior_name(AscBehavior *behavior, const char *name) { - cx_strfree(&behavior->name); - if (name == NULL) { - behavior->name = asc_util_gen_name(behavior); - } else { - behavior->name.ptr = strdup(name); - behavior->name.length = strlen(name); - } -} - cxstring asc_behavior_get_name(const AscBehavior *behavior) { assert(behavior != NULL); return cx_strcast(behavior->name); @@ -102,14 +95,14 @@ // TODO: add asc_behavior_find() and asc_behavior_find_global() void asc_behavior_enable_all(AscSceneNode *node) { - CxIterator iter = cxListIterator(node->behaviors); + CxMapIterator iter = cxMapIteratorValues(node->behaviors); cx_foreach(AscBehavior*, behavior, iter) { behavior->enabled = true; } } void asc_behavior_disable_all(AscSceneNode *node) { - CxIterator iter = cxListIterator(node->behaviors); + CxMapIterator iter = cxMapIteratorValues(node->behaviors); cx_foreach(AscBehavior*, behavior, iter) { behavior->enabled = false; }