From 1c4ecf2a26d43530663aa66f23cc9efc2939f375 Mon Sep 17 00:00:00 2001 From: Daniel Schmidt Date: Fri, 30 Aug 2024 16:30:32 +0200 Subject: [PATCH] fix: fix tutorial for wasm --- game/session.go | 13 +++++++++++-- ui/menus/mainmenu/mainmenu.go | 5 ++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/game/session.go b/game/session.go index 68ccea1..eeb1d8d 100644 --- a/game/session.go +++ b/game/session.go @@ -157,6 +157,8 @@ func NewSession(options ...func(s *Session)) *Session { session.SetOnLuaError(nil) session.luaState, session.luaDocs = SessionAdapter(session) + session.resources = NewResourcesManager(session.luaState, session.luaDocs, session.log) + session.resources.MarkBaseGame() for i := range options { if options[i] == nil { @@ -165,8 +167,6 @@ func NewSession(options ...func(s *Session)) *Session { options[i](session) } - session.resources = NewResourcesManager(session.luaState, session.luaDocs, session.log) - session.resources.MarkBaseGame() session.loadMods(session.loadedMods) session.log.Println("Session started!") @@ -212,6 +212,15 @@ func WithMods(mods []string) func(s *Session) { } } +// WithLuaString sets the lua code that should be executed. +func WithLuaString(lua string) func(s *Session) { + return func(s *Session) { + if err := s.luaState.DoString(lua); err != nil { + s.logLuaError("WithLuaString", "", err) + } + } +} + // WithSeed sets the seed for the random number generator. func WithSeed(seed uint64) func(s *Session) { return func(s *Session) { diff --git a/ui/menus/mainmenu/mainmenu.go b/ui/menus/mainmenu/mainmenu.go index 85ddff5..d549c71 100644 --- a/ui/menus/mainmenu/mainmenu.go +++ b/ui/menus/mainmenu/mainmenu.go @@ -4,7 +4,6 @@ import ( "fmt" "log" "os" - "path/filepath" "strings" "time" @@ -151,7 +150,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case ChoiceTutorial: audio.Play("btn_menu") - tutorialLua, err := filepath.Abs("./assets/tutorial/tutorial.lua") + tutorialLua, err := fs.ReadFile("./assets/tutorial/tutorial.lua") if err != nil { panic(err) } @@ -160,7 +159,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, tea.Sequence( cmd, root.Push(gameview.New(m, m.zones, game.NewSession( - game.WithMods([]string{tutorialLua}), + game.WithLuaString(string(tutorialLua)), ))), ) case ChoiceAbout: