diff options
Diffstat (limited to 'extension')
3 files changed, 186 insertions, 0 deletions
diff --git a/extension/src/openvic-extension/register_types.cpp b/extension/src/openvic-extension/register_types.cpp index 7927b22..01ebb23 100644 --- a/extension/src/openvic-extension/register_types.cpp +++ b/extension/src/openvic-extension/register_types.cpp @@ -24,16 +24,19 @@ #include "openvic-extension/singletons/Checksum.hpp" #include "openvic-extension/singletons/GameSingleton.hpp" #include "openvic-extension/singletons/LoadLocalisation.hpp" +#include "openvic-extension/singletons/MapItemSingleton.hpp" #include "openvic-extension/singletons/MenuSingleton.hpp" #include "openvic-extension/singletons/ModelSingleton.hpp" #include "openvic-extension/singletons/SoundSingleton.hpp" + using namespace godot; using namespace OpenVic; static Checksum* _checksum_singleton = nullptr; static LoadLocalisation* _load_localisation = nullptr; static GameSingleton* _game_singleton = nullptr; +static MapItemSingleton* _map_item_singleton = nullptr; static MenuSingleton* _menu_singleton = nullptr; static ModelSingleton* _model_singleton = nullptr; static AssetManager* _asset_manager_singleton = nullptr; @@ -56,6 +59,10 @@ void initialize_openvic_types(ModuleInitializationLevel p_level) { _game_singleton = memnew(GameSingleton); Engine::get_singleton()->register_singleton("GameSingleton", GameSingleton::get_singleton()); + ClassDB::register_class<MapItemSingleton>(); + _map_item_singleton = memnew(MapItemSingleton); + Engine::get_singleton()->register_singleton("MapItemSingleton", MapItemSingleton::get_singleton()); + ClassDB::register_class<MenuSingleton>(); _menu_singleton = memnew(MenuSingleton); Engine::get_singleton()->register_singleton("MenuSingleton", MenuSingleton::get_singleton()); @@ -117,6 +124,9 @@ void uninitialize_openvic_types(ModuleInitializationLevel p_level) { Engine::get_singleton()->unregister_singleton("GameSingleton"); memdelete(_game_singleton); + Engine::get_singleton()->unregister_singleton("MapItemSingleton"); + memdelete(_map_item_singleton); + Engine::get_singleton()->unregister_singleton("MenuSingleton"); memdelete(_menu_singleton); diff --git a/extension/src/openvic-extension/singletons/MapItemSingleton.cpp b/extension/src/openvic-extension/singletons/MapItemSingleton.cpp new file mode 100644 index 0000000..57b5ccb --- /dev/null +++ b/extension/src/openvic-extension/singletons/MapItemSingleton.cpp @@ -0,0 +1,142 @@ + + +//billboards, projections, and progress bar + +#include "MapItemSingleton.hpp" +#include <string_view> + +//#include <numbers> + +#include <godot_cpp/variant/utility_functions.hpp> + +#include <openvic-simulation/map/ProvinceInstance.hpp> + +#include "openvic-extension/singletons/GameSingleton.hpp" +#include "openvic-extension/utility/ClassBindings.hpp" +#include "openvic-extension/utility/Utilities.hpp" +#include "godot_cpp/variant/packed_vector2_array.hpp" +#include "godot_cpp/variant/typed_array.hpp" +#include "godot_cpp/variant/vector2.hpp" +#include "openvic-simulation/interface/GFXObject.hpp" +#include "openvic-simulation/utility/Logger.hpp" + +using namespace godot; +using namespace OpenVic; + +void MapItemSingleton::_bind_methods() { + /*OV_BIND_METHOD(MapItemSingleton::get_units); + OV_BIND_METHOD(MapItemSingleton::get_cultural_gun_model, { "culture" }); + OV_BIND_METHOD(MapItemSingleton::get_cultural_helmet_model, { "culture" }); + OV_BIND_METHOD(MapItemSingleton::get_flag_model, { "floating" }); + OV_BIND_METHOD(MapItemSingleton::get_buildings);*/ + //OV_BIND_METHOD(MapItemSingleton::get_billboard, {"name"}); + OV_BIND_METHOD(MapItemSingleton::get_billboards); + OV_BIND_METHOD(MapItemSingleton::get_province_positions); + +} + +MapItemSingleton* MapItemSingleton::get_singleton() { + return singleton; +} + +MapItemSingleton::MapItemSingleton() { + ERR_FAIL_COND(singleton != nullptr); + singleton = this; +} + +MapItemSingleton::~MapItemSingleton() { + ERR_FAIL_COND(singleton != this); + singleton = nullptr; +} + +// Get the billboard object from the loaded objects + +GFX::Billboard const* MapItemSingleton::get_billboard(std::string_view name, bool error_on_fail) const { + GameSingleton const* game_singleton = GameSingleton::get_singleton(); + ERR_FAIL_NULL_V(game_singleton, nullptr); + + GFX::Billboard const* billboard = + game_singleton->get_definition_manager().get_ui_manager().get_cast_object_by_identifier<GFX::Billboard>(name); + + if (error_on_fail) { + //ERR_FAIL_NULL_V_MSG(billboard, nullptr, vformat("Failed to find billboard \"%s\"", Utilities::std_to_godot_string(name))); + ERR_FAIL_NULL_V_MSG(billboard, nullptr, "Failed to find billboard inner"); + } + + return billboard; +} + +// repackage the billboard object into a godot dictionnary for the Billboard manager to work with +bool MapItemSingleton::add_billboard_dict(std::string_view name, TypedArray<Dictionary>& billboard_dict_array) { + + static const StringName name_key = "name"; + static const StringName texture_key = "texture"; + static const StringName scale_key = "scale"; + static const StringName noOfFrames_key = "noFrames"; + //TODO: might need font, font_size, and offset keys in the future + + + GFX::Billboard const* billboard = get_billboard(name,false); + /*ERR_FAIL_NULL_V_MSG( + billboard, false, vformat( + "Failed to find \"%s\" billboard", Utilities::std_to_godot_string(name) + ) + );*/ + ERR_FAIL_NULL_V_MSG( + billboard, false, "Failed to find billboard" + ); + + Dictionary dict; + + dict[name_key] = Utilities::std_to_godot_string(billboard->get_name()); + dict[texture_key] = Utilities::std_to_godot_string(billboard->get_texture_file()); + dict[scale_key] = billboard->get_scale().to_float(); + dict[noOfFrames_key] = billboard->get_no_of_frames(); + + billboard_dict_array.push_back(dict); + + return true; +} + + +//get an array of all the billboard dictionnaries +TypedArray<Dictionary> MapItemSingleton::get_billboards(){ + GameSingleton const* game_singleton = GameSingleton::get_singleton(); + ERR_FAIL_NULL_V(game_singleton, {}); + //InstanceManager const* instance_manager = game_singleton->get_instance_manager(); + //ERR_FAIL_NULL_V(instance_manager, {}); + + TypedArray<Dictionary> ret; + + std::vector<std::string_view> identifiers = + game_singleton->get_definition_manager().get_ui_manager().get_object_identifiers(); + + for(auto i : identifiers){ + GFX::Object const* obj = game_singleton->get_definition_manager().get_ui_manager().get_object_by_identifier(i); + if(obj->is_type<GFX::Billboard>()){ + add_billboard_dict(obj->get_name(),ret); + } + } + + return ret; +} + +PackedVector2Array MapItemSingleton::get_province_positions(){ + GameSingleton const* game_singleton = GameSingleton::get_singleton(); + ERR_FAIL_NULL_V(game_singleton, {}); + + /*std::vector<std::string_view> prov_identifiers = + game_singleton->get_definition_manager().get_map_definition(). + get_province_definition_identifiers(); + */ + + PackedVector2Array billboard_pos = PackedVector2Array(); + + int prov_count = game_singleton->get_definition_manager().get_map_definition().get_province_definition_count(); + for(int i = 0; i < prov_count; i++){ + ProvinceDefinition const* prov = game_singleton->get_definition_manager().get_map_definition().get_province_definition_by_index(i); + billboard_pos.push_back(Vector2(prov->get_city_position().x,prov->get_city_position().y)); + } + + return billboard_pos; +}
\ No newline at end of file diff --git a/extension/src/openvic-extension/singletons/MapItemSingleton.hpp b/extension/src/openvic-extension/singletons/MapItemSingleton.hpp new file mode 100644 index 0000000..5b10446 --- /dev/null +++ b/extension/src/openvic-extension/singletons/MapItemSingleton.hpp @@ -0,0 +1,34 @@ +#pragma once + +#include <godot_cpp/classes/object.hpp> + +#include <openvic-simulation/interface/GFXObject.hpp> +//#include <openvic-simulation/military/UnitInstanceGroup.hpp> +#include <openvic-simulation/types/OrderedContainers.hpp> + +//billboards, projections, and progress bar + + +namespace OpenVic { + class MapItemSingleton : public godot::Object { + GDCLASS(MapItemSingleton, godot::Object) + + static inline MapItemSingleton* singleton = nullptr; + + protected: + static void _bind_methods(); + + public: + static MapItemSingleton* get_singleton(); + + MapItemSingleton(); + ~MapItemSingleton(); + + private: + GFX::Billboard const* get_billboard(std::string_view name, bool error_on_fail = true) const; + bool add_billboard_dict(std::string_view name, godot::TypedArray<godot::Dictionary>& billboard_dict_array); + godot::TypedArray<godot::Dictionary> get_billboards(); + godot::PackedVector2Array get_province_positions(); + }; + +}
\ No newline at end of file |