Skip to content

Commit

Permalink
Teleport camera when clicking on minimap, closes #63
Browse files Browse the repository at this point in the history
  • Loading branch information
Scony committed Jul 31, 2023
1 parent e33625a commit 8124e11
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 5 deletions.
43 changes: 42 additions & 1 deletion source/match/hud/Minimap.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ const MINIMAP_PIXELS_PER_WORLD_METER = 2
const RESOURCE_UNIT_REPRESENTATION_COLOR = Color.YELLOW

var _unit_to_corresponding_node_mapping = {}
var _camera_movement_active = false

@onready var _match = find_parent("Match")
@onready var _camera_indicator = find_child("CameraIndicator")
@onready var _viewport = find_child("MinimapViewport")
@onready var _viewport_background = find_child("Background")
@onready var _texture_rect = find_child("MinimapTextureRect")


func _ready():
Expand All @@ -22,6 +23,7 @@ func _ready():
find_child("MinimapViewport").size = (
_match.find_child("Map").size * MINIMAP_PIXELS_PER_WORLD_METER
)
_texture_rect.gui_input.connect(_on_gui_input)


func _physics_process(_delta):
Expand Down Expand Up @@ -104,3 +106,42 @@ func _update_camera_indicator():
corner_mapped_to_3d_position_on_ground_level.z
)
)


func _on_gui_input(event):
if event is InputEventMouseButton:
if event.is_pressed() and event.button_index == MOUSE_BUTTON_LEFT:
_try_teleporting_camera_based_on_local_texture_rect_position(event.position)
_camera_movement_active = true
if not event.is_pressed() and event.button_index == MOUSE_BUTTON_LEFT:
_camera_movement_active = false
elif event is InputEventMouseMotion and _camera_movement_active:
_try_teleporting_camera_based_on_local_texture_rect_position(event.position)


func _try_teleporting_camera_based_on_local_texture_rect_position(position_2d_within_texture_rect):
assert(
_texture_rect.stretch_mode == _texture_rect.STRETCH_KEEP_ASPECT_CENTERED,
"world 3d position retrieval algorithm assumes 'STRETCH_KEEP_ASPECT_CENTERED'"
)
var texture_rect_size = _texture_rect.size
var texture_size = _texture_rect.texture.get_size()
var proportions = texture_rect_size / texture_size
var scaling_factor = proportions.x if proportions.x < proportions.y else proportions.y
var scaled_texture_size = texture_size * scaling_factor
var scaled_texture_position_within_texture_rect = (
(texture_rect_size - scaled_texture_size) / 2.0
)
var rect_containing_scaled_texture = Rect2(
scaled_texture_position_within_texture_rect, scaled_texture_size
)
if rect_containing_scaled_texture.has_point(position_2d_within_texture_rect):
var position_2d_within_minimap = (
(position_2d_within_texture_rect - rect_containing_scaled_texture.position)
/ scaling_factor
)
var world_position_3d = (
Vector3(position_2d_within_minimap.x, 0.0, position_2d_within_minimap.y)
/ MINIMAP_PIXELS_PER_WORLD_METER
)
get_viewport().get_camera_3d().set_position_safely(world_position_3d)
71 changes: 71 additions & 0 deletions tests/manual/TestNonQuadraticMap.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
[gd_scene load_steps=17 format=3 uid="uid://c54pjqg0defrj"]

[ext_resource type="PackedScene" uid="uid://camns8fqod8d4" path="res://source/match/Match.tscn" id="1_e8lxh"]
[ext_resource type="Script" path="res://tests/manual/Match.gd" id="2_2ib5j"]
[ext_resource type="Script" path="res://source/model/MatchSettings.gd" id="3_52248"]
[ext_resource type="PackedScene" uid="uid://d8wcem3ievht" path="res://tests/manual/maps/NonQuadratic.tscn" id="3_dldkj"]
[ext_resource type="Script" path="res://source/model/PlayerSettings.gd" id="3_eliam"]
[ext_resource type="Material" uid="uid://co8vfcoqqs5i8" path="res://source/match/resources/materials/terrain.material.tres" id="4_d4g7r"]
[ext_resource type="Shader" path="res://source/shaders/3d/simple_fog_of_war.gdshader" id="5_arsbx"]
[ext_resource type="Shader" path="res://source/shaders/2d/white_transparent.gdshader" id="6_2nbfa"]

[sub_resource type="Resource" id="Resource_vkdak"]
script = ExtResource("3_eliam")
color = Color(0.4, 0.694118, 1, 1)
controller = 1
spawn_index = -1

[sub_resource type="Resource" id="Resource_qgn77"]
script = ExtResource("3_52248")
players = Array[Resource]([SubResource("Resource_vkdak")])
visibility = 0
visible_player = 0

[sub_resource type="PlaneMesh" id="PlaneMesh_7oss6"]
resource_local_to_scene = true
material = ExtResource("4_d4g7r")
size = Vector2(50, 50)
center_offset = Vector3(25, 0, 25)

[sub_resource type="ViewportTexture" id="ViewportTexture_x0ak8"]
viewport_path = NodePath("FogOfWar/CombinedViewport")

[sub_resource type="ShaderMaterial" id="ShaderMaterial_g62le"]
resource_local_to_scene = true
render_priority = 2
shader = ExtResource("5_arsbx")
shader_parameter/color = Color(0, 0, 0, 1)
shader_parameter/texture_units_per_world_unit = 2
shader_parameter/debug_texture_view = false
shader_parameter/world_visibility_texture = SubResource("ViewportTexture_x0ak8")

[sub_resource type="ViewportTexture" id="ViewportTexture_pww1d"]
viewport_path = NodePath("FogOfWar/CombinedViewport")

[sub_resource type="ShaderMaterial" id="ShaderMaterial_s3a4s"]
resource_local_to_scene = true
shader = ExtResource("6_2nbfa")
shader_parameter/reference_texture = SubResource("ViewportTexture_pww1d")

[sub_resource type="ViewportTexture" id="ViewportTexture_un6ry"]
viewport_path = NodePath("HUD/MarginContainer/Minimap/MarginContainer/MinimapViewport")

[node name="Match" instance=ExtResource("1_e8lxh")]
script = ExtResource("2_2ib5j")
allow_resources_deficit_spending = true
settings = SubResource("Resource_qgn77")
map_to_load_and_plug = ExtResource("3_dldkj")

[node name="Terrain" parent="Map/Geometry" index="1"]
mesh = SubResource("PlaneMesh_7oss6")

[node name="ScreenOverlay" parent="FogOfWar" index="1"]
material_override = SubResource("ShaderMaterial_g62le")

[node name="FogOfWarMask" parent="HUD/MarginContainer/Minimap/MarginContainer/MinimapViewport" index="3"]
material = SubResource("ShaderMaterial_s3a4s")

[node name="MinimapTextureRect" parent="HUD/MarginContainer/Minimap/MarginContainer" index="1"]
texture = SubResource("ViewportTexture_un6ry")

[editable path="Map"]
20 changes: 16 additions & 4 deletions tests/manual/TestOneUnit.tscn
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
[gd_scene load_steps=14 format=3 uid="uid://qhgcfjxa2ykg"]
[gd_scene load_steps=17 format=3 uid="uid://qhgcfjxa2ykg"]

[ext_resource type="PackedScene" uid="uid://camns8fqod8d4" path="res://source/match/Match.tscn" id="1_fe2sx"]
[ext_resource type="Script" path="res://tests/manual/Match.gd" id="2_sflin"]
[ext_resource type="Script" path="res://source/model/PlayerSettings.gd" id="3_3drrn"]
[ext_resource type="Script" path="res://source/model/MatchSettings.gd" id="4_go41p"]
[ext_resource type="Material" uid="uid://co8vfcoqqs5i8" path="res://source/match/resources/materials/terrain.material.tres" id="5_x8bni"]
[ext_resource type="Shader" path="res://source/shaders/2d/white_transparent.gdshader" id="8_fifyq"]
[ext_resource type="PackedScene" uid="uid://i58ffvwxbbwm" path="res://source/match/units/Tank.tscn" id="10_tqe5i"]
[ext_resource type="Shader" path="res://source/shaders/3d/simple_fog_of_war.gdshader" id="12_ke5ti"]

Expand Down Expand Up @@ -38,9 +39,17 @@ shader_parameter/texture_units_per_world_unit = 2
shader_parameter/debug_texture_view = false
shader_parameter/world_visibility_texture = SubResource("ViewportTexture_v46ss")

[sub_resource type="ViewportTexture" id="ViewportTexture_38cip"]
[sub_resource type="ViewportTexture" id="ViewportTexture_ll23y"]
viewport_path = NodePath("FogOfWar/CombinedViewport")

[sub_resource type="ShaderMaterial" id="ShaderMaterial_kdlxq"]
resource_local_to_scene = true
shader = ExtResource("8_fifyq")
shader_parameter/reference_texture = SubResource("ViewportTexture_ll23y")

[sub_resource type="ViewportTexture" id="ViewportTexture_hw1xu"]
viewport_path = NodePath("HUD/MarginContainer/Minimap/MarginContainer/MinimapViewport")

[node name="Match" instance=ExtResource("1_fe2sx")]
script = ExtResource("2_sflin")
allow_resources_deficit_spending = true
Expand All @@ -55,7 +64,10 @@ material_override = SubResource("ShaderMaterial_67o07")
[node name="Tank" parent="Units/Player0" index="0" instance=ExtResource("10_tqe5i")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 13.263, 0, 12.637)

[node name="TextureRect" parent="HUD/MarginContainer/Minimap/MarginContainer" index="0"]
texture = SubResource("ViewportTexture_38cip")
[node name="FogOfWarMask" parent="HUD/MarginContainer/Minimap/MarginContainer/MinimapViewport" index="3"]
material = SubResource("ShaderMaterial_kdlxq")

[node name="MinimapTextureRect" parent="HUD/MarginContainer/Minimap/MarginContainer/Control" index="0"]
texture = SubResource("ViewportTexture_hw1xu")

[editable path="Map"]
19 changes: 19 additions & 0 deletions tests/manual/maps/NonQuadratic.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[gd_scene load_steps=4 format=3 uid="uid://d8wcem3ievht"]

[ext_resource type="PackedScene" uid="uid://b7c1crf36x1li" path="res://source/match/Map.tscn" id="1_ame05"]
[ext_resource type="Material" uid="uid://co8vfcoqqs5i8" path="res://source/match/resources/materials/terrain.material.tres" id="2_cl6u2"]

[sub_resource type="PlaneMesh" id="PlaneMesh_0gpme"]
resource_local_to_scene = true
material = ExtResource("2_cl6u2")
size = Vector2(100, 50)
center_offset = Vector3(50, 0, 25)

[node name="Map" instance=ExtResource("1_ame05")]
size = Vector2(100, 50)

[node name="Terrain" parent="Geometry" index="1"]
mesh = SubResource("PlaneMesh_0gpme")

[node name="Marker3D" type="Marker3D" parent="SpawnPoints" index="0"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 20, 0, 18)

0 comments on commit 8124e11

Please sign in to comment.