Level.gd 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. extends Node3D
  2. class_name Level
  3. var barrels_amount = 0
  4. var barrels_disposed = 0
  5. var barrels_finished = false
  6. var barrels_remaining = 0
  7. var puddles_amount = 0
  8. var puddles_cleaned = 0
  9. var puddles_finished = false
  10. var puddles_remaining = 0
  11. var movables_list
  12. var game_scene
  13. var debug_text_scene
  14. signal all_barrels_disposed
  15. signal all_puddles_cleaned
  16. signal level_finished
  17. func _init():
  18. add_to_group("level")
  19. #update_movable_list
  20. self.child_entered_tree.connect(Callable(self, "_on_child_entered_tree"))
  21. self.child_exiting_tree.connect(Callable(self, "_on_child_exited_tree"))
  22. func _on_child_entered_tree(node: Node):
  23. if node.is_in_group("movable"):
  24. update_movable_list()
  25. func _on_child_exited_tree(node: Node):
  26. if node.is_in_group("movable"):
  27. update_movable_list()
  28. func update_movable_list():
  29. movables_list = get_tree().get_nodes_in_group("movable")
  30. func _ready() -> void:
  31. game_scene = SceneManager.game_scene
  32. if game_scene != null:
  33. debug_text_scene = game_scene.debug_text_scene
  34. barrels_amount = get_tree().get_nodes_in_group("barrel").size()
  35. puddles_amount = get_tree().get_nodes_in_group("puddle").size()
  36. for barrel in get_tree().get_nodes_in_group("barrel"):
  37. barrel.barrel_disposed.connect(Callable(self, "_on_barrel_disposed"))
  38. for puddle in get_tree().get_nodes_in_group("puddle"):
  39. puddle.cleaned.connect(Callable(self, "_on_puddle_cleaned"))
  40. update_movable_list()
  41. print ("barrels found: " + str(barrels_amount))
  42. print ("puddles found: " + str(puddles_amount))
  43. barrels_finished = !has_barrels()
  44. puddles_finished = !has_puddles()
  45. barrels_remaining = barrels_amount
  46. puddles_remaining = puddles_amount
  47. for n in get_children():
  48. if n.is_class("GridMap"):
  49. if n.position != Vector3(0, -1.6, 0):
  50. print ("WARNING: gridmap: " + str(n.name + " has bad positioning - pos: " + str(n.position)))
  51. if debug_text_scene != null:
  52. debug_text_scene.show_debug_text("a gridmap is not aligned, see console output for details, that can lead to problems with the player and other movable objects not able to move")
  53. func new_puddle_added():
  54. puddles_amount += 1
  55. puddles_remaining +=1
  56. if not SceneManager.game_scene:
  57. return
  58. SceneManager.game_scene.update_labels()
  59. print ("new puddle added")
  60. puddles_finished = false
  61. func _on_puddle_cleaned() -> void:
  62. puddles_cleaned += 1
  63. print ("-- puddles cleaned: " + str(puddles_cleaned) + "/" + str(puddles_amount) + " --")
  64. puddles_remaining = puddles_amount - puddles_cleaned
  65. if puddles_cleaned == puddles_amount:
  66. emit_signal("all_barrels_disposed")
  67. puddles_finished = true
  68. check_all_goals()
  69. if not SceneManager.game_scene:
  70. return
  71. SceneManager.game_scene.update_labels()
  72. print(puddles_remaining)
  73. func _on_barrel_disposed():
  74. barrels_disposed += 1
  75. print ("-- barrels disposed: " + str(barrels_disposed) + "/" + str(barrels_amount) + " --")
  76. barrels_remaining = barrels_amount - barrels_disposed
  77. if barrels_disposed == barrels_amount:
  78. emit_signal("all_puddles_cleaned")
  79. barrels_finished = true
  80. check_all_goals()
  81. print (SceneManager)
  82. if not SceneManager.game_scene:
  83. return
  84. SceneManager.game_scene.update_labels()
  85. func has_barrels() -> bool:
  86. return barrels_amount > 0
  87. func has_puddles() -> bool:
  88. return puddles_amount > 0
  89. func check_all_goals():
  90. await get_tree().create_timer(0.1).timeout
  91. if barrels_finished and puddles_finished:
  92. print ("all finished")
  93. print(puddles_remaining)
  94. emit_signal("level_finished")
  95. func is_target_free(movable: GridBasedMovable, target_pos: Vector3) -> bool:
  96. if movable == null:
  97. return true
  98. for mv in movables_list:
  99. if mv != null and movable != mv:
  100. if (target_pos - mv.target_position).length() < 0.5:
  101. return false
  102. return true
  103. func find_object_at_position(movable: GridBasedMovable, target_pos: Vector3):
  104. if movable == null:
  105. return null
  106. for mv in movables_list:
  107. #print (mv)
  108. if mv != null and movable != mv:
  109. if (target_pos - mv.target_position).length() < 0.5:
  110. return mv
  111. return null