BooRaceHandler.gd 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. class_name BooRaceHandler
  2. extends Node
  3. @export var boo: Node2D = null
  4. static var boo_colour := 0
  5. @export var boo_block_times := [5, 4, 4.5, 3, 3]
  6. @export var level_id := 0
  7. static var current_level_id := 0
  8. @export var is_custom := false
  9. static var countdown_active := false
  10. static var best_times := [
  11. -1.0, -1.0, -1.0, -1.0,
  12. -1.0, -1.0, -1.0, -1.0
  13. ]
  14. static var cleared_boo_levels := "00000000"
  15. const SILENCE = preload("res://Assets/Audio/BGM/Silence.json")
  16. func _ready() -> void:
  17. SpeedrunHandler.show_timer = true
  18. SpeedrunHandler.timer = 0
  19. SpeedrunHandler.timer_active = false
  20. SpeedrunHandler.best_time = best_times[level_id]
  21. TimedBooBlock.can_tick = false
  22. current_level_id = level_id
  23. if is_custom == false:
  24. Global.current_game_mode = Global.GameMode.BOO_RACE
  25. do_countdown()
  26. func do_countdown() -> void:
  27. var old_music = Global.current_level.music
  28. Global.current_level.music = SILENCE
  29. countdown_active = true
  30. get_tree().paused = false
  31. await get_tree().physics_frame
  32. TimedBooBlock.can_tick = false
  33. $Animation.play("CountdownBeep")
  34. Global.can_time_tick = false
  35. for i in get_tree().get_nodes_in_group("Players"):
  36. i.state_machine.transition_to("Freeze")
  37. await get_tree().create_timer(3, false).timeout
  38. Global.can_time_tick = true
  39. for i in get_tree().get_nodes_in_group("Players"):
  40. i.state_machine.transition_to("Normal")
  41. $Timer.wait_time = boo_block_times[boo_colour]
  42. $Timer.start()
  43. countdown_active = false
  44. SpeedrunHandler.start_timer()
  45. boo.move_tween()
  46. TimedBooBlock.can_tick = true
  47. await get_tree().create_timer(0.5, false).timeout
  48. Global.current_level.music = old_music
  49. func tally_time() -> void:
  50. pass
  51. func player_win_race() -> void:
  52. SpeedrunHandler.run_finished()
  53. run_best_time_check()
  54. TimedBooBlock.can_tick = false
  55. if int(BooRaceHandler.cleared_boo_levels[level_id]) <= BooRaceHandler.boo_colour:
  56. BooRaceHandler.cleared_boo_levels[level_id] = str(BooRaceHandler.boo_colour + 1)
  57. print(BooRaceHandler.cleared_boo_levels)
  58. SaveManager.write_save(Global.current_campaign)
  59. boo.flag_die()
  60. if cleared_boo_levels.contains("0") == false:
  61. match Global.current_campaign:
  62. "SMB1": Global.unlock_achievement(Global.AchievementID.SMB1_BOO)
  63. "SMBLL": Global.unlock_achievement(Global.AchievementID.SMBLL_BOO)
  64. "SMBS": Global.unlock_achievement(Global.AchievementID.SMBS_BOO)
  65. if cleared_boo_levels == "55555555":
  66. match Global.current_campaign:
  67. "SMB1": Global.unlock_achievement(Global.AchievementID.SMB1_GOLD_BOO)
  68. "SMBLL": Global.unlock_achievement(Global.AchievementID.SMBLL_GOLD_BOO)
  69. "SMBS": Global.unlock_achievement(Global.AchievementID.SMBS_GOLD_BOO)
  70. await tree_exiting
  71. if boo_colour < 4:
  72. boo_colour += 1
  73. func run_best_time_check() -> void:
  74. if SpeedrunHandler.timer <= best_times[level_id] or best_times[level_id] < 0:
  75. best_times[level_id] = SpeedrunHandler.timer
  76. func _exit_tree() -> void:
  77. countdown_active = false
  78. func on_timeout() -> void:
  79. if boo.moving:
  80. boo.play_laugh_animation()
  81. AudioManager.play_global_sfx("boo_laugh")
  82. await get_tree().create_timer(1, false).timeout
  83. get_tree().call_group("BooSwitchBlocks", "on_boo_hit")