Game.gd 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. var playerActionsLeft = ["p1_left", "p2_left", "p3_left"]
  11. var playerActionsRight = ["p1_right", "p2_right", "p3_right"]
  12. func _ready():
  13. for k in range(levelNames.size()):
  14. levelScenes.append(null)
  15. func _process(_d):
  16. move_camera()
  17. func _unhandled_input(event):
  18. if event.is_action("ui_accept") and event.is_pressed():
  19. if get_tree().has_group("living"):
  20. var living = get_tree().get_nodes_in_group("living")
  21. if !living.empty():
  22. return
  23. start()
  24. func start():
  25. # Clear if required
  26. if (get_tree().has_group("players")):
  27. var players = get_tree().get_nodes_in_group("players")
  28. for p in players:
  29. p.queue_free()
  30. if (levelCurrentNode):
  31. remove_child(levelCurrentNode)
  32. levelCurrentNode.queue_free()
  33. # Select level
  34. levelIndex += 1
  35. if (levelIndex >= levelScenes.size()):
  36. levelIndex = 0
  37. if !levelScenes[levelIndex]:
  38. levelScenes[levelIndex] = load(levelNames[levelIndex])
  39. # Load level
  40. levelCurrentNode = levelScenes[levelIndex].instance()
  41. add_child(levelCurrentNode)
  42. # Retrieve player spawn points
  43. var spawners = get_tree().get_nodes_in_group("spawn")
  44. # Create players
  45. var player
  46. for k in 3:
  47. player = playerScene.instance()
  48. player.grid = levelCurrentNode
  49. player.turn_left_action = playerActionsLeft[k]
  50. player.turn_right_action = playerActionsRight[k]
  51. player.position = spawners[k].position
  52. add_child(player)
  53. # TODO Timer
  54. get_tree().call_group("players", "_on_game_start")
  55. func move_camera():
  56. if !get_tree().has_group("players"):
  57. return
  58. var living
  59. if get_tree().has_group("living"):
  60. living = get_tree().get_nodes_in_group("living")
  61. if !living or living.size() == 0:
  62. # Show every players
  63. var players = get_tree().get_nodes_in_group("players")
  64. center_on(players)
  65. else:
  66. # Show remaining players only
  67. center_on(living)
  68. func center_on(players):
  69. var position_accumulator = Vector2(0.0, 0.0)
  70. for p in players:
  71. position_accumulator += p.position
  72. cam.position = position_accumulator / players.size()
  73. var dist = 0
  74. for i in range(players.size() - 1):
  75. for j in range(i + 1, players.size()):
  76. dist = max(dist, players[i].position.distance_to(players[j].position))
  77. # Extend camera zoom if liners are far from each other
  78. var zoom = 1.0
  79. if dist > dist_max:
  80. zoom += (dist - dist_max) * zoom_rate
  81. cam.zoom = Vector2(zoom, zoom)