Advertisement
rzuf

Untitled

Oct 16th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. extends Node
  2.  
  3. """
  4. Simple music player with cross-fade support.
  5. Make a scene, attach this script to it and put it into autoload.
  6. Every single song shout be a child of the root node, like this:
  7. https://imgur.com/a/f3bmzWq
  8.  
  9. """
  10.  
  11. const FADE_SPEED = 12.0
  12.  
  13. var _songs : Array
  14. var _current_song = null
  15.  
  16. func _ready():
  17. _songs = get_children()
  18.  
  19. func _process(delta):
  20. for song in _songs:
  21. if song == _current_song:
  22. song.volume_db = move_towards(song.volume_db, linear2db(1.0), delta * FADE_SPEED)
  23. elif song.playing:
  24. song.volume_db = move_towards(song.volume_db, linear2db(0.1), delta * FADE_SPEED)
  25. if song.volume_db <= linear2db(0.1):
  26. song.stop()
  27.  
  28. func play(song_name, fade = true):
  29. var new_song = find_node(song_name)
  30. if _current_song == new_song:
  31. return
  32.  
  33. _current_song = new_song
  34. if _current_song != null:
  35. if fade:
  36. _current_song.volume_db = linear2db(0.1)
  37. else:
  38. _current_song.volume_db = linear2db(1.0)
  39. _current_song.play()
  40.  
  41. func stop(fade = true):
  42. if !fade && _current_song != null:
  43. _current_song.stop()
  44. _current_song.volume_db = 0.1
  45. _current_song = null
  46.  
  47. func move_towards(from, to, max_step):
  48. if to > from:
  49. from += max_step
  50. if from > to:
  51. from = to
  52. elif to < from:
  53. from -= max_step
  54. if from < to:
  55. from = to
  56. return from
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement