about summary refs log tree commit diff stats
path: root/client/Archipelago/minimap.gd
blob: 2702bb166b15d1c3826fd1400548095f56ec81e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
extends CanvasLayer

var player
var camera
var label
var map_bounds = null


func _ready():
	player = get_tree().get_root().get_node("scene/player")

	var svc = SubViewportContainer.new()
	svc.anchor_left = 1.0
	svc.anchor_top = 1.0
	svc.anchor_right = 1.0
	svc.anchor_bottom = 1.0
	svc.offset_left = -320.0
	svc.offset_top = -320.0
	svc.offset_right = -64.0
	svc.offset_bottom = -64.0
	add_child(svc)

	var sv = SubViewport.new()
	sv.size = Vector2i(256, 256)
	svc.add_child(sv)

	camera = Camera3D.new()
	camera.position.y = 5
	camera.rotation_degrees.x = -90
	camera.projection = Camera3D.PROJECTION_ORTHOGONAL
	camera.size = 128.0
	camera.far = 10
	sv.add_child(camera)

	label = Label.new()
	label.theme = preload("res://assets/themes/baseUI.tres")
	label.add_theme_font_size_override("font_size", 32)
	label.text = "@"
	add_child(label)

	var gridmap = get_tree().get_root().get_node("scene/GridMap")
	if gridmap != null:
		var cell_left = 0
		var cell_top = 0
		var cell_right = 0
		var cell_bottom = 0

		for pos in gridmap.get_used_cells():
			if pos.x < cell_left:
				cell_left = pos.x
			if pos.x > cell_right:
				cell_right = pos.x
			if pos.z < cell_top:
				cell_top = pos.z
			if pos.z > cell_bottom:
				cell_bottom = pos.z

		var local_tl = gridmap.map_to_local(Vector3i(cell_left, 0, cell_top))
		var local_br = gridmap.map_to_local(Vector3i(cell_right, 0, cell_bottom))
		var global_tl = gridmap.to_global(local_tl)
		var global_br = gridmap.to_global(local_br)
		map_bounds = [
			min(global_tl.x, global_br.x) + 64,
			max(global_tl.x, global_br.x) - 64,
			min(global_tl.z, global_br.z) + 64,
			max(global_tl.z, global_br.z) - 64
		]

		if map_bounds[1] < map_bounds[0]:
			map_bounds[0] = (map_bounds[0] + map_bounds[1]) / 2
			map_bounds[1] = map_bounds[0]

		if map_bounds[3] < map_bounds[2]:
			map_bounds[2] = (map_bounds[2] + map_bounds[3]) / 2
			map_bounds[3] = map_bounds[2]

	if map_bounds == null:
		label.position.x = 1712.0
		label.position.y = 872.0
		#label.offset_right = 1743.0
		#label.offset_bottom = 907.0


func _process(_delta):
	if map_bounds == null:
		camera.position.x = player.position.x
		camera.position.z = player.position.z
	else:
		camera.position.x = clamp(player.position.x, map_bounds[0], map_bounds[1])
		camera.position.z = clamp(player.position.z, map_bounds[2], map_bounds[3])

		label.position.x = 1600 + (player.position.x - camera.position.x) * 2 + 128 - 16
		label.position.y = 760 + (player.position.z - camera.position.z) * 2 + 128 - 16