Browse Source

Fix players going out of camera field

DricomDragon 4 years ago
parent
commit
bd9479741f
1 changed files with 14 additions and 9 deletions
  1. 14 9
      Scripts/MainCamera.gd

+ 14 - 9
Scripts/MainCamera.gd

@@ -4,7 +4,8 @@ extends Camera2D
 export var zoom_border = 100
 
 var zoom_rate:float
-var dist_max:float
+var dist_max_x:float
+var dist_max_y:float
 var view:Viewport
 
 
@@ -42,24 +43,28 @@ func center_on(players):
 
 	position = position_accumulator / players.size()
 
-	# TODO : compute axis aligned distance instead
-	var dist = 0
+	# Compute axis aligned distances
+	var dist_x = 0
+	var dist_y = 0
 	for i in range(players.size() - 1):
 		for j in range(i + 1, players.size()):
-			dist = max(dist, players[i].position.distance_to(players[j].position))
+			dist_x = max(dist_x, abs(players[i].position.x - players[j].position.x))
+			dist_y = max(dist_y, abs(players[i].position.y - players[j].position.y))
 
 	# Extend camera zoom if liners are far from each other
 	var new_zoom = 1.0
-	if dist > dist_max:
-		new_zoom += (dist - dist_max) * zoom_rate
+	if dist_x > dist_max_x:
+		new_zoom += (dist_x - dist_max_x) * zoom_rate
+	if dist_y > dist_max_y:
+		new_zoom = max(new_zoom, 1.0 + (dist_y - dist_max_y) * 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
+	dist_max_x = view.size.x - 2 * zoom_border
+	dist_max_y = view.size.y - 2 * zoom_border
+	zoom_rate = 1.0 / min(dist_max_x, dist_max_y)
 
 
 func _on_size_changed():