From 6f890cbd2a8b577feab039a4d2f35ff0713c2fef Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Mon, 1 Nov 2021 20:35:30 -0400 Subject: [PATCH] music for hollywoo-flixel --- src/hollywoo_flixel/FlxDirector.hx | 3 ++- src/hollywoo_flixel/FlxDirector.kiss | 29 +++++++++++++++++++++++----- src/hollywoo_flixel/FlxMovie.hx | 2 +- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/hollywoo_flixel/FlxDirector.hx b/src/hollywoo_flixel/FlxDirector.hx index 8c3fcf0..7408f59 100644 --- a/src/hollywoo_flixel/FlxDirector.hx +++ b/src/hollywoo_flixel/FlxDirector.hx @@ -13,6 +13,7 @@ import hollywoo_flixel.FlxMovie; import flixel.util.FlxColor; import flixel.text.FlxText; import flixel.system.FlxSound; +import flixel.util.FlxTimer; @:build(kiss.Kiss.build()) -class FlxDirector implements Director {} +class FlxDirector implements Director {} diff --git a/src/hollywoo_flixel/FlxDirector.kiss b/src/hollywoo_flixel/FlxDirector.kiss index 1be98a8..baf0e15 100644 --- a/src/hollywoo_flixel/FlxDirector.kiss +++ b/src/hollywoo_flixel/FlxDirector.kiss @@ -102,8 +102,27 @@ (cc) })) -(method :Void playSound [:FlxSound sound :Float volumeMod :Continuation cc] - // TODO preserve its original volume - (set sound.volume volumeMod) - (set sound.onComplete cc) - (sound.play)) +(method :Void playSound [:FlxSound sound :Float volumeMod :Bool waitForEnd :Continuation cc] + (let [originalVolume sound.volume + restoreOriginalVolume ->(set sound.volume originalVolume)] + (*= sound.volume volumeMod) + (set sound.onComplete + (if waitForEnd + ->{(restoreOriginalVolume) (cc)} + restoreOriginalVolume))) + (sound.play) + (unless waitForEnd (cc))) + +(prop &mut :FlxSound music) +(prop MUSIC_FADE_SEC 1) +(prop MUSIC_FADE_STEPS 10) +(method :Void playSong [:String song :Float volumeMod :Bool loop :Bool waitForEnd :Continuation cc] + (set music (FlxG.sound.play song 0 loop null true (if waitForEnd cc ->{}))) + (.start (new FlxTimer) + (/ MUSIC_FADE_SEC MUSIC_FADE_STEPS) + ->:Void _ (+= music.volume (/ volumeMod MUSIC_FADE_STEPS)) + MUSIC_FADE_STEPS) + (set music.persist true) + (unless waitForEnd (cc))) + +(method :Void stopSong [] (when music (music.stop))) diff --git a/src/hollywoo_flixel/FlxMovie.hx b/src/hollywoo_flixel/FlxMovie.hx index 423fd9b..8250dd0 100644 --- a/src/hollywoo_flixel/FlxMovie.hx +++ b/src/hollywoo_flixel/FlxMovie.hx @@ -29,4 +29,4 @@ enum FlxScreenPosition { /** * Model/controller of a Hollywoo-Flixel film, and main execution script */ -class FlxMovie extends Movie {} +class FlxMovie extends Movie {}