aboutsummaryrefslogtreecommitdiff
path: root/extension/src/openvic-extension/singletons/MapItemSingleton.cpp
blob: d62e0fd8a1b746e90c28867b8277b0402c6af74b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160


//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/types/Vector.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);
   OV_BIND_METHOD(MapItemSingleton::get_province_count);
}

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;
}

int MapItemSingleton::get_province_count(){
   GameSingleton const* game_singleton = GameSingleton::get_singleton();
   ERR_FAIL_NULL_V(game_singleton, 0);
   return game_singleton->get_definition_manager().get_map_definition().get_province_definition_count();
}

PackedVector2Array MapItemSingleton::get_province_positions(){
   GameSingleton const* game_singleton = GameSingleton::get_singleton();
   ERR_FAIL_NULL_V(game_singleton, PackedVector2Array());

   /*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();
   Logger::info("prov_count ", prov_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+1);

      fvec2_t city_pos = prov->get_city_position();
      Vector2 pos = Utilities::to_godot_fvec2(city_pos) / game_singleton->get_map_dims();
      billboard_pos.push_back(pos);
      /*if(i % 4){
         Logger::info("prov_x ", prov->get_city_position().x);
      }*/

      //province_dict[position_key] = Utilities::to_godot_fvec2(province.get_text_position()) / get_map_dims();
   }

   return billboard_pos;
}