[MIRROR] Refactors and fixes sound_player [MDB IGNORE] (#11198)

* Refactors and fixes sound_player (#64443)

idk if this is considered a refactor since its not creating a new component, so remove my refactor label if you believe so.

Overhauls how sound_player works, and makes it actually work.

* Refactors and fixes sound_player

Co-authored-by: John Willard <53777086+JohnFulpWillard@users.noreply.github.com>
This commit is contained in:
SkyratBot
2022-01-31 23:24:59 -05:00
committed by GitHub
co-authored by John Willard
parent 0edc27b4c0
commit 7f5e526bb1
4 changed files with 42 additions and 45 deletions
+39
View File
@@ -0,0 +1,39 @@
/**
* Sound Player component
*
* Component that will play a sound upon recieving some signal
*/
/datum/component/sound_player
///Volume of the sound when played
var/volume
///The list of sounds played, picked randomly.
var/list/sounds
///Uses left before the sound player deletes itself. If set to a negative number that will mean infinite uses.
var/uses
/datum/component/sound_player/Initialize(
volume = 30,
sounds = list('sound/items/bikehorn.ogg'),
uses = -1,
signal_list = list(COMSIG_ATOM_ATTACK_HAND),
)
src.volume = volume
src.sounds = sounds
src.uses = uses
RegisterSignal(parent, signal_list, .proc/play_sound)
/**
* Attempt to play the sound on parent
*
* If out of uses, will qdel itself.
*/
/datum/component/sound_player/proc/play_sound()
SIGNAL_HANDLER
playsound(parent, pick(sounds), volume, TRUE)
if(uses <= -1)
return
uses--
if(!uses)
qdel(src)
-42
View File
@@ -1,42 +0,0 @@
/*This is the sound_player component. It can be attached to any datum and register any signal to play the sound(s) you want, when you want. Used for the honk virus as an example
Usage :
target.AddComponent(/datum/component/sound_player, args)
Arguments :
custom_volume : Used to define a custom volume. Default : 30
custom_sounds : Used to define a list of custom sounds that will be picked at random when play_sound() is triggered. Default : list('sound/items/bikehorn.ogg')
amount : Used to define an amount of time the component will work before deleting itself. Default : -1
signal_or_sig_list : Used to register the signal(s) you want to play the sound when they are sent. Default : None
*/
/datum/component/sound_player
var/volume = 30
var/list/sounds = list('sound/items/bikehorn.ogg')
var/amount_left = -1
/datum/component/sound_player/Initialize(custom_volume, custom_sounds, amount, signal_or_sig_list)
if(!isnull(custom_volume))
volume = custom_volume
if(!isnull(custom_sounds))
sounds = custom_sounds
if(!isnull(amount))
amount_left = amount
RegisterSignal(parent, signal_or_sig_list, .proc/play_sound) //Registers all the signals in signal_or_sig_list.
/*play_sound() os the proc that actually plays the sound.
If amount_left is equal to -1, the component is infinite and will never delete itself.
*/
/datum/component/sound_player/proc/play_sound()
SIGNAL_HANDLER
playsound(parent, pick_weight(sounds), volume, TRUE)
switch(amount_left)
if(-1)
return
if(1) //Last use.
qdel(src)
return
else
amount_left --
+2 -2
View File
@@ -1083,8 +1083,8 @@ GLOBAL_LIST_EMPTY(PDAs)
sig_list += list(COMSIG_AIRLOCK_OPEN, COMSIG_AIRLOCK_CLOSE)
else
sig_list += list(COMSIG_ATOM_ATTACK_HAND)
target.AddComponent(/datum/component/sound_player, amount = (rand(15,20)), signal_or_sig_list = sig_list)
installed_cartridge.charges --
target.AddComponent(/datum/component/sound_player, uses = rand(15,20), signal_list = sig_list)
installed_cartridge.charges--
return TRUE
+1 -1
View File
@@ -792,7 +792,7 @@
#include "code\datums\components\sizzle.dm"
#include "code\datums\components\slippery.dm"
#include "code\datums\components\soulstoned.dm"
#include "code\datums\components\soundplayer.dm"
#include "code\datums\components\sound_player.dm"
#include "code\datums\components\spawner.dm"
#include "code\datums\components\spill.dm"
#include "code\datums\components\spinny.dm"