From 3cad3af3fe5ebdc7191f8146d3191189aedcff15 Mon Sep 17 00:00:00 2001 From: Sara Date: Tue, 28 May 2024 16:28:36 +0200 Subject: [PATCH] feat: swapped from godot:: to utils:: namespace --- game_mode.cpp | 14 +++---- game_mode.hpp | 26 +++++++------ game_root.cpp | 93 +++++++++++++++++++++++----------------------- game_root.hpp | 46 ++++++++++++----------- game_state.cpp | 4 +- game_state.hpp | 9 +++-- godot_macros.h | 38 +++++++++---------- level.cpp | 24 ++---------- level.hpp | 29 +++++---------- player.cpp | 2 +- player.hpp | 11 ++++-- player_input.cpp | 49 ++++++++++++------------ player_input.hpp | 67 +++++++++++++++++---------------- register_types.cpp | 22 ++++++----- register_types.hpp | 2 + spawn_point.cpp | 4 +- spawn_point.hpp | 11 ++++-- 17 files changed, 222 insertions(+), 229 deletions(-) diff --git a/game_mode.cpp b/game_mode.cpp index f6fdf15..bbbb107 100644 --- a/game_mode.cpp +++ b/game_mode.cpp @@ -5,25 +5,25 @@ #include "utils/godot_macros.h" #include "game_state.hpp" -namespace godot { +namespace utils { void GameMode::_bind_methods() { #define CLASSNAME GameMode - GDPROPERTY_HINTED(game_state, Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, "GameState"); - GDPROPERTY_HINTED(player_scene, Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, "PackedScene"); + GDPROPERTY_HINTED(game_state, gd::Variant::OBJECT, gd::PROPERTY_HINT_RESOURCE_TYPE, "GameState"); + GDPROPERTY_HINTED(player_scene, gd::Variant::OBJECT, gd::PROPERTY_HINT_RESOURCE_TYPE, "PackedScene"); } void GameMode::_begin() {} void GameMode::_end() {} -void GameMode::set_player_scene(Ref scene) { +void GameMode::set_player_scene(gd::Ref scene) { this->player_scene = scene; } -Ref GameMode::get_player_scene() const { +gd::Ref GameMode::get_player_scene() const { return this->player_scene; } -void GameMode::set_game_state(Ref state) { +void GameMode::set_game_state(gd::Ref state) { if(state.is_null() || !state.is_valid()) { this->game_state.unref(); return; @@ -31,7 +31,7 @@ void GameMode::set_game_state(Ref state) { this->game_state = state; } -Ref GameMode::get_game_state() { +gd::Ref GameMode::get_game_state() { return this->game_state; } } diff --git a/game_mode.hpp b/game_mode.hpp index 737e03f..cf2f7d3 100644 --- a/game_mode.hpp +++ b/game_mode.hpp @@ -1,26 +1,28 @@ -#ifndef GAME_MODE_H -#define GAME_MODE_H +#ifndef GAME_MODE_HPP +#define GAME_MODE_HPP #include #include #include "game_state.hpp" -namespace godot { -class GameMode : public Resource { - GDCLASS(GameMode, Resource); +namespace gd = godot; + +namespace utils { +class GameMode : public gd::Resource { + GDCLASS(GameMode, gd::Resource); static void _bind_methods(); public: virtual void _begin(); virtual void _end(); - void set_player_scene(Ref scene); - Ref get_player_scene() const; - void set_game_state(Ref state); - Ref get_game_state(); + void set_player_scene(gd::Ref scene); + gd::Ref get_player_scene() const; + void set_game_state(gd::Ref state); + gd::Ref get_game_state(); private: - Ref player_scene{}; - Ref game_state{}; + gd::Ref player_scene{}; + gd::Ref game_state{}; }; } -#endif // !GAME_MODE_H +#endif // !GAME_MODE_HPP diff --git a/game_root.cpp b/game_root.cpp index 0348042..9b487de 100644 --- a/game_root.cpp +++ b/game_root.cpp @@ -4,6 +4,7 @@ #include "level.hpp" #include "player.hpp" #include "player_input.hpp" +#include "player.hpp" #include "spawn_point.hpp" #include #include @@ -15,14 +16,14 @@ #include #include -namespace godot { +namespace utils { void GameRoot3D::_bind_methods() { #define CLASSNAME GameRoot3D GDFUNCTION(reset_game_mode); - GDPROPERTY_HINTED(first_boot_level, Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, "PackedScene"); - GDSIGNAL("player_connected", PropertyInfo(Variant::OBJECT, "player_input", PROPERTY_HINT_NODE_TYPE, "PlayerInput")); - GDSIGNAL("player_disconnected", PropertyInfo(Variant::OBJECT, "player_input", PROPERTY_HINT_NODE_TYPE, "PlayerInput")); - GDSIGNAL("player_spawned", PropertyInfo(Variant::OBJECT, "player_info", PROPERTY_HINT_NODE_TYPE, "Node")); + GDPROPERTY_HINTED(first_boot_level, gd::Variant::OBJECT, gd::PROPERTY_HINT_RESOURCE_TYPE, "PackedScene"); + GDSIGNAL("player_connected", gd::PropertyInfo(gd::Variant::OBJECT, "player_input", gd::PROPERTY_HINT_NODE_TYPE, "PlayerInput")); + GDSIGNAL("player_disconnected", gd::PropertyInfo(gd::Variant::OBJECT, "player_input", gd::PROPERTY_HINT_NODE_TYPE, "PlayerInput")); + GDSIGNAL("player_spawned", gd::PropertyInfo(gd::Variant::OBJECT, "player_info", gd::PROPERTY_HINT_NODE_TYPE, "Node")); } GameRoot3D *GameRoot3D::get_singleton() { @@ -53,16 +54,16 @@ void GameRoot3D::player_input_connected() { PlayerInput *input = memnew(PlayerInput); this->add_child(input); this->players.insert(this->next_player_id++, {input, nullptr}); - this->emit_signal(StringName("player_connected"), input); + this->emit_signal(gd::StringName("player_connected"), input); } void GameRoot3D::remove_player(uint32_t player_id) { if(!this->players.has(player_id)) return; // convert player object to node - Node *node = this->players.get(player_id).second->to_node(); + gd::Node *node = this->players.get(player_id).second->to_node(); if(node == nullptr) { - UtilityFunctions::push_error("IPlayer::to_node failed for player with id '", player_id, "'"); + gd::UtilityFunctions::push_error("IPlayer::to_node failed for player with id '", player_id, "'"); return; } node->queue_free(); @@ -71,7 +72,7 @@ void GameRoot3D::remove_player(uint32_t player_id) { void GameRoot3D::remove_all_players() { // free all player instances in use - for(KeyValue> &pair : this->players) { + for(gd::KeyValue> &pair : this->players) { // skip unused player slots if(pair.value.second == nullptr) continue; @@ -84,7 +85,7 @@ bool GameRoot3D::initialize_player(IPlayer *player, uint32_t id) { if(!this->players.has(id)) return false; // register the player - Pair &found{this->players.get(id)}; + gd::Pair &found{this->players.get(id)}; found.second = player; // set player id player->player_id = id; @@ -95,20 +96,20 @@ bool GameRoot3D::initialize_player(IPlayer *player, uint32_t id) { } void GameRoot3D::reset_game_mode() { - this->set_game_mode(Ref()); + this->set_game_mode(gd::Ref()); } -Level3D *GameRoot3D::load_level(Ref level) { - return this->load_level_at(level, Transform3D()); +Level3D *GameRoot3D::load_level(gd::Ref level) { + return this->load_level_at(level, gd::Transform3D()); } -Level3D *GameRoot3D::load_level_at(Ref level, Transform3D at) { +Level3D *GameRoot3D::load_level_at(gd::Ref level, gd::Transform3D at) { if(!GameRoot3D::is_valid_level(level)) { return nullptr; } Level3D *instance = Object::cast_to(level->instantiate()); if(instance == nullptr) { - UtilityFunctions::push_error("Unexpected failure to instantiate level scene '", level->get_path(), "'."); + gd::UtilityFunctions::push_error("Unexpected failure to instantiate level scene '", level->get_path(), "'."); return nullptr; } this->levels.insert(level->get_path(), instance); @@ -122,7 +123,7 @@ Level3D *GameRoot3D::load_level_at(Ref level, Transform3D at) { this->add_child(instance); instance->set_global_transform(at); if(switch_game_mode && this->game_mode.is_valid()) { - for(KeyValue> const &kvp : this->players) { + for(gd::KeyValue> const &kvp : this->players) { this->place_player_at_spawnpoint(kvp.value.second); } } @@ -130,21 +131,21 @@ Level3D *GameRoot3D::load_level_at(Ref level, Transform3D at) { } void GameRoot3D::unload_all_levels() { - HashMap levels = this->get_levels(); - for(KeyValue &kvp : levels) + gd::HashMap levels = this->get_levels(); + for(gd::KeyValue &kvp : levels) kvp.value->call_deferred("queue_free"); this->get_levels().clear(); this->reset_game_mode(); } -void GameRoot3D::replace_levels(Ref scene) { +void GameRoot3D::replace_levels(gd::Ref scene) { this->unload_all_levels(); this->load_level(scene); } void GameRoot3D::register_spawn_point(SpawnPoint3D *spawn_point) { if(this->spawn_points.has(spawn_point)) { - UtilityFunctions::push_error("Duplicate attempt to register spawnpoint '", spawn_point->get_path(), "'"); + gd::UtilityFunctions::push_error("Duplicate attempt to register spawnpoint '", spawn_point->get_path(), "'"); return; } if(!this->spawn_points.has(spawn_point)) @@ -153,7 +154,7 @@ void GameRoot3D::register_spawn_point(SpawnPoint3D *spawn_point) { void GameRoot3D::unregister_spawn_point(SpawnPoint3D *spawn_point) { if(!this->spawn_points.has(spawn_point)) { - UtilityFunctions::push_error("Attempt to unregister spawnpoint '", spawn_point->get_path(), "', which is not registered."); + gd::UtilityFunctions::push_error("Attempt to unregister spawnpoint '", spawn_point->get_path(), "', which is not registered."); return; } this->spawn_points.erase(spawn_point); @@ -166,12 +167,12 @@ void GameRoot3D::place_player_at_spawnpoint(IPlayer *player) { } void GameRoot3D::player_despawned(uint32_t id) { - Pair &pair = this->players.get(id); + gd::Pair &pair = this->players.get(id); pair.second = nullptr; pair.first->clear_listeners(); } -void GameRoot3D::set_game_mode(Ref prototype) { +void GameRoot3D::set_game_mode(gd::Ref prototype) { this->remove_all_players(); // allow "unsetting" the gamemode by passing an invalid gamemode // shorthand for this behaviour is reset_game_mode @@ -197,33 +198,33 @@ void GameRoot3D::set_game_mode(Ref prototype) { } } -Ref GameRoot3D::get_game_mode() const { +gd::Ref GameRoot3D::get_game_mode() const { return this->game_mode; } -Ref GameRoot3D::get_game_state() const { +gd::Ref GameRoot3D::get_game_state() const { return this->game_mode->get_game_state(); } -void GameRoot3D::set_first_boot_level(Ref level) { +void GameRoot3D::set_first_boot_level(gd::Ref level) { if(level.is_null() || !level.is_valid()) { this->first_boot_level.unref(); return; } - StringName const root_type = level->get_state()->get_node_type(0); - if(!ClassDB::is_parent_class(root_type, "Level3D")) { - UtilityFunctions::push_error("First boot level cannot be of type '", root_type, "'. First boot level has to inherit from Level3D"); + gd::StringName const root_type = level->get_state()->get_node_type(0); + if(!gd::ClassDB::is_parent_class(root_type, "Level3D")) { + gd::UtilityFunctions::push_error("First boot level cannot be of type '", root_type, "'. First boot level has to inherit from Level3D"); this->first_boot_level.unref(); return; } this->first_boot_level = level; } -Ref GameRoot3D::get_first_boot_level() const { +gd::Ref GameRoot3D::get_first_boot_level() const { return this->first_boot_level; } -HashMap &GameRoot3D::get_levels() { +gd::HashMap &GameRoot3D::get_levels() { return this->levels; } @@ -231,9 +232,9 @@ IPlayer *GameRoot3D::get_player(uint32_t id) { return this->players[id].second; } -Vector GameRoot3D::get_players() { - Vector players{}; - for(KeyValue> pair : this->players) { +gd::Vector GameRoot3D::get_players() { + gd::Vector players{}; + for(gd::KeyValue> pair : this->players) { players.push_back(pair.value.second); } return players; @@ -242,7 +243,7 @@ Vector GameRoot3D::get_players() { void GameRoot3D::grab_singleton() { if(GameRoot3D::has_singleton()) { this->set_process_mode(PROCESS_MODE_DISABLED); - UtilityFunctions::push_error("More than one GameRoot instance active"); + gd::UtilityFunctions::push_error("More than one GameRoot instance active"); } else { GameRoot3D::singleton_instance = this; } @@ -252,12 +253,12 @@ void GameRoot3D::release_singleton() { if(GameRoot3D::singleton_instance == this) { GameRoot3D::singleton_instance = nullptr; } else { - UtilityFunctions::push_error("GameRoot instance attempted to release singleton while it is not the singleton instance"); + gd::UtilityFunctions::push_error("GameRoot instance attempted to release singleton while it is not the singleton instance"); } } uint32_t GameRoot3D::find_empty_player_slot() const { - for(KeyValue> const &kvp : this->players) { + for(gd::KeyValue> const &kvp : this->players) { if(kvp.value.second == nullptr) { return kvp.key; } @@ -267,17 +268,17 @@ uint32_t GameRoot3D::find_empty_player_slot() const { IPlayer *GameRoot3D::spawn_player(uint32_t id) { if(id == 0) { - UtilityFunctions::push_error("Failed to find any valid player slot when spawning player"); + gd::UtilityFunctions::push_error("Failed to find any valid player slot when spawning player"); return nullptr; } Node *player_node = this->game_mode->get_player_scene()->instantiate(); if(player_node == nullptr) { - UtilityFunctions::push_error("Failed to instantiate player scene '", this->game_mode->get_player_scene()->get_path(), "'"); + gd::UtilityFunctions::push_error("Failed to instantiate player scene '", this->game_mode->get_player_scene()->get_path(), "'"); return nullptr; } IPlayer *player = dynamic_cast(player_node); if(player == nullptr) { - UtilityFunctions::push_error("Player scene does not implement required IPlayer interface"); + gd::UtilityFunctions::push_error("Player scene does not implement required IPlayer interface"); player_node->queue_free(); return nullptr; } @@ -285,18 +286,18 @@ IPlayer *GameRoot3D::spawn_player(uint32_t id) { return player; } -void GameRoot3D::level_unloaded(StringName scene_path) { +void GameRoot3D::level_unloaded(gd::StringName scene_path) { this->levels.erase(scene_path); } -bool GameRoot3D::is_valid_level(Ref &level) { +bool GameRoot3D::is_valid_level(gd::Ref &level) { if(level.is_null() || !level.is_valid() || !level->can_instantiate()) { - UtilityFunctions::push_error("Can't load level from invalid packed scene"); + gd::UtilityFunctions::push_error("Can't load level from invalid packed scene"); return false; } - StringName const root_type = level->get_state()->get_node_type(0); - if(!ClassDB::is_parent_class(root_type, "Level3D")) { - UtilityFunctions::push_error("Can't load level with root type '", root_type, "'. Root node has to be of type Level3D"); + gd::StringName const root_type = level->get_state()->get_node_type(0); + if(!gd::ClassDB::is_parent_class(root_type, "Level3D")) { + gd::UtilityFunctions::push_error("Can't load level with root type '", root_type, "'. Root node has to be of type Level3D"); return false; } return true; diff --git a/game_root.hpp b/game_root.hpp index 529397d..cb9f6a3 100644 --- a/game_root.hpp +++ b/game_root.hpp @@ -2,23 +2,25 @@ #define GAME_ROOT_HPP #include "game_mode.hpp" -#include "godot_cpp/classes/random_number_generator.hpp" #include "level.hpp" #include #include +#include #include #include #include #include #include -namespace godot { +namespace gd = godot; + +namespace utils { class PlayerInput; class IPlayer; class SpawnPoint3D; -class GameRoot3D : public Node { - GDCLASS(GameRoot3D, Node); +class GameRoot3D : public gd::Node { + GDCLASS(GameRoot3D, gd::Node); static void _bind_methods(); public: // get the current active singleton instance of GameRoot @@ -46,12 +48,12 @@ public: void reset_game_mode(); // shorthand for load_level(level, Transform3D()) - Level3D *load_level(Ref level); + Level3D *load_level(gd::Ref level); // load a level, only works if 'level' is a valid scene where the root Node can cast to 'Level3D' // sets the level's root node's global transform - Level3D *load_level_at(Ref level, Transform3D at); + Level3D *load_level_at(gd::Ref level, gd::Transform3D at); void unload_all_levels(); - void replace_levels(Ref level); + void replace_levels(gd::Ref level); // register a spawnpoint for use when spawning players void register_spawn_point(SpawnPoint3D *spawn_point); @@ -63,14 +65,14 @@ public: // ----- getter / setters ----- // override the current gamemode // force-respawns all players - void set_game_mode(Ref prototype); - Ref get_game_mode() const; - Ref get_game_state() const; - void set_first_boot_level(Ref level); - Ref get_first_boot_level() const; - HashMap &get_levels(); + void set_game_mode(gd::Ref prototype); + gd::Ref get_game_mode() const; + gd::Ref get_game_state() const; + void set_first_boot_level(gd::Ref level); + gd::Ref get_first_boot_level() const; + gd::HashMap &get_levels(); IPlayer *get_player(uint32_t id); - Vector get_players(); + gd::Vector get_players(); protected: // attempt to make 'this' the current singleton instance void grab_singleton(); @@ -79,20 +81,20 @@ protected: void release_singleton(); uint32_t find_empty_player_slot() const; IPlayer *spawn_player(uint32_t id); - void level_unloaded(StringName scene_path); - static bool is_valid_level(Ref &level); + void level_unloaded(gd::StringName scene_path); + static bool is_valid_level(gd::Ref &level); protected: static GameRoot3D *singleton_instance; uint32_t next_player_id{1}; // 0 is the "invalid" player id - HashMap> players{}; - Ref game_mode{}; + gd::HashMap> players{}; + gd::Ref game_mode{}; private: - RandomNumberGenerator rng{}; - HashMap levels{}; - Vector spawn_points{}; + gd::RandomNumberGenerator rng{}; + gd::HashMap levels{}; + gd::Vector spawn_points{}; - Ref first_boot_level{}; + gd::Ref first_boot_level{}; }; } diff --git a/game_state.cpp b/game_state.cpp index 1976867..342381f 100644 --- a/game_state.cpp +++ b/game_state.cpp @@ -1,6 +1,8 @@ #include "game_state.hpp" -namespace godot { +using namespace godot; + +namespace utils { void GameState::_bind_methods() { #define CLASSNAME GameState } diff --git a/game_state.hpp b/game_state.hpp index e3b5615..1947285 100644 --- a/game_state.hpp +++ b/game_state.hpp @@ -1,10 +1,11 @@ #ifndef GAME_STATE_HPP #define GAME_STATE_HPP -#include "godot_cpp/classes/resource.hpp" -namespace godot { -class GameState : public Resource { - GDCLASS(GameState, Resource); +#include + +namespace utils { +class GameState : public godot::Resource { + GDCLASS(GameState, godot::Resource); static void _bind_methods(); public: }; diff --git a/godot_macros.h b/godot_macros.h index 2983803..bd13dc5 100644 --- a/godot_macros.h +++ b/godot_macros.h @@ -4,37 +4,33 @@ #include "godot_cpp/classes/engine.hpp" #include "godot_cpp/core/class_db.hpp" + #define MACRO_STRING_INNER(_Arg) #_Arg #define MACRO_STRING(_Arg) MACRO_STRING_INNER(_Arg) -#define GDPROPERTY(_PropName, _PropType) \ - ClassDB::bind_method(D_METHOD("get_" #_PropName), &CLASSNAME::get_##_PropName); \ - ClassDB::bind_method(D_METHOD("set_" #_PropName, "value"), &CLASSNAME::set_##_PropName); \ - ClassDB::add_property(MACRO_STRING(CLASSNAME), PropertyInfo(_PropType, #_PropName), "set_" #_PropName, \ - "get_" #_PropName) +#define GDPROPERTY(_PropName, _PropType) \ + godot::ClassDB::bind_method(godot::D_METHOD("get_" #_PropName), &CLASSNAME::get_##_PropName); \ + godot::ClassDB::bind_method(godot::D_METHOD("set_" #_PropName, "value"), &CLASSNAME::set_##_PropName); \ + godot::ClassDB::add_property(MACRO_STRING(CLASSNAME), godot::PropertyInfo(_PropType, #_PropName), "set_" #_PropName, "get_" #_PropName) -#define GDPROPERTY_HINTED(_PropName, _PropType, ...) \ - ClassDB::bind_method(D_METHOD("get_" #_PropName), &CLASSNAME::get_##_PropName); \ - ClassDB::bind_method(D_METHOD("set_" #_PropName, "value"), &CLASSNAME::set_##_PropName); \ - ClassDB::add_property(MACRO_STRING(CLASSNAME), PropertyInfo(_PropType, #_PropName, __VA_ARGS__), \ - "set_" #_PropName, "get_" #_PropName) +#define GDPROPERTY_HINTED(_PropName, _PropType, ...) \ + godot::ClassDB::bind_method(godot::D_METHOD("get_" #_PropName), &CLASSNAME::get_##_PropName); \ + godot::ClassDB::bind_method(godot::D_METHOD("set_" #_PropName, "value"), &CLASSNAME::set_##_PropName); \ + godot::ClassDB::add_property(MACRO_STRING(CLASSNAME), godot::PropertyInfo(_PropType, #_PropName, __VA_ARGS__), "set_" #_PropName, "get_" #_PropName) -#define GDFUNCTION(_FnName) ClassDB::bind_method(D_METHOD(#_FnName), &CLASSNAME::_FnName) +#define GDFUNCTION(_FnName) godot::ClassDB::bind_method(godot::D_METHOD(#_FnName), &CLASSNAME::_FnName) -#define GDFUNCTION_ARGS(_FnName, ...) ClassDB::bind_method(D_METHOD(#_FnName, __VA_ARGS__), &CLASSNAME::_FnName) +#define GDFUNCTION_ARGS(_FnName, ...) godot::ClassDB::bind_method(godot::D_METHOD(#_FnName, __VA_ARGS__), &CLASSNAME::_FnName) -#define GDFUNCTION_STATIC(_FnName) \ - ClassDB::bind_static_method(MACRO_STRING(CLASSNAME), D_METHOD(#_FnName), &CLASSNAME::_FnName) +#define GDFUNCTION_STATIC(_FnName) godot::ClassDB::bind_static_method(MACRO_STRING(CLASSNAME), godot::D_METHOD(#_FnName), &CLASSNAME::_FnName) -#define GDFUNCTION_STATIC_ARGS(_FnName, ...) \ - ClassDB::bind_static_method(MACRO_STRING(CLASSNAME), D_METHOD(#_FnName, __VA_ARGS__), &CLASSNAME::_FnName) +#define GDFUNCTION_STATIC_ARGS(_FnName, ...) godot::ClassDB::bind_static_method(MACRO_STRING(CLASSNAME), godot::D_METHOD(#_FnName, __VA_ARGS__), &CLASSNAME::_FnName) -#define GDSIGNAL(...)\ - ClassDB::add_signal(MACRO_STRING(CLASSNAME), MethodInfo(__VA_ARGS__)) +#define GDSIGNAL(...) godot::ClassDB::add_signal(MACRO_STRING(CLASSNAME), godot::MethodInfo(__VA_ARGS__)) -#define GDRESOURCETYPE(_Class) vformat("%s/%s:%s", Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, #_Class) +#define GDRESOURCETYPE(_Class) godot::vformat("%s/%s:%s", godot::Variant::OBJECT, godot::PROPERTY_HINT_RESOURCE_TYPE, #_Class) -#define GDEDITORONLY() if(!Engine::get_singleton()->is_editor_hint()) return; -#define GDGAMEONLY() if(Engine::get_singleton()->is_editor_hint()) return; +#define GDEDITORONLY() if(!godot::Engine::get_singleton()->is_editor_hint()) return; +#define GDGAMEONLY() if(godot::Engine::get_singleton()->is_editor_hint()) return; #endif // !UC_GODOT_MACROS_H diff --git a/level.cpp b/level.cpp index 7b0e148..7c4f97f 100644 --- a/level.cpp +++ b/level.cpp @@ -1,33 +1,17 @@ #include "level.hpp" #include "utils/godot_macros.h" -namespace godot { +namespace utils { void Level3D::_bind_methods() { #define CLASSNAME Level3D - GDPROPERTY_HINTED(game_mode_prototype, Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, "GameMode"); + GDPROPERTY_HINTED(game_mode_prototype, gd::Variant::OBJECT, gd::PROPERTY_HINT_RESOURCE_TYPE, "GameMode"); } -void Level3D::set_game_mode_prototype(Ref prototype) { +void Level3D::set_game_mode_prototype(gd::Ref prototype) { this->game_mode_prototype = prototype; } -Ref Level3D::get_game_mode_prototype() const { +gd::Ref Level3D::get_game_mode_prototype() const { return this->game_mode_prototype; } - -#undef CLASSNAME // Level3D - -void Level2D::_bind_methods() { -#define CLASSNAME Level3D - GDPROPERTY_HINTED(game_mode_prototype, Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, "GameMode"); -} - -void Level2D::set_game_mode_prototype(Ref prototype) { - this->game_mode_prototype = prototype; -} - -Ref Level2D::get_game_mode_prototype() const { - return this->game_mode_prototype; -} - } diff --git a/level.hpp b/level.hpp index 0319a3c..4f614d1 100644 --- a/level.hpp +++ b/level.hpp @@ -1,32 +1,21 @@ #ifndef LEVEL_HPP #define LEVEL_HPP -#include -#include #include "game_mode.hpp" +#include -namespace godot { -class Level3D : public Node3D { - GDCLASS(Level3D, Node3D); +namespace gd = godot; + +namespace utils { +class Level3D : public gd::Node3D { + GDCLASS(Level3D, gd::Node3D); static void _bind_methods(); public: - void set_game_mode_prototype(Ref prototype); - Ref get_game_mode_prototype() const; + void set_game_mode_prototype(gd::Ref prototype); + gd::Ref get_game_mode_prototype() const; private: - Ref game_mode_prototype{}; + gd::Ref game_mode_prototype{}; }; - -class Level2D : public Node2D { - GDCLASS(Level2D, Node2D); - static void _bind_methods(); -public: - void set_game_mode_prototype(Ref prototype); - Ref get_game_mode_prototype() const; -private: - Ref game_mode_prototype{}; -}; - - } #endif // !LEVEL_HPP diff --git a/player.cpp b/player.cpp index 4594e8e..5af63ad 100644 --- a/player.cpp +++ b/player.cpp @@ -1,6 +1,6 @@ #include "player.hpp" -namespace godot { +namespace utils { uint32_t IPlayer::get_player_id() { return this->player_id.value_or(0); } diff --git a/player.hpp b/player.hpp index 3a3fe7f..df7c137 100644 --- a/player.hpp +++ b/player.hpp @@ -5,16 +5,19 @@ #include #include -namespace godot { +namespace godot { class Node; } + +namespace gd = godot; + +namespace utils { class PlayerInput; -class Node; class IPlayer { friend class GameRoot3D; public: virtual void setup_player_input(PlayerInput *input) = 0; - virtual Node *to_node() = 0; - virtual void spawn_at_position(Transform3D const &at) = 0; + virtual gd::Node *to_node() = 0; + virtual void spawn_at_position(gd::Transform3D const &at) = 0; uint32_t get_player_id(); diff --git a/player_input.cpp b/player_input.cpp index ec11200..bbf24eb 100644 --- a/player_input.cpp +++ b/player_input.cpp @@ -1,31 +1,32 @@ #include "player_input.hpp" #include "godot_macros.h" -#include "godot_cpp/classes/input.hpp" -#include "godot_cpp/classes/input_event.hpp" -#include "godot_cpp/classes/input_event_mouse_motion.hpp" -#include "godot_cpp/variant/callable.hpp" -#include +#include +#include +#include +#include #include -namespace godot { -void PlayerInput::_bind_methods() {} +namespace utils { +void PlayerInput::_bind_methods() { +#define CLASSNAME PlayerInput +} -Vector2 PlayerInput::lastMouseMotion{0.f, 0.f}; +gd::Vector2 PlayerInput::lastMouseMotion{0.f, 0.f}; bool PlayerInput::primaryExists{false}; -PlayerInput::Listener::Listener(String positive, String negative, Callable callable) +PlayerInput::Listener::Listener(gd::String positive, gd::String negative, gd::Callable callable) : actionNegative{negative} , actionPositive{positive} , callable{callable} , isMouseEvent{positive.begins_with("_mouse_") || negative.begins_with("_mouse_")} {} -PlayerInput::Listener::Listener(String action, Callable callable) -: PlayerInput::Listener::Listener(action, String(), callable) {} +PlayerInput::Listener::Listener(gd::String action, gd::Callable callable) +: PlayerInput::Listener::Listener(action, gd::String(), callable) {} -std::optional PlayerInput::Listener::evaluate_action(String const &action) { - Input *input = Input::get_singleton(); +std::optional PlayerInput::Listener::evaluate_action(gd::String const &action) { + gd::Input *input = gd::Input::get_singleton(); if(action.begins_with("_mouse_")) { - Vector2 vector = PlayerInput::get_last_mouse_motion(); + gd::Vector2 vector = PlayerInput::get_last_mouse_motion(); if(action.ends_with("_up")) return vector.y > 0.f ? vector.y : 0.f; else if(action.ends_with("_down")) @@ -42,14 +43,14 @@ std::optional PlayerInput::Listener::evaluate_action(String const &action } } -bool PlayerInput::Listener::has_changed(Ref const &event) { +bool PlayerInput::Listener::has_changed(gd::Ref const &event) { bool const mouse_changed{this->isMouseEvent && event->is_class("InputEventMouseMotion")}; bool const negative_changed{!this->actionNegative.is_empty() && event->is_action(this->actionNegative)}; bool const positive_changed{!this->actionPositive.is_empty() && event->is_action(this->actionPositive)}; return mouse_changed || negative_changed || positive_changed; } -float PlayerInput::Listener::evaluate(Ref const &event) { +float PlayerInput::Listener::evaluate(gd::Ref const &event) { std::optional positive = PlayerInput::Listener::evaluate_action(this->actionPositive); std::optional negative = PlayerInput::Listener::evaluate_action(this->actionNegative); if(!positive.has_value() || !negative.has_value()) @@ -60,13 +61,13 @@ float PlayerInput::Listener::evaluate(Ref const &event) { return (this->lastCached = newest); } -bool PlayerInput::Listener::operator==(godot::PlayerInput::Listener const& b) { +bool PlayerInput::Listener::operator==(PlayerInput::Listener const& b) const { return this->callable == b.callable && this->actionNegative == b.actionNegative && this->actionPositive == b.actionPositive; } -Vector2 PlayerInput::get_last_mouse_motion() { +gd::Vector2 PlayerInput::get_last_mouse_motion() { return PlayerInput::lastMouseMotion; } @@ -86,10 +87,10 @@ void PlayerInput::_exit_tree() { } } -void PlayerInput::_unhandled_input(Ref const &event) { +void PlayerInput::_unhandled_input(gd::Ref const &event) { GDGAMEONLY(); if(this->isPrimary && event->is_class("InputEventMouseMotion")) - PlayerInput::lastMouseMotion = Object::cast_to(*event)->get_relative(); + PlayerInput::lastMouseMotion = gd::Object::cast_to(*event)->get_relative(); for(Listener& listener: this->listeners) { if(listener.has_changed(event)) { listener.evaluate(event); @@ -108,18 +109,16 @@ void PlayerInput::listen_to(Listener const& listener) { void PlayerInput::stop_listening(Node *node) { for(size_t i = 0; i < this->listeners.size(); ++i) { - Listener& l = this->listeners.at(i); + Listener l = this->listeners.get(i); if(l.callable.get_object() == node) { - this->listeners.erase(this->listeners.begin() + i); + this->listeners.remove_at(i); i--; } } } void PlayerInput::stop_listening(Listener const& listener) { - std::vector::iterator itr = std::find(this->listeners.begin(), this->listeners.end(), listener); - if(itr != this->listeners.end()) - this->listeners.erase(itr); + this->listeners.erase(listener); } void PlayerInput::clear_listeners() { diff --git a/player_input.hpp b/player_input.hpp index 68978f6..dd50adc 100644 --- a/player_input.hpp +++ b/player_input.hpp @@ -1,55 +1,60 @@ #ifndef PLAYER_INPUT_HPP #define PLAYER_INPUT_HPP -#include #include -#include "godot_cpp/classes/input.hpp" -#include "godot_cpp/classes/input_event.hpp" -#include "godot_cpp/classes/node.hpp" -#include "godot_cpp/variant/callable.hpp" +#include +#include +#include +#include +#include -namespace godot { -class PlayerInput : public Node { - GDCLASS(PlayerInput, Node) +namespace gd = godot; + +namespace utils { +/*! An event-driven input observer. + * + * Listen for events with `listen_to`, registering godot input action names to callbacks. It's possible to register an "axis" by registering a listener with a positive and negative action. + */ +class PlayerInput : public gd::Node { + GDCLASS(PlayerInput, gd::Node) static void _bind_methods(); public: - // a listener is a combination of a positive and negative action and a listener function. - // listener functions use godot's Object::call function. - // So they require a Node instance and a function name. - // The expected signature is void(Ref, float) - // actions can also be "special" actions prefixed with _. - // special actions include _mouse_up, _mouse_down, _mouse_left and _mouse_right - // which rather than checking action_is_down, - // will use PlayerInput::get_last_mouse_motion() to poll the current state. + /*! A PlayerInput action listener. + * A listener is a combination of a positive and negative action and a callable. + * The expected callable signature is `void (godot::Ref event, float value)` + * actions can also be "special" actions prefixed with `_`. + * Special actions include `_mouse_up`, `_mouse_down`, `_mouse_left`, and `_mouse_right`. + */ struct Listener { friend class PlayerInput; private: // the two actions, evaluated as positive - negative - String actionNegative{""}; - String actionPositive{""}; + gd::String actionNegative{""}; + gd::String actionPositive{""}; // the last cached action, if the newest result matches this, the event will be considered // duplicate and ignored (not passed to listener) float lastCached{0.f}; - Callable callable; + gd::Callable callable; // if either actionNegative or actionPositive is a _mouse_ event this will be true bool isMouseEvent{false}; public: - Listener(String positive, String negative, Callable callable); - Listener(String action, Callable callable); + Listener() = default; + Listener(gd::String positive, gd::String negative, gd::Callable callable); + Listener(gd::String action, gd::Callable callable); // evaluate the current state of an action. - static std::optional evaluate_action(String const &action); + static std::optional evaluate_action(gd::String const &action); // check if this event has any chance to result in a trigger, does not evaluate the event or // poll current input state - bool has_changed(Ref const &event); + bool has_changed(gd::Ref const &event); // evaluate the event for changes to either actionPositive or actionNegative - float evaluate(Ref const &event); + float evaluate(gd::Ref const &event); - bool operator==(godot::PlayerInput::Listener const& b); + bool operator==(PlayerInput::Listener const& b) const; }; private: // the last mouse motion, updated by the primary instance - static Vector2 lastMouseMotion; + static gd::Vector2 lastMouseMotion; // does a primary instance exist static bool primaryExists; // is this the primary instance @@ -58,18 +63,18 @@ private: bool isPrimary{false}; // current listeners for this instance - std::vector listeners{}; + gd::Vector listeners{}; public: - static Vector2 get_last_mouse_motion(); + static gd::Vector2 get_last_mouse_motion(); virtual void _enter_tree() override; virtual void _exit_tree() override; - virtual void _unhandled_input(Ref const &event) override; + virtual void _unhandled_input(gd::Ref const &event) override; virtual void _process(double deltaTime) override; - void listen_to(Listener const& listener); + void listen_to(Listener const &listener); void stop_listening(Node *node); - void stop_listening(Listener const& listener); + void stop_listening(Listener const &listener); void clear_listeners(); }; } diff --git a/register_types.cpp b/register_types.cpp index 85b5f56..023b00c 100644 --- a/register_types.cpp +++ b/register_types.cpp @@ -1,17 +1,21 @@ #include "register_types.hpp" -#include -#include "game_root.hpp" #include "game_mode.hpp" +#include "game_root.hpp" #include "game_state.hpp" #include "level.hpp" +#include "player_input.hpp" +#include "spawn_point.hpp" +#include -using namespace godot; +namespace gd = godot; +namespace utils { void godot_cpp_utils_register_types() { - ClassDB::register_class(); - ClassDB::register_class(); - ClassDB::register_class(); - ClassDB::register_class(); - ClassDB::register_class(); - ClassDB::register_class(); + gd::ClassDB::register_class(); + gd::ClassDB::register_class(); + gd::ClassDB::register_class(); + gd::ClassDB::register_class(); + gd::ClassDB::register_class(); + gd::ClassDB::register_class(); +} } diff --git a/register_types.hpp b/register_types.hpp index 11841ff..e11b0aa 100644 --- a/register_types.hpp +++ b/register_types.hpp @@ -1,6 +1,8 @@ #ifndef UTILS_REGISTER_TYPES_HPP #define UTILS_REGISTER_TYPES_HPP +namespace utils { void godot_cpp_utils_register_types(); +} #endif // !UTILS_REGISTER_TYPES_HPP diff --git a/spawn_point.cpp b/spawn_point.cpp index 22badca..e95aad8 100644 --- a/spawn_point.cpp +++ b/spawn_point.cpp @@ -1,12 +1,12 @@ #include "spawn_point.hpp" #include "utils/game_root.hpp" -namespace godot { +namespace utils { void SpawnPoint3D::_bind_methods() { } void SpawnPoint3D::_enter_tree() { - GameRoot3D *root = Object::cast_to(GameRoot3D::get_singleton()); + GameRoot3D *root = gd::Object::cast_to(GameRoot3D::get_singleton()); if(root == nullptr) { return; } diff --git a/spawn_point.hpp b/spawn_point.hpp index e0649ce..e2667c9 100644 --- a/spawn_point.hpp +++ b/spawn_point.hpp @@ -1,10 +1,13 @@ #ifndef UTILS_SPAWN_POINT_HPP #define UTILS_SPAWN_POINT_HPP -#include "godot_cpp/classes/node3d.hpp" -namespace godot { -class SpawnPoint3D : public Node3D { - GDCLASS(SpawnPoint3D, Node3D); +#include + +namespace gd = godot; + +namespace utils { +class SpawnPoint3D : public gd::Node3D { + GDCLASS(SpawnPoint3D, gd::Node3D); static void _bind_methods(); public: virtual void _enter_tree() override;