Game.gd 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. extends Node2D
  2. onready var cam = $MainCamera
  3. export var dist_max = 900
  4. export var zoom_rate = 0.001
  5. var levelNames = ["res://Levels/Lab.tscn", "res://Levels/Square.tscn"]
  6. var levelScenes = []
  7. var levelCurrentNode
  8. var levelIndex = -1
  9. var playerScene = preload("res://Player.tscn")
  10. func _ready():
  11. for k in range(levelNames.size()):
  12. levelScenes.append(null)
  13. func _process(_d):
  14. move_camera()
  15. func _unhandled_input(event):
  16. if event.is_action("ui_accept") and event.is_pressed():
  17. if get_tree().has_group("living"):
  18. var living = get_tree().get_nodes_in_group("living")
  19. if !living.empty():
  20. return
  21. start()
  22. func start():
  23. # Clear if required
  24. if (get_tree().has_group("players")):
  25. var players = get_tree().get_nodes_in_group("players")
  26. for p in players:
  27. p.queue_free()
  28. if (levelCurrentNode):
  29. levelCurrentNode.queue_free()
  30. # Select level
  31. levelIndex += 1
  32. if (levelIndex >= levelScenes.size()):
  33. levelIndex = 0
  34. if !levelScenes[levelIndex]:
  35. levelScenes[levelIndex] = load(levelNames[levelIndex])
  36. # Load level
  37. levelCurrentNode = levelScenes[levelIndex].instance()
  38. add_child(levelCurrentNode)
  39. # Create players
  40. var player
  41. # Player 1
  42. player = playerScene.instance()
  43. player.grid = levelCurrentNode
  44. player.turn_left_action = "player_1_turn_left"
  45. player.turn_right_action = "player_1_turn_right"
  46. player.position = Vector2(4 * 64 + 32, 8 * 64 + 32)
  47. add_child(player)
  48. # Player 2
  49. player = playerScene.instance()
  50. player.grid = levelCurrentNode
  51. player.turn_left_action = "player_2_turn_left"
  52. player.turn_right_action = "player_2_turn_right"
  53. player.position = Vector2(6 * 64 + 32, 8 * 64 + 32)
  54. add_child(player)
  55. # TODO Timer
  56. get_tree().call_group("players", "_on_game_start")
  57. func move_camera():
  58. if !get_tree().has_group("players"):
  59. return
  60. var living
  61. if get_tree().has_group("living"):
  62. living = get_tree().get_nodes_in_group("living")
  63. if !living or living.size() == 0:
  64. # Show every players
  65. var players = get_tree().get_nodes_in_group("players")
  66. center_on(players)
  67. else:
  68. # Show remaining players only
  69. center_on(living)
  70. func center_on(players):
  71. var position_accumulator = Vector2(0.0, 0.0)
  72. for p in players:
  73. position_accumulator += p.position
  74. cam.position = position_accumulator / players.size()
  75. var dist = 0
  76. for i in range(players.size() - 1):
  77. for j in range(i + 1, players.size()):
  78. dist = max(dist, players[i].position.distance_to(players[j].position))
  79. # Extend camera zoom if liners are far from each other
  80. var zoom = 1.0
  81. if dist > dist_max:
  82. zoom += (dist - dist_max) * zoom_rate
  83. cam.zoom = Vector2(zoom, zoom)