Add RunLimit

This commit is contained in:
Serhii Snitsaruk 2022-08-28 15:24:01 +02:00
parent 37ac75dc78
commit fbe9165d8e
4 changed files with 58 additions and 3 deletions

View File

@ -1,7 +1,7 @@
/* bt_delay.h */ /* bt_delay.h */
#ifndef BT_DELAY #ifndef BT_DELAY_H
#define BT_DELAY #define BT_DELAY_H
#include "bt_decorator.h" #include "bt_decorator.h"
#include "core/object.h" #include "core/object.h"
@ -28,4 +28,4 @@ public:
float get_seconds() const { return seconds; }; float get_seconds() const { return seconds; };
}; };
#endif // BT_DELAY #endif // BT_DELAY_H

23
limboai/bt_run_limit.cpp Normal file
View File

@ -0,0 +1,23 @@
/* bt_run_limit.cpp */
#include "bt_run_limit.h"
String BTRunLimit::_generate_name() const {
return vformat("RunLimit x%d", run_limit);
}
int BTRunLimit::_tick(float p_delta) {
if (get_child(0)->get_status() != RUNNING) {
if (_num_runs >= run_limit) {
return FAILURE;
}
_num_runs += 1;
}
return get_child(0)->execute(p_delta);
}
void BTRunLimit::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_run_limit", "p_value"), &BTRunLimit::set_run_limit);
ClassDB::bind_method(D_METHOD("get_run_limit"), &BTRunLimit::get_run_limit);
ADD_PROPERTY(PropertyInfo(Variant::INT, "run_limit"), "set_run_limit", "get_run_limit");
}

30
limboai/bt_run_limit.h Normal file
View File

@ -0,0 +1,30 @@
/* bt_run_limit.h */
#ifndef BT_RUN_LIMIT_H
#define BT_RUN_LIMIT_H
#include "bt_decorator.h"
#include "core/object.h"
class BTRunLimit : public BTDecorator {
GDCLASS(BTRunLimit, BTDecorator);
private:
int run_limit = 1;
int _num_runs = 0;
protected:
static void _bind_methods();
virtual String _generate_name() const;
virtual int _tick(float p_delta);
public:
void set_run_limit(int p_value) {
run_limit = p_value;
emit_changed();
};
int get_run_limit() const { return run_limit; };
};
#endif // BT_RUN_LIMIT_H

View File

@ -16,6 +16,7 @@
#include "bt_repeat.h" #include "bt_repeat.h"
#include "bt_repeat_until_failure.h" #include "bt_repeat_until_failure.h"
#include "bt_repeat_until_success.h" #include "bt_repeat_until_success.h"
#include "bt_run_limit.h"
#include "bt_selector.h" #include "bt_selector.h"
#include "bt_sequence.h" #include "bt_sequence.h"
#include "bt_task.h" #include "bt_task.h"
@ -38,6 +39,7 @@ void register_limboai_types() {
ClassDB::register_class<BTRepeat>(); ClassDB::register_class<BTRepeat>();
ClassDB::register_class<BTRepeatUntilFailure>(); ClassDB::register_class<BTRepeatUntilFailure>();
ClassDB::register_class<BTRepeatUntilSuccess>(); ClassDB::register_class<BTRepeatUntilSuccess>();
ClassDB::register_class<BTRunLimit>();
LimboStringNames::create(); LimboStringNames::create();
} }