Game.gd 924 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. extends Node2D
  2. onready var cam = $Grid/Camera1
  3. var players = []
  4. var dist_max = 900
  5. var zoom_rate = 0.001
  6. func _ready():
  7. players.append($Grid/Player1)
  8. players.append($Grid/Player2)
  9. func _process(d):
  10. center_camera()
  11. func center_camera():
  12. var living_players = []
  13. var position_accumulator = Vector2(0.0, 0.0)
  14. for p in players:
  15. if (p.is_alive()):
  16. living_players.append(p)
  17. position_accumulator += p.position
  18. if living_players.size() == 0:
  19. # Keep camera in the last position
  20. return
  21. cam.position = position_accumulator / living_players.size()
  22. var dist = 0
  23. for i in range(living_players.size() - 1):
  24. for j in range(i + 1, living_players.size()):
  25. dist = max(dist, living_players[i].position.distance_to(living_players[j].position))
  26. # Extend camera zoom if liners are far from each other
  27. var zoom = 1.0
  28. if dist > dist_max:
  29. zoom += (dist - dist_max) * zoom_rate
  30. cam.zoom = Vector2(zoom, zoom)