12345678910111213141516171819202122232425262728293031323334353637383940 |
- extends Node2D
- onready var cam = $Grid/Camera1
- var players = []
- var dist_max = 900
- var zoom_rate = 0.001
- func _ready():
- players.append($Grid/Player1)
- players.append($Grid/Player2)
- func _process(d):
- center_camera()
- func center_camera():
- var living_players = []
- var position_accumulator = Vector2(0.0, 0.0)
- 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)
|