Skip to content

Commit

Permalink
inheritance for character and controller classes implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
vorlac committed Oct 6, 2023
1 parent 615a465 commit 3e9d690
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 100 deletions.
45 changes: 23 additions & 22 deletions src/api/extension_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -20,14 +21,14 @@
#include <godot_cpp/core/memory.hpp>
#include <godot_cpp/variant/string_name.hpp>

namespace godot
namespace rl
{
static inline rl::Console<RichTextLabel>* console_singleton{ nullptr };
static inline console* console_singleton{ nullptr };

void initialize_static_objects()
{
console_singleton = memnew(rl::Console<RichTextLabel>);
rl::engine::get()->register_singleton("Console", rl::Console<RichTextLabel>::get());
console_singleton = memnew(console);
rl::engine::get()->register_singleton("Console", console::get());
}

void teardown_static_objects()
Expand All @@ -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<rl::Console<RichTextLabel>>();
godot::ClassDB::register_class<rl::Projectile>();
godot::ClassDB::register_class<rl::ProjectileSpawner>();

ClassDB::register_class<rl::Projectile>();
ClassDB::register_class<rl::ProjectileSpawner>();
godot::ClassDB::register_abstract_class<rl::CharacterController>();
godot::ClassDB::register_class<rl::PlayerController>();
godot::ClassDB::register_class<rl::EnemyController>();

ClassDB::register_class<rl::CharacterController>();
ClassDB::register_class<rl::PlayerController>();
// ClassDB::register_class<rl::EnemyController>();
godot::ClassDB::register_class<rl::Camera>();
godot::ClassDB::register_class<rl::Character>();
godot::ClassDB::register_class<rl::Enemy>();
godot::ClassDB::register_class<rl::Player>();

ClassDB::register_class<rl::Camera>();
ClassDB::register_class<rl::Character>();
ClassDB::register_class<rl::Enemy>();
ClassDB::register_class<rl::Player>();
godot::ClassDB::register_class<rl::Level>();
godot::ClassDB::register_class<rl::MainDialog>();
godot::ClassDB::register_class<rl::Main>();

ClassDB::register_class<rl::Level>();
ClassDB::register_class<rl::MainDialog>();
ClassDB::register_class<rl::Main>();
godot::ClassDB::register_class<console>();

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();
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/core/concepts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace rl::inline utils
|| std::same_as<std::remove_cvref_t<std::remove_pointer_t<T>>, godot::Object>;

template <typename T>
concept VariantConstructable = requires(const T& t) { godot::Variant(t); };
concept VariantConstructable = requires(const T& t) { static_cast<godot::Variant>(t); };

template <typename T>
concept VariantConvertable = requires(T t) { static_cast<T>(t); };
Expand Down
23 changes: 17 additions & 6 deletions src/entity/character/character.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <concepts>

Expand Down Expand Up @@ -52,8 +54,19 @@ namespace rl
protected:
template <typename T>
requires std::derived_from<T, Character>
static void bind_member_functions()
static void bind_members()
{
log::print("Character::bind_members<{}>() called",
to<std::string_view>(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);
Expand All @@ -62,22 +75,20 @@ namespace rl
bind_property(T, movement_friction, double);
bind_property(T, rotation_speed, double);

using pos_changed_signal_t
= std::type_identity_t<signal_binding<T, event::position_changed>>;
using pos_changed_signal_t = signal_binding<T, event::position_changed>;
using add_pos_changed_signal_binding_t
= pos_changed_signal_t::template add<godot::Object*, godot::Vector2>;
add_pos_changed_signal_binding_t();

using spawn_projectile_signal_t
= std::type_identity_t<signal_binding<T, event::spawn_projectile>>;
using spawn_projectile_signal_t = signal_binding<T, event::spawn_projectile>;
using add_spawn_projectile_signal_binding_t
= spawn_projectile_signal_t::template add<godot::Object*, godot::Vector2>;
add_spawn_projectile_signal_binding_t();
}

static void _bind_methods()
{
bind_member_functions<Character>();
Character::bind_members<Character>();
}

protected:
Expand Down
4 changes: 4 additions & 0 deletions src/entity/character/enemy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ namespace rl
{
scene::node::set_unique_name(this, name::character::enemy);
}

void Enemy::_bind_methods()
{
}
}
10 changes: 2 additions & 8 deletions src/entity/character/enemy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,9 @@ namespace rl
public:
Enemy();

void _ready() override
{
Character::_ready();
}
using Character::_ready;

protected:
static void _bind_methods()
{
signal_binding<Enemy, event::spawn_projectile>::add<>();
}
static void _bind_methods();
};
}
1 change: 0 additions & 1 deletion src/entity/character/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ namespace rl

void Player::_bind_methods()
{
Character::bind_member_functions<Player>();
}
}
32 changes: 16 additions & 16 deletions src/entity/controller/character_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<CharacterController>();
}
// 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<CharacterController>();
// }
}
52 changes: 33 additions & 19 deletions src/entity/controller/character_controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "core/concepts.hpp"
#include "core/constants.hpp"
#include "util/bind.hpp"
#include "util/io.hpp"

#include <type_traits>

Expand All @@ -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 <typename T>
requires std::derived_from<T, CharacterController>
static void bind_member_functions()
static void bind_members()
{
using character_move_signal_t
= std::type_identity_t<signal_binding<T, event::character_move>>;
log::print("CharacterController::bind_members<{}>() called",
to<std::string_view>(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<T, event::character_move>;
using add_character_move_signal_binding_t
= character_move_signal_t::template add<godot::Vector2, double>;
add_character_move_signal_binding_t();

using character_rotate_signal_t
= std::type_identity_t<signal_binding<T, event::character_rotate>>;
using character_rotate_signal_t = signal_binding<T, event::character_rotate>;
using add_character_rotate_signal_binding_t
= character_rotate_signal_t::template add<double, double>;
add_character_rotate_signal_binding_t();

using character_shoot_signal_t
= std::type_identity_t<signal_binding<T, event::character_shoot>>;
using character_shoot_signal_t = signal_binding<T, event::character_shoot>;
using add_character_shoot_signal_binding_t
= character_shoot_signal_t::template add<godot::Object*>;
add_character_shoot_signal_binding_t();

using position_changed_signal_t
= std::type_identity_t<signal_binding<T, event::position_changed>>;
using position_changed_signal_t = signal_binding<T, event::position_changed>;
using add_position_changed_signal_binding_t
= position_changed_signal_t::template add<godot::Object*, godot::Vector2>;
add_position_changed_signal_binding_t();
}

static void _bind_methods();
static void _bind_methods()
{
CharacterController::bind_members<CharacterController>();
}

protected:
// the active input mode for character controls
Expand Down
25 changes: 20 additions & 5 deletions src/entity/controller/enemy_controller.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#pragma once

#include "entity/character/character_controller.hpp"
#include "entity/controller/character_controller.hpp"

namespace godot
{
class Input;
}

namespace rl
{
Expand All @@ -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()
{
}
};
}
5 changes: 0 additions & 5 deletions src/entity/controller/player_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<PlayerController>();
}
}
Loading

0 comments on commit 3e9d690

Please sign in to comment.