Skip to content

Commit 7b00819

Browse files
authored
audio config map (#225)
# Changes * Added a new config file: `audio.yaml` * Audio can be played by an ID or by filepath. * When ID is used, other config values taken into consideration such as optional volume setting.
1 parent 25a6ca8 commit 7b00819

File tree

13 files changed

+172
-38
lines changed

13 files changed

+172
-38
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Sound that plays when a "change" confirmation/activation occurs
2+
change:
3+
filepath: sound/other/change.mp3
4+
volume: 80
5+
# When a target is hit in combat
6+
hit-other:
7+
filepath: sound/combat/hit-other.mp3
8+
# When receiving a hit in combat
9+
hit-self:
10+
filepath: sound/combat/hit-self.mp3
11+
# Plays at login screen
12+
intro:
13+
filepath: music/intro.mp3
14+
volume: 20
15+
# When Leveling up
16+
levelup:
17+
filepath: sound/other/levelup.mp3
18+
# When missing a hit in combat
19+
miss:
20+
filepath: sound/combat/miss1.mp3
21+
# When a purchase is made
22+
purchase:
23+
filepath: sound/other/buy.mp3
24+
# When someone leaves the room
25+
room-exit:
26+
filepath: sound/movement/room-exit.mp3
27+
# When someone enters the room
28+
room-enter:
29+
filepath: sound/movement/room-enter.mp3

_datafiles/world/empty/audio.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Sound that plays when a "change" confirmation/activation occurs
2+
change:
3+
filepath: sound/other/change.mp3
4+
volume: 80
5+
# When a target is hit in combat
6+
hit-other:
7+
filepath: sound/combat/hit-other.mp3
8+
# When receiving a hit in combat
9+
hit-self:
10+
filepath: sound/combat/hit-self.mp3
11+
# Plays at login screen
12+
intro:
13+
filepath: music/intro.mp3
14+
volume: 20
15+
# When Leveling up
16+
levelup:
17+
filepath: sound/other/levelup.mp3
18+
# When missing a hit in combat
19+
miss:
20+
filepath: sound/combat/miss1.mp3
21+
# When a purchase is made
22+
purchase:
23+
filepath: sound/other/buy.mp3
24+
# When someone leaves the room
25+
room-exit:
26+
filepath: sound/movement/room-exit.mp3
27+
# When someone enters the room
28+
room-enter:
29+
filepath: sound/movement/room-enter.mp3

internal/audio/audio.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package audio
2+
3+
import (
4+
"log/slog"
5+
"os"
6+
"time"
7+
8+
"github.com/pkg/errors"
9+
"github.com/volte6/gomud/internal/configs"
10+
"gopkg.in/yaml.v2"
11+
)
12+
13+
type AudioConfig struct {
14+
FilePath string `yaml:"filepath,omitempty"`
15+
Volume int `yaml:"volume,omitempty"`
16+
}
17+
18+
var (
19+
audioLookup = map[string]AudioConfig{}
20+
)
21+
22+
func GetFile(identifier string) AudioConfig {
23+
if f, ok := audioLookup[identifier]; ok {
24+
return f
25+
}
26+
return AudioConfig{}
27+
}
28+
29+
func LoadAudioConfig() {
30+
31+
start := time.Now()
32+
33+
path := string(configs.GetConfig().FolderDataFiles) + `/audio.yaml`
34+
35+
bytes, err := os.ReadFile(path)
36+
if err != nil {
37+
panic(errors.Wrap(err, `filepath: `+path))
38+
}
39+
40+
clear(audioLookup)
41+
42+
err = yaml.Unmarshal(bytes, &audioLookup)
43+
if err != nil {
44+
panic(errors.Wrap(err, `filepath: `+path))
45+
}
46+
47+
slog.Info("...LoadAudioConfig()", "loadedCount", len(audioLookup), "Time Taken", time.Since(start))
48+
}

internal/combat/combat.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ func AttackPlayerVsMob(user *users.UserRecord, mob *mobs.Mob) AttackResult {
4343
mob.Character.TrackPlayerDamage(user.UserId, attackResult.DamageToTarget)
4444

4545
if attackResult.Hit {
46-
user.PlaySound(`sound/combat/hit-other.mp3`, `combat`)
46+
user.PlaySound(`hit-other`, `combat`)
4747
} else {
48-
user.PlaySound(`sound/combat/miss1.mp3`, `combat`)
48+
user.PlaySound(`miss`, `combat`)
4949
}
5050

5151
return attackResult
@@ -67,10 +67,10 @@ func AttackPlayerVsPlayer(userAtk *users.UserRecord, userDef *users.UserRecord)
6767
}
6868

6969
if attackResult.Hit {
70-
userAtk.PlaySound(`sound/combat/hit-other.mp3`, `combat`)
71-
userDef.PlaySound(`sound/combat/hit-self.mp3`, `combat`)
70+
userAtk.PlaySound(`hit-other`, `combat`)
71+
userDef.PlaySound(`hit-self`, `combat`)
7272
} else {
73-
userAtk.PlaySound(`sound/combat/miss1.mp3`, `combat`)
73+
userAtk.PlaySound(`miss`, `combat`)
7474
}
7575

7676
return attackResult
@@ -89,7 +89,7 @@ func AttackMobVsPlayer(mob *mobs.Mob, user *users.UserRecord) AttackResult {
8989
}
9090

9191
if attackResult.Hit {
92-
user.PlaySound(`sound/combat/hit-self.mp3`, `combat`)
92+
user.PlaySound(`hit-self`, `combat`)
9393
}
9494

9595
return attackResult

internal/mobcommands/go.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ func Go(rest string, mob *mobs.Mob, room *rooms.Room) (bool, error) {
138138

139139
destRoom.SendTextToExits(`You hear someone moving around.`, true, room.GetPlayers(rooms.FindAll)...)
140140

141-
room.PlaySound(`sound/movement/room-exit.mp3`, `movement`, 100)
142-
destRoom.PlaySound(`sound/movement/room-enter.mp3`, `movement`, 100)
141+
room.PlaySound(`room-exit`, `movement`)
142+
destRoom.PlaySound(`room-enter`, `movement`)
143143

144144
return true, nil
145145
}

internal/rooms/rooms.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99
"time"
1010

11+
"github.com/volte6/gomud/internal/audio"
1112
"github.com/volte6/gomud/internal/buffs"
1213
"github.com/volte6/gomud/internal/configs"
1314
"github.com/volte6/gomud/internal/events"
@@ -248,12 +249,14 @@ func (r *Room) SendText(txt string, excludeUserIds ...int) {
248249

249250
}
250251

251-
func (r *Room) PlaySound(soundFile string, category string, volume int, excludeUserIds ...int) {
252+
func (r *Room) PlaySound(soundId string, category string, excludeUserIds ...int) {
252253

253-
if volume < 1 {
254-
volume = 1
255-
} else if volume > 100 {
256-
volume = 100
254+
volume := 100
255+
if soundConfig := audio.GetFile(soundId); soundConfig.FilePath != `` {
256+
soundId = soundConfig.FilePath
257+
if soundConfig.Volume > 0 && soundConfig.Volume <= 100 {
258+
volume = soundConfig.Volume
259+
}
257260
}
258261

259262
for _, userId := range r.players {
@@ -277,7 +280,7 @@ func (r *Room) PlaySound(soundFile string, category string, volume int, excludeU
277280
events.AddToQueue(events.MSP{
278281
UserId: userId,
279282
SoundType: `SOUND`,
280-
SoundFile: soundFile,
283+
SoundFile: soundId,
281284
Volume: volume,
282285
Category: category,
283286
})

internal/usercommands/buy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ func tryPurchase(request string, user *users.UserRecord, room *rooms.Room, shopM
332332
// Give them the item
333333
newItm := items.New(matchedShopItem.ItemId)
334334
user.Character.StoreItem(newItm)
335-
user.PlaySound(`sound/other/buy.mp3`, `other`)
335+
user.PlaySound(`purchase`, `other`)
336336

337337
iSpec := newItm.GetSpec()
338338
if iSpec.QuestToken != `` {

internal/usercommands/go.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,8 @@ func Go(rest string, user *users.UserRecord, room *rooms.Room) (bool, error) {
321321

322322
scripting.TryRoomScriptEvent(`onEnter`, user.UserId, destRoom.RoomId)
323323

324-
room.PlaySound(`sound/movement/room-exit.mp3`, `movement`, user.UserId)
325-
destRoom.PlaySound(`sound/movement/room-enter.mp3`, `movement`, user.UserId)
324+
room.PlaySound(`room-exit`, `movement`, user.UserId)
325+
destRoom.PlaySound(`room-enter`, `movement`, user.UserId)
326326
}
327327

328328
}

internal/usercommands/use.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func Use(rest string, user *users.UserRecord, room *rooms.Room) (bool, error) {
4747
container.AddItem(newItem)
4848
room.Containers[containerName] = container
4949

50-
room.PlaySound(`sound/other/change.mp3`, `other`, 100)
50+
room.PlaySound(`change`, `other`)
5151

5252
user.SendText(``)
5353
user.SendText(fmt.Sprintf(`The <ansi fg="container">%s</ansi> produces a <ansi fg="itemname">%s</ansi>!`, containerName, newItem.DisplayName()))

internal/users/userrecord.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010
"time"
1111

12+
"github.com/volte6/gomud/internal/audio"
1213
"github.com/volte6/gomud/internal/buffs"
1314
"github.com/volte6/gomud/internal/characters"
1415
"github.com/volte6/gomud/internal/configs"
@@ -145,33 +146,39 @@ func (u *UserRecord) GrantXP(amt int, source string) {
145146

146147
}
147148

148-
func (u *UserRecord) PlayMusic(musicFile string) {
149+
func (u *UserRecord) PlayMusic(musicFileOrId string) {
150+
151+
v := 100
152+
if soundConfig := audio.GetFile(musicFileOrId); soundConfig.FilePath != `` {
153+
musicFileOrId = soundConfig.FilePath
154+
if soundConfig.Volume > 0 && soundConfig.Volume <= 100 {
155+
v = soundConfig.Volume
156+
}
157+
}
149158

150159
events.AddToQueue(events.MSP{
151160
UserId: u.UserId,
152161
SoundType: `MUSIC`,
153-
SoundFile: musicFile,
154-
Volume: 100,
162+
SoundFile: musicFileOrId,
163+
Volume: v,
155164
})
156165

157166
}
158167

159-
func (u *UserRecord) PlaySound(soundFile string, category string, volume ...int) {
168+
func (u *UserRecord) PlaySound(soundId string, category string) {
160169

161170
v := 100
162-
if len(volume) > 0 {
163-
v = volume[0]
164-
if v < 1 {
165-
v = 1
166-
} else if v > 100 {
167-
v = 100
171+
if soundConfig := audio.GetFile(soundId); soundConfig.FilePath != `` {
172+
soundId = soundConfig.FilePath
173+
if soundConfig.Volume > 0 && soundConfig.Volume <= 100 {
174+
v = soundConfig.Volume
168175
}
169176
}
170177

171178
events.AddToQueue(events.MSP{
172179
UserId: u.UserId,
173180
SoundType: `SOUND`,
174-
SoundFile: soundFile,
181+
SoundFile: soundId,
175182
Volume: v,
176183
Category: category,
177184
})

0 commit comments

Comments
 (0)