Add Repeat, RepeatUntilFailure and RepeatUntilSuccess

This commit is contained in:
Serhii Snitsaruk 2022-08-28 14:49:02 +02:00
parent b116d1fdcd
commit 37ac75dc78
7 changed files with 132 additions and 0 deletions

37
limboai/bt_repeat.cpp Normal file
View File

@ -0,0 +1,37 @@
/* bt_repeat.cpp */
#include "bt_repeat.h"
#include "core/object.h"
#include "core/variant.h"
String BTRepeat::_generate_name() const {
return vformat("Repeat x%s", times);
}
void BTRepeat::_enter() {
_cur_iteration = 1;
}
int BTRepeat::_tick(float p_delta) {
int status = get_child(0)->execute(p_delta);
if (status == RUNNING) {
return RUNNING;
} else if (status == FAILURE && abort_on_failure) {
return FAILURE;
} else if (_cur_iteration >= times) {
return SUCCESS;
} else {
_cur_iteration += 1;
return RUNNING;
}
}
void BTRepeat::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_times", "p_value"), &BTRepeat::set_times);
ClassDB::bind_method(D_METHOD("get_times"), &BTRepeat::get_times);
ClassDB::bind_method(D_METHOD("set_abort_on_failure", "p_value"), &BTRepeat::set_abort_on_failure);
ClassDB::bind_method(D_METHOD("get_abort_on_failure"), &BTRepeat::get_abort_on_failure);
ADD_PROPERTY(PropertyInfo(Variant::INT, "times", PROPERTY_HINT_RANGE, "1,65535"), "set_times", "get_times");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "abort_on_failure"), "set_abort_on_failure", "get_abort_on_failure");
}

37
limboai/bt_repeat.h Normal file
View File

@ -0,0 +1,37 @@
/* bt_repeat.h */
#ifndef BT_REPEAT_H
#define BT_REPEAT_H
#include "bt_decorator.h"
#include "core/object.h"
class BTRepeat : public BTDecorator {
GDCLASS(BTRepeat, BTDecorator);
private:
int times = 1;
bool abort_on_failure = false;
int _cur_iteration = 0;
protected:
static void _bind_methods();
virtual String _generate_name() const;
virtual void _enter();
virtual int _tick(float p_delta);
public:
void set_times(int p_value) {
times = p_value;
emit_changed();
};
int get_times() const { return times; };
void set_abort_on_failure(bool p_value) {
abort_on_failure = p_value;
emit_changed();
};
bool get_abort_on_failure() const { return abort_on_failure; };
};
#endif // BT_REPEAT_H

View File

@ -0,0 +1,10 @@
/* bt_repeat_until_failure.cpp */
#include "bt_repeat_until_failure.h"
int BTRepeatUntilFailure::_tick(float p_delta) {
if (get_child(0)->execute(p_delta) == FAILURE) {
return SUCCESS;
}
return RUNNING;
}

View File

@ -0,0 +1,16 @@
/* bt_repeat_until_failure.h */
#ifndef BT_REPEAT_UNTIL_FAILURE_H
#define BT_REPEAT_UNTIL_FAILURE_H
#include "bt_decorator.h"
#include "core/object.h"
class BTRepeatUntilFailure : public BTDecorator {
GDCLASS(BTRepeatUntilFailure, BTDecorator);
protected:
virtual int _tick(float p_delta);
};
#endif // BT_REPEAT_UNTIL_FAILURE_H

View File

@ -0,0 +1,10 @@
/* bt_repeat_until_success.cpp */
#include "bt_repeat_until_success.h"
int BTRepeatUntilSuccess::_tick(float p_delta) {
if (get_child(0)->execute(p_delta) == SUCCESS) {
return SUCCESS;
}
return RUNNING;
}

View File

@ -0,0 +1,16 @@
/* bt_repeat_until_success.h */
#ifndef BT_REPEAT_UNTIL_SUCCESS_H
#define BT_REPEAT_UNTIL_SUCCESS_H
#include "bt_decorator.h"
#include "core/object.h"
class BTRepeatUntilSuccess : public BTDecorator {
GDCLASS(BTRepeatUntilSuccess, BTDecorator);
protected:
virtual int _tick(float p_delta);
};
#endif // BT_REPEAT_UNTIL_SUCCESS_H

View File

@ -13,6 +13,9 @@
#include "bt_delay.h"
#include "bt_invert.h"
#include "bt_parallel.h"
#include "bt_repeat.h"
#include "bt_repeat_until_failure.h"
#include "bt_repeat_until_success.h"
#include "bt_selector.h"
#include "bt_sequence.h"
#include "bt_task.h"
@ -32,6 +35,9 @@ void register_limboai_types() {
ClassDB::register_class<BTAlwaysFail>();
ClassDB::register_class<BTAlwaysSucceed>();
ClassDB::register_class<BTDelay>();
ClassDB::register_class<BTRepeat>();
ClassDB::register_class<BTRepeatUntilFailure>();
ClassDB::register_class<BTRepeatUntilSuccess>();
LimboStringNames::create();
}