-
Notifications
You must be signed in to change notification settings - Fork 0
/
assets.go
115 lines (96 loc) · 2.08 KB
/
assets.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"fmt"
"log"
"os"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"gopkg.in/yaml.v3"
)
type AssetType string
type AssetInfo struct {
image *ebiten.Image
FrameCount int
Size int
Type AssetType
FilePath string
Name string
}
type Asset interface {
GetImage() *ebiten.Image
GetFrameCount() int
GetSize() int
GetType() AssetType
GetFilePath() string
GetName() string
}
type AssetManagerInfo struct {
assets map[string]map[AssetType]Asset
}
type AssetManager interface {
GetAssetInfo(name string, assetType AssetType) Asset
}
func (am *AssetManagerInfo) GetAssetInfo(name string, assetType AssetType) Asset {
return am.assets[name][assetType]
}
func LoadAssets() (AssetManager, error) {
am := &AssetManagerInfo{}
yamlFile, err := os.ReadFile("config/assets.yml")
if err != nil {
return nil, err
}
var assets []*AssetInfo
err = yaml.Unmarshal(yamlFile, &assets)
if err != nil {
return nil, err
}
am.assets = make(map[string]map[AssetType]Asset)
for _, a := range assets {
if am.assets[a.Name] == nil {
am.assets[a.Name] = make(map[AssetType]Asset)
}
am.assets[a.Name][a.Type] = a
err = loadImageAsset(a)
if err != nil {
return nil, err
}
}
return am, nil
}
func loadImageAsset(a *AssetInfo) error {
var typePath string
switch a.Type {
case "walk", "attack", "death":
typePath = fmt.Sprintf("characters/%s", a.Name)
case "item":
typePath = "items"
case "tile":
typePath = "tiles"
}
filepath := fmt.Sprintf("assets/%s/%s", typePath, a.FilePath)
img, _, err := ebitenutil.NewImageFromFile(filepath)
if err != nil {
log.Fatalf("failed to load asset %v: %v", a, err)
return err
}
a.image = img
return nil
}
func (a *AssetInfo) GetImage() *ebiten.Image {
return a.image
}
func (a *AssetInfo) GetFrameCount() int {
return a.FrameCount
}
func (a *AssetInfo) GetSize() int {
return a.Size
}
func (a *AssetInfo) GetType() AssetType {
return a.Type
}
func (a *AssetInfo) GetFilePath() string {
return a.FilePath
}
func (a *AssetInfo) GetName() string {
return a.Name
}