diff --git a/cmd/krampus19/context.go b/cmd/krampus19/context.go index caa7037..7315cdc 100644 --- a/cmd/krampus19/context.go +++ b/cmd/krampus19/context.go @@ -3,6 +3,7 @@ package main import ( "time" + "github.com/spf13/afero" "opslag.de/schobers/geom" "opslag.de/schobers/krampus19/alui" @@ -23,6 +24,7 @@ func newTexture(bmp *allg5.Bitmap) texture { type Context struct { DisplaySize geom.Point Resources vfs.CopyDir + ResourcesFs afero.Fs Textures map[string]texture Sprites map[string]sprite Levels map[string]levelPack diff --git a/cmd/krampus19/game.go b/cmd/krampus19/game.go index 95a5f0e..a7ba679 100644 --- a/cmd/krampus19/game.go +++ b/cmd/krampus19/game.go @@ -6,7 +6,11 @@ import ( "image/png" "io" "log" + "os" + "path/filepath" + "strings" + "github.com/spf13/afero" "opslag.de/schobers/allg5" "opslag.de/schobers/fs/vfs" "opslag.de/schobers/geom" @@ -100,7 +104,31 @@ func (g *game) loadTextures(pathToName map[string]string) error { func (g *game) loadLevelPacks() error { g.ctx.Levels = map[string]levelPack{} - ids := []string{"1"} + + var ids []string + const root = "levels" + err := afero.Walk(g.ctx.ResourcesFs, root, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if !info.IsDir() { + return nil + } + name := filepath.Base(path) + if name == root { + return nil + } + + if !strings.HasPrefix(name, "pack") { + return filepath.SkipDir + } + ids = append(ids, name[4:]) + return filepath.SkipDir + }) + if err != nil { + return err + } + for _, id := range ids { err := g.loadLevelPack(id) if err != nil { @@ -173,12 +201,12 @@ func (g *game) loadAssets() error { } log.Printf("Loaded %d textures.\n", len(g.ctx.Textures)) - log.Println("Loading levels...") + log.Println("Loading level packs...") err = g.loadLevelPacks() if err != nil { return err } - log.Printf("Loaded %d levels.\n", len(g.ctx.Levels)) + log.Printf("Loaded %d levels packs.\n", len(g.ctx.Levels)) log.Println("Loading fonts...") err = g.loadFonts() @@ -201,9 +229,9 @@ func (g *game) Destroy() { g.ctx.Destroy() } -func (g *game) Init(disp *allg5.Display, settings settings, res vfs.CopyDir, cons *gut.Console, fps *gut.FPS) error { +func (g *game) Init(disp *allg5.Display, settings settings, res vfs.CopyDir, resFs afero.Fs, cons *gut.Console, fps *gut.FPS) error { log.Print("Initializing game...") - g.ctx = &Context{Resources: res, Textures: map[string]texture{}, Settings: settings, Navigation: navigation{game: g}} + g.ctx = &Context{Resources: res, ResourcesFs: resFs, Textures: map[string]texture{}, Settings: settings, Navigation: navigation{game: g}} g.ctx.DisplaySize = geom.Pt(disp.Width(), disp.Height()) g.ctx.SpriteDrawer.ctx = g.ctx err := g.ctx.Progress.load() diff --git a/cmd/krampus19/krampus19.go b/cmd/krampus19/krampus19.go index f230bec..be83ef0 100644 --- a/cmd/krampus19/krampus19.go +++ b/cmd/krampus19/krampus19.go @@ -24,10 +24,15 @@ func main() { } } -func resources() (vfs.CopyDir, error) { +func resources() (vfs.CopyDir, afero.Fs, error) { var embeddedFs = ricefs.NewFs(rice.MustFindBox("res")) var osFs = afero.NewBasePathFs(afero.NewOsFs(), "./res") - return vfs.NewCopyDir(vfs.NewFallbackFs(osFs, embeddedFs)) + var fs = vfs.NewFallbackFs(osFs, embeddedFs) + copy, err := vfs.NewCopyDir(fs) + if err != nil { + return nil, nil, err + } + return copy, fs, nil } func run() error { @@ -35,7 +40,7 @@ func run() error { cons := &gut.Console{} log.SetOutput(io.MultiWriter(log.Writer(), cons)) - res, err := resources() + res, resFs, err := resources() if err != nil { return err } @@ -107,7 +112,7 @@ func run() error { defer fps.Destroy() game := &game{} - err = game.Init(disp, settings, res, cons, fps) + err = game.Init(disp, settings, res, resFs, cons, fps) if err != nil { return err }