From 2c218459914ceaf583c4425e29260810c01b74bb Mon Sep 17 00:00:00 2001 From: Sara Date: Fri, 9 Aug 2024 15:45:20 +0200 Subject: [PATCH] feat: added print debug info interaction to player and unit --- godot/project.godot | 5 +++++ src/rts_player.cpp | 20 ++++++++++++++++++++ src/rts_player.hpp | 9 +++++---- src/unit.cpp | 31 +++++++++++++++++++++++++++++-- src/unit.hpp | 4 ++++ 5 files changed, 63 insertions(+), 6 deletions(-) diff --git a/godot/project.godot b/godot/project.godot index eb1236d..63dbb3b 100644 --- a/godot/project.godot +++ b/godot/project.godot @@ -82,6 +82,11 @@ rotate_right={ "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":69,"key_label":0,"unicode":101,"echo":false,"script":null) ] } +DEBUG_toggle_debug={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":96,"key_label":0,"unicode":96,"echo":false,"script":null) +] +} [layer_names] diff --git a/src/rts_player.cpp b/src/rts_player.cpp index bf4fadd..739d17b 100644 --- a/src/rts_player.cpp +++ b/src/rts_player.cpp @@ -41,6 +41,10 @@ void RTSPlayer::setup_player_input(utils::PlayerInput *input) { input->listen_to("rotate_left", "rotate_right", callable_mp(this, &RTSPlayer::on_rotate_horizontal)); input->listen_to("_mouse_left", "_mouse_right", callable_mp(this, &RTSPlayer::on_mouse_horizontal)); input->listen_to("_mouse_down", "_mouse_up", callable_mp(this, &RTSPlayer::on_mouse_vertical)); +#ifdef DEBUG_ENABLED + input->listen_to("DEBUG_toggle_debug", callable_mp(this, &RTSPlayer::DEBUG_enable_debug)); + gd::UtilityFunctions::print("!!! DEBUG_ENABLED"); +#endif } gd::Node *RTSPlayer::to_node() { @@ -81,6 +85,12 @@ gd::Node3D *RTSPlayer::node_under_cursor(uint32_t layers) const { void RTSPlayer::select_unit_under_cursor() { Unit *unit = Object::cast_to(this->node_under_cursor(0x2)); +#ifdef DEBUG_ENABLED + if(unit != nullptr && this->DEBUG_debug_enabled) { + unit->DEBUG_print_debug_info(); + return; + } +#endif if(unit != nullptr) this->select_unit(unit); } @@ -192,3 +202,13 @@ void RTSPlayer::set_ground_marker_scene(gd::Ref scene) { gd::Ref RTSPlayer::get_ground_marker_scene() const { return this->ground_marker_scene; } + +#ifdef DEBUG_ENABLED +void RTSPlayer::DEBUG_enable_debug(gd::Ref, float value) { + if(value != 0) { + this->DEBUG_debug_enabled = !this->DEBUG_debug_enabled; + gd::UtilityFunctions::print("DEBUG toggled debug actions. Now ", this->DEBUG_debug_enabled); + } +} +#endif + diff --git a/src/rts_player.hpp b/src/rts_player.hpp index e30f562..44ed5c4 100644 --- a/src/rts_player.hpp +++ b/src/rts_player.hpp @@ -72,13 +72,9 @@ private: gd::Vector2 camera_keys_motion{0.f, 0.f}; float camera_keys_rotation{0.f}; gd::Vector2 total_cursor_motion{}; - gd::Vector3 cursor_camera_normal{0.f, 0.f, 0.f}; - gd::Camera3D *camera{nullptr}; - gd::Ref ground_marker_scene{nullptr}; - gd::Callable const on_selected_unit_destroyed{callable_mp(this, &RTSPlayer::clear_selected_unit)}; float const camera_keys_speed{10.f}; @@ -86,6 +82,11 @@ private: float const camera_mouse_speed{0.01f}; float const camera_mouse_rotation_speed{-0.003f}; double const time_to_held{0.1}; +#ifdef DEBUG_ENABLED +private: + bool DEBUG_debug_enabled{false}; + void DEBUG_enable_debug(gd::Ref, float value); +#endif // DEBUG_ENABLED }; #endif // !RTS_PLAYER_HPP diff --git a/src/unit.cpp b/src/unit.cpp index 4d5c3a5..131c9bd 100644 --- a/src/unit.cpp +++ b/src/unit.cpp @@ -135,13 +135,13 @@ void Unit::next_action() { return; goap::Action const *action{this->current_plan.get(0)}; if(!action->is_possible(this->world_state)) { - this->call_deferred("emit_signal", "plan_interrupted"); + this->emit_signal("plan_interrupted"); return; } // pop next action and apply state this->state = action->get_apply_state(this->world_state); if(state == nullptr) { - this->call_deferred("emit_signal", "plan_interrupted"); + this->emit_signal("plan_interrupted"); return; } this->current_plan.remove_at(0); @@ -197,3 +197,30 @@ void Unit::set_movement_speed(float speed) { float Unit::get_movement_speed() const { return this->movement_speed; } + +#ifdef DEBUG_ENABLED +gd::String Unit::DEBUG_print_debug_info() { + gd::String debug_info{"goal: "}; + if(!this->current_goal.is_valid()) { + debug_info += "No goal assigned"; + } else { + debug_info += this->current_goal->get_path(); + debug_info += "\nplan:"; + if(this->state != nullptr) { + debug_info += "\n "; + debug_info += this->state->get_action()->get_class(); + debug_info += " (currently in-progress)"; + } + for(goap::Action const *action : this->current_plan) { + debug_info += "\n "; + debug_info += action->get_class(); + } + } + if(this->state != nullptr) { + debug_info += "\ncurrent state: "; + debug_info += this->state->get_class(); + } + gd::UtilityFunctions::print(this->get_path(), "debug info:\n", debug_info); + return debug_info; +} +#endif diff --git a/src/unit.hpp b/src/unit.hpp index cbcf4ce..6f46824 100644 --- a/src/unit.hpp +++ b/src/unit.hpp @@ -69,6 +69,10 @@ protected: goap::Planner *planner{nullptr}; EntityHealth *health{nullptr}; UnitWorldState *world_state{nullptr}; +#ifdef DEBUG_ENABLED +public: + gd::String DEBUG_print_debug_info(); +#endif // DEBUG_ENABLED }; #endif // !RTS_UNIT_HPP