Skip to content

Commit

Permalink
Sync units & camera on minimap, #63
Browse files Browse the repository at this point in the history
  • Loading branch information
Scony committed Jul 30, 2023
1 parent 0296913 commit f525130
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 6 deletions.
1 change: 0 additions & 1 deletion source/FeatureFlags.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@

[node name="FeatureFlags" type="Node"]
script = ExtResource("1_wihie")
show_minimap = false
god_mode = true
1 change: 1 addition & 0 deletions source/match/Match.gd
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func _plug_custom_map(custom_map):
remove_child(map)
map.queue_free()
map = custom_map
map.owner = self


func _create_players():
Expand Down
7 changes: 7 additions & 0 deletions source/match/Match.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,13 @@ grow_horizontal = 2
grow_vertical = 2
color = Color(0, 0, 0, 1)

[node name="CameraIndicator" type="Line2D" parent="HUD/MarginContainer/Minimap/MarginContainer/MinimapViewport"]
points = PackedVector2Array(5, 5, 5, 30, 30, 30, 30, 5, 5, 5)
width = 1.0
begin_cap_mode = 1
end_cap_mode = 1
antialiased = true

[node name="MinimapTextureRect" type="TextureRect" parent="HUD/MarginContainer/Minimap/MarginContainer"]
layout_mode = 2
texture = SubResource("ViewportTexture_8q44e")
Expand Down
92 changes: 91 additions & 1 deletion source/match/hud/Minimap.gd
Original file line number Diff line number Diff line change
@@ -1,7 +1,97 @@
extends PanelContainer

const GROUND_LEVEL_PLANE = Plane(Vector3.UP, 0)
const MINIMAP_PIXELS_PER_WORLD_METER = 2

var _unit_to_corresponding_node_mapping = {}

@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")


func _ready():
if not FeatureFlags.show_minimap:
queue_free()
find_child("MinimapViewport").size = find_parent("Match").find_child("Map").size
_remove_dummy_nodes()
await _match.ready # make sure Match is ready as it may change map on setup
find_child("MinimapViewport").size = (
_match.find_child("Map").size * MINIMAP_PIXELS_PER_WORLD_METER
)


func _physics_process(_delta):
_sync_real_units_with_minimap_representations()
_update_camera_indicator()


func _remove_dummy_nodes():
for dummy_node in find_children("Dummy*"):
dummy_node.queue_free()


func _sync_real_units_with_minimap_representations():
var units_synced = {}
var units_to_sync = get_tree().get_nodes_in_group("units")
for unit in units_to_sync:
if not unit.visible:
continue
units_synced[unit] = 1
if not _unit_is_mapped(unit):
_map_unit(unit)
_sync_unit(unit)
for mapped_unit in _unit_to_corresponding_node_mapping:
if not mapped_unit in units_synced:
_cleanup_mapping(mapped_unit)


func _unit_is_mapped(unit):
return unit in _unit_to_corresponding_node_mapping


func _map_unit(unit):
var node_representing_unit = ColorRect.new()
node_representing_unit.size = Vector2(3, 3)
_viewport_background.add_sibling(node_representing_unit)
node_representing_unit.pivot_offset = node_representing_unit.size / 2.0
_unit_to_corresponding_node_mapping[unit] = node_representing_unit


func _sync_unit(unit):
var unit_pos_3d = unit.global_transform.origin
var unit_pos_2d = Vector2(unit_pos_3d.x, unit_pos_3d.z) * MINIMAP_PIXELS_PER_WORLD_METER
_unit_to_corresponding_node_mapping[unit].position = unit_pos_2d
_unit_to_corresponding_node_mapping[unit].color = unit.player.color


func _cleanup_mapping(unit):
_unit_to_corresponding_node_mapping[unit].queue_free()
_unit_to_corresponding_node_mapping.erase(unit)


func _update_camera_indicator():
var viewport = get_viewport()
var camera = viewport.get_camera_3d()
var camera_corners = [
Vector2.ZERO,
Vector2(0, viewport.size.y),
viewport.size,
Vector2(viewport.size.x, 0),
Vector2.ZERO
]
for index in range(camera_corners.size()):
var corner_mapped_to_3d_position_on_ground_level = (
GROUND_LEVEL_PLANE.intersects_ray(
camera.project_ray_origin(camera_corners[index]),
camera.project_ray_normal(camera_corners[index])
)
* MINIMAP_PIXELS_PER_WORLD_METER
)
_camera_indicator.set_point_position(
index,
Vector2(
corner_mapped_to_3d_position_on_ground_level.x,
corner_mapped_to_3d_position_on_ground_level.z
)
)
20 changes: 16 additions & 4 deletions tests/manual/TestPlayerVsAI.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=24 format=3 uid="uid://dvqwcc562qxs5"]
[gd_scene load_steps=27 format=3 uid="uid://dvqwcc562qxs5"]

[ext_resource type="PackedScene" uid="uid://camns8fqod8d4" path="res://source/match/Match.tscn" id="1_cpaaw"]
[ext_resource type="Script" path="res://tests/manual/Match.gd" id="2_mi5ae"]
Expand All @@ -16,6 +16,7 @@
[ext_resource type="PackedScene" uid="uid://bf0r3fovbvf1m" path="res://source/match/units/Worker.tscn" id="11_mhynl"]
[ext_resource type="Shader" path="res://source/shaders/3d/simple_fog_of_war.gdshader" id="12_cnst8"]
[ext_resource type="PackedScene" uid="uid://1qlllsy6h5tc" path="res://source/match/players/simple-clairvoyant-ai/SimpleClairvoyantAI.tscn" id="15_6w32d"]
[ext_resource type="Shader" path="res://source/shaders/2d/white_transparent.gdshader" id="16_0dnxm"]

[sub_resource type="Resource" id="Resource_sm6ga"]
script = ExtResource("3_8b007")
Expand Down Expand Up @@ -53,9 +54,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_26xhd"]
viewport_path = NodePath("FogOfWar/CombinedViewport")

[sub_resource type="ShaderMaterial" id="ShaderMaterial_ummkj"]
resource_local_to_scene = true
shader = ExtResource("16_0dnxm")
shader_parameter/reference_texture = SubResource("ViewportTexture_26xhd")

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

[node name="Match" instance=ExtResource("1_cpaaw")]
script = ExtResource("2_mi5ae")
allow_resources_deficit_spending = false
Expand Down Expand Up @@ -94,8 +103,11 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 17.2469, 0, 5.45996)
[node name="Worker" parent="Units/Player0" index="8" instance=ExtResource("11_mhynl")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 11.5856, 0, 14.6178)

[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_ummkj")

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

[node name="Placeholder" type="Node" parent="Players" index="0"]

Expand Down

0 comments on commit f525130

Please sign in to comment.