aboutsummaryrefslogtreecommitdiff
path: root/game/src/Game/GameSession/MapView.gd
diff options
context:
space:
mode:
Diffstat (limited to 'game/src/Game/GameSession/MapView.gd')
-rw-r--r--game/src/Game/GameSession/MapView.gd64
1 files changed, 51 insertions, 13 deletions
diff --git a/game/src/Game/GameSession/MapView.gd b/game/src/Game/GameSession/MapView.gd
index f522bcb..2ab7c34 100644
--- a/game/src/Game/GameSession/MapView.gd
+++ b/game/src/Game/GameSession/MapView.gd
@@ -1,6 +1,9 @@
+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"
@@ -22,9 +25,9 @@ var _drag_active : bool = false
var _mouse_over_viewport : bool = true
var _window_in_focus : bool = true
-@export var _zoom_target_min : float = 0.15
+@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,
@@ -35,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
@@ -50,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
@@ -61,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
@@ -105,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
@@ -119,6 +136,10 @@ func _notification(what : int) -> void:
func _world_to_map_coords(pos : Vector3) -> Vector2:
return (Vector2(pos.x, pos.z) - _map_mesh_corner) / _map_mesh_dims
+func _map_to_world_coords(pos : Vector2) -> Vector3:
+ pos = pos * _map_mesh_dims + _map_mesh_corner
+ return Vector3(pos.x, 0, pos.y)
+
func _viewport_to_map_coords(pos_viewport : Vector2) -> Vector2:
var ray_origin := _camera.project_ray_origin(pos_viewport)
var ray_normal := _camera.project_ray_normal(pos_viewport)
@@ -149,12 +170,13 @@ func _on_province_selected(index : int) -> void:
# REQUIREMENTS
# * SS-31
func _unhandled_input(event : InputEvent) -> void:
- 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):
- GameSingleton.set_selected_province(GameSingleton.get_province_index_from_uv_coords(_mouse_pos_map))
- else:
- print("Clicked outside the map!")
+ if event.is_action_pressed(_action_click):
+ if _mouse_over_viewport:
+ # Check if the mouse is outside of bounds
+ if _map_mesh.is_valid_uv_coord(_mouse_pos_map):
+ GameSingleton.set_selected_province(GameSingleton.get_province_index_from_uv_coords(_mouse_pos_map))
+ else:
+ print("Clicked outside the map!")
elif event.is_action_pressed(_action_drag):
if _drag_active:
push_warning("Drag being activated while already active!")
@@ -221,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
@@ -237,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(-10 * _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:
@@ -253,9 +291,9 @@ func _update_minimap_viewport() -> void:
map_view_camera_changed.emit(near_left, far_left, far_right, near_right)
func _update_mouse_map_position() -> void:
- _mouse_pos_map = _viewport_to_map_coords(_mouse_pos_viewport)
- var hover_index := GameSingleton.get_province_index_from_uv_coords(_mouse_pos_map)
if _mouse_over_viewport:
+ _mouse_pos_map = _viewport_to_map_coords(_mouse_pos_viewport)
+ var hover_index := GameSingleton.get_province_index_from_uv_coords(_mouse_pos_map)
_map_shader_material.set_shader_parameter(GameLoader.ShaderManager.param_hover_index, hover_index)
func _on_mouse_entered_viewport() -> void: