diff options
author | Nemrav <> | 2024-11-17 22:51:35 +0100 |
---|---|---|
committer | Nemrav <> | 2024-11-17 22:51:35 +0100 |
commit | 0d1f42829b154c172608450e3689c56cbfacb4fd (patch) | |
tree | f5aa2633ea252ba2e602c88fea5d9b77a1b83283 | |
parent | 54ddd94739d7efa3b7539d638edaf89b023ef903 (diff) |
billboards placement
5 files changed, 47 insertions, 16 deletions
diff --git a/extension/src/openvic-extension/singletons/MapItemSingleton.cpp b/extension/src/openvic-extension/singletons/MapItemSingleton.cpp index 67af22e..d62e0fd 100644 --- a/extension/src/openvic-extension/singletons/MapItemSingleton.cpp +++ b/extension/src/openvic-extension/singletons/MapItemSingleton.cpp @@ -18,6 +18,7 @@ #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; @@ -32,7 +33,7 @@ void MapItemSingleton::_bind_methods() { //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() { @@ -121,9 +122,15 @@ TypedArray<Dictionary> MapItemSingleton::get_billboards(){ 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, {}); + ERR_FAIL_NULL_V(game_singleton, PackedVector2Array()); /*std::vector<std::string_view> prov_identifiers = game_singleton->get_definition_manager().get_map_definition(). @@ -136,11 +143,17 @@ PackedVector2Array MapItemSingleton::get_province_positions(){ 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); - billboard_pos.push_back(Vector2(prov->get_city_position().x,prov->get_city_position().y)); - if(i % 4){ + 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; diff --git a/extension/src/openvic-extension/singletons/MapItemSingleton.hpp b/extension/src/openvic-extension/singletons/MapItemSingleton.hpp index 5b10446..fd89119 100644 --- a/extension/src/openvic-extension/singletons/MapItemSingleton.hpp +++ b/extension/src/openvic-extension/singletons/MapItemSingleton.hpp @@ -28,6 +28,7 @@ namespace OpenVic { 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(); + int get_province_count(); godot::PackedVector2Array get_province_positions(); }; diff --git a/game/src/Game/GameSession/BillboardManager.gd b/game/src/Game/GameSession/BillboardManager.gd index 5a6fea6..ac1610b 100644 --- a/game/src/Game/GameSession/BillboardManager.gd +++ b/game/src/Game/GameSession/BillboardManager.gd @@ -4,8 +4,10 @@ extends MultiMeshInstance3D #var billboard_names = { #} -var billboard_names = [] +@export var _map_view : MapView +var billboard_names = [] +const SCALE_FACTOR = 1.0/64.0 # Called when the node enters the scene tree for the first time. func _ready(): const name_key : StringName = &"name" @@ -26,7 +28,7 @@ func _ready(): var noFrames = billboard[noOfFrames_key] textures.push_back(AssetManager.get_texture(texture)) frames.push_back(noFrames) - scales.push_back(b_scale) + scales.push_back(b_scale*SCALE_FACTOR) billboard_names.push_back(b_name) var material:ShaderMaterial = load("res://src/Game/GameSession/billboards.tres") @@ -50,18 +52,30 @@ func _ready(): #GameSingleton.get_mapmode_identifier(0) #GameSingleton.get_mapmode_count() - var positions: PackedVector2Array = MapItemSingleton.get_province_positions() + var positions_len = MapItemSingleton.get_province_count() + + print(positions_len) # Then resize (otherwise, changing the format is not allowed). - #multimesh.instance_count = positions.size() - #multimesh.visible_instance_count = positions.size() + multimesh.instance_count = positions_len + multimesh.visible_instance_count = positions_len print("===============") + var positions = MapItemSingleton.get_province_positions() + print("===============") + + var map_positions : PackedVector3Array = PackedVector3Array() + for pos_in in positions: + var pos = _map_view._map_to_world_coords(pos_in)# + Vector3(0, 0.5 * SCALE_FACTOR, 0) + map_positions.push_back(pos) #print(positions) #print(positions.size()) # Set the transform of the instances. - """for i in multimesh.visible_instance_count: + #model.set_position( + #_map_view._map_to_world_coords(unit_dict[position_key]) + # + Vector3(0, 0.1 * MODEL_SCALE, 0)) + for i in multimesh.visible_instance_count: multimesh.set_instance_transform(i, Transform3D(Basis(), - Vector3(positions[i].x, 0.5, positions[i].y) + map_positions[i] )) # custom data is a single vec4 float sent as a color # Info send to the shader is as follows: @@ -73,7 +87,7 @@ func _ready(): var im = r(textures.size()-1) multimesh.set_instance_custom_data(i,Color( im,r(frames[im]),1.0,0 - ))""" + )) func r(x:int=1)->int: return randi_range(0,x) diff --git a/game/src/Game/GameSession/GameSession.tscn b/game/src/Game/GameSession/GameSession.tscn index e8354a5..9356202 100644 --- a/game/src/Game/GameSession/GameSession.tscn +++ b/game/src/Game/GameSession/GameSession.tscn @@ -41,8 +41,9 @@ _game_session_menu = NodePath("UICanvasLayer/UI/GameSessionMenu") script = ExtResource("3_qwk4j") _map_view = NodePath("../MapView") -[node name="BillboardManager" type="MultiMeshInstance3D" parent="."] +[node name="BillboardManager" type="MultiMeshInstance3D" parent="." node_paths=PackedStringArray("_map_view")] script = ExtResource("4_b3l7b") +_map_view = NodePath("../MapView") [node name="UICanvasLayer" type="CanvasLayer" parent="."] diff --git a/game/src/Game/GameSession/billboard.gdshader b/game/src/Game/GameSession/billboard.gdshader index e207ff9..e854bcf 100644 --- a/game/src/Game/GameSession/billboard.gdshader +++ b/game/src/Game/GameSession/billboard.gdshader @@ -5,6 +5,8 @@ uniform sampler2D billboards[255]; uniform int numframes[255]; uniform float sizes[255]; +uniform float height_add_factor = 0.3; + //COLOR/INSTANCE_CUSTOM is our custom data, used as follows: // x=image index // y=frame in image index @@ -19,7 +21,7 @@ void vertex() { vec3 vert_pos_world = cam_right_worldspace * VERTEX.x * size + cam_up_worldspace * VERTEX.y * size; - VERTEX = vert_pos_world; + VERTEX = vert_pos_world + vec3(0,height_add_factor*size,0); } void fragment() { |