26 */ |
26 */ |
27 |
27 |
28 #include "ascension/behavior.h" |
28 #include "ascension/behavior.h" |
29 #include "ascension/context.h" |
29 #include "ascension/context.h" |
30 #include "ascension/scene.h" |
30 #include "ascension/scene.h" |
|
31 #include "ascension/util.h" |
|
32 #include "ascension/error.h" |
31 |
33 |
32 #include <cx/array_list.h> |
34 #include <cx/array_list.h> |
33 #include <cx/tree.h> |
35 #include <cx/tree.h> |
34 |
36 |
35 #include <assert.h> |
37 #include <assert.h> |
36 |
38 |
37 static void asc_behavior_destroy(void *b) { |
39 static void asc_behavior_destroy(void *b) { |
38 AscBehavior *behavior = b; |
40 AscBehavior *behavior = b; |
|
41 asc_dprintf("Destroy behavior: %"CX_PRIstr, CX_SFMT(behavior->name)); |
|
42 cx_strfree(&behavior->name); |
39 if (behavior->destroy_func) { |
43 if (behavior->destroy_func) { |
40 behavior->destroy_func(behavior); |
44 behavior->destroy_func(behavior); |
41 } |
45 } |
42 } |
46 } |
43 |
47 |
44 static CxList *asc_behavior_new_list(void) { |
48 static CxList *asc_behavior_new_list(void) { |
45 // TODO: add comparator when we know how to identify behaviors in order to remove them |
|
46 CxList *list = cxArrayListCreate(NULL, NULL, sizeof(AscBehavior), 4); |
49 CxList *list = cxArrayListCreate(NULL, NULL, sizeof(AscBehavior), 4); |
47 cxDefineDestructor(list, asc_behavior_destroy); |
50 cxDefineDestructor(list, asc_behavior_destroy); |
48 return list; |
51 return list; |
49 } |
52 } |
50 |
53 |
60 behavior->func = args.func; |
63 behavior->func = args.func; |
61 behavior->destroy_func = args.destroy_func; |
64 behavior->destroy_func = args.destroy_func; |
62 behavior->data = args.data; |
65 behavior->data = args.data; |
63 behavior->interval = args.interval; |
66 behavior->interval = args.interval; |
64 behavior->last_execution = 0; |
67 behavior->last_execution = 0; |
|
68 asc_behavior_name(behavior, args.name); |
|
69 asc_dprintf("Create behavior: %"CX_PRIstr, CX_SFMT(behavior->name)); |
65 return behavior; |
70 return behavior; |
66 } |
71 } |
67 |
72 |
68 void asc_behavior_remove(AscBehavior *behavior) { |
73 void asc_behavior_remove(AscBehavior *behavior) { |
69 // TODO: implement some sort of ID for behaviors which can also be used for logging |
74 // TODO: implement some sort of ID for behaviors which can also be used for logging |
76 if (behavior->last_execution + behavior->interval > asc_context.total_nanos) return; |
81 if (behavior->last_execution + behavior->interval > asc_context.total_nanos) return; |
77 |
82 |
78 behavior->func(behavior); |
83 behavior->func(behavior); |
79 behavior->last_execution = asc_context.total_nanos; |
84 behavior->last_execution = asc_context.total_nanos; |
80 } |
85 } |
|
86 |
|
87 void asc_behavior_name(AscBehavior *behavior, const char *name) { |
|
88 cx_strfree(&behavior->name); |
|
89 if (name == NULL) { |
|
90 behavior->name = asc_util_gen_name(behavior, "behavior"); |
|
91 } else { |
|
92 behavior->name.ptr = strdup(name); |
|
93 behavior->name.length = strlen(name); |
|
94 } |
|
95 } |