Add Wait, WaitTicks and RandomWait

This commit is contained in:
Serhii Snitsaruk 2022-08-29 14:06:48 +02:00
parent 8bfd8e1bef
commit 936bf749d3
9 changed files with 193 additions and 0 deletions

View File

@ -0,0 +1,28 @@
/* bt_random_wait.cpp */
#include "bt_random_wait.h"
String BTRandomWait::_generate_name() const {
return vformat("Wait %s to %s sec", duration_min_max.x, duration_min_max.y);
}
void BTRandomWait::_enter() {
_time_passed = 0.0;
_duration = Math::random(duration_min_max.x, duration_min_max.y);
}
int BTRandomWait::_tick(float p_delta) {
_time_passed += p_delta;
if (_time_passed < _duration) {
return RUNNING;
} else {
return SUCCESS;
}
}
void BTRandomWait::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_duration_min_max", "p_value"), &BTRandomWait::set_duration_min_max);
ClassDB::bind_method(D_METHOD("get_duration_min_max"), &BTRandomWait::get_duration_min_max);
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "duration_min_max"), "set_duration_min_max", "get_duration_min_max");
}

View File

@ -0,0 +1,33 @@
/* bt_random_wait.h */
#ifndef BT_RANDOM_WAIT_H
#define BT_RANDOM_WAIT_H
#include "bt_action.h"
#include "core/object.h"
class BTRandomWait : public BTAction {
GDCLASS(BTRandomWait, BTAction);
private:
Vector2 duration_min_max = Vector2(1.0, 2.0);
float _time_passed = 0.0;
float _duration = 0.0;
protected:
static void _bind_methods();
virtual String _generate_name() const;
virtual void _enter();
virtual int _tick(float p_delta);
public:
void set_duration_min_max(Vector2 p_value) {
duration_min_max = p_value;
emit_changed();
}
Vector2 get_duration_min_max() const { return duration_min_max; }
};
#endif // BT_RANDOM_WAIT_H

30
limboai/bt/bt_wait.cpp Normal file
View File

@ -0,0 +1,30 @@
/* bt_wait.cpp */
#include "bt_wait.h"
#include "core/class_db.h"
#include "core/object.h"
#include "core/variant.h"
String BTWait::_generate_name() const {
return vformat("Wait %ss", duration);
}
void BTWait::_enter() {
_time_passed = 0.0;
}
int BTWait::_tick(float p_delta) {
_time_passed += p_delta;
if (_time_passed < duration) {
return RUNNING;
} else {
return SUCCESS;
}
}
void BTWait::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_duration", "p_value"), &BTWait::set_duration);
ClassDB::bind_method(D_METHOD("get_duration"), &BTWait::get_duration);
ADD_PROPERTY(PropertyInfo(Variant::REAL, "duration"), "set_duration", "get_duration");
}

32
limboai/bt/bt_wait.h Normal file
View File

@ -0,0 +1,32 @@
/* bt_wait.h */
#ifndef BT_WAIT_H
#define BT_WAIT_H
#include "bt_action.h"
#include "core/object.h"
class BTWait : public BTAction {
GDCLASS(BTWait, BTAction);
private:
float duration = 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_duration(float p_value) {
duration = p_value;
emit_changed();
}
float get_duration() const { return duration; }
};
#endif // BT_WAIT_H

View File

@ -0,0 +1,30 @@
/* bt_wait_ticks.cpp */
#include "bt_wait_ticks.h"
#include "core/class_db.h"
#include "core/object.h"
#include "core/variant.h"
String BTWaitTicks::_generate_name() const {
return vformat("WaitTicks x%d", num_ticks);
}
void BTWaitTicks::_enter() {
_num_passed = 0;
}
int BTWaitTicks::_tick(float p_delta) {
_num_passed += 1;
if (_num_passed < num_ticks) {
return RUNNING;
} else {
return SUCCESS;
}
}
void BTWaitTicks::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_num_ticks", "p_value"), &BTWaitTicks::set_num_ticks);
ClassDB::bind_method(D_METHOD("get_num_ticks"), &BTWaitTicks::get_num_ticks);
ADD_PROPERTY(PropertyInfo(Variant::INT, "num_ticks"), "set_num_ticks", "get_num_ticks");
}

View File

@ -0,0 +1,32 @@
/* bt_wait_num_ticks.h */
#ifndef BT_WAIT_TICKS_H
#define BT_WAIT_TICKS_H
#include "bt_action.h"
#include "core/object.h"
class BTWaitTicks : public BTAction {
GDCLASS(BTWaitTicks, BTAction);
private:
int num_ticks = 1;
int _num_passed = 0;
protected:
static void _bind_methods();
virtual String _generate_name() const;
virtual void _enter();
virtual int _tick(float p_delta);
public:
void set_num_ticks(int p_value) {
num_ticks = p_value;
emit_changed();
}
int get_num_ticks() const { return num_ticks; }
};
#endif // BT_WAIT_TICKS_H

View File

@ -0,0 +1 @@
<svg viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"><path d="m12.63 5.19.74-.74-1.08-1.08-.78.77a5.81 5.81 0 0 0 -1.89-.91v-2.23h-3.24v2.23a6 6 0 1 0 6.25 2zm-4.63 8.51a4.69 4.69 0 0 1 -.65-9.33v4.63h1.3v-4.63a4.69 4.69 0 0 1 -.65 9.33z" fill="#e0e0e0"/></svg>

After

Width:  |  Height:  |  Size: 268 B

View File

@ -0,0 +1 @@
<svg viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"><path d="m12.63 5.19.74-.74-1.08-1.08-.78.77a5.81 5.81 0 0 0 -1.89-.91v-2.23h-3.24v2.23a6 6 0 1 0 6.25 2zm-4.63 8.51a4.69 4.69 0 0 1 -.65-9.33v4.63h1.3v-4.63a4.69 4.69 0 0 1 -.65 9.33z" fill="#e0e0e0"/></svg>

After

Width:  |  Height:  |  Size: 268 B

View File

@ -20,6 +20,7 @@
#include "bt/bt_probability.h"
#include "bt/bt_random_selector.h"
#include "bt/bt_random_sequence.h"
#include "bt/bt_random_wait.h"
#include "bt/bt_repeat.h"
#include "bt/bt_repeat_until_failure.h"
#include "bt/bt_repeat_until_success.h"
@ -28,6 +29,8 @@
#include "bt/bt_sequence.h"
#include "bt/bt_task.h"
#include "bt/bt_time_limit.h"
#include "bt/bt_wait.h"
#include "bt/bt_wait_ticks.h"
#include "limbo_string_names.h"
#include "limbo_utility.h"
@ -61,6 +64,9 @@ void register_limboai_types() {
ClassDB::register_class<BTProbability>();
ClassDB::register_class<BTFail>();
ClassDB::register_class<BTWait>();
ClassDB::register_class<BTRandomWait>();
ClassDB::register_class<BTWaitTicks>();
LimboStringNames::create();
}