Parcourir la source

Automatically adjust zoom with any number of player

Camera is centered to see the remaining living players.
DricomDragon il y a 5 ans
Parent
commit
282602ceb1
2 fichiers modifiés avec 32 ajouts et 10 suppressions
  1. 29 10
      Game.gd
  2. 3 0
      Player.gd

+ 29 - 10
Game.gd

@@ -1,21 +1,40 @@
 extends Node2D
 
 onready var cam = $Grid/Camera1
-var player = []
+var players = []
 var dist_max = 900
 var zoom_rate = 0.001
 
 func _ready():
-	player.append($Grid/Player1)
-	player.append($Grid/Player2)
+	players.append($Grid/Player1)
+	players.append($Grid/Player2)
 
 func _process(d):
-	cam.position = 0.5 * (player[0].position + player[1].position)
+	center_camera()
 
-	var dist = (player[0].position).distance_to(player[1].position)
+func center_camera():
+	var living_players = []
+	var position_accumulator = Vector2(0.0, 0.0)
 
-	if dist < dist_max:
-		cam.zoom = Vector2(1.0, 1.0)
-	else:
-		var zoom = 1.0 + (dist - dist_max) * zoom_rate
-		cam.zoom = Vector2(zoom, zoom)
+	for p in players:
+		if (p.is_alive()):
+			living_players.append(p)
+			position_accumulator += p.position
+
+	if living_players.size() == 0:
+		# Keep camera in the last position
+		return
+
+	cam.position = position_accumulator / living_players.size()
+
+	var dist = 0
+	for i in range(living_players.size() - 1):
+		for j in range(i + 1, living_players.size()):
+			dist = max(dist, living_players[i].position.distance_to(living_players[j].position))
+
+	# Extend camera zoom if liners are far from each other
+	var zoom = 1.0
+	if dist > dist_max:
+		zoom += (dist - dist_max) * zoom_rate
+
+	cam.zoom = Vector2(zoom, zoom)

+ 3 - 0
Player.gd

@@ -114,3 +114,6 @@ func turn(dir:int):
 		diry += 1
 	else:
 		dirx -= 1
+
+func is_alive():
+	return alive