Game.gd 1.4 KB

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