123456789101112131415161718192021222324252627282930313233343536373839404142 |
- extends Camera2D
- export var dist_max = 900
- export var zoom_rate = 0.001
- func _process(_d):
- move()
- func move():
- if !get_tree().has_group("players"):
- return
- var living
- if get_tree().has_group("living"):
- living = get_tree().get_nodes_in_group("living")
- if !living or living.size() == 0:
- # Show every players
- var players = get_tree().get_nodes_in_group("players")
- center_on(players)
- else:
- # Show remaining players only
- center_on(living)
- func center_on(players):
- var position_accumulator = Vector2(0.0, 0.0)
- for p in players:
- position_accumulator += p.position
- position = position_accumulator / players.size()
- var dist = 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))
- # 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
- zoom = Vector2(new_zoom, new_zoom)
|