Refactor BTAction classes

This commit is contained in:
Serhii Snitsaruk 2022-12-17 11:26:48 +01:00
parent 0ba36cb3dd
commit 3693c3fc50
8 changed files with 40 additions and 40 deletions

View File

@ -11,42 +11,42 @@ String BTConsolePrint::_generate_name() const {
tx = text.substr(0, 30) + "..."; tx = text.substr(0, 30) + "...";
} }
tx = tx.replace("\"", "\\\""); tx = tx.replace("\"", "\\\"");
if (format_var_args.size() > 0) { if (bb_format_parameters.size() > 0) {
return vformat("ConsolePrint text: \"%s\" format_args: %s", tx, format_var_args); return vformat("ConsolePrint text: \"%s\" format_parameters: %s", tx, bb_format_parameters);
} }
return vformat("ConsolePrint \"%s\"", tx); return vformat("ConsolePrint \"%s\"", tx);
} }
int BTConsolePrint::_tick(float p_delta) { int BTConsolePrint::_tick(float p_delta) {
switch (format_var_args.size()) { switch (bb_format_parameters.size()) {
case 0: { case 0: {
print_line(text); print_line(text);
} break; } break;
case 1: { case 1: {
print_line(vformat(text, get_blackboard()->get_var(format_var_args[0], ""))); print_line(vformat(text, get_blackboard()->get_var(bb_format_parameters[0], "")));
} break; } break;
case 2: { case 2: {
print_line(vformat(text, get_blackboard()->get_var(format_var_args[0], ""), print_line(vformat(text, get_blackboard()->get_var(bb_format_parameters[0], ""),
get_blackboard()->get_var(format_var_args[1], ""))); get_blackboard()->get_var(bb_format_parameters[1], "")));
} break; } break;
case 3: { case 3: {
print_line(vformat(text, get_blackboard()->get_var(format_var_args[0], ""), print_line(vformat(text, get_blackboard()->get_var(bb_format_parameters[0], ""),
get_blackboard()->get_var(format_var_args[1], ""), get_blackboard()->get_var(bb_format_parameters[1], ""),
get_blackboard()->get_var(format_var_args[2], ""))); get_blackboard()->get_var(bb_format_parameters[2], "")));
} break; } break;
case 4: { case 4: {
print_line(vformat(text, get_blackboard()->get_var(format_var_args[0], ""), print_line(vformat(text, get_blackboard()->get_var(bb_format_parameters[0], ""),
get_blackboard()->get_var(format_var_args[1], ""), get_blackboard()->get_var(bb_format_parameters[1], ""),
get_blackboard()->get_var(format_var_args[2], ""), get_blackboard()->get_var(bb_format_parameters[2], ""),
get_blackboard()->get_var(format_var_args[3], ""))); get_blackboard()->get_var(bb_format_parameters[3], "")));
} break; } break;
case 5: case 5:
default: { default: {
print_line(vformat(text, get_blackboard()->get_var(format_var_args[0], ""), print_line(vformat(text, get_blackboard()->get_var(bb_format_parameters[0], ""),
get_blackboard()->get_var(format_var_args[1], ""), get_blackboard()->get_var(bb_format_parameters[1], ""),
get_blackboard()->get_var(format_var_args[2], ""), get_blackboard()->get_var(bb_format_parameters[2], ""),
get_blackboard()->get_var(format_var_args[3], ""), get_blackboard()->get_var(bb_format_parameters[3], ""),
get_blackboard()->get_var(format_var_args[4], ""))); get_blackboard()->get_var(bb_format_parameters[4], "")));
} break; } break;
} }
return SUCCESS; return SUCCESS;
@ -57,7 +57,7 @@ String BTConsolePrint::get_configuration_warning() const {
if (!warning.is_empty()) { if (!warning.is_empty()) {
warning += "\n"; warning += "\n";
} }
if (format_var_args.size() > 5) { if (bb_format_parameters.size() > 5) {
warning += "ConsolePrint supports up to 5 format arguments.\n"; warning += "ConsolePrint supports up to 5 format arguments.\n";
} }
return warning; return warning;
@ -66,9 +66,9 @@ String BTConsolePrint::get_configuration_warning() const {
void BTConsolePrint::_bind_methods() { void BTConsolePrint::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_text", "p_text"), &BTConsolePrint::set_text); ClassDB::bind_method(D_METHOD("set_text", "p_text"), &BTConsolePrint::set_text);
ClassDB::bind_method(D_METHOD("get_text"), &BTConsolePrint::get_text); ClassDB::bind_method(D_METHOD("get_text"), &BTConsolePrint::get_text);
ClassDB::bind_method(D_METHOD("set_format_var_args", "p_variables"), &BTConsolePrint::set_format_var_args); ClassDB::bind_method(D_METHOD("set_bb_format_parameters", "p_variables"), &BTConsolePrint::set_bb_format_parameters);
ClassDB::bind_method(D_METHOD("get_format_var_args"), &BTConsolePrint::get_format_var_args); ClassDB::bind_method(D_METHOD("get_bb_format_parameters"), &BTConsolePrint::get_bb_format_parameters);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text"); ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "format_var_args"), "set_format_var_args", "get_format_var_args"); ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "bb_format_parameters"), "set_bb_format_parameters", "get_bb_format_parameters");
} }

View File

@ -12,7 +12,7 @@ class BTConsolePrint : public BTAction {
private: private:
String text; String text;
PackedStringArray format_var_args; PackedStringArray bb_format_parameters;
protected: protected:
static void _bind_methods(); static void _bind_methods();
@ -27,11 +27,11 @@ public:
} }
String get_text() const { return text; } String get_text() const { return text; }
void set_format_var_args(const PackedStringArray &p_value) { void set_bb_format_parameters(const PackedStringArray &p_value) {
format_var_args = p_value; bb_format_parameters = p_value;
emit_changed(); emit_changed();
} }
PackedStringArray get_format_var_args() const { return format_var_args; } PackedStringArray get_bb_format_parameters() const { return bb_format_parameters; }
virtual String get_configuration_warning() const override; virtual String get_configuration_warning() const override;
}; };

View File

@ -7,13 +7,13 @@ String BTRandomWait::_generate_name() const {
} }
void BTRandomWait::_enter() { void BTRandomWait::_enter() {
_time_passed = 0.0; time_passed = 0.0;
_duration = Math::random(duration_min_max.x, duration_min_max.y); duration = Math::random(duration_min_max.x, duration_min_max.y);
} }
int BTRandomWait::_tick(float p_delta) { int BTRandomWait::_tick(float p_delta) {
_time_passed += p_delta; time_passed += p_delta;
if (_time_passed < _duration) { if (time_passed < duration) {
return RUNNING; return RUNNING;
} else { } else {
return SUCCESS; return SUCCESS;

View File

@ -12,8 +12,8 @@ class BTRandomWait : public BTAction {
private: private:
Vector2 duration_min_max = Vector2(1.0, 2.0); Vector2 duration_min_max = Vector2(1.0, 2.0);
float _time_passed = 0.0; float time_passed = 0.0;
float _duration = 0.0; float duration = 0.0;
protected: protected:
static void _bind_methods(); static void _bind_methods();

View File

@ -10,12 +10,12 @@ String BTWait::_generate_name() const {
} }
void BTWait::_enter() { void BTWait::_enter() {
_time_passed = 0.0; time_passed = 0.0;
} }
int BTWait::_tick(float p_delta) { int BTWait::_tick(float p_delta) {
_time_passed += p_delta; time_passed += p_delta;
if (_time_passed < duration) { if (time_passed < duration) {
return RUNNING; return RUNNING;
} else { } else {
return SUCCESS; return SUCCESS;

View File

@ -12,7 +12,7 @@ class BTWait : public BTAction {
private: private:
float duration = 1.0; float duration = 1.0;
float _time_passed = 0.0; float time_passed = 0.0;
protected: protected:
static void _bind_methods(); static void _bind_methods();

View File

@ -10,12 +10,12 @@ String BTWaitTicks::_generate_name() const {
} }
void BTWaitTicks::_enter() { void BTWaitTicks::_enter() {
_num_passed = 0; num_passed = 0;
} }
int BTWaitTicks::_tick(float p_delta) { int BTWaitTicks::_tick(float p_delta) {
_num_passed += 1; num_passed += 1;
if (_num_passed < num_ticks) { if (num_passed < num_ticks) {
return RUNNING; return RUNNING;
} else { } else {
return SUCCESS; return SUCCESS;

View File

@ -12,7 +12,7 @@ class BTWaitTicks : public BTAction {
private: private:
int num_ticks = 1; int num_ticks = 1;
int _num_passed = 0; int num_passed = 0;
protected: protected:
static void _bind_methods(); static void _bind_methods();