1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- extends Camera2D
- # Handle zoom and move for player tracking
- export var zoom_border = 100
- var zoom_rate:float
- var dist_max_x:float
- var dist_max_y:float
- var view:Viewport
- 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()
- # 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_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_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():
- 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():
- update_zoom_reference()
|