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