diff options
Diffstat (limited to 'game/src/GameSession/MapView.gd')
-rw-r--r-- | game/src/GameSession/MapView.gd | 67 |
1 files changed, 62 insertions, 5 deletions
diff --git a/game/src/GameSession/MapView.gd b/game/src/GameSession/MapView.gd index 982271d..958fabd 100644 --- a/game/src/GameSession/MapView.gd +++ b/game/src/GameSession/MapView.gd @@ -1,19 +1,29 @@ extends Node3D +var ProvScene = preload("res://src/GameSession/ProvinceOverviewPanel.tscn") +var ProvinceShape = MapSingleton.get_province_shape_image() + +@onready var viewport_size = get_viewport().size + const _action_north : StringName = &"map_north" const _action_east : StringName = &"map_east" const _action_south : StringName = &"map_south" const _action_west : StringName = &"map_west" const _action_zoomin : StringName = &"map_zoomin" const _action_zoomout : StringName = &"map_zoomout" - +const _action_drag : StringName = &"map_drag" +const _action_click : StringName = &"map_click" const _shader_param_provinces : StringName = &"province_tex" const _shader_param_mouse_pos : StringName = &"mouse_pos" + + @export var _camera : Camera3D @export var _move_speed : float = 1.0 - +@export var _edge_move_threshold: float = 50.0 +@export var _edge_move_speed: float = 0.02 +@export var _dragSensitivity: float = 0.005 @export var _zoom_target_min : float = 0.2 @export var _zoom_target_max : float = 5.0 @export var _zoom_target_step : float = 0.1 @@ -28,9 +38,7 @@ var _map_shader_material : ShaderMaterial var _map_aspect_ratio : float = 1.0 var _map_mesh_corner : Vector2 var _map_mesh_dims : Vector2 - var _mouse_pos : Vector2 = Vector2(0.5, 0.5) - func _ready(): if _camera == null: push_error("MapView's _camera variable hasn't been set!") @@ -71,7 +79,41 @@ func _ready(): var texture := ImageTexture.create_from_image(province_shape_image) _map_shader_material.set_shader_parameter(_shader_param_provinces, texture) + func _input(event : InputEvent): + if Input.is_action_pressed(_action_click, true) or event is InputEventMouseMotion: + # Check if the mouse is outside of bounds + var _mouse_inside_flag = false + if _mouse_pos.x > 1.0 or _mouse_pos.x < 0.0 and _mouse_pos.y > 1.0 or _mouse_pos.y < 0.0: + _mouse_inside_flag = false + else: + _mouse_inside_flag = true + + # Convert the relative event position from 3D to 2D + # Could do one-liner but here split for readability + var mouse_pos2D = _mouse_pos + mouse_pos2D.x = mouse_pos2D.x * 2.0 - 0.5 + mouse_pos2D.x = mouse_pos2D.x * ProvinceShape.get_size().x + mouse_pos2D.y = mouse_pos2D.y * ProvinceShape.get_size().y + + if Input.is_action_pressed(_action_click, true) and _mouse_inside_flag == true and not event is InputEventMouseMotion: + var pxColour = ProvinceShape.get_pixel(int(mouse_pos2D.x), int(mouse_pos2D.y)) + get_node('MapMeshInstance').material_override.set_shader_parameter("selection_hover", 1.2) + if get_parent().has_node("ProvinceOverviewPanel"): + get_parent().get_node("ProvinceOverviewPanel").ProvinceID = MapSingleton.get_province_id(pxColour.to_html(false)) + get_parent().get_node("ProvinceOverviewPanel").set_id() + else: + var Province = ProvScene.instantiate() + Province.ProvinceID = MapSingleton.get_province_id(pxColour.to_html(false)) + get_parent().add_child(Province) + else: + get_node('MapMeshInstance').material_override.set_shader_parameter("selection_hover", 0.8) +# + + if event is InputEventMouseMotion and Input.is_action_pressed(_action_drag, true): + _camera.position.x -= event.relative.x * _dragSensitivity + _camera.position.z -= event.relative.y * _dragSensitivity + if event.is_action_pressed(_action_zoomin, true): _zoom_target -= _zoom_target_step elif event.is_action_pressed(_action_zoomout, true): @@ -80,6 +122,7 @@ func _input(event : InputEvent): func _physics_process(delta : float): # Process movement _move_process(delta) + _edge_scrolling() # Keep within map bounds _clamp_over_map() # Process zooming @@ -88,7 +131,21 @@ func _physics_process(delta : float): _update_orientation() # Calculate where the mouse lies on the map _update_mouse_map_position() - + + +func _edge_scrolling() -> void: + var local_mouse_pos = get_viewport().get_mouse_position() + + if local_mouse_pos.y < _edge_move_threshold: + _camera.position.z -= _edge_move_speed + elif local_mouse_pos.y > get_viewport().size.y - _edge_move_threshold: + _camera.position.z += _edge_move_speed + + if local_mouse_pos.x < _edge_move_threshold: + _camera.position.x -= _edge_move_speed + elif local_mouse_pos.x > get_viewport().size.x - _edge_move_threshold: + _camera.position.x += _edge_move_speed + func _move_process(delta : float) -> void: var move := Vector3( float(Input.is_action_pressed(_action_east)) - float(Input.is_action_pressed(_action_west)), |