Game.gd 1.5 KB

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