diff --git a/assets/audio/music/energetic_orthogonal_expansions.mp3 b/assets/audio/music/energetic_orthogonal_expansions.mp3 new file mode 100644 index 0000000..fbadebc Binary files /dev/null and b/assets/audio/music/energetic_orthogonal_expansions.mp3 differ diff --git a/assets/audio/music/planet_mining.mp3 b/assets/audio/music/planet_mining.mp3 new file mode 100644 index 0000000..a10890b Binary files /dev/null and b/assets/audio/music/planet_mining.mp3 differ diff --git a/assets/audio/theme.wav b/assets/audio/theme.wav deleted file mode 100755 index 9a647fc..0000000 Binary files a/assets/audio/theme.wav and /dev/null differ diff --git a/assets/scripts/events.lua b/assets/scripts/events.lua index 52704e5..bc38195 100644 --- a/assets/scripts/events.lua +++ b/assets/scripts/events.lua @@ -50,6 +50,8 @@ As you struggle to gather your bearings, you notice a blinking panel on the wall } }, on_enter = function() + play_music("energetic_orthogonal_expansions") + -- Give the player it's start cards give_card("MELEE_HIT", PLAYER_ID) give_card("MELEE_HIT", PLAYER_ID) diff --git a/audio/audio.go b/audio/audio.go index c9f2170..52d002f 100644 --- a/audio/audio.go +++ b/audio/audio.go @@ -1,23 +1,25 @@ package audio import ( + "github.com/faiface/beep/mp3" + "github.com/faiface/beep/wav" "io/fs" "log" "os" "path/filepath" "runtime" "strings" + "sync" "github.com/faiface/beep" "github.com/faiface/beep/effects" - "github.com/faiface/beep/mp3" "github.com/faiface/beep/speaker" - "github.com/faiface/beep/wav" ) const sampleRate = 44100 const baseVolume = -1 +var mtx = sync.Mutex{} var sounds = map[string]*beep.Buffer{} var enabled = false var music = &beep.Ctrl{ @@ -32,47 +34,66 @@ func InitAudio() { return } + wg := sync.WaitGroup{} + _ = filepath.Walk("./assets/audio", func(path string, info fs.FileInfo, err error) error { if err != nil { return nil } - var streamer beep.StreamSeekCloser - var format beep.Format + wg.Add(1) + go func() { + defer wg.Done() - if !info.IsDir() { - if strings.HasSuffix(path, ".mp3") { - f, err := os.Open(path) - if err != nil { - return err - } + var streamer beep.StreamSeekCloser + var format beep.Format - streamer, format, err = mp3.Decode(f) - if err != nil { - return err - } - } else if strings.HasSuffix(path, ".wav") { - f, err := os.Open(path) - if err != nil { - return err - } + if !info.IsDir() { + if strings.HasSuffix(path, ".mp3") { + f, err := os.Open(path) + if err != nil { + log.Println("Audio error:", err) + return + } - streamer, format, err = wav.Decode(f) - if err != nil { - return err + streamer, format, err = mp3.Decode(f) + if err != nil { + log.Println("Audio error:", err) + return + } + } else if strings.HasSuffix(path, ".wav") { + f, err := os.Open(path) + if err != nil { + log.Println("Audio error:", err) + return + } + + streamer, format, err = wav.Decode(f) + if err != nil { + log.Println("Audio error:", err) + return + } } } - } - if streamer != nil { - buf := beep.NewBuffer(beep.Format{ - SampleRate: sampleRate, - NumChannels: 2, - Precision: 2, - }) - buf.Append(beep.Resample(6, format.SampleRate, sampleRate, streamer)) - sounds[strings.Split(filepath.Base(path), ".")[0]] = buf - } + if streamer != nil { + buf := beep.NewBuffer(beep.Format{ + SampleRate: sampleRate, + NumChannels: 2, + Precision: 2, + }) + + if format.SampleRate == sampleRate { + buf.Append(streamer) + } else { + buf.Append(beep.Resample(3, format.SampleRate, sampleRate, streamer)) + } + + mtx.Lock() + sounds[strings.Split(filepath.Base(path), ".")[0]] = buf + mtx.Unlock() + } + }() return nil }) @@ -81,6 +102,8 @@ func InitAudio() { panic(err) } + wg.Wait() + speaker.Play(music) enabled = true diff --git a/cmd/game/main.go b/cmd/game/main.go index 575e13a..2bf3f75 100644 --- a/cmd/game/main.go +++ b/cmd/game/main.go @@ -8,6 +8,8 @@ import ( "github.com/BigJk/end_of_eden/gen/faces" "github.com/BigJk/end_of_eden/ui/menus/gameview" "github.com/BigJk/end_of_eden/ui/menus/mainmenu" + "github.com/BigJk/end_of_eden/ui/style" + "github.com/charmbracelet/lipgloss" zone "github.com/lrstanley/bubblezone" "github.com/samber/lo" "golang.design/x/clipboard" @@ -21,6 +23,7 @@ import ( ) var prog *tea.Program +var loadStyle = lipgloss.NewStyle().Bold(true).Italic(true).Foreground(style.BaseGray) func main() { audioFlag := flag.Bool("audio", true, "disable audio") @@ -30,24 +33,36 @@ func main() { testGameState := flag.String("game_state", "", "test game state") flag.Parse() + fmt.Println(lipgloss.NewStyle().Bold(true).Foreground(style.BaseRed).Render("End Of Eden")) + // Init clipboard - if err := clipboard.Init(); err != nil { - panic(err) + fmt.Println(loadStyle.Render("Initializing Clipboard. Please wait...")) + { + if err := clipboard.Init(); err != nil { + panic(err) + } } + fmt.Println(loadStyle.Render("Done!")) // Init audio if *audioFlag { + fmt.Println(loadStyle.Render("Initializing audio. Please wait...")) audio.InitAudio() - audio.PlayMusic("theme") + audio.PlayMusic("planet_mining") + fmt.Println(loadStyle.Render("Done!")) } - // Init face generator - if err := faces.InitGlobal("./assets/gen/faces"); err != nil { - panic(err) - } + fmt.Println(loadStyle.Render("Initializing Proc-Gen. Please wait...")) + { + // Init face generator + if err := faces.InitGlobal("./assets/gen/faces"); err != nil { + panic(err) + } - // Init other gens - gen.InitGen() + // Init other gens + gen.InitGen() + } + fmt.Println(loadStyle.Render("Done!")) // Redirect log to file _ = os.Mkdir("./logs", 0777) diff --git a/game/lua.go b/game/lua.go index 5ec983d..4f6f1cc 100644 --- a/game/lua.go +++ b/game/lua.go @@ -161,6 +161,11 @@ fun = require "fun" return 0 })) + l.SetGlobal("play_music", l.NewFunction(func(state *lua.LState) int { + audio.PlayMusic(state.ToString(1)) + return 0 + })) + // Game State l.SetGlobal("set_event", l.NewFunction(func(state *lua.LState) int { diff --git a/game/session.go b/game/session.go index 2ff38f6..703ac1b 100644 --- a/game/session.go +++ b/game/session.go @@ -968,7 +968,11 @@ func (s *Session) RemoveStatusEffect(guid string) { // GetActorStatusEffects returns the guids of all the status effects a certain actor owns. func (s *Session) GetActorStatusEffects(guid string) []string { - return s.actors[guid].StatusEffects.ToSlice() + if actor, ok := s.actors[guid]; ok { + actor.StatusEffects.ToSlice() + } + + return []string{} } // AddStatusEffectStacks increases the stacks of a certain status effect by guid.