61 lines
1.0 KiB
Go
61 lines
1.0 KiB
Go
package data
|
|
|
|
import (
|
|
"embed"
|
|
"encoding/json"
|
|
"image"
|
|
"log"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// FS is our exported FS.
|
|
//
|
|
//go:embed images/* mapping.json
|
|
var fs embed.FS
|
|
|
|
var Images map[string]image.Image
|
|
var Mapping map[string][]string
|
|
var Tiles []string
|
|
|
|
func Init() {
|
|
Images = make(map[string]image.Image)
|
|
dir, err := fs.ReadDir("images")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
for _, e := range dir {
|
|
if !e.IsDir() {
|
|
ext := filepath.Ext(e.Name())
|
|
name := strings.TrimSuffix(e.Name(), ext)
|
|
f, err := fs.Open(path.Join("images", e.Name()))
|
|
if err != nil {
|
|
log.Println(err)
|
|
continue
|
|
}
|
|
defer f.Close()
|
|
img, _, err := image.Decode(f)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
Images[name] = img
|
|
}
|
|
}
|
|
// Load mapping.
|
|
f, err := fs.Open("mapping.json")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
d := json.NewDecoder(f)
|
|
err = d.Decode(&Mapping)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Add all types to the common map.
|
|
for k := range Mapping {
|
|
Mapping[""] = append(Mapping[""], k)
|
|
}
|
|
}
|