Add Delay, Invert, AlwaysSucceed and AlwaysFail

This commit is contained in:
Serhii Snitsaruk 2022-08-28 13:56:43 +02:00
parent aac0acd51e
commit b116d1fdcd
9 changed files with 149 additions and 0 deletions

View File

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

16
limboai/bt_always_fail.h Normal file
View File

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

View File

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

View File

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

29
limboai/bt_delay.cpp Normal file
View File

@ -0,0 +1,29 @@
/* bt_delay.cpp */
#include "bt_delay.h"
#include "core/array.h"
#include "core/class_db.h"
#include "core/object.h"
#include "core/variant.h"
String BTDelay::_generate_name() const {
return vformat("Delay %ss", seconds);
}
void BTDelay::_enter() {
time_passed = 0.0;
}
int BTDelay::_tick(float p_delta) {
time_passed += p_delta;
if (time_passed <= seconds) {
return RUNNING;
}
return get_child(0)->execute(p_delta);
}
void BTDelay::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_seconds", "p_value"), &BTDelay::set_seconds);
ClassDB::bind_method(D_METHOD("get_seconds"), &BTDelay::get_seconds);
ADD_PROPERTY(PropertyInfo(Variant::REAL, "seconds"), "set_seconds", "get_seconds");
}

31
limboai/bt_delay.h Normal file
View File

@ -0,0 +1,31 @@
/* bt_delay.h */
#ifndef BT_DELAY
#define BT_DELAY
#include "bt_decorator.h"
#include "core/object.h"
class BTDelay : public BTDecorator {
GDCLASS(BTDelay, BTDecorator);
private:
float seconds = 1.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_seconds(float p_value) {
seconds = p_value;
emit_changed();
};
float get_seconds() const { return seconds; };
};
#endif // BT_DELAY

13
limboai/bt_invert.cpp Normal file
View File

@ -0,0 +1,13 @@
/* bt_invert.cpp */
#include "bt_invert.h"
int BTInvert::_tick(float p_delta) {
int status = get_child(0)->execute(p_delta);
if (status == SUCCESS) {
status = FAILURE;
} else if (status == FAILURE) {
status = SUCCESS;
}
return status;
}

16
limboai/bt_invert.h Normal file
View File

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

View File

@ -5,9 +5,13 @@
#include "core/class_db.h" #include "core/class_db.h"
#include "bt_action.h" #include "bt_action.h"
#include "bt_always_fail.h"
#include "bt_always_succeed.h"
#include "bt_composite.h" #include "bt_composite.h"
#include "bt_condition.h" #include "bt_condition.h"
#include "bt_decorator.h" #include "bt_decorator.h"
#include "bt_delay.h"
#include "bt_invert.h"
#include "bt_parallel.h" #include "bt_parallel.h"
#include "bt_selector.h" #include "bt_selector.h"
#include "bt_sequence.h" #include "bt_sequence.h"
@ -24,6 +28,10 @@ void register_limboai_types() {
ClassDB::register_class<BTSequence>(); ClassDB::register_class<BTSequence>();
ClassDB::register_class<BTSelector>(); ClassDB::register_class<BTSelector>();
ClassDB::register_class<BTParallel>(); ClassDB::register_class<BTParallel>();
ClassDB::register_class<BTInvert>();
ClassDB::register_class<BTAlwaysFail>();
ClassDB::register_class<BTAlwaysSucceed>();
ClassDB::register_class<BTDelay>();
LimboStringNames::create(); LimboStringNames::create();
} }