Game.gd 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. if !levelScenes[levelIndex]:
  48. levelScenes[levelIndex] = load_level(levelNames[levelIndex])
  49. levelCurrentNode = levelScenes[levelIndex].instance()
  50. add_child(levelCurrentNode)
  51. # Retrieve player spawn points
  52. var spawners = get_tree().get_nodes_in_group("spawn")
  53. # Create players
  54. var player
  55. for k in 2:
  56. player = playerScene.instance()
  57. player.grid = levelCurrentNode
  58. player.turn_left_action = playerActionsLeft[k]
  59. player.turn_right_action = playerActionsRight[k]
  60. player.position = spawners[k].position
  61. player.rotation = spawners[k].rotation
  62. add_child(player)
  63. # Delay start
  64. timer.start()
  65. func _on_game_start():
  66. get_tree().call_group("players", "_on_game_start")