Game.gd 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. extends Node2D
  2. onready var cam = $MainCamera
  3. onready var timer = $StartTimer
  4. # Used to test a level in creation
  5. export (String) var levelToLoop
  6. var levelSceneToLoop
  7. export (Array, String) var levelNames = ["Square"]
  8. var levelScenes = []
  9. var levelCurrentNode
  10. var levelIndex = -1
  11. var playerScene = preload("res://Player.tscn")
  12. var playerActionsLeft = ["p1_left", "p2_left", "p3_left"]
  13. var playerActionsRight = ["p1_right", "p2_right", "p3_right"]
  14. func _ready():
  15. for _k in range(levelNames.size()):
  16. levelScenes.append(null)
  17. if levelToLoop:
  18. levelSceneToLoop = load_level(levelToLoop)
  19. if !levelSceneToLoop:
  20. push_error("Cant't load level " + levelToLoop)
  21. func load_level(name):
  22. var fullName = "res://Levels/" + name + ".tscn"
  23. return load(fullName)
  24. func _unhandled_input(event):
  25. if event.is_action("ui_accept") and event.is_pressed():
  26. if get_tree().has_group("living"):
  27. var living = get_tree().get_nodes_in_group("living")
  28. if !living.empty():
  29. return
  30. create_game()
  31. func create_game():
  32. # Clear if required
  33. if get_tree().has_group("players"):
  34. var players = get_tree().get_nodes_in_group("players")
  35. for p in players:
  36. p.queue_free()
  37. if levelCurrentNode:
  38. remove_child(levelCurrentNode)
  39. levelCurrentNode.queue_free()
  40. if levelToLoop:
  41. levelCurrentNode = levelSceneToLoop.instance()
  42. else:
  43. # Select next level
  44. levelIndex += 1
  45. if (levelIndex >= levelScenes.size()):
  46. levelIndex = 0
  47. levelScenes.shuffle()
  48. if !levelScenes[levelIndex]:
  49. levelScenes[levelIndex] = load_level(levelNames[levelIndex])
  50. levelCurrentNode = levelScenes[levelIndex].instance()
  51. add_child(levelCurrentNode)
  52. # Retrieve player spawn points
  53. var spawners = get_tree().get_nodes_in_group("spawn")
  54. spawners.shuffle()
  55. # Create players
  56. var player
  57. for k in 3:
  58. player = playerScene.instance()
  59. player.grid = levelCurrentNode
  60. player.turn_left_action = playerActionsLeft[k]
  61. player.turn_right_action = playerActionsRight[k]
  62. player.position = spawners[k].position
  63. player.rotation = spawners[k].rotation
  64. add_child(player)
  65. # Delay start
  66. timer.start()
  67. func _on_game_start():
  68. get_tree().call_group("players", "_on_game_start")