Refactor composite and decorator classes
This commit is contained in:
parent
3693c3fc50
commit
370efcba73
|
@ -4,19 +4,19 @@
|
|||
|
||||
void BTRandomSelector::_enter() {
|
||||
last_running_idx = 0;
|
||||
if (_indicies.size() != get_child_count()) {
|
||||
_indicies.resize(get_child_count());
|
||||
if (indicies.size() != get_child_count()) {
|
||||
indicies.resize(get_child_count());
|
||||
for (int i = 0; i < get_child_count(); i++) {
|
||||
_indicies.set(i, i);
|
||||
indicies.set(i, i);
|
||||
}
|
||||
}
|
||||
_indicies.shuffle();
|
||||
indicies.shuffle();
|
||||
}
|
||||
|
||||
int BTRandomSelector::_tick(float p_delta) {
|
||||
int status = FAILURE;
|
||||
for (int i = last_running_idx; i < get_child_count(); i++) {
|
||||
status = get_child(_indicies[i])->execute(p_delta);
|
||||
status = get_child(indicies[i])->execute(p_delta);
|
||||
if (status != FAILURE) {
|
||||
last_running_idx = i;
|
||||
break;
|
||||
|
|
|
@ -11,7 +11,7 @@ class BTRandomSelector : public BTComposite {
|
|||
|
||||
private:
|
||||
int last_running_idx = 0;
|
||||
Array _indicies;
|
||||
Array indicies;
|
||||
|
||||
protected:
|
||||
virtual void _enter() override;
|
||||
|
|
|
@ -4,19 +4,19 @@
|
|||
|
||||
void BTRandomSequence::_enter() {
|
||||
last_running_idx = 0;
|
||||
if (_indicies.size() != get_child_count()) {
|
||||
_indicies.resize(get_child_count());
|
||||
if (indicies.size() != get_child_count()) {
|
||||
indicies.resize(get_child_count());
|
||||
for (int i = 0; i < get_child_count(); i++) {
|
||||
_indicies.set(i, i);
|
||||
indicies.set(i, i);
|
||||
}
|
||||
}
|
||||
_indicies.shuffle();
|
||||
indicies.shuffle();
|
||||
}
|
||||
|
||||
int BTRandomSequence::_tick(float p_delta) {
|
||||
int status = SUCCESS;
|
||||
for (int i = last_running_idx; i < get_child_count(); i++) {
|
||||
status = get_child(_indicies[i])->execute(p_delta);
|
||||
status = get_child(indicies[i])->execute(p_delta);
|
||||
if (status != SUCCESS) {
|
||||
last_running_idx = i;
|
||||
break;
|
||||
|
|
|
@ -11,7 +11,7 @@ class BTRandomSequence : public BTComposite {
|
|||
|
||||
private:
|
||||
int last_running_idx = 0;
|
||||
Array _indicies;
|
||||
Array indicies;
|
||||
|
||||
protected:
|
||||
virtual void _enter() override;
|
||||
|
|
|
@ -8,7 +8,7 @@ String BTCondition::get_configuration_warning() const {
|
|||
warning += "\n";
|
||||
}
|
||||
if (get_child_count() != 0) {
|
||||
warning += "Condition can't have child tasks.\n";
|
||||
warning += "Condition task can't have child tasks.\n";
|
||||
}
|
||||
return warning;
|
||||
}
|
|
@ -33,17 +33,17 @@ int BTCooldown::_tick(float p_delta) {
|
|||
|
||||
void BTCooldown::_chill() {
|
||||
get_blackboard()->set_var(cooldown_state_var, true);
|
||||
if (_timer.is_valid()) {
|
||||
_timer->set_time_left(duration);
|
||||
if (timer.is_valid()) {
|
||||
timer->set_time_left(duration);
|
||||
} else {
|
||||
_timer = SceneTree::get_singleton()->create_timer(duration, process_pause);
|
||||
_timer->connect("timeout", callable_mp(this, &BTCooldown::_on_timeout), CONNECT_ONE_SHOT);
|
||||
timer = SceneTree::get_singleton()->create_timer(duration, process_pause);
|
||||
timer->connect("timeout", callable_mp(this, &BTCooldown::_on_timeout), CONNECT_ONE_SHOT);
|
||||
}
|
||||
}
|
||||
|
||||
void BTCooldown::_on_timeout() {
|
||||
get_blackboard()->set_var(cooldown_state_var, false);
|
||||
_timer.unref();
|
||||
timer.unref();
|
||||
}
|
||||
|
||||
void BTCooldown::_bind_methods() {
|
||||
|
|
|
@ -17,7 +17,7 @@ private:
|
|||
bool trigger_on_failure = false;
|
||||
String cooldown_state_var = "";
|
||||
|
||||
Ref<SceneTreeTimer> _timer = nullptr;
|
||||
Ref<SceneTreeTimer> timer = nullptr;
|
||||
|
||||
void _chill();
|
||||
void _on_timeout();
|
||||
|
|
|
@ -12,15 +12,6 @@ void BTNewScope::initialize(Node *p_agent, const Ref<Blackboard> &p_blackboard)
|
|||
|
||||
Ref<Blackboard> bb = memnew(Blackboard);
|
||||
|
||||
// if (blackboard_data.empty()) {
|
||||
// bb->set_parent_scope(p_blackboard);
|
||||
// } else {
|
||||
// Ref<Blackboard> ro = memnew(Blackboard);
|
||||
// ro->set_data(blackboard_data);
|
||||
// ro->set_parent_scope(p_blackboard);
|
||||
// bb->set_parent_scope(ro);
|
||||
// }
|
||||
|
||||
bb->set_data(blackboard_data.duplicate());
|
||||
bb->set_parent_scope(p_blackboard);
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@ String BTRepeat::_generate_name() const {
|
|||
}
|
||||
|
||||
void BTRepeat::_enter() {
|
||||
_cur_iteration = 1;
|
||||
cur_iteration = 1;
|
||||
}
|
||||
|
||||
int BTRepeat::_tick(float p_delta) {
|
||||
|
@ -19,10 +19,10 @@ int BTRepeat::_tick(float p_delta) {
|
|||
return RUNNING;
|
||||
} else if (status == FAILURE && abort_on_failure) {
|
||||
return FAILURE;
|
||||
} else if (_cur_iteration >= times) {
|
||||
} else if (cur_iteration >= times) {
|
||||
return SUCCESS;
|
||||
} else {
|
||||
_cur_iteration += 1;
|
||||
cur_iteration += 1;
|
||||
return RUNNING;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ class BTRepeat : public BTDecorator {
|
|||
private:
|
||||
int times = 1;
|
||||
bool abort_on_failure = false;
|
||||
int _cur_iteration = 0;
|
||||
int cur_iteration = 0;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
|
|
@ -9,10 +9,10 @@ String BTRunLimit::_generate_name() const {
|
|||
int BTRunLimit::_tick(float p_delta) {
|
||||
ERR_FAIL_COND_V_MSG(get_child_count() == 0, FAILURE, "BT decorator has no child.");
|
||||
if (get_child(0)->get_status() != RUNNING) {
|
||||
if (_num_runs >= run_limit) {
|
||||
if (num_runs >= run_limit) {
|
||||
return FAILURE;
|
||||
}
|
||||
_num_runs += 1;
|
||||
num_runs += 1;
|
||||
}
|
||||
return get_child(0)->execute(p_delta);
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ class BTRunLimit : public BTDecorator {
|
|||
|
||||
private:
|
||||
int run_limit = 1;
|
||||
int _num_runs = 0;
|
||||
int num_runs = 0;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
|
|
@ -7,14 +7,14 @@ String BTTimeLimit::_generate_name() const {
|
|||
}
|
||||
|
||||
void BTTimeLimit::_enter() {
|
||||
_time_passed = 0.0;
|
||||
time_passed = 0.0;
|
||||
}
|
||||
|
||||
int BTTimeLimit::_tick(float p_delta) {
|
||||
ERR_FAIL_COND_V_MSG(get_child_count() == 0, FAILURE, "BT decorator has no child.");
|
||||
_time_passed += p_delta;
|
||||
time_passed += p_delta;
|
||||
int status = get_child(0)->execute(p_delta);
|
||||
if (status == RUNNING and _time_passed >= time_limit) {
|
||||
if (status == RUNNING and time_passed >= time_limit) {
|
||||
get_child(0)->cancel();
|
||||
return FAILURE;
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ class BTTimeLimit : public BTDecorator {
|
|||
|
||||
private:
|
||||
float time_limit = 5.0;
|
||||
float _time_passed = 0.0;
|
||||
float time_passed = 0.0;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
[gd_resource type="BehaviorTree" load_steps=8 format=3 uid="uid://cjkqi41oagagd"]
|
||||
[gd_resource type="BehaviorTree" load_steps=9 format=3 uid="uid://cjkqi41oagagd"]
|
||||
|
||||
[ext_resource type="Script" path="res://ai/tasks/arrive_pos.gd" id="1_rhs33"]
|
||||
[ext_resource type="Script" path="res://ai/tasks/play_animation.gd" id="2_dg0ss"]
|
||||
|
@ -22,8 +22,12 @@ children = [SubResource("BTSequence_a2ng0")]
|
|||
array_var = "waypoints"
|
||||
save_var = "wp"
|
||||
|
||||
[sub_resource type="BTSelector" id="BTSelector_5dclr"]
|
||||
[sub_resource type="BTCooldown" id="BTCooldown_gen0l"]
|
||||
children = [SubResource("BTForEach_0cp04")]
|
||||
duration = 3.0
|
||||
|
||||
[sub_resource type="BTSelector" id="BTSelector_5dclr"]
|
||||
children = [SubResource("BTCooldown_gen0l")]
|
||||
|
||||
[resource]
|
||||
root_task = SubResource("BTSelector_5dclr")
|
||||
|
|
Loading…
Reference in New Issue