diff options
-rw-r--r-- | extension/src/MapMesh.cpp | 2 | ||||
-rw-r--r-- | game/src/GameSession/GameSession.tscn | 6 | ||||
-rw-r--r-- | game/src/GameSession/MapView.gd | 50 | ||||
-rw-r--r-- | game/src/GameSession/MapView.tscn | 2 | ||||
-rw-r--r-- | game/src/GameSession/Minimap.gd | 15 | ||||
-rw-r--r-- | game/src/GameSession/RectangularCamera.gd | 29 |
6 files changed, 47 insertions, 57 deletions
diff --git a/extension/src/MapMesh.cpp b/extension/src/MapMesh.cpp index ff69390..f0fc819 100644 --- a/extension/src/MapMesh.cpp +++ b/extension/src/MapMesh.cpp @@ -8,7 +8,7 @@ using namespace OpenVic2; void MapMesh::_bind_methods() { ClassDB::bind_method(D_METHOD("set_aspect_ratio", "ratio"), &MapMesh::set_aspect_ratio); ClassDB::bind_method(D_METHOD("get_aspect_ratio"), &MapMesh::get_aspect_ratio); - + ClassDB::bind_method(D_METHOD("set_repeat_proportion", "proportion"), &MapMesh::set_repeat_proportion); ClassDB::bind_method(D_METHOD("get_repeat_proportion"), &MapMesh::get_repeat_proportion); diff --git a/game/src/GameSession/GameSession.tscn b/game/src/GameSession/GameSession.tscn index 640e1a6..73259b4 100644 --- a/game/src/GameSession/GameSession.tscn +++ b/game/src/GameSession/GameSession.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=10 format=3 uid="uid://bgnupcshe1m7r"] +[gd_scene load_steps=9 format=3 uid="uid://bgnupcshe1m7r"] [ext_resource type="Script" path="res://src/GameSession/GameSession.gd" id="1_eklvp"] [ext_resource type="PackedScene" uid="uid://g524p8lr574w" path="res://src/GameSession/MapControlPanel.tscn" id="3_afh6d"] @@ -6,7 +6,6 @@ [ext_resource type="PackedScene" uid="uid://dkehmdnuxih2r" path="res://src/GameSession/MapView.tscn" id="4_xkg5j"] [ext_resource type="Texture2D" uid="uid://c0sm1jfu4kyv3" path="res://common/map/minimap.png" id="5_l05wo"] [ext_resource type="PackedScene" uid="uid://byq323jbel48u" path="res://src/GameSession/ProvinceOverviewPanel.tscn" id="5_osjnn"] -[ext_resource type="Script" path="res://src/GameSession/Minimap.gd" id="5_stb8q"] [ext_resource type="Texture2D" uid="uid://vr1hq2stk8ny" path="res://common/map/MapFrame.png" id="6_ujbv2"] [ext_resource type="Script" path="res://src/GameSession/RectangularCamera.gd" id="7_go8y3"] @@ -46,7 +45,6 @@ offset_left = -350.0 offset_top = -175.0 grow_horizontal = 0 grow_vertical = 0 -script = ExtResource("5_stb8q") [node name="TextureRect" type="TextureRect" parent="Minimap"] layout_mode = 0 @@ -83,7 +81,7 @@ anchor_right = 0.15625 offset_top = 0.0 offset_right = 0.0 -[connection signal="map_view_camera_change" from="MapView" to="Minimap/RectangularCamera" method="_on_map_view_map_view_camera_change"] +[connection signal="map_view_camera_changed" from="MapView" to="Minimap/RectangularCamera" method="_on_map_view_map_view_camera_changed"] [connection signal="province_selected" from="MapView" to="ProvinceOverviewPanel" method="_on_province_selected"] [connection signal="close_button_pressed" from="GameSessionMenu" to="." method="_on_game_session_menu_close_button_pressed"] [connection signal="game_session_menu_button_pressed" from="MapControlPanel" to="." method="_on_game_session_menu_button_pressed"] diff --git a/game/src/GameSession/MapView.gd b/game/src/GameSession/MapView.gd index 8540536..77fea96 100644 --- a/game/src/GameSession/MapView.gd +++ b/game/src/GameSession/MapView.gd @@ -1,8 +1,7 @@ extends Node3D signal province_selected(identifier : String) -signal map_view_camera_change(camera_position : Vector3) - +signal map_view_camera_changed(near_left : Vector2, far_left : Vector2, far_right : Vector2, near_right : Vector2) const _action_north : StringName = &"map_north" const _action_east : StringName = &"map_east" @@ -45,11 +44,13 @@ var _map_mesh_dims : Vector2 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) func _ready(): if _camera == null: push_error("MapView's _camera variable hasn't been set!") return + _zoom_target = _camera.position.y if _map_mesh_instance == null: push_error("MapView's _map_mesh variable hasn't been set!") return @@ -94,6 +95,22 @@ func _ready(): var province_colour_texture := ImageTexture.create_from_image(province_colour_image) _map_shader_material.set_shader_parameter(_shader_param_province_colour, province_colour_texture) +func _world_to_map_coords(pos : Vector3) -> Vector2: + return (Vector2(pos.x, pos.z) - _map_mesh_corner) / _map_mesh_dims + +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) + # Plane with normal (0,1,0) facing upwards, at a distance 0 from the origin + var intersection = Plane(0, 1, 0, 0).intersects_ray(ray_origin, ray_normal) + if typeof(intersection) == TYPE_VECTOR3: + return _world_to_map_coords(intersection as Vector3) + else: + # Normals parallel to the xz-plane could cause null intersections, + # but the camera's orientation should prevent such normals + push_error("Invalid intersection: ", intersection) + return Vector2(0.5, 0.5) + func _unhandled_input(event : InputEvent): if event.is_action_pressed(_action_click): # Check if the mouse is outside of bounds @@ -118,6 +135,7 @@ func _unhandled_input(event : InputEvent): func _physics_process(delta : float): _mouse_pos_viewport = get_viewport().get_mouse_position() + _viewport_dims = Vector2(Resolution.get_current_resolution()) # Process movement _movement_process(delta) # Keep within map bounds @@ -125,8 +143,9 @@ func _physics_process(delta : float): # Process zooming _zoom_process(delta) # Orient based on height - map_view_camera_change.emit(_camera.position) _update_orientation() + # Update viewport on minimap + _update_minimap_viewport() # Calculate where the mouse lies on the map _update_mouse_map_position() @@ -141,8 +160,7 @@ func _movement_process(delta : float) -> void: _camera.position += Vector3(direction.x, 0, direction.y) func _edge_scrolling_vector() -> Vector2: - var viewport_dims := Vector2(Resolution.get_current_resolution()) - var mouse_vector := _mouse_pos_viewport / viewport_dims - Vector2(0.5, 0.5); + var mouse_vector := _mouse_pos_viewport / _viewport_dims - Vector2(0.5, 0.5); if pow(mouse_vector.x, 4) + pow(mouse_vector.y, 4) < pow(0.5 - _edge_move_threshold, 4): mouse_vector *= 0 return mouse_vector * _edge_move_speed @@ -172,17 +190,13 @@ func _update_orientation() -> void: var dir := Vector3(0, -1, -exp(-_camera.position.y * 2.0 + 0.5)) _camera.look_at(_camera.position + dir) +func _update_minimap_viewport() -> void: + var near_left := _viewport_to_map_coords(Vector2(0, _viewport_dims.y)) + var far_left := _viewport_to_map_coords(Vector2(0, 0)) + var far_right := _viewport_to_map_coords(Vector2(_viewport_dims.x, 0)) + var near_right := _viewport_to_map_coords(_viewport_dims) + map_view_camera_changed.emit(near_left, far_left, far_right, near_right) + func _update_mouse_map_position() -> void: - var ray_origin := _camera.project_ray_origin(_mouse_pos_viewport) - var ray_normal := _camera.project_ray_normal(_mouse_pos_viewport) - # Plane with normal (0,1,0) facing upwards, at a distance 0 from the origin - var intersection = Plane(0, 1, 0, 0).intersects_ray(ray_origin, ray_normal) - if typeof(intersection) == TYPE_VECTOR3: - var intersection_vec := intersection as Vector3 - # This loops both horizontally (good) and vertically (bad) - _mouse_pos_map = (Vector2(intersection_vec.x, intersection_vec.z) - _map_mesh_corner) / _map_mesh_dims - _map_shader_material.set_shader_parameter(_shader_param_hover_pos, _mouse_pos_map) - else: - # Normals parallel to the xz-plane could cause null intersections, - # but the camera's orientation should prevent such normals - push_error("Invalid intersection: ", intersection) + _mouse_pos_map = _viewport_to_map_coords(_mouse_pos_viewport) + _map_shader_material.set_shader_parameter(_shader_param_hover_pos, _mouse_pos_map) diff --git a/game/src/GameSession/MapView.tscn b/game/src/GameSession/MapView.tscn index 63d16ef..2df06fb 100644 --- a/game/src/GameSession/MapView.tscn +++ b/game/src/GameSession/MapView.tscn @@ -19,7 +19,7 @@ _camera = NodePath("MapCamera") _map_mesh_instance = NodePath("MapMeshInstance") [node name="MapCamera" type="Camera3D" parent="."] -transform = Transform3D(1, 0, 0, 0, 0.707107, 0.707107, 0, -0.707107, 0.707107, 0, 1, 0) +transform = Transform3D(1, 0, 0, 0, 0.707107, 0.707107, 0, -0.707107, 0.707107, 0.25, 1.5, -2.75) [node name="MapMeshInstance" type="MeshInstance3D" parent="."] transform = Transform3D(10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0) diff --git a/game/src/GameSession/Minimap.gd b/game/src/GameSession/Minimap.gd deleted file mode 100644 index 4bf028b..0000000 --- a/game/src/GameSession/Minimap.gd +++ /dev/null @@ -1,15 +0,0 @@ -extends Control - - -var MouseInside: bool = false - -func _process(delta): - if MouseInside: - print(get_local_mouse_position()) - -func _on_mouse_entered(): - MouseInside = true - - -func _on_mouse_exited(): - MouseInside = false diff --git a/game/src/GameSession/RectangularCamera.gd b/game/src/GameSession/RectangularCamera.gd index 55deb1d..bc7ca1e 100644 --- a/game/src/GameSession/RectangularCamera.gd +++ b/game/src/GameSession/RectangularCamera.gd @@ -1,24 +1,17 @@ extends Control - -@export var pos_x = 100.0 -@export var pos_y = 100.0 -@export var zoom_x = 10 -@export var zoom_y = 10 -@onready var _frame: TextureRect = $"../TextureRect" - -var RectangularCamera = Rect2(pos_x,pos_y,zoom_x,zoom_y) +@onready var _frame : TextureRect = $"../TextureRect" +var _viewport_points : PackedVector2Array func _draw() -> void: - draw_rect(RectangularCamera,Color.BLACK,false,-1) - -func _on_map_view_map_view_camera_change(camera_position): - - zoom_x = camera_position.y*(_frame.size.x/20)*1.77 - zoom_y = camera_position.y*(_frame.size.y/20)*1.77 - - pos_x = camera_position.x*(_frame.size.x/20) + _frame.size.x/2 - zoom_x/2 # + _frame.size.x/2 - zoom_x/2 - Translation from camera_position.x to pos_x (i.e. 0px for center on camera_pos -> 175px for center on rect_camera) - pos_y = camera_position.z*(_frame.size.y/10) + _frame.size.y/2 - zoom_y/2 # + _frame.size.y/2 - zoom_y/2 - Translation from camera_position.y to pos_y (i.e. 0px for center on camera_pos -> 87.5px for center on rect_camera) + if _viewport_points.size() > 1: + draw_polyline(_viewport_points, Color.WHITE, -1) - RectangularCamera = Rect2(pos_x,pos_y,zoom_x,zoom_y) +func _on_map_view_map_view_camera_changed(near_left : Vector2, far_left : Vector2, far_right : Vector2, near_right : Vector2) -> void: + _viewport_points.clear() + _viewport_points.push_back(near_left * _frame.size) + _viewport_points.push_back(far_left * _frame.size) + _viewport_points.push_back(far_right * _frame.size) + _viewport_points.push_back(near_right * _frame.size) + _viewport_points.push_back(_viewport_points[0]) queue_redraw() |