From 3e9d690234d21eaab7c061e5e533b9436a7b6b98 Mon Sep 17 00:00:00 2001 From: sal Date: Fri, 6 Oct 2023 13:48:19 -0400 Subject: [PATCH] inheritance for character and controller classes implemented --- src/api/extension_interface.cpp | 45 ++++++++-------- src/core/concepts.hpp | 2 +- src/entity/character/character.hpp | 23 +++++--- src/entity/character/enemy.cpp | 4 ++ src/entity/character/enemy.hpp | 10 +--- src/entity/character/player.cpp | 1 - .../controller/character_controller.cpp | 32 ++++++------ .../controller/character_controller.hpp | 52 ++++++++++++------- src/entity/controller/enemy_controller.hpp | 25 +++++++-- src/entity/controller/player_controller.cpp | 5 -- src/entity/controller/player_controller.hpp | 20 ++----- src/entity/projectile/projectile.hpp | 1 + src/util/io.hpp | 5 +- 13 files changed, 125 insertions(+), 100 deletions(-) diff --git a/src/api/extension_interface.cpp b/src/api/extension_interface.cpp index 34248d8..3e6745f 100644 --- a/src/api/extension_interface.cpp +++ b/src/api/extension_interface.cpp @@ -5,6 +5,7 @@ #include "entity/character/enemy.hpp" #include "entity/character/player.hpp" #include "entity/controller/character_controller.hpp" +#include "entity/controller/enemy_controller.hpp" #include "entity/controller/player_controller.hpp" #include "entity/level.hpp" #include "entity/projectile/projectile_spawner.hpp" @@ -20,14 +21,14 @@ #include #include -namespace godot +namespace rl { - static inline rl::Console* console_singleton{ nullptr }; + static inline console* console_singleton{ nullptr }; void initialize_static_objects() { - console_singleton = memnew(rl::Console); - rl::engine::get()->register_singleton("Console", rl::Console::get()); + console_singleton = memnew(console); + rl::engine::get()->register_singleton("Console", console::get()); } void teardown_static_objects() @@ -36,35 +37,35 @@ namespace godot memdelete(console_singleton); } - void initialize_extension_module(ModuleInitializationLevel init_level) + void initialize_extension_module(godot::ModuleInitializationLevel init_level) { - if (init_level != MODULE_INITIALIZATION_LEVEL_SCENE) + if (init_level != godot::MODULE_INITIALIZATION_LEVEL_SCENE) return; - ClassDB::register_class>(); + godot::ClassDB::register_class(); + godot::ClassDB::register_class(); - ClassDB::register_class(); - ClassDB::register_class(); + godot::ClassDB::register_abstract_class(); + godot::ClassDB::register_class(); + godot::ClassDB::register_class(); - ClassDB::register_class(); - ClassDB::register_class(); - // ClassDB::register_class(); + godot::ClassDB::register_class(); + godot::ClassDB::register_class(); + godot::ClassDB::register_class(); + godot::ClassDB::register_class(); - ClassDB::register_class(); - ClassDB::register_class(); - ClassDB::register_class(); - ClassDB::register_class(); + godot::ClassDB::register_class(); + godot::ClassDB::register_class(); + godot::ClassDB::register_class(); - ClassDB::register_class(); - ClassDB::register_class(); - ClassDB::register_class(); + godot::ClassDB::register_class(); initialize_static_objects(); } - void uninitialize_extension_module(ModuleInitializationLevel init_level) + void uninitialize_extension_module(godot::ModuleInitializationLevel init_level) { - if (init_level != MODULE_INITIALIZATION_LEVEL_SCENE) + if (init_level != godot::MODULE_INITIALIZATION_LEVEL_SCENE) return; teardown_static_objects(); @@ -76,7 +77,7 @@ namespace godot GDExtensionClassLibraryPtr lib, GDExtensionInitialization* init) { - const auto init_level = MODULE_INITIALIZATION_LEVEL_SCENE; + const auto init_level = godot::MODULE_INITIALIZATION_LEVEL_SCENE; godot::GDExtensionBinding::InitObject init_obj(addr, lib, init); init_obj.register_initializer(initialize_extension_module); diff --git a/src/core/concepts.hpp b/src/core/concepts.hpp index bd66763..d9fbf2f 100644 --- a/src/core/concepts.hpp +++ b/src/core/concepts.hpp @@ -26,7 +26,7 @@ namespace rl::inline utils || std::same_as>, godot::Object>; template - concept VariantConstructable = requires(const T& t) { godot::Variant(t); }; + concept VariantConstructable = requires(const T& t) { static_cast(t); }; template concept VariantConvertable = requires(T t) { static_cast(t); }; diff --git a/src/entity/character/character.hpp b/src/entity/character/character.hpp index 2601cbb..771ebed 100644 --- a/src/entity/character/character.hpp +++ b/src/entity/character/character.hpp @@ -3,7 +3,9 @@ #include "core/attributes.hpp" #include "core/constants.hpp" #include "entity/camera.hpp" +#include "singletons/console.hpp" #include "util/bind.hpp" +#include "util/io.hpp" #include @@ -52,8 +54,19 @@ namespace rl protected: template requires std::derived_from - static void bind_member_functions() + static void bind_members() { + log::print("Character::bind_members<{}>() called", + to(T::get_class_static())); + log::print(" - method: {} binded", "on_character_movement"); + log::print(" - method: {} binded", "on_character_rotate"); + log::print(" - method: {} binded", "on_character_shoot"); + log::print(" - property: {} added", "movement_speed"); + log::print(" - property: {} added", "movement_friction"); + log::print(" - property: {} added", "rotation_speed"); + log::print(" - signal: {} added", event::character_shoot); + log::print(" - signal: {} added", event::spawn_projectile); + bind_member_function(T, on_character_movement); bind_member_function(T, on_character_rotate); bind_member_function(T, on_character_shoot); @@ -62,14 +75,12 @@ namespace rl bind_property(T, movement_friction, double); bind_property(T, rotation_speed, double); - using pos_changed_signal_t - = std::type_identity_t>; + using pos_changed_signal_t = signal_binding; using add_pos_changed_signal_binding_t = pos_changed_signal_t::template add; add_pos_changed_signal_binding_t(); - using spawn_projectile_signal_t - = std::type_identity_t>; + using spawn_projectile_signal_t = signal_binding; using add_spawn_projectile_signal_binding_t = spawn_projectile_signal_t::template add; add_spawn_projectile_signal_binding_t(); @@ -77,7 +88,7 @@ namespace rl static void _bind_methods() { - bind_member_functions(); + Character::bind_members(); } protected: diff --git a/src/entity/character/enemy.cpp b/src/entity/character/enemy.cpp index e8cbaf1..1576471 100644 --- a/src/entity/character/enemy.cpp +++ b/src/entity/character/enemy.cpp @@ -11,4 +11,8 @@ namespace rl { scene::node::set_unique_name(this, name::character::enemy); } + + void Enemy::_bind_methods() + { + } } diff --git a/src/entity/character/enemy.hpp b/src/entity/character/enemy.hpp index 9e92032..ef701d6 100644 --- a/src/entity/character/enemy.hpp +++ b/src/entity/character/enemy.hpp @@ -13,15 +13,9 @@ namespace rl public: Enemy(); - void _ready() override - { - Character::_ready(); - } + using Character::_ready; protected: - static void _bind_methods() - { - signal_binding::add<>(); - } + static void _bind_methods(); }; } diff --git a/src/entity/character/player.cpp b/src/entity/character/player.cpp index dced6b1..a1de800 100644 --- a/src/entity/character/player.cpp +++ b/src/entity/character/player.cpp @@ -14,6 +14,5 @@ namespace rl void Player::_bind_methods() { - Character::bind_member_functions(); } } diff --git a/src/entity/controller/character_controller.cpp b/src/entity/controller/character_controller.cpp index e629e9a..599f95f 100644 --- a/src/entity/controller/character_controller.cpp +++ b/src/entity/controller/character_controller.cpp @@ -34,20 +34,20 @@ namespace rl } } - void CharacterController::process_action_input(godot::Input* const input, double delta_time) - { - } - - void CharacterController::process_movement_input(godot::Input* const input, double delta_time) - { - } - - void CharacterController::process_rotation_input(godot::Input* const input, double delta_time) - { - } - - void CharacterController::_bind_methods() - { - bind_member_functions(); - } + // void CharacterController::process_action_input(godot::Input* const input, double delta_time) + //{ + // } + + // void CharacterController::process_movement_input(godot::Input* const input, double delta_time) + //{ + // } + + // void CharacterController::process_rotation_input(godot::Input* const input, double delta_time) + //{ + // } + + // void CharacterController::_bind_methods() + //{ + // CharacterController::bind_members(); + // } } diff --git a/src/entity/controller/character_controller.hpp b/src/entity/controller/character_controller.hpp index 9fb3548..2e2e86b 100644 --- a/src/entity/controller/character_controller.hpp +++ b/src/entity/controller/character_controller.hpp @@ -3,6 +3,7 @@ #include "core/concepts.hpp" #include "core/constants.hpp" #include "util/bind.hpp" +#include "util/io.hpp" #include @@ -17,56 +18,69 @@ namespace rl { GDCLASS(CharacterController, godot::Node2D); + public: + enum class InputMode + { + MouseAndKeyboard, + Controller, + AI + }; + public: CharacterController() = default; virtual ~CharacterController() = default; void _process(double delta_time) override; - virtual void process_action_input(godot::Input* const input, double delta_time); - virtual void process_movement_input(godot::Input* const input, double delta_time); - virtual void process_rotation_input(godot::Input* const input, double delta_time); + virtual void process_action_input(godot::Input* const input, double delta_time) + { + } - protected: - enum class InputMode + virtual void process_movement_input(godot::Input* const input, double delta_time) { - MouseAndKeyboard, - Controller - }; + } - InputMode get_input_mode(godot::Input* const input); + virtual void process_rotation_input(godot::Input* const input, double delta_time) + { + } protected: template requires std::derived_from - static void bind_member_functions() + static void bind_members() { - using character_move_signal_t - = std::type_identity_t>; + log::print("CharacterController::bind_members<{}>() called", + to(T::get_class_static())); + log::print(" - signal: {} added", event::character_move); + log::print(" - signal: {} added", event::character_rotate); + log::print(" - signal: {} added", event::character_shoot); + log::print(" - signal: {} added", event::position_changed); + + using character_move_signal_t = signal_binding; using add_character_move_signal_binding_t = character_move_signal_t::template add; add_character_move_signal_binding_t(); - using character_rotate_signal_t - = std::type_identity_t>; + using character_rotate_signal_t = signal_binding; using add_character_rotate_signal_binding_t = character_rotate_signal_t::template add; add_character_rotate_signal_binding_t(); - using character_shoot_signal_t - = std::type_identity_t>; + using character_shoot_signal_t = signal_binding; using add_character_shoot_signal_binding_t = character_shoot_signal_t::template add; add_character_shoot_signal_binding_t(); - using position_changed_signal_t - = std::type_identity_t>; + using position_changed_signal_t = signal_binding; using add_position_changed_signal_binding_t = position_changed_signal_t::template add; add_position_changed_signal_binding_t(); } - static void _bind_methods(); + static void _bind_methods() + { + CharacterController::bind_members(); + } protected: // the active input mode for character controls diff --git a/src/entity/controller/enemy_controller.hpp b/src/entity/controller/enemy_controller.hpp index 2648648..1ff8aed 100644 --- a/src/entity/controller/enemy_controller.hpp +++ b/src/entity/controller/enemy_controller.hpp @@ -1,6 +1,11 @@ #pragma once -#include "entity/character/character_controller.hpp" +#include "entity/controller/character_controller.hpp" + +namespace godot +{ + class Input; +} namespace rl { @@ -12,11 +17,21 @@ namespace rl EnemyController() = default; ~EnemyController() = default; - void process_action_input(godot::Input* const input, double delta_time) override; - void process_movement_input(godot::Input* const input, double delta_time) override; - void process_rotation_input(godot::Input* const input, double delta_time) override; + void process_action_input(godot::Input* const input, double delta_time) override + { + } + + void process_movement_input(godot::Input* const input, double delta_time) override + { + } + + void process_rotation_input(godot::Input* const input, double delta_time) override + { + } protected: - static void _bind_methods(); + static void _bind_methods() + { + } }; } diff --git a/src/entity/controller/player_controller.cpp b/src/entity/controller/player_controller.cpp index 4fcaa2f..7b91d0b 100644 --- a/src/entity/controller/player_controller.cpp +++ b/src/entity/controller/player_controller.cpp @@ -82,9 +82,4 @@ namespace rl this->emit_signal(event::character_rotate, m_rotation_angle, delta_time); } - - void PlayerController::_bind_methods() - { - CharacterController::bind_member_functions(); - } } diff --git a/src/entity/controller/player_controller.hpp b/src/entity/controller/player_controller.hpp index c721265..0280e6d 100644 --- a/src/entity/controller/player_controller.hpp +++ b/src/entity/controller/player_controller.hpp @@ -8,6 +8,7 @@ namespace rl { GDCLASS(PlayerController, CharacterController); + public: public: PlayerController() = default; ~PlayerController() = default; @@ -15,24 +16,11 @@ namespace rl void process_action_input(godot::Input* const input, double delta_time) override; void process_movement_input(godot::Input* const input, double delta_time) override; void process_rotation_input(godot::Input* const input, double delta_time) override; - - protected: - enum class InputMode - { - MouseAndKeyboard, - Controller - }; - InputMode get_input_mode(godot::Input* const input); - static void _bind_methods(); - protected: - // the active input mode for character controls - InputMode m_input_mode{ InputMode::MouseAndKeyboard }; - // target rotation - double m_rotation_angle{ 0.0 }; - // elapsed runtime (seconds) - double m_elapsed_time{ 0.0 }; + static void _bind_methods() + { + } }; } diff --git a/src/entity/projectile/projectile.hpp b/src/entity/projectile/projectile.hpp index b1cc50d..454e278 100644 --- a/src/entity/projectile/projectile.hpp +++ b/src/entity/projectile/projectile.hpp @@ -27,6 +27,7 @@ namespace rl [[property]] double get_max_travel_dist() const; [[property]] double get_acceleration() const; [[property]] double get_velocity() const; + [[property]] void set_movement_speed(double speed); [[property]] void set_time_to_live(double ttl); [[property]] void set_max_travel_dist(double dist); diff --git a/src/util/io.hpp b/src/util/io.hpp index 3d63b9b..919b4b3 100644 --- a/src/util/io.hpp +++ b/src/util/io.hpp @@ -15,6 +15,8 @@ #include #include +#include + namespace rl::inline utils { namespace io @@ -125,7 +127,8 @@ namespace rl::inline utils template static inline void print(spdlog::format_string_t fmt, Args&&... args) { - spdlog::debug(fmt, std::forward(args)...); + std::string msg{ fmt::format(fmt, std::forward(args)...) }; + ERR_PRINT_ED(msg.data()); } }; }