Game.gd 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. extends Node2D
  2. onready var cam = $MainCamera
  3. onready var level = $Grid
  4. var players = []
  5. var dist_max = 900
  6. var zoom_rate = 0.001
  7. var playerScene = preload("res://Player.tscn")
  8. func _ready():
  9. var player
  10. # Player 1
  11. player = playerScene.instance()
  12. player.grid = level
  13. player.turn_left_action = "player_1_turn_left"
  14. player.turn_right_action = "player_1_turn_right"
  15. player.position = Vector2(4 * 64 + 32, 8 * 64 + 32)
  16. players.append(player) # TODO : use groups
  17. add_child(player)
  18. func _process(d):
  19. center_camera()
  20. func center_camera():
  21. var living_players = []
  22. var position_accumulator = Vector2(0.0, 0.0)
  23. for p in players:
  24. if (p.is_alive()):
  25. living_players.append(p)
  26. position_accumulator += p.position
  27. if living_players.size() == 0:
  28. # Keep camera in the last position
  29. return
  30. cam.position = position_accumulator / living_players.size()
  31. var dist = 0
  32. for i in range(living_players.size() - 1):
  33. for j in range(i + 1, living_players.size()):
  34. dist = max(dist, living_players[i].position.distance_to(living_players[j].position))
  35. # Extend camera zoom if liners are far from each other
  36. var zoom = 1.0
  37. if dist > dist_max:
  38. zoom += (dist - dist_max) * zoom_rate
  39. cam.zoom = Vector2(zoom, zoom)