Game.gd 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 position_accumulator = Vector2(0.0, 0.0)
  27. var players = get_tree().get_nodes_in_group("players")
  28. var living = get_tree().get_nodes_in_group("living")
  29. if living.size() == 0:
  30. # Keep camera in the last position
  31. return
  32. for p in living:
  33. position_accumulator += p.position
  34. cam.position = position_accumulator / living.size()
  35. var dist = 0
  36. for i in range(living.size() - 1):
  37. for j in range(i + 1, living.size()):
  38. dist = max(dist, living[i].position.distance_to(living[j].position))
  39. # Extend camera zoom if liners are far from each other
  40. var zoom = 1.0
  41. if dist > dist_max:
  42. zoom += (dist - dist_max) * zoom_rate
  43. cam.zoom = Vector2(zoom, zoom)