blib/lib/Games/Axmud/Obj/Sound.pm untested

filter: Statementstmt 0.0% 0 / 34 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 121 CC 4 SCAR 14.1
CC
4
Cov
0%
stop
12.0
new
6.0
session
2.0
CRAP 4.1
1 # Copyright (C) 2011-2024 A S Lewis
2 #
3 # This program is free software: you can redistribute it and/or modify it under the terms of the GNU
4 # General Public License as published by the Free Software Foundation, either version 3 of the
5 # License, or (at your option) any later version.
6 #
7 # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
8 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # General Public License for more details.
10 #
11 # You should have received a copy of the GNU General Public License along with this program. If not,
12 # see <http://www.gnu.org/licenses/>.
13 #
14 #
15 # Games::Axmud::Obj::Sound
16 # Handles playing of sounds other than Axmud sound effects (mostly those initiated by MSP)
17  
18 { package Games::Axmud::Obj::Sound;
19  
20 use strict;
21 use warnings;
22 # use diagnostics;
23  
24 use Glib qw(TRUE FALSE);
25  
26 our @ISA = qw(Games::Axmud);
27  
28 ##################
29 # Constructors
30  
31 sub new {
32  
33 # Called by GA::Client->playSoundFile
34 # Creates a new instance of the sound object
35 #
36 # Expected arguments
37 # $session - The calling GA::Session
38 # $number - Unique harness number for this session (not necessarily unique among all
39 # existing sessions), used to identify this object in its registry,
40 # GA::Session->soundHarnessHash
41 # $path - Full file path of the sound file that was played
42 # $harness - The IPC::Run harness used to play the sound
43 # $delFlag - Set to TRUE if the file $path should be deleted, when it has finished
44 # playing, set to FALSE (or 'undef') otherwise. (Used for MXP sound files
45 # which are converted from a world-specific file format, and which should
46 # be deleted after being played)
47 # $type - For MSP-generated sounds, the sound trigger type - 'sound' or 'music'. For
48 # anything else, set to 'other'
49 # $volume - For MSP-generated sounds, the volume (a value between 0-100)
50 # $repeat - For MSP-generated sounds, the number of repeats. 1 - play once, 2 - play
51 # twice (etc), -1 - play indefinitely
52 # $priority - For MSP-generated sounds, the sound priority (a value between 0-100)
53 # $continue - For MSP-generated sounds, a continue flag. 1 - sound continues playing if
54 # requested again; 0 - sound restarts if requested again
55 #
56 # Return values
57 # 'undef' on improper arguments
58 # Blessed reference to the newly-created object on success
59  
60 my (
61 $class, $session, $number, $path, $harness, $delFlag, $type, $volume, $repeat,
62 $priority, $continue, $check,
63 ) = @_;
64  
65 # Check for improper arguments
66 if (
67 ! defined $class || ! defined $session || ! defined $number || ! defined $path
68 || ! defined $harness || ! defined $delFlag || ! defined $type || ! defined $volume
69 || ! defined $repeat || ! defined $priority || ! defined $continue || defined $check
70 ) {
71 return $axmud::CLIENT->writeImproper($class . '->new', @_);
72 }
73  
74 # Setup
75 my $self = {
76 _objName => 'sound_process_' . $number,
77 _parentFile => undef, # No parent file object
78 _parentWorld => undef, # No parent file object
79 _objClass => $class,
80 _privFlag => FALSE, # All IVs are public
81  
82 # IVs
83 # ---
84  
85 # The calling GA::Session
86 $session => $session,
87 # Unique harness number for this session (not necessarily unique among all existing
88 # sessions)
89 number => $number,
90 # Full file path of the sound file that was played
91 path => $path,
92 # The IPC::Run harness used to play the sound (and required for stopping the sound, when
93 # it's finished or when MSP instructs us)
94 harness => $harness,
95 # Set to TRUE if the file $path should be deleted, when it has finished playing, set to
96 # FALSE (or 'undef') otherwise. (Used for MXP sound files which are converted from a
97 # world-specific file format, and which should be deleted after being played)
98 delFlag => $delFlag,
99 # For MSP-generated sounds, the sound trigger type - 'sound' or 'music'. For anything
100 # else, set to 'other'
101 type => $type,
102 # For MSP-generated sounds, the volume (a value between 0-100)
103 volume => $volume,
104 # For MSP-generated sounds, the number of repeats. 1 - play once, 2 - play twice (etc),
105 # -1 - play indefinitely
106 repeat => $repeat,
107 # For MSP-generated sounds, the sound priority (a value between 0-100)
108 priority => $priority,
109 # For MSP-generated sounds, a continue flag. 1 - sound continues playing if requested
110 # again; 0 - sound restarts if requested again
111 continue => $continue,
112 };
113  
114 # Bless the object into existence
115 bless $self, $class;
116 return $self;
117 }
118  
119 sub stop {
120  
121 # Called by whichever part of the code stops the sound from playing (or reacts, when the
122 # sound has finished playing)
123 # Makes sure the sound has stopped playing, updates the harness, and deletes the sound file
124 # itself if it's been marked for deletion
125 #
126 # Expected arguments
127 # (none besides $self)
128 #
129 # Optional arguments
130 # $noDelFlag - Set to TRUE if this sound has been stopped, so that it can be restarted
131 # (in which case the sound file should not be deleted). Otherwise set to
132 # FALSE (or 'undef')
133 #
134 # Return values
135 # 'undef' on improper arguments
136 # 1 otherwise
137  
138 my ($self, $noDelFlag, $check) = @_;
139  
140 # Check for improper arguments
141 if (defined $check) {
142  
143 return $axmud::CLIENT->writeImproper($self->_objClass . '->stop', @_);
144 }
145  
146 $self->harness->finish();
147 if (! $noDelFlag && $self->delFlag && -e $self->path) {
148  
149 unlink $self->path;
150 }
151  
152 return 1;
153 }
154  
155 ##################
156 # Methods
157  
158 ##################
159 # Accessors - set
160  
161 ##################
162 # Accessors - get
163  
164 sub session
165 { $_[0]->{session} }
166 sub number
167 { $_[0]->{number} }
168 sub path
169 { $_[0]->{path} }
170 sub harness
171 { $_[0]->{harness} }
172 sub delFlag
173 { $_[0]->{delFlag} }
174 sub type
175 { $_[0]->{type} }
176 sub volume
177 { $_[0]->{volume} }
178 sub repeat
179 { $_[0]->{repeat} }
180 sub priority
181 { $_[0]->{priority} }
182 sub continue
183 { $_[0]->{continue} }
184 }
185  
186 # Package must return a true value
187 1