MobSpawner.gd 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. class_name MobSpawner
  2. extends Path2D
  3. # Responsible of enemy life-cycle
  4. signal reset
  5. export (Array, PackedScene) var mob_list
  6. var _mob_storage: Array
  7. var _running_mob: Array
  8. func _ready():
  9. assert(!mob_list.empty())
  10. func spawn():
  11. $MobSpawnLocation.set_offset(randi())
  12. var newRotation = $MobSpawnLocation.rotation + rand_range(PI / 4, 3 * PI / 4)
  13. var Mob = _pick_a_mob()
  14. var newMob = Mob.instance()
  15. newMob.attack($MobSpawnLocation.position, newRotation)
  16. connect("reset", newMob, "_on_Spawner_reset")
  17. add_child(newMob)
  18. func _on_SpawnTimer_timeout():
  19. spawn()
  20. $SpawnTimer.start()
  21. func _on_NewMobTimer_timeout():
  22. _add_mob()
  23. if _has_remaining_mob_in_storage():
  24. $NewMobTimer.start()
  25. func _on_HUD_start_game():
  26. _reset()
  27. func _on_Main_game_started():
  28. _add_mob()
  29. $SpawnTimer.start()
  30. $NewMobTimer.start()
  31. func _on_Main_game_over():
  32. $SpawnTimer.stop()
  33. $NewMobTimer.stop()
  34. func _pick_a_mob() -> PackedScene:
  35. assert(!_running_mob.empty())
  36. _running_mob.shuffle()
  37. return _running_mob.front()
  38. func _add_mob():
  39. assert(!_mob_storage.empty())
  40. _running_mob.append(_mob_storage.pop_front())
  41. func _has_remaining_mob_in_storage() -> bool:
  42. return !_mob_storage.empty()
  43. func _reset():
  44. _mob_storage = mob_list.duplicate()
  45. _running_mob.clear()
  46. emit_signal("reset")