瀏覽代碼

Improve screen zoom

DricomDragon 4 年之前
父節點
當前提交
9e048144c7
共有 1 個文件被更改,包括 19 次插入2 次删除
  1. 19 2
      MainCamera.gd

+ 19 - 2
MainCamera.gd

@@ -1,7 +1,16 @@
 extends Camera2D
 
-export var dist_max = 900
-export var zoom_rate = 0.001
+export var zoom_border = 100
+
+var zoom_rate:float
+
+var view:Viewport
+var dist_max:float
+
+func _ready():
+	view = get_tree().root
+	view.connect("size_changed", self, "_on_size_changed")
+	update_zoom_reference()
 
 func _process(_d):
 	move()
@@ -40,3 +49,11 @@ func center_on(players):
 		new_zoom += (dist - dist_max) * zoom_rate
 
 	zoom = Vector2(new_zoom, new_zoom)
+
+func update_zoom_reference():
+	var ref_length = min(view.size.x, view.size.y)
+	dist_max = ref_length - 2 * zoom_border
+	zoom_rate = 1.0 / dist_max
+
+func _on_size_changed():
+	update_zoom_reference()