Add TimeLimit

This commit is contained in:
Serhii Snitsaruk 2022-08-28 15:34:04 +02:00
parent fbe9165d8e
commit 89b861d5f8
3 changed files with 62 additions and 0 deletions

27
limboai/bt_time_limit.cpp Normal file
View File

@ -0,0 +1,27 @@
/* bt_time_limit.cpp */
#include "bt_time_limit.h"
String BTTimeLimit::_generate_name() const {
return vformat("TimeLimit %ss", time_limit);
}
void BTTimeLimit::_enter() {
_time_passed = 0.0;
}
int BTTimeLimit::_tick(float p_delta) {
_time_passed += p_delta;
int status = get_child(0)->execute(p_delta);
if (status == RUNNING and _time_passed >= time_limit) {
get_child(0)->cancel();
return FAILURE;
}
return status;
}
void BTTimeLimit::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_time_limit", "p_value"), &BTTimeLimit::set_time_limit);
ClassDB::bind_method(D_METHOD("get_time_limit"), &BTTimeLimit::get_time_limit);
ADD_PROPERTY(PropertyInfo(Variant::REAL, "time_limit"), "set_time_limit", "get_time_limit");
}

31
limboai/bt_time_limit.h Normal file
View File

@ -0,0 +1,31 @@
/* bt_time_limit.h */
#ifndef BT_TIME_LIMIT_H
#define BT_TIME_LIMIT_H
#include "bt_decorator.h"
#include "core/object.h"
class BTTimeLimit : public BTDecorator {
GDCLASS(BTTimeLimit, BTDecorator);
private:
float time_limit = 5.0;
float _time_passed = 0.0;
protected:
static void _bind_methods();
virtual String _generate_name() const;
virtual void _enter();
virtual int _tick(float p_delta);
public:
void set_time_limit(float p_value) {
time_limit = p_value;
emit_changed();
};
float get_time_limit() const { return time_limit; };
};
#endif // BT_TIME_LIMIT_H

View File

@ -20,15 +20,18 @@
#include "bt_selector.h"
#include "bt_sequence.h"
#include "bt_task.h"
#include "bt_time_limit.h"
#include "limbo_string_names.h"
#include "limbo_utility.h"
void register_limboai_types() {
ClassDB::register_class<BTTask>();
ClassDB::register_class<BTComposite>();
ClassDB::register_class<BTDecorator>();
ClassDB::register_class<BTAction>();
ClassDB::register_class<BTCondition>();
ClassDB::register_class<BTSequence>();
ClassDB::register_class<BTSelector>();
ClassDB::register_class<BTParallel>();
@ -40,6 +43,7 @@ void register_limboai_types() {
ClassDB::register_class<BTRepeatUntilFailure>();
ClassDB::register_class<BTRepeatUntilSuccess>();
ClassDB::register_class<BTRunLimit>();
ClassDB::register_class<BTTimeLimit>();
LimboStringNames::create();
}