61 AscBehavior *behavior = cxMapEmplace(node->behaviors, name); |
61 AscBehavior *behavior = cxMapEmplace(node->behaviors, name); |
62 assert(behavior != NULL); |
62 assert(behavior != NULL); |
63 behavior->enabled = !args.start_disabled; |
63 behavior->enabled = !args.start_disabled; |
64 behavior->always_enabled = args.always_enabled; |
64 behavior->always_enabled = args.always_enabled; |
65 behavior->killed = false; |
65 behavior->killed = false; |
66 behavior->unpaused = false; |
66 behavior->reactivated = false; |
67 behavior->node = node; |
67 behavior->node = node; |
68 behavior->func = args.func; |
68 behavior->func = args.func; |
69 behavior->destroy_func = args.destroy_func; |
69 behavior->destroy_func = args.destroy_func; |
70 behavior->data = args.data; |
70 behavior->data = args.data; |
71 behavior->interval = args.interval; |
71 behavior->interval = args.interval; |
72 behavior->last_execution = 0; |
72 behavior->last_execution = 0; |
73 behavior->pause_while_hidden = args.pause_while_hidden; |
73 behavior->disable_while_hidden = args.disable_while_hidden; |
74 behavior->name = name; |
74 behavior->name = name; |
75 asc_dprintf("Create behavior: %"CX_PRIstr, CX_SFMT(behavior->name)); |
75 asc_dprintf("Create behavior: %"CX_PRIstr, CX_SFMT(behavior->name)); |
76 return behavior; |
76 return behavior; |
77 } |
77 } |
78 |
78 |
83 } |
83 } |
84 |
84 |
85 void asc_behavior_trigger(AscBehavior *behavior) { |
85 void asc_behavior_trigger(AscBehavior *behavior) { |
86 // the behavior is not enabled at the moment |
86 // the behavior is not enabled at the moment |
87 if (!behavior->enabled) { |
87 if (!behavior->enabled) { |
88 behavior->unpaused = true; |
88 behavior->reactivated = true; |
89 return; |
89 return; |
90 } |
90 } |
91 // the behavior is not scheduled for execution |
91 // the behavior is not scheduled for execution |
92 if (behavior->last_execution + behavior->interval > asc_context.total_nanos) { |
92 if (behavior->last_execution + behavior->interval > asc_context.total_nanos) { |
93 return; |
93 return; |
94 } |
94 } |
95 // the behavior is paused while the node is hidden |
95 // the behavior is disable while the node is hidden |
96 if (!behavior->always_enabled && behavior->pause_while_hidden && asc_scene_node_is_hidden(behavior->node)) { |
96 if (!behavior->always_enabled && behavior->disable_while_hidden && asc_scene_node_is_hidden(behavior->node)) { |
97 behavior->unpaused = true; |
97 behavior->reactivated = true; |
98 return; |
98 return; |
99 } |
99 } |
100 |
100 |
101 behavior->func(behavior); |
101 behavior->func(behavior); |
102 behavior->unpaused = false; |
102 behavior->reactivated = false; |
103 behavior->last_execution = asc_context.total_nanos; |
103 behavior->last_execution = asc_context.total_nanos; |
104 } |
104 } |
105 |
105 |
106 cxstring asc_behavior_get_name(const AscBehavior *behavior) { |
106 cxstring asc_behavior_get_name(const AscBehavior *behavior) { |
107 assert(behavior != NULL); |
107 assert(behavior != NULL); |
124 cx_foreach(AscBehavior*, behavior, iter) { |
124 cx_foreach(AscBehavior*, behavior, iter) { |
125 asc_behavior_disable(behavior); |
125 asc_behavior_disable(behavior); |
126 } |
126 } |
127 } |
127 } |
128 |
128 |
129 void asc_behavior_pause_all_while_hidden(AscSceneNode *node) { |
129 void asc_behavior_disable_all_while_hidden(AscSceneNode *node) { |
130 CxMapIterator iter = cxMapIteratorValues(node->behaviors); |
130 CxMapIterator iter = cxMapIteratorValues(node->behaviors); |
131 cx_foreach(AscBehavior*, behavior, iter) { |
131 cx_foreach(AscBehavior*, behavior, iter) { |
132 asc_behavior_pause_while_hidden(behavior); |
132 asc_behavior_disable_while_hidden(behavior); |
133 } |
133 } |
134 } |
134 } |
135 |
135 |
136 void asc_behavior_continue_all_while_hidden(AscSceneNode *node) { |
136 void asc_behavior_enable_all_while_hidden(AscSceneNode *node) { |
137 CxMapIterator iter = cxMapIteratorValues(node->behaviors); |
137 CxMapIterator iter = cxMapIteratorValues(node->behaviors); |
138 cx_foreach(AscBehavior*, behavior, iter) { |
138 cx_foreach(AscBehavior*, behavior, iter) { |
139 asc_behavior_continue_while_hidden(behavior); |
139 asc_behavior_enable_while_hidden(behavior); |
140 } |
140 } |
141 } |
141 } |