MusicNoteBlock.gd 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. extends NoteBlock
  2. const INTRUMENT_SFX := [preload("uid://dia0bsspwrqsn"), preload("uid://d2elbhakm1yfq"), preload("uid://vxox7t6qvyvu"), preload("uid://w44jys81bxjj"), preload("uid://b2lj4akov8ami"), preload("uid://c03nay4r4a2lm"), preload("uid://d0pdbnpfcm80i"), preload("uid://dodww1no4v6qh")]
  3. var pitch := 0.0
  4. var sfx_stream = null
  5. static var can_play := false
  6. @export var play_on_load := false
  7. @export_enum("Bass", "Flute", "Marimba", "Piano", "Rhodes", "Steel", "Trumpet", "Violin") var instrument := 0:
  8. set(value):
  9. sfx_stream = INTRUMENT_SFX[value]
  10. instrument = value
  11. play_sfx_preview()
  12. @export_enum("A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#") var note := 3:
  13. set(value):
  14. note = value
  15. pitch = get_pitch_scale()
  16. play_sfx_preview()
  17. @export_range(1, 5) var octave := 2:
  18. set(value):
  19. octave = value
  20. pitch = get_pitch_scale()
  21. play_sfx_preview()
  22. func _ready() -> void:
  23. await get_tree().create_timer(0.1, true).timeout
  24. can_play = true
  25. func _exit_tree() -> void:
  26. can_play = false
  27. func get_pitch_scale() -> float:
  28. var semitone_offset = (octave - 2) * 12 + (note - 3) # C4 is the base note (note index 3)
  29. return 2.0 ** (semitone_offset / 12.0)
  30. func _process(_delta: float) -> void:
  31. %Note.frame = note
  32. %Octave.frame = octave + 12
  33. func play_sfx_preview() -> void:
  34. if get_node_or_null("Instrument") != null and can_play:
  35. print($Instrument.pitch_scale)
  36. $Instrument.stream = sfx_stream
  37. $Instrument.pitch_scale = pitch
  38. $Instrument.play()
  39. func on_screen_entered() -> void:
  40. if play_on_load and LevelEditor.playing_level:
  41. play_sfx_preview()