Allow loadable images in mods.

This commit is contained in:
Daniel Schmidt 2023-05-09 13:14:58 +02:00
parent 71e915e7ce
commit 0f9987d305
5 changed files with 57 additions and 5 deletions

View File

@ -268,6 +268,10 @@ func (s *Session) GetResources() *ResourcesManager {
return s.resources
}
func (s *Session) GetLoadedMods() []string {
return s.loadedMods
}
//
// Internal
//

View File

@ -1,12 +1,30 @@
package image
import (
"errors"
"github.com/BigJk/imeji"
"github.com/BigJk/imeji/charmaps"
"github.com/muesli/termenv"
"os"
"path/filepath"
)
// TODO: Better decoupling in relation to session
var searchPaths []string
func init() {
ResetSearchPaths()
}
func AddSearchPaths(paths ...string) {
searchPaths = append(searchPaths, paths...)
}
func ResetSearchPaths() {
searchPaths = []string{"./assets/images/"}
}
// Fetch fetches an image from ./assets/images and converts it to an ansi string.
//
// env EOE_IMG_SIMPLE = 1: Forces the usage of a simpler character set. Can be used for stock cmd on windows.
@ -29,9 +47,11 @@ func Fetch(name string, options ...imeji.Option) (string, error) {
options = append(options, imeji.WithTrueColor())
}
res, err := imeji.FileString("./assets/images/"+name, options...)
if err != nil {
return "", err
for i := range searchPaths {
res, err := imeji.FileString(filepath.Join(searchPaths[i], name), options...)
if err == nil {
return res, nil
}
}
return res, nil
return "", errors.New("could not load image")
}

View File

@ -16,3 +16,21 @@ register_enemy(
}
}
)
register_event("TEST_EVENT", {
name = "Test Test",
description = [[!!mod_test.png
Testing loading a image from mod folder.]],
choices = {
{
description = "Go...",
callback = function()
return nil
end
}
},
on_end = function()
return GAME_STATE_RANDOM
end
})

Binary file not shown.

After

Width:  |  Height:  |  Size: 236 KiB

View File

@ -1,6 +1,7 @@
package mainmenu
import (
"fmt"
"github.com/BigJk/end_of_eden/audio"
"github.com/BigJk/end_of_eden/game"
"github.com/BigJk/end_of_eden/image"
@ -75,6 +76,10 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
game.WithLogging(log.New(f, "SESSION ", log.Ldate|log.Ltime|log.Lshortfile)),
lo.Ternary(os.Getenv("EOE_DEBUG") == "1", game.WithDebugEnabled(8272), nil),
)
image.ResetSearchPaths()
image.AddSearchPaths(lo.Map(session.GetLoadedMods(), func(item string, index int) string {
return fmt.Sprintf("./mods/%s/images/", item)
})...)
err = session.GobDecode(saved)
if err != nil {
@ -94,6 +99,11 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
panic(err)
}
image.ResetSearchPaths()
image.AddSearchPaths(lo.Map(settings.LoadedSettings.Mods, func(item string, index int) string {
return fmt.Sprintf("./mods/%s/images/", item)
})...)
m.choices = m.choices.Clear()
return m, root.Push(gameview.New(m, m.zones, game.NewSession(
game.WithLogging(log.New(f, "SESSION ", log.Ldate|log.Ltime|log.Lshortfile)),