Game.gd 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. move_camera()
  25. func move_camera():
  26. var living = get_tree().get_nodes_in_group("living")
  27. if living.size() == 0:
  28. # Show every players
  29. var players = get_tree().get_nodes_in_group("players")
  30. center_on(players)
  31. else:
  32. # Show remaining players only
  33. center_on(living)
  34. func center_on(players):
  35. var position_accumulator = Vector2(0.0, 0.0)
  36. for p in players:
  37. position_accumulator += p.position
  38. cam.position = position_accumulator / players.size()
  39. var dist = 0
  40. for i in range(players.size() - 1):
  41. for j in range(i + 1, players.size()):
  42. dist = max(dist, players[i].position.distance_to(players[j].position))
  43. # Extend camera zoom if liners are far from each other
  44. var zoom = 1.0
  45. if dist > dist_max:
  46. zoom += (dist - dist_max) * zoom_rate
  47. cam.zoom = Vector2(zoom, zoom)