1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-16 12:35:22 +00:00
OpenDiablo2/sound/SoundEffect.go

49 lines
957 B
Go
Raw Normal View History

2019-11-07 03:12:15 +00:00
package sound
2019-10-27 00:09:33 +00:00
import (
"log"
"github.com/hajimehoshi/ebiten/audio/wav"
2019-11-07 03:12:15 +00:00
"github.com/OpenDiablo2/OpenDiablo2/common"
2019-10-27 00:09:33 +00:00
"github.com/hajimehoshi/ebiten/audio"
)
type SoundEffect struct {
player *audio.Player
}
2019-11-07 03:12:15 +00:00
func CreateSoundEffect(sfx string, fileProvider common.FileProvider, context *audio.Context, volume float64) *SoundEffect {
2019-10-27 00:09:33 +00:00
result := &SoundEffect{}
var soundFile string
2019-11-07 03:12:15 +00:00
if _, exists := common.Sounds[sfx]; exists {
soundEntry := common.Sounds[sfx]
soundFile = soundEntry.FileName
} else {
soundFile = sfx
}
audioData := fileProvider.LoadFile(soundFile)
2019-10-27 00:09:33 +00:00
d, err := wav.Decode(context, audio.BytesReadSeekCloser(audioData))
if err != nil {
log.Fatal(err)
}
player, err := audio.NewPlayer(context, d)
if err != nil {
log.Fatal(err)
}
player.SetVolume(volume)
result.player = player
return result
}
func (v *SoundEffect) Play() {
v.player.Rewind()
v.player.Play()
}
2019-10-27 16:48:25 +00:00
func (v *SoundEffect) Stop() {
v.player.Pause()
}