Game.gd 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. extends Node
  2. var current_level
  3. var current_level_scene
  4. var level_scene_instance
  5. var player_scene
  6. var is_finished
  7. @onready var level_done_screen = $LevelDoneScreen
  8. @onready var pause_screen = $PauseScreen
  9. @onready var info_box_scene = $InfoBox
  10. @onready var debug_text_scene = $LevelDebugText
  11. @onready var barrel_counter_scene = $HUD/HBoxContainer/BarrelCounter
  12. @onready var puddle_counter_scene = $HUD/HBoxContainer/PuddleCounter
  13. @onready var health_counter_scene = $HUD/HBoxContainer/HealthCounter
  14. @onready var bomb_counter_scene = $HUD/HBoxContainer/BombCounterUI
  15. func _ready() -> void:
  16. print ("Game.. Ready")
  17. set_process(true)
  18. level_done_screen.visible = false
  19. barrel_counter_scene.visible = false
  20. puddle_counter_scene.visible = false
  21. health_counter_scene.visible = false
  22. bomb_counter_scene.visible = false
  23. bomb_counter_scene.label.text = "error"
  24. is_finished = false
  25. func _process(delta: float) -> void:
  26. if is_finished == false:
  27. if Input.is_action_just_pressed("ui_cancel"):
  28. pause_game(true)
  29. func load_level(level):
  30. print(level)
  31. current_level = level
  32. current_level_scene = load(current_level)
  33. #if current_level_scene == null:
  34. # print ("do nothing: level not found")
  35. # return
  36. level_scene_instance = current_level_scene.instantiate()
  37. level_scene_instance.level_finished.connect(Callable(self, "on_level_done"))
  38. add_child(level_scene_instance)
  39. barrel_counter_scene.visible = level_scene_instance.has_barrels()
  40. barrel_counter_scene.label.text = str(level_scene_instance.barrels_amount)
  41. puddle_counter_scene.visible = level_scene_instance.has_puddles()
  42. puddle_counter_scene.label.text = str(level_scene_instance.puddles_amount)
  43. player_scene = get_tree().get_first_node_in_group("player")
  44. assert(player_scene != null)
  45. health_counter_scene.visible = true
  46. health_counter_scene.get_node("HealthCounterLabel").text = str(player_scene.get_node("Health").max_health)
  47. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  48. func on_level_done():
  49. is_finished = true
  50. #wait a short time, that sounds can play to the end before pausing the game and showing the level done ui
  51. await get_tree().create_timer(0.15).timeout
  52. print ("-- level finished --")
  53. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  54. level_done_screen.visible = true
  55. get_tree().paused = true
  56. func update_labels():
  57. if level_scene_instance != null:
  58. barrel_counter_scene.label.text = str(level_scene_instance.barrels_remaining)
  59. puddle_counter_scene.label.text = str(level_scene_instance.puddles_remaining)
  60. if puddle_counter_scene.visible == false and level_scene_instance.has_puddles():
  61. puddle_counter_scene.visible = true
  62. health_counter_scene.get_node("HealthCounterLabel").text = str(player_scene.get_node("Health").health)
  63. func pause_game(paused: bool):
  64. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  65. pause_screen.visible = true
  66. pause_screen.just_paused = true
  67. get_tree().paused = true
  68. func update_bomb_ui():
  69. bomb_counter_scene.visible = player_scene.bomb_counter.has_bombs()
  70. bomb_counter_scene.label.text = str(player_scene.bomb_counter.bombs_count)