1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- extends Camera2D
- 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()
- func move():
- if !get_tree().has_group("players"):
- return
- var running
- if get_tree().has_group("running"):
- running = get_tree().get_nodes_in_group("running")
- if !running or running.size() == 0:
- # Show every players
- var players = get_tree().get_nodes_in_group("players")
- center_on(players)
- else:
- # Show remaining players only
- center_on(running)
- 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)
- 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()
|