diff --git a/projects/hollywoo-flixel/src/hollywoo_flixel/FlxDirector.hx b/projects/hollywoo-flixel/src/hollywoo_flixel/FlxDirector.hx index 8c3fcf04..7408f59b 100644 --- a/projects/hollywoo-flixel/src/hollywoo_flixel/FlxDirector.hx +++ b/projects/hollywoo-flixel/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/projects/hollywoo-flixel/src/hollywoo_flixel/FlxDirector.kiss b/projects/hollywoo-flixel/src/hollywoo_flixel/FlxDirector.kiss index 1be98a8a..baf0e15d 100644 --- a/projects/hollywoo-flixel/src/hollywoo_flixel/FlxDirector.kiss +++ b/projects/hollywoo-flixel/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/projects/hollywoo-flixel/src/hollywoo_flixel/FlxMovie.hx b/projects/hollywoo-flixel/src/hollywoo_flixel/FlxMovie.hx index 423fd9b9..8250dd01 100644 --- a/projects/hollywoo-flixel/src/hollywoo_flixel/FlxMovie.hx +++ b/projects/hollywoo-flixel/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 {} diff --git a/projects/hollywoo/src/hollywoo/Director.hx b/projects/hollywoo/src/hollywoo/Director.hx index 28ba3f92..44dba5bb 100644 --- a/projects/hollywoo/src/hollywoo/Director.hx +++ b/projects/hollywoo/src/hollywoo/Director.hx @@ -9,10 +9,12 @@ enum Appearance { typedef Continuation = Void -> Void; -interface Director { +interface Director { function showScene(scene:Scene, appearance:Appearance, cc:Continuation):Void; function showCharacter(character:Character, appearance:Appearance, cc:Continuation):Void; - function playSound(sound:Sound, volumeMod:Float, cc:Continuation):Void; + function playSound(sound:Sound, volumeMod:Float, waitForEnd:Bool, cc:Continuation):Void; + function playSong(song:Song, volumeMod:Float, loop:Bool, waitForEnd:Bool, cc:Continuation):Void; + function stopSong():Void; function waitForInputOrDelay(delaySeconds:Float, cc:Continuation):Void; function showDialog(speakerName:String, type:SpeechType, wryly:String, dialog:String, cc:Continuation):Void; } diff --git a/projects/hollywoo/src/hollywoo/Movie.hx b/projects/hollywoo/src/hollywoo/Movie.hx index 0ba48614..c1f83a3a 100644 --- a/projects/hollywoo/src/hollywoo/Movie.hx +++ b/projects/hollywoo/src/hollywoo/Movie.hx @@ -1,5 +1,6 @@ package hollywoo; +import haxe.Constraints.Function; import kiss.AsyncEmbeddedScript; import kiss.Prelude; import hollywoo.Scene; @@ -9,7 +10,7 @@ import hollywoo.Director; * Model/controller of a Hollywoo film, and main execution script */ @:build(kiss.Kiss.build()) -class Movie extends AsyncEmbeddedScript { +class Movie extends AsyncEmbeddedScript { // TODO for some reason this wasn't working when declared in Movie.kiss: // Mutable representation of frames in time: var scenes:Map> = []; diff --git a/projects/hollywoo/src/hollywoo/Movie.kiss b/projects/hollywoo/src/hollywoo/Movie.kiss index 88aaf22b..e4b80477 100644 --- a/projects/hollywoo/src/hollywoo/Movie.kiss +++ b/projects/hollywoo/src/hollywoo/Movie.kiss @@ -2,6 +2,7 @@ (prop :Map sets (new Map)) (prop :Map actors (new Map)) (prop :Map sounds (new Map)) +(prop :Map songs (new Map)) // TODO for some reason this won't work when declared in Kiss syntax: // Mutable representation of frames in time: @@ -26,7 +27,7 @@ (defNew [ // "View" in the Model-View-Controller architecture: - &prop :Director director + &prop :Director director ] (super)) @@ -62,10 +63,31 @@ (assert isLoading) (dictSet sounds name s)) -(method playSound [name :Continuation cc &opt :Float volumeMod] +(method playSound [name :Continuation cc &opt :Float volumeMod :Bool waitForEnd] (set volumeMod (or volumeMod 1)) (assert (<= 0 volumeMod 1)) - (director.playSound (dictGet sounds name) volumeMod cc)) + (director.playSound (dictGet sounds name) volumeMod ?waitForEnd cc)) + +(method awaitPlaySound [name :Continuation cc &opt :Float volumeMod] + (playSound name cc volumeMod true)) + +(method newSong [name :Song song] + (assert isLoading) + (dictSet songs name song)) + +(method playSong [name :Continuation cc &opt :Float volumeMod :Bool loop :Bool waitForEnd] + (set volumeMod (or volumeMod 1)) + (assert (<= 0 volumeMod 1)) + (director.playSong (dictGet songs name) volumeMod ?loop ?waitForEnd cc)) + +(method awaitPlaySong [name :Continuation cc &opt :Float volumeMod] + (playSong name cc volumeMod false true)) + +(method loopSong [name :Continuation cc &opt :Float volumeMod] + (playSong name cc volumeMod true false)) + +(method stopSong [] + (director.stopSong)) (method newActor [name :Actor actor] (assert isLoading)