Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more functions and variables to FlxSoundGroup #3284

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 149 additions & 10 deletions flixel/sound/FlxSoundGroup.hx
Original file line number Diff line number Diff line change
@@ -1,29 +1,80 @@
package flixel.sound;

import flixel.util.FlxStringUtil;

/**
* A way of grouping sounds for things such as collective volume control
*/
class FlxSoundGroup
class FlxSoundGroup extends FlxBasic
{
/**
* The sounds in this group
*/
public var sounds:Array<FlxSound> = [];

/**
* The volume of this group
*/
public var volume(default, set):Float;


#if FLX_PITCH
/**
* The pitch of this group
*/
public var pitch(default, set):Float;
#end

/**
* The position of this group in milliseconds.
* If set while paused, changes only come into effect after a `resume()` call.
*/
public var time(default, set):Float;

/**
* The length of the *longest* sound in the group
*/
public var length(get, never):Float;

/**
* Whether or not the sound group is currently playing.
*/
public var playing(default, null):Bool;

/**
* Create a new sound group
* @param volume The initial volume of this group
*/
public function new(volume:Float = 1)
{
super();
this.volume = volume;
#if FLX_SOUND_SYSTEM
FlxG.signals.focusLost.add(onFocusLost);
FlxG.signals.focusGained.add(onFocus);
#end
}


override public function destroy()
{
for (sound in sounds)
sound.destroy();
super.destroy();
}

override public function kill():Void
{
super.kill();
for (sound in sounds)
sound.kill();
}

override public function update(elapsed:Float):Void
{
super.update(elapsed);
for (sound in sounds)
sound.update(elapsed);
}

/**
* Add a sound to this group, will remove the sound from any group it is currently in
* @param sound The sound to add to this group
Expand All @@ -36,7 +87,7 @@ class FlxSoundGroup
// remove from prev group
if (sound.group != null)
sound.group.sounds.remove(sound);

sounds.push(sound);
@:bypassAccessor
sound.group = this;
Expand All @@ -45,7 +96,7 @@ class FlxSoundGroup
}
return false;
}

/**
* Remove a sound from this group
* @param sound The sound to remove
Expand All @@ -63,7 +114,7 @@ class FlxSoundGroup
}
return false;
}

/**
* Call this function to pause all sounds in this group.
* @since 4.3.0
Expand All @@ -72,8 +123,31 @@ class FlxSoundGroup
{
for (sound in sounds)
sound.pause();
playing = false;
}


/**
* Call this function to pause all sounds in this group.
* @since 5.9.0
*/
public function play():Void
{
for (sound in sounds)
sound.play();
playing = true;
}

/**
* Call this function to pause all sounds in this group.
* @since 5.9.0
*/
public function stop():Void
{
for (sound in sounds)
sound.stop();
playing = false;
}

/**
* Unpauses all sounds in this group. Only works on sounds that have been paused.
* @since 4.3.0
Expand All @@ -82,15 +156,80 @@ class FlxSoundGroup
{
for (sound in sounds)
sound.resume();
playing = true;
}

function set_volume(volume:Float):Float
{
this.volume = volume;
for (sound in sounds)
{
sound.updateTransform();
sound.volume = volume;
}
return volume;
}

function set_time(time:Float):Float
{
this.time = time;
for (sound in sounds)
{
sound.time = time;
}
return time;
}

#if FLX_PITCH
function set_pitch(pitch:Float):Float
{
this.pitch = pitch;
for (sound in sounds)
{
sound.pitch = pitch;
}
return pitch;
}
#end

function get_time():Float
{
return time;
}

function get_length():Float
{
var maxLength:Float = 0.0;
for (sound in sounds)
{
if (sound.length > maxLength)
{
maxLength = sound.length;
}
}
return maxLength;
}

#if FLX_SOUND_SYSTEM
function onFocus():Void
{
for(sound in sounds)
@:privateAccess sound.onFocus();
}

function onFocusLost():Void
{
for(sound in sounds)
@:privateAccess sound.onFocusLost();
}
#end

override public function toString():String
{
return FlxStringUtil.getDebugString([
LabelValuePair.weak("playing", playing),
LabelValuePair.weak("time", time),
LabelValuePair.weak("length", length),
LabelValuePair.weak("volume", volume)
]);
}
}
Loading