MainCamera.gd 983 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. extends Camera2D
  2. export var dist_max = 900
  3. export var zoom_rate = 0.001
  4. func _process(_d):
  5. move()
  6. func move():
  7. if !get_tree().has_group("players"):
  8. return
  9. var living
  10. if get_tree().has_group("living"):
  11. living = get_tree().get_nodes_in_group("living")
  12. if !living or living.size() == 0:
  13. # Show every players
  14. var players = get_tree().get_nodes_in_group("players")
  15. center_on(players)
  16. else:
  17. # Show remaining players only
  18. center_on(living)
  19. func center_on(players):
  20. var position_accumulator = Vector2(0.0, 0.0)
  21. for p in players:
  22. position_accumulator += p.position
  23. position = position_accumulator / players.size()
  24. var dist = 0
  25. for i in range(players.size() - 1):
  26. for j in range(i + 1, players.size()):
  27. dist = max(dist, players[i].position.distance_to(players[j].position))
  28. # Extend camera zoom if liners are far from each other
  29. var new_zoom = 1.0
  30. if dist > dist_max:
  31. new_zoom += (dist - dist_max) * zoom_rate
  32. zoom = Vector2(new_zoom, new_zoom)