A game created for the Godot Wild Jam #21
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.3KB

  1. extends Node2D
  2. var _timer = 0.0
  3. var _level_running = true
  4. var _next_level = ""
  5. var _level_node = null
  6. func get_hours():
  7. return floor(_timer / 3600.0)
  8. func get_minutes():
  9. return floor(fmod((_timer / 60), 60.0))
  10. func get_seconds():
  11. return floor(fmod(_timer, 60.0))
  12. func load_and_play_music(src):
  13. if src != "":
  14. var fn = "res://Data/Sound/Music/" + src
  15. if File.new().file_exists(fn):
  16. $Music.stream = load(fn)
  17. $Music.play()
  18. func load_level(src):
  19. $Music.stop()
  20. _level_running = false
  21. get_node("Player").hide()
  22. var lvl = get_node("lvl")
  23. if _level_node != null:
  24. lvl.disconnect("exit_level", self, "_on_exit_level")
  25. lvl.remove_child(_level_node)
  26. _level_node.call_deferred("free")
  27. _level_node = null
  28. var lvlres = load("res://Data/Scenes/Levels/" + src)
  29. if lvlres:
  30. _level_node = lvlres.instance()
  31. lvl.add_child(_level_node)
  32. _level_node.connect("exit_level", self, "_on_exit_level")
  33. load_and_play_music(_level_node.get_level_music())
  34. reset()
  35. if _level_node.is_main_menu():
  36. $GUI/PlayerUI.hide()
  37. $GUI/Timer.hide()
  38. else:
  39. $GUI/PlayerUI.show()
  40. $GUI/Timer.show()
  41. _level_running = true
  42. func reset():
  43. if _level_node:
  44. get_tree().paused = true
  45. var p = get_node("Player")
  46. if _level_node.has_method("get_starting_position"):
  47. p.position = _level_node.get_starting_position()
  48. else:
  49. p.position = Vector2.ZERO
  50. p.reset()
  51. if _level_node.is_main_menu():
  52. p.mode = RigidBody2D.MODE_KINEMATIC
  53. else:
  54. p.mode = RigidBody2D.MODE_RIGID
  55. p.show()
  56. _timer = 0.0
  57. get_tree().paused = false
  58. func pause(enable = true):
  59. if get_tree().paused != enable:
  60. _level_running = !enable
  61. get_tree().paused = enable
  62. func _ready():
  63. set_physics_process(true)
  64. load_level("MainMenuLogo.tscn")
  65. func _animate_level_exit(exit):
  66. $Player.mode = RigidBody2D.MODE_KINEMATIC
  67. $ExitTween.interpolate_property($Player, "position", $Player.position, exit.position, 2.0, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT)
  68. $ExitTween.start()
  69. func _physics_process(delta):
  70. if _level_running:
  71. _timer += delta
  72. func _on_PM_Quit_pressed():
  73. get_tree().quit()
  74. func _on_exit_level(exit, body, next_level):
  75. if next_level != "":
  76. _next_level = next_level
  77. if exit != null:
  78. _animate_level_exit(exit)
  79. else:
  80. load_level(next_level)
  81. func _on_ExitTween_tween_all_completed():
  82. $Player.mode = RigidBody2D.MODE_RIGID
  83. if _next_level != "":
  84. load_level(_next_level)