From 7e2700514652212e50a006ad525e7c7ca8e7840c Mon Sep 17 00:00:00 2001 From: Hop311 Date: Sat, 29 Apr 2023 20:00:44 +0100 Subject: Terrain textures + province colour alpha channel --- game/src/GameSession/GameSession.tscn | 1 + game/src/GameSession/MapView.gd | 14 ++++++++++---- game/src/GameSession/MapView.tscn | 6 ++---- game/src/GameSession/ProvinceOverviewPanel.gd | 4 +++- game/src/GameSession/TerrainMap.gdshader | 19 +++++++------------ 5 files changed, 23 insertions(+), 21 deletions(-) (limited to 'game/src/GameSession') diff --git a/game/src/GameSession/GameSession.tscn b/game/src/GameSession/GameSession.tscn index 98b43dd..c146b3e 100644 --- a/game/src/GameSession/GameSession.tscn +++ b/game/src/GameSession/GameSession.tscn @@ -74,5 +74,6 @@ grow_horizontal = 0 [connection signal="mouse_exited" from="MapControlPanel" to="MapView" method="_on_mouse_entered_viewport"] [connection signal="zoom_in_button_pressed" from="MapControlPanel" to="MapView" method="zoom_in"] [connection signal="zoom_out_button_pressed" from="MapControlPanel" to="MapView" method="zoom_out"] +[connection signal="province_deselected" from="ProvinceOverviewPanel" to="MapView" method="_deselect_province"] [connection signal="back_button_pressed" from="OptionsMenu" to="MapView" method="enable_processing"] [connection signal="back_button_pressed" from="OptionsMenu" to="OptionsMenu" method="hide"] diff --git a/game/src/GameSession/MapView.gd b/game/src/GameSession/MapView.gd index 340083f..2e2c5e6 100644 --- a/game/src/GameSession/MapView.gd +++ b/game/src/GameSession/MapView.gd @@ -71,7 +71,8 @@ func _ready(): # Set map mesh size and get bounds _map_mesh.aspect_ratio = GameSingleton.get_aspect_ratio() - _map_shader_material.set_shader_parameter(Events.ShaderManager.param_terrain_tile_factor, float(GameSingleton.get_height()) / 64.0) + _map_shader_material.set_shader_parameter(Events.ShaderManager.param_terrain_tile_factor, + float(GameSingleton.get_height()) / 128.0) var map_mesh_aabb := _map_mesh.get_core_aabb() * _map_mesh_instance.transform _map_mesh_corner = Vector2( min(map_mesh_aabb.position.x, map_mesh_aabb.end.x), @@ -111,15 +112,20 @@ func zoom_in() -> void: func zoom_out() -> void: _zoom_target += _zoom_target_step +func _select_province(index : int) -> void: + _map_shader_material.set_shader_parameter(Events.ShaderManager.param_selected_index, index) + province_selected.emit(index) + +func _deselect_province() -> void: + _select_province(0) + # REQUIREMENTS # * SS-31 func _unhandled_input(event : InputEvent): if _mouse_over_viewport and event.is_action_pressed(_action_click): # Check if the mouse is outside of bounds if _map_mesh.is_valid_uv_coord(_mouse_pos_map): - var selected_index := GameSingleton.get_province_index_from_uv_coords(_mouse_pos_map) - _map_shader_material.set_shader_parameter(Events.ShaderManager.param_selected_index, selected_index) - province_selected.emit(selected_index) + _select_province(GameSingleton.get_province_index_from_uv_coords(_mouse_pos_map)) else: print("Clicked outside the map!") elif event.is_action_pressed(_action_drag): diff --git a/game/src/GameSession/MapView.tscn b/game/src/GameSession/MapView.tscn index efbad4e..fa6ffcd 100644 --- a/game/src/GameSession/MapView.tscn +++ b/game/src/GameSession/MapView.tscn @@ -1,17 +1,15 @@ -[gd_scene load_steps=6 format=3 uid="uid://dkehmdnuxih2r"] +[gd_scene load_steps=5 format=3 uid="uid://dkehmdnuxih2r"] [ext_resource type="Script" path="res://src/GameSession/MapView.gd" id="1_exccw"] [ext_resource type="Shader" path="res://src/GameSession/TerrainMap.gdshader" id="1_upocn"] -[ext_resource type="Texture2D" uid="uid://ckf222w5usrsu" path="res://art/terrain/farmlands.png" id="3_47mq1"] [sub_resource type="ShaderMaterial" id="ShaderMaterial_tayeg"] render_priority = 0 shader = ExtResource("1_upocn") -shader_parameter/province_index_subdivisions = null +shader_parameter/province_shape_subdivisions = null shader_parameter/hover_index = null shader_parameter/selected_index = null shader_parameter/terrain_tile_factor = null -shader_parameter/farmlands_tex = ExtResource("3_47mq1") [sub_resource type="MapMesh" id="MapMesh_3gtsd"] diff --git a/game/src/GameSession/ProvinceOverviewPanel.gd b/game/src/GameSession/ProvinceOverviewPanel.gd index 17da9d0..5a914f7 100644 --- a/game/src/GameSession/ProvinceOverviewPanel.gd +++ b/game/src/GameSession/ProvinceOverviewPanel.gd @@ -1,5 +1,7 @@ extends PanelContainer +signal province_deselected + @export var _province_name_label : Label @export var _region_name_label : Label @export var _life_rating_bar : ProgressBar @@ -92,4 +94,4 @@ func _on_province_selected(index : int) -> void: _selected_index = index func _on_close_button_pressed() -> void: - _selected_index = 0 + province_deselected.emit() diff --git a/game/src/GameSession/TerrainMap.gdshader b/game/src/GameSession/TerrainMap.gdshader index 05928f3..cab757b 100644 --- a/game/src/GameSession/TerrainMap.gdshader +++ b/game/src/GameSession/TerrainMap.gdshader @@ -10,27 +10,22 @@ uniform sampler2D province_colour_tex: source_color, repeat_enable, filter_neare uniform uint hover_index; // Index of the currently selected province uniform uint selected_index; -// Cosmetic farmlands terrain texture -uniform sampler2D farmlands_tex: source_color, repeat_enable, filter_linear; +// Cosmetic terrain textures +uniform sampler2DArray terrain_tex: source_color, repeat_enable, filter_linear; // The number of times the terrain textures should tile vertically uniform float terrain_tile_factor; -const vec3 water_colour = vec3(0, 0, 1); - vec3 get_terrain_colour(vec2 uv, vec2 corner, vec2 half_pixel_size, vec2 terrain_uv) { uvec3 province_data = read_uvec3(fma(corner, half_pixel_size, uv)); + vec4 province_colour = texelFetch(province_colour_tex, ivec2(province_data.rg), 0); + vec3 terrain_colour = texture(terrain_tex, vec3(terrain_uv, float(province_data.b))).rgb; uint index = uvec2_to_uint(province_data.rg); - float is_land = float(province_data.b != 0u); - vec3 province_colour = texelFetch(province_colour_tex, ivec2(province_data.rg), 0).rgb; - vec3 farmlands_colour = texture(farmlands_tex, terrain_uv).rgb; - vec3 terrain_colour = mix(water_colour, farmlands_colour, is_land); - float mix_val = 0.4 + float(index == hover_index) * 0.2 + float(index == selected_index) * 0.2; - vec3 mixed_colour = mix(terrain_colour, province_colour, mix_val); - return mixed_colour; + float mix_val = province_colour.a + float(index == hover_index) * 0.2 + float(index == selected_index) * 0.2; + return mix(terrain_colour, province_colour.rgb, mix_val); } vec3 mix_terrain_colour(vec2 uv) { - vec2 map_size = vec2(textureSize(province_shape_tex, 0).xy); + vec2 map_size = vec2(textureSize(province_shape_tex, 0).xy) * province_shape_subdivisions; vec2 pixel_offset = fract(fma(uv, map_size, vec2(0.5))); vec2 half_pixel_size = 0.49 / map_size; -- cgit v1.2.3-56-ga3b1 From bce925ad8efa7bbf508e79cab2110416a71cb8ee Mon Sep 17 00:00:00 2001 From: Hop311 Date: Sun, 30 Apr 2023 20:09:35 +0100 Subject: Highlight selected province on minimap --- game/src/GameSession/GameSession.tscn | 1 + game/src/GameSession/MapControlPanel.gd | 4 ++++ game/src/GameSession/MapControlPanel.tscn | 4 +++- game/src/GameSession/Minimap.gd | 10 +++++++++- game/src/GameSession/Minimap.gdshader | 13 ++++++++++++- 5 files changed, 29 insertions(+), 3 deletions(-) (limited to 'game/src/GameSession') diff --git a/game/src/GameSession/GameSession.tscn b/game/src/GameSession/GameSession.tscn index c146b3e..70f0583 100644 --- a/game/src/GameSession/GameSession.tscn +++ b/game/src/GameSession/GameSession.tscn @@ -66,6 +66,7 @@ offset_right = 0.0 grow_horizontal = 0 [connection signal="map_view_camera_changed" from="MapView" to="MapControlPanel" method="_on_map_view_camera_changed"] +[connection signal="province_selected" from="MapView" to="MapControlPanel" method="_on_province_selected"] [connection signal="province_selected" from="MapView" to="ProvinceOverviewPanel" method="_on_province_selected"] [connection signal="options_button_pressed" from="GameSessionMenu" to="OptionsMenu" method="show"] [connection signal="game_session_menu_button_pressed" from="MapControlPanel" to="." method="_on_game_session_menu_button_pressed"] diff --git a/game/src/GameSession/MapControlPanel.gd b/game/src/GameSession/MapControlPanel.gd index 508f692..e9249b3 100644 --- a/game/src/GameSession/MapControlPanel.gd +++ b/game/src/GameSession/MapControlPanel.gd @@ -3,6 +3,7 @@ extends PanelContainer signal game_session_menu_button_pressed signal map_view_camera_changed(near_left : Vector2, far_left : Vector2, far_right : Vector2, near_right : Vector2) signal minimap_clicked(pos_clicked : Vector2) +signal province_selcted(index : int) signal zoom_in_button_pressed signal zoom_out_button_pressed @@ -47,6 +48,9 @@ func _on_map_view_camera_changed(near_left : Vector2, far_left : Vector2, far_ri func _on_minimap_clicked(pos_clicked : Vector2) -> void: minimap_clicked.emit(pos_clicked) +func _on_province_selected(index : int) -> void: + province_selcted.emit(index) + # REQUIREMENTS: # * UIFUN-269 func _on_zoom_in_button_pressed() -> void: diff --git a/game/src/GameSession/MapControlPanel.tscn b/game/src/GameSession/MapControlPanel.tscn index ae4c3dc..82b9c86 100644 --- a/game/src/GameSession/MapControlPanel.tscn +++ b/game/src/GameSession/MapControlPanel.tscn @@ -7,6 +7,7 @@ [sub_resource type="ShaderMaterial" id="ShaderMaterial_bhuqb"] shader = ExtResource("2_rinsg") shader_parameter/province_shape_subdivisions = null +shader_parameter/selected_index = null [sub_resource type="InputEventAction" id="InputEventAction_5nck3"] action = &"ui_cancel" @@ -56,7 +57,7 @@ color = Color(0.921569, 0.835294, 0.701961, 1) [node name="ViewportQuad" type="Control" parent="MapPanelMargin/MapPanelList/MapDisplayList/Minimap" node_paths=PackedStringArray("_minimap_texture")] layout_mode = 2 -mouse_filter = 2 +mouse_filter = 1 script = ExtResource("3_s4dml") _minimap_texture = NodePath("../MinimapTexture") @@ -100,6 +101,7 @@ mouse_filter = 1 text = "-" [connection signal="map_view_camera_changed" from="." to="MapPanelMargin/MapPanelList/MapDisplayList/Minimap/ViewportQuad" method="_on_map_view_camera_changed"] +[connection signal="province_selcted" from="." to="MapPanelMargin/MapPanelList/MapDisplayList/Minimap/ViewportQuad" method="_on_province_selected"] [connection signal="minimap_clicked" from="MapPanelMargin/MapPanelList/MapDisplayList/Minimap/ViewportQuad" to="." method="_on_minimap_clicked"] [connection signal="pressed" from="MapPanelMargin/MapPanelList/AuxiliaryPanel/GameSessionMenuButton" to="." method="_on_game_session_menu_button_pressed"] [connection signal="pressed" from="MapPanelMargin/MapPanelList/AuxiliaryPanel/ZoomButtonsContainer/ZoomInButton" to="." method="_on_zoom_in_button_pressed"] diff --git a/game/src/GameSession/Minimap.gd b/game/src/GameSession/Minimap.gd index 05c662b..f5d972a 100644 --- a/game/src/GameSession/Minimap.gd +++ b/game/src/GameSession/Minimap.gd @@ -5,13 +5,21 @@ signal minimap_clicked(pos_clicked : Vector2) const _action_click : StringName = &"map_click" @export var _minimap_texture : Control +var _minimap_shader : ShaderMaterial var _viewport_points : PackedVector2Array func _ready(): _minimap_texture.custom_minimum_size = Vector2(GameSingleton.get_aspect_ratio(), 1.0) * 150 - if Events.ShaderManager.set_up_shader(_minimap_texture.get_material(), false) != OK: + var minimap_material := _minimap_texture.get_material() + if Events.ShaderManager.set_up_shader(minimap_material, false) != OK: push_error("Failed to set up minimap shader") + else: + _minimap_shader = minimap_material + +func _on_province_selected(index : int) -> void: + if _minimap_shader != null: + _minimap_shader.set_shader_parameter(Events.ShaderManager.param_selected_index, index) # REQUIREMENTS # * SS-80 diff --git a/game/src/GameSession/Minimap.gdshader b/game/src/GameSession/Minimap.gdshader index 9afd470..8b68108 100644 --- a/game/src/GameSession/Minimap.gdshader +++ b/game/src/GameSession/Minimap.gdshader @@ -2,6 +2,17 @@ shader_type canvas_item; #include "ProvinceIndexSampler.gdshaderinc" +// Index of the currently selected province +uniform uint selected_index; + +const vec3 land_colour = vec3(0.5); +const vec3 selected_colour = vec3(1.0, 1.0, 0.0); + void fragment() { - COLOR.rgb = mix(COLOR.rgb, vec3(0.5), float(read_uvec3(UV).b != 0u)); + uvec3 data = read_uvec3(UV); + uint index = uvec2_to_uint(data.rg); + float is_land = float(data.b != 0u); + float is_selected = float(index == selected_index); + COLOR.rgb = mix(COLOR.rgb, land_colour, is_land); + COLOR.rgb = mix(COLOR.rgb, selected_colour, is_selected); } -- cgit v1.2.3-56-ga3b1 From 2fec521cc6bbe7b2cda0eef3b830acbfc8b68333 Mon Sep 17 00:00:00 2001 From: Hop311 Date: Sun, 30 Apr 2023 23:59:54 +0100 Subject: Hashmaps instead of linear + better province hover --- extension/src/openvic2/Types.hpp | 17 ++++++++++------- extension/src/openvic2/map/Map.cpp | 30 ++++++++++-------------------- extension/src/openvic2/map/Map.hpp | 11 +++++++---- game/src/GameSession/MapView.gd | 1 + 4 files changed, 28 insertions(+), 31 deletions(-) (limited to 'game/src/GameSession') diff --git a/extension/src/openvic2/Types.hpp b/extension/src/openvic2/Types.hpp index 1359fbe..e4a0e2d 100644 --- a/extension/src/openvic2/Types.hpp +++ b/extension/src/openvic2/Types.hpp @@ -1,9 +1,9 @@ #pragma once -#include #include #include #include +#include #include "Logger.hpp" @@ -74,9 +74,12 @@ namespace OpenVic2 { */ template::value>::type* = nullptr> class IdentifierRegistry { + using identifier_index_map_t = std::map; + const std::string name; std::vector items; bool locked = false; + identifier_index_map_t identifier_index_map; public: IdentifierRegistry(std::string const& new_name) : name(new_name) {} return_t add_item(T&& item) { @@ -89,6 +92,7 @@ namespace OpenVic2 { Logger::error("Cannot add item to the ", name, " registry - an item with the identifier \"", item.get_identifier(), "\" already exists!"); return FAILURE; } + identifier_index_map[item.get_identifier()] = items.size(); items.push_back(std::move(item)); return SUCCESS; } @@ -104,6 +108,7 @@ namespace OpenVic2 { return locked; } void reset() { + identifier_index_map.clear(); items.clear(); locked = false; } @@ -111,15 +116,13 @@ namespace OpenVic2 { return items.size(); } T* get_item_by_identifier(std::string const& identifier) { - if (!identifier.empty()) - for (T& item : items) - if (item.get_identifier() == identifier) return &item; + const identifier_index_map_t::const_iterator it = identifier_index_map.find(identifier); + if (it != identifier_index_map.end()) return &items[it->second]; return nullptr; } T const* get_item_by_identifier(std::string const& identifier) const { - if (!identifier.empty()) - for (T const& item : items) - if (item.get_identifier() == identifier) return &item; + const identifier_index_map_t::const_iterator it = identifier_index_map.find(identifier); + if (it != identifier_index_map.end()) return &items[it->second]; return nullptr; } T* get_item_by_index(size_t index) { diff --git a/extension/src/openvic2/map/Map.cpp b/extension/src/openvic2/map/Map.cpp index ab035ad..1f44c43 100644 --- a/extension/src/openvic2/map/Map.cpp +++ b/extension/src/openvic2/map/Map.cpp @@ -36,11 +36,12 @@ return_t Map::add_province(std::string const& identifier, colour_t colour) { return FAILURE; } Province new_province{ static_cast(provinces.get_item_count() + 1), identifier, colour }; - Province const* old_province = get_province_by_colour(colour); - if (old_province != nullptr) { - Logger::error("Duplicate province colours: ", old_province->to_string(), " and ", new_province.to_string()); + const index_t index = get_index_from_colour(colour); + if (index != NULL_INDEX) { + Logger::error("Duplicate province colours: ", get_province_by_index(index)->to_string(), " and ", new_province.to_string()); return FAILURE; } + colour_index_map[new_province.get_colour()] = new_province.get_index(); return provinces.add_item(std::move(new_province)); } @@ -142,20 +143,10 @@ Province const* Map::get_province_by_identifier(std::string const& identifier) c return provinces.get_item_by_identifier(identifier); } -Province* Map::get_province_by_colour(colour_t colour) { - if (colour != NULL_COLOUR) - for (Province& province : provinces.get_items()) - if (province.get_colour() == colour) return &province; - return nullptr; -} - -Province const* Map::get_province_by_colour(colour_t colour) const { - if (colour == NULL_COLOUR) { return nullptr; } - for (Province const& province : provinces.get_items()) { - if (province.get_colour() == colour) - return &province; - } - return nullptr; +index_t Map::get_index_from_colour(colour_t colour) const { + const colour_index_map_t::const_iterator it = colour_index_map.find(colour); + if (it != colour_index_map.end()) return it->second; + return NULL_INDEX; } index_t Map::get_province_index_at(size_t x, size_t y) const { @@ -236,9 +227,8 @@ return_t Map::generate_province_shape_image(size_t new_width, size_t new_height, continue; } } - Province const* province = get_province_by_colour(province_colour); - if (province != nullptr) { - const index_t index = province->get_index(); + const index_t index = get_index_from_colour(province_colour); + if (index != NULL_INDEX) { province_checklist[index - 1] = true; province_shape_image[idx].index = index; continue; diff --git a/extension/src/openvic2/map/Map.hpp b/extension/src/openvic2/map/Map.hpp index a075505..cb8dcb1 100644 --- a/extension/src/openvic2/map/Map.hpp +++ b/extension/src/openvic2/map/Map.hpp @@ -1,7 +1,6 @@ #pragma once #include -#include #include "Region.hpp" @@ -27,7 +26,8 @@ namespace OpenVic2 { */ struct Map { using terrain_t = uint8_t; - using terrain_variant_map_t = std::unordered_map; + using terrain_variant_map_t = std::map; + #pragma pack(push, 1) struct shape_pixel_t { index_t index; @@ -35,6 +35,8 @@ namespace OpenVic2 { }; #pragma pack(pop) private: + using colour_index_map_t = std::map; + IdentifierRegistry provinces; IdentifierRegistry regions; IdentifierRegistry mapmodes; @@ -43,6 +45,9 @@ namespace OpenVic2 { size_t width = 0, height = 0; std::vector province_shape_image; + colour_index_map_t colour_index_map; + + index_t get_index_from_colour(colour_t colour) const; public: Map(); @@ -58,8 +63,6 @@ namespace OpenVic2 { Province const* get_province_by_index(index_t index) const; Province* get_province_by_identifier(std::string const& identifier); Province const* get_province_by_identifier(std::string const& identifier) const; - Province* get_province_by_colour(colour_t colour); - Province const* get_province_by_colour(colour_t colour) const; index_t get_province_index_at(size_t x, size_t y) const; Region* get_region_by_identifier(std::string const& identifier); diff --git a/game/src/GameSession/MapView.gd b/game/src/GameSession/MapView.gd index 2e2c5e6..e2c8519 100644 --- a/game/src/GameSession/MapView.gd +++ b/game/src/GameSession/MapView.gd @@ -228,6 +228,7 @@ func _on_mouse_entered_viewport(): func _on_mouse_exited_viewport(): _mouse_over_viewport = false + _map_shader_material.set_shader_parameter(Events.ShaderManager.param_hover_index, 0) func _on_minimap_clicked(pos_clicked : Vector2): pos_clicked *= _map_mesh_dims -- cgit v1.2.3-56-ga3b1