about summary refs log tree commit diff stats
path: root/client/Archipelago/minimap.gd
blob: 15993fbc0aea8475e4fefe89e5f1eea8f8efc831 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
extends CanvasLayer

var player
var drawer
var sprite
var label

var cell_left
var cell_top
var cell_right
var cell_bottom
var center_x_min
var center_x_max
var center_y_min
var center_y_max


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)
	sv.disable_3d = true
	svc.add_child(sv)

	var background_color = Color.WHITE

	var world_env = get_tree().get_root().get_node("scene/WorldEnvironment")
	if world_env != null and world_env.environment != null:
		if world_env.environment.background_mode == Environment.BG_COLOR:
			background_color = world_env.environment.background_color
		elif (
			world_env.environment.background_mode == Environment.BG_SKY
			and world_env.environment.sky != null
			and world_env.environment.sky.sky_material != null
		):
			var sky = world_env.environment.sky.sky_material
			if sky is PhysicalSkyMaterial:
				background_color = sky.ground_color
			elif sky is ProceduralSkyMaterial:
				background_color = sky.sky_top_color

	var background_image = Image.create_empty(256, 256, false, Image.FORMAT_RGBA8)
	background_image.fill(background_color)

	var background_texture = ImageTexture.create_from_image(background_image)
	var background = Sprite2D.new()
	background.texture = background_texture
	background.texture_filter = CanvasItem.TEXTURE_FILTER_NEAREST
	background.centered = false
	sv.add_child(background)

	drawer = Node2D.new()
	sv.add_child(drawer)

	var gridmap = get_tree().get_root().get_node("scene/GridMap")
	if gridmap == null:
		visible = false
		return

	cell_left = 0
	cell_top = 0
	cell_right = 0
	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 cell_width = cell_right - cell_left + 1
	var cell_height = cell_bottom - cell_top + 1

	var heights = {}

	var rendered = Image.create_empty(cell_width, cell_height, false, Image.FORMAT_RGBA8)
	rendered.fill(Color.TRANSPARENT)

	for pos in gridmap.get_used_cells():
		var in_plane = Vector2i(pos.x, pos.z)

		if in_plane in heights and heights[in_plane] > pos.y:
			continue

		heights[in_plane] = pos.y

		var cell_item = gridmap.get_cell_item(pos)
		var mesh = gridmap.mesh_library.get_item_mesh(cell_item)
		var material = mesh.surface_get_material(0)
		var color = material.albedo_color

		rendered.set_pixel(pos.x - cell_left, pos.z - cell_top, color)

	var image_texture = ImageTexture.create_from_image(rendered)
	sprite = Sprite2D.new()
	sprite.texture = image_texture
	sprite.texture_filter = CanvasItem.TEXTURE_FILTER_NEAREST
	sprite.scale = Vector2(2, 2)
	sprite.centered = false
	drawer.add_child(sprite)

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

	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)

	center_x_min = 0
	center_x_max = cell_width - 128
	center_y_min = 0
	center_y_max = cell_height - 128

	if center_x_max < center_x_min:
		center_x_min = (center_x_min + center_x_max) / 2
		center_x_max = center_x_min

	if center_y_max < center_y_min:
		center_y_min = (center_y_min + center_y_max) / 2
		center_y_max = center_y_min


func _process(_delta):
	if visible == false:
		return

	drawer.position.x = clamp(player.position.x - cell_left - 64, center_x_min, center_x_max) * -2
	drawer.position.y = clamp(player.position.z - cell_top - 64, center_y_min, center_y_max) * -2

	label.position.x = (player.position.x - cell_left) * 2 - 16
	label.position.y = (player.position.z - cell_top) * 2 - 16