From 0fb9d5c8fb4771ef8001efdd8de1dc2dab4bb5dc Mon Sep 17 00:00:00 2001 From: hop311 Date: Wed, 24 Apr 2024 19:24:56 +0100 Subject: Add province names and improve zooming --- extension/deps/openvic-simulation | 2 +- .../openvic-extension/singletons/GameSingleton.cpp | 35 +++++++++++++++ .../openvic-extension/singletons/GameSingleton.hpp | 2 + game/src/Game/GameSession/MapText.gd | 50 ++++++++++++++++++++++ game/src/Game/GameSession/MapView.gd | 40 +++++++++++++++-- game/src/Game/GameSession/MapView.tscn | 12 +++++- 6 files changed, 134 insertions(+), 7 deletions(-) create mode 100644 game/src/Game/GameSession/MapText.gd diff --git a/extension/deps/openvic-simulation b/extension/deps/openvic-simulation index d0f8ec5..e286cfe 160000 --- a/extension/deps/openvic-simulation +++ b/extension/deps/openvic-simulation @@ -1 +1 @@ -Subproject commit d0f8ec5484a0ea49d778c0ebb6c2ba2e6df9b7d1 +Subproject commit e286cfef29d7c431ba33cd77283e838e6fba05d2 diff --git a/extension/src/openvic-extension/singletons/GameSingleton.cpp b/extension/src/openvic-extension/singletons/GameSingleton.cpp index 8618276..838542d 100644 --- a/extension/src/openvic-extension/singletons/GameSingleton.cpp +++ b/extension/src/openvic-extension/singletons/GameSingleton.cpp @@ -60,6 +60,8 @@ void GameSingleton::_bind_methods() { OV_BIND_METHOD(GameSingleton::get_province_shape_texture); OV_BIND_METHOD(GameSingleton::get_province_colour_texture); + OV_BIND_METHOD(GameSingleton::get_province_names); + OV_BIND_METHOD(GameSingleton::get_mapmode_count); OV_BIND_METHOD(GameSingleton::get_mapmode_identifier); OV_BIND_METHOD(GameSingleton::set_mapmode, { "identifier" }); @@ -243,6 +245,39 @@ Error GameSingleton::_update_colour_image() { return err; } +TypedArray GameSingleton::get_province_names() const { + static const StringName identifier_key = "identifier"; + static const StringName position_key = "position"; + static const StringName rotation_key = "rotation"; + static const StringName scale_key = "scale"; + + TypedArray ret; + ERR_FAIL_COND_V(ret.resize(game_manager.get_map().get_province_count()) != OK, {}); + + for (int32_t index = 0; index < game_manager.get_map().get_province_count(); ++index) { + Province const& province = game_manager.get_map().get_provinces()[index]; + + Dictionary province_dict; + + province_dict[identifier_key] = std_view_to_godot_string(province.get_identifier()); + province_dict[position_key] = Utilities::to_godot_fvec2(province.get_text_position()) / get_map_dims(); + + const float rotation = province.get_text_rotation().to_float(); + if (rotation != 0.0f) { + province_dict[rotation_key] = rotation; + } + + const float scale = province.get_text_scale().to_float(); + if (scale != 1.0f) { + province_dict[scale_key] = scale; + } + + ret[index] = std::move(province_dict); + } + + return ret; +} + int32_t GameSingleton::get_mapmode_count() const { return game_manager.get_map().get_mapmode_count(); } diff --git a/extension/src/openvic-extension/singletons/GameSingleton.hpp b/extension/src/openvic-extension/singletons/GameSingleton.hpp index 397b64c..f2b88ac 100644 --- a/extension/src/openvic-extension/singletons/GameSingleton.hpp +++ b/extension/src/openvic-extension/singletons/GameSingleton.hpp @@ -95,6 +95,8 @@ namespace OpenVic { /* The base and stripe colours for each province. */ godot::Ref get_province_colour_texture() const; + godot::TypedArray get_province_names() const; + int32_t get_mapmode_count() const; godot::String get_mapmode_identifier(int32_t index) const; godot::Error set_mapmode(godot::String const& identifier); diff --git a/game/src/Game/GameSession/MapText.gd b/game/src/Game/GameSession/MapText.gd new file mode 100644 index 0000000..22eba10 --- /dev/null +++ b/game/src/Game/GameSession/MapText.gd @@ -0,0 +1,50 @@ +class_name MapText +extends Node3D + +@export var _map_view : MapView + +var _province_name_font : Font + +const _province_name_scale : float = 1.0 / 48.0 + +func _ready() -> void: + _province_name_font = AssetManager.get_font(&"mapfont_56") + +func _clear_children() -> void: + var child_count : int = get_child_count() + while child_count > 0: + child_count -= 1 + remove_child(get_child(child_count)) + +func generate_map_names() -> void: + _clear_children() + + for dict : Dictionary in GameSingleton.get_province_names(): + _add_province_name(dict) + +func _add_province_name(dict : Dictionary) -> void: + const identifier_key : StringName = &"identifier" + const position_key : StringName = &"position" + const rotation_key : StringName = &"rotation" + const scale_key : StringName = &"scale" + + var label : Label3D = Label3D.new() + + label.set_draw_flag(Label3D.FLAG_DOUBLE_SIDED, false) + label.set_modulate(Color.BLACK) + label.set_outline_size(0) + label.set_font(_province_name_font) + label.set_vertical_alignment(VERTICAL_ALIGNMENT_BOTTOM) + + var identifier : String = dict[identifier_key] + label.set_name(identifier) + label.set_text(GUINode.format_province_name(identifier)) + + label.set_position(_map_view._map_to_world_coords(dict[position_key]) + Vector3(0, 0.001, 0)) + + label.rotate_x(-PI / 2) + label.rotate_y(dict.get(rotation_key, 0.0)) + + label.scale *= dict.get(scale_key, 1.0) * _province_name_scale + + add_child(label) diff --git a/game/src/Game/GameSession/MapView.gd b/game/src/Game/GameSession/MapView.gd index 810f6e5..2ab7c34 100644 --- a/game/src/Game/GameSession/MapView.gd +++ b/game/src/Game/GameSession/MapView.gd @@ -2,6 +2,8 @@ class_name MapView extends Node3D signal map_view_camera_changed(near_left : Vector2, far_left : Vector2, far_right : Vector2, near_right : Vector2) +signal parchment_view_changed(is_parchment_view : bool) +signal detailed_view_changed(is_detailed_view : bool) const _action_north : StringName = &"map_north" const _action_east : StringName = &"map_east" @@ -25,7 +27,7 @@ var _window_in_focus : bool = true @export var _zoom_target_min : float = 0.10 @export var _zoom_target_max : float = 5.0 -@export var _zoom_target_step : float = (_zoom_target_max - _zoom_target_min) / 64.0 +@export var _zoom_target_step : float = (_zoom_target_max - _zoom_target_min) / 40.0 @export var _zoom_epsilon : float = _zoom_target_step * 0.005 @export var _zoom_speed : float = 5.0 # _zoom_target's starting value is ignored as it is updated to the camera's height by _ready, @@ -36,8 +38,13 @@ var _zoom_target : float = _zoom_target_max: const _zoom_position_multiplier = 3.14159 # Horizontal movement coefficient during zoom var _zoom_position : Vector2 -# Display the detailed terrain map below this height, and the parchment map above it +# Display the parchment map above this height @export var _zoom_parchment_threshold : float = _zoom_target_min + (_zoom_target_max - _zoom_target_min) / 4 +# Display details like models and province names below this height +@export var _zoom_detailed_threshold : float = _zoom_parchment_threshold / 2 + +var _is_parchment_view : bool = false +var _is_detailed_view : bool = false @export var _map_mesh_instance : MeshInstance3D var _map_mesh : MapMesh @@ -51,6 +58,8 @@ var _mouse_pos_viewport : Vector2 = Vector2(0.5, 0.5) var _mouse_pos_map : Vector2 = Vector2(0.5, 0.5) var _viewport_dims : Vector2 = Vector2(1, 1) +@export var _map_text : MapText + # ??? Strange Godot/GDExtension Bug ??? # Upon first opening a clone of this repo with the Godot Editor, # if GameSingleton.get_province_index_image is called before MapMesh @@ -62,7 +71,12 @@ func _ready() -> void: if not _camera: push_error("MapView's _camera variable hasn't been set!") return + + # Start just under the parchment threshold + _camera.position.y = _zoom_parchment_threshold - _zoom_target_step _zoom_target = _camera.position.y + _update_view_states(true) + if not _map_mesh_instance: push_error("MapView's _map_mesh_instance variable hasn't been set!") return @@ -106,6 +120,8 @@ func _ready() -> void: scaled_dims.z *= 2.0 (_map_background_instance.mesh as PlaneMesh).set_size(Vector2(scaled_dims.x, scaled_dims.z)) + _map_text.generate_map_names() + func _notification(what : int) -> void: match what: NOTIFICATION_WM_MOUSE_ENTER: # Mouse inside window @@ -227,6 +243,17 @@ func _clamp_over_map() -> void: _camera.position.x = _map_mesh_corner.x + fposmod(_camera.position.x - _map_mesh_corner.x, _map_mesh_dims.x) _camera.position.z = clamp(_camera.position.z, _map_mesh_corner.y, _map_mesh_corner.y + _map_mesh_dims.y) +func _update_view_states(force_signal : bool) -> void: + var new_is_parchment_view : bool = _camera.position.y >= _zoom_parchment_threshold - _zoom_epsilon + if force_signal or new_is_parchment_view != _is_parchment_view: + _is_parchment_view = new_is_parchment_view + parchment_view_changed.emit(_is_parchment_view) + + var new_is_detailed_view : bool = _camera.position.y <= _zoom_detailed_threshold + _zoom_epsilon + if force_signal or new_is_detailed_view != _is_detailed_view: + _is_detailed_view = new_is_detailed_view + detailed_view_changed.emit(_is_detailed_view) + # REQUIREMENTS # * SS-74 # * UIFUN-123 @@ -243,12 +270,17 @@ func _zoom_process(delta : float) -> void: _zoom_position.y * zoom_delta * int(_mouse_over_viewport) ) # TODO - smooth transition similar to smooth zoom - var parchment_mapmode : bool = GameSingleton.is_parchment_mapmode_allowed() and _camera.position.y > _zoom_parchment_threshold + _update_view_states(false) + var parchment_mapmode : bool = GameSingleton.is_parchment_mapmode_allowed() and _is_parchment_view _map_shader_material.set_shader_parameter(GameLoader.ShaderManager.param_parchment_mix, float(parchment_mapmode)) func _update_orientation() -> void: const up := Vector3(0, 0, -1) - var dir := Vector3(0, -1, -1.25 * exp(-1.5 * _camera.position.y - _zoom_target_min)) + var dir := Vector3(0, -1, 0) + if _is_detailed_view: + # Zero at the transition point, increases as you zoom further in + var delta : float = (_zoom_detailed_threshold - _camera.position.y) / _zoom_detailed_threshold + dir.z = -(delta ** 4) _camera.look_at(_camera.position + dir, up) func _update_minimap_viewport() -> void: diff --git a/game/src/Game/GameSession/MapView.tscn b/game/src/Game/GameSession/MapView.tscn index bf22ef8..dff02a6 100644 --- a/game/src/Game/GameSession/MapView.tscn +++ b/game/src/Game/GameSession/MapView.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=7 format=3 uid="uid://dkehmdnuxih2r"] +[gd_scene load_steps=8 format=3 uid="uid://dkehmdnuxih2r"] [ext_resource type="Script" path="res://src/Game/GameSession/MapView.gd" id="1_exccw"] [ext_resource type="Shader" path="res://src/Game/GameSession/TerrainMap.gdshader" id="1_upocn"] +[ext_resource type="Script" path="res://src/Game/GameSession/MapText.gd" id="2_13bgq"] [sub_resource type="ShaderMaterial" id="ShaderMaterial_tayeg"] render_priority = 0 @@ -23,17 +24,22 @@ albedo_color = Color(0, 0, 0, 1) material = SubResource("StandardMaterial3D_irk50") size = Vector2(6, 2) -[node name="MapView" type="Node3D" node_paths=PackedStringArray("_camera", "_map_mesh_instance", "_map_background_instance")] +[node name="MapView" type="Node3D" node_paths=PackedStringArray("_camera", "_map_mesh_instance", "_map_background_instance", "_map_text")] editor_description = "SS-73" script = ExtResource("1_exccw") _camera = NodePath("MapCamera") _map_mesh_instance = NodePath("MapMeshInstance") _map_background_instance = NodePath("MapBackgroundInstance") +_map_text = NodePath("MapText") [node name="MapCamera" type="Camera3D" parent="."] transform = Transform3D(1, 0, 0, 0, 0.707107, 0.707107, 0, -0.707107, 0.707107, 0.25, 1.5, -2.75) near = 0.01 +[node name="MapText" type="Node3D" parent="." node_paths=PackedStringArray("_map_view")] +script = ExtResource("2_13bgq") +_map_view = NodePath("..") + [node name="MapMeshInstance" type="MeshInstance3D" parent="."] editor_description = "FS-343" transform = Transform3D(10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0) @@ -43,3 +49,5 @@ mesh = SubResource("MapMesh_3gtsd") [node name="MapBackgroundInstance" type="MeshInstance3D" parent="."] transform = Transform3D(10, 0, 0, 0, 10, 0, 0, 0, 10, 0, -1, 0) mesh = SubResource("PlaneMesh_fnhgl") + +[connection signal="detailed_view_changed" from="." to="MapText" method="set_visible"] -- cgit v1.2.3-56-ga3b1