Made loading of audio samples more graceful (game will play without them).

This commit is contained in:
Sander Schobers 2021-08-13 10:20:03 +02:00
parent 3c99e5881b
commit 0d49482036
3 changed files with 13 additions and 4 deletions

View File

@ -12,3 +12,4 @@
- [ ] Add exploding animation of monsters. - [ ] Add exploding animation of monsters.
- [ ] Add audio settings (music & sound volume). - [ ] Add audio settings (music & sound volume).
- [X] Hearts must be saved as well for resume. - [X] Hearts must be saved as well for resume.
- [ ] Add demo mode.

View File

@ -1,6 +1,8 @@
package main package main
import ( import (
"log"
"opslag.de/schobers/tins2021" "opslag.de/schobers/tins2021"
"opslag.de/schobers/zntg/play" "opslag.de/schobers/zntg/play"
"opslag.de/schobers/zntg/ui" "opslag.de/schobers/zntg/ui"
@ -73,7 +75,7 @@ func (a *app) Init(ctx ui.Context) error {
"player_move.mp3", "player_move.mp3",
) )
if err != nil { if err != nil {
return err log.Printf("failed to load samples; %v\n", err)
} }
return nil return nil

View File

@ -9,6 +9,8 @@ import (
"github.com/faiface/beep/effects" "github.com/faiface/beep/effects"
"github.com/faiface/beep/mp3" "github.com/faiface/beep/mp3"
"github.com/faiface/beep/speaker" "github.com/faiface/beep/speaker"
"github.com/pkg/errors"
"opslag.de/schobers/ut"
"opslag.de/schobers/zntg/ui" "opslag.de/schobers/zntg/ui"
) )
@ -39,12 +41,16 @@ func NewAudioPlayer(resources ui.Resources, prefix string) *AudioPlayer {
} }
func (p *AudioPlayer) LoadSample(name ...string) error { func (p *AudioPlayer) LoadSample(name ...string) error {
var all error
for _, name := range name { for _, name := range name {
if _, err := p.openSample(name); err != nil { if _, err := p.openSample(name); err != nil {
return err all = ut.ErrCombine(all, errors.Wrap(err, "failed to open sample"))
} }
} }
return nil if all == nil {
return nil
}
return errors.WithMessage(all, "failed to open one or more samples")
} }
func (p *AudioPlayer) openSample(name string) (Sample, error) { func (p *AudioPlayer) openSample(name string) (Sample, error) {