-
Notifications
You must be signed in to change notification settings - Fork 1
/
import.go
119 lines (115 loc) · 2.56 KB
/
import.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
116
117
118
119
package olded
import (
"errors"
"os"
"path/filepath"
"time"
"github.com/mb0/babasite/game"
"github.com/mb0/babasite/game/ids"
"github.com/mb0/babasite/game/lvl"
"github.com/mb0/babasite/game/pix"
)
type store struct {
Maps map[string]*World
Sets map[string]*Tset
Assets map[string]*Asset
Pals map[string]*Pal
}
func Import(dir string) (res []*game.World, err error) {
fs := &store{
Maps: make(map[string]*World),
Sets: make(map[string]*Tset),
Assets: make(map[string]*Asset),
Pals: make(map[string]*Pal),
}
err = fs.loadMaps(filepath.Join(dir, "maped"))
if err != nil {
return nil, err
}
err = fs.loadAssets(filepath.Join(dir, "chared"))
if err != nil {
if !errors.Is(os.ErrNotExist, err) {
return nil, err
}
}
var def *game.World
var tstab lvl.TsetTable
tsmap := make(map[string]ids.Tset)
for _, ts := range fs.Sets {
nts, _ := tstab.New()
nts.Name = ts.Name
for _, old := range ts.Infos {
nts.Infos = append(nts.Infos, lvl.TileInfo{
Tile: old.Tile,
Name: old.Name,
Color: old.Color,
Block: old.Block,
Group: old.Group,
})
}
tsmap[ts.Name] = nts.ID
}
for _, tm := range fs.Maps {
var w game.World
w.Name = tm.Name
w.Vers.Mod = time.Now().Unix()
w.Lvl.Tset = tstab
for _, l := range tm.Levels {
ng, _ := w.Lvl.Grid.New()
ng.Tiles = l.Tiles
nl, _ := w.Lvl.Lvl.New()
nl.Name = l.Name
nl.Dim = tm.Dim
nl.Tset = tsmap[tm.WorldInfo.Tileset]
nl.Grid = ng.ID
}
if def == nil || len(w.Lvl.Lvl.List) > len(def.Lvl.Lvl.List) {
def = &w
}
res = append(res, &w)
}
if len(fs.Pals) == 0 {
return res, nil
}
if def == nil {
def = &game.World{}
def.Name = "default"
res = append(res, def)
}
palmap := make(map[string]ids.Pal)
for _, pal := range fs.Pals {
np, _ := def.Pix.Pal.New()
np.Name = pal.Name
for _, old := range pal.Feats {
f := &pix.Feat{Name: old.Name}
for _, oldc := range old.Colors {
f.Colors = append(f.Colors, pix.Color(oldc))
}
np.Feats = append(np.Feats, f)
}
palmap[pal.Name] = np.ID
}
for _, ass := range fs.Assets {
ni, _ := def.Pix.Img.New()
ni.Name = ass.Name
ni.Kind = ass.Kind
ni.Dim = ass.Dim
ni.Pal = palmap[ass.AssetInfo.Pal]
picmap := make(map[ids.Pic]ids.Pic)
for pid, p := range ass.Pics {
np, _ := def.Pix.Pic.New()
np.Pix = p.Pix
picmap[pid] = np.ID
}
for _, seq := range ass.Seq {
nc, _ := def.Pix.Clip.New()
nc.Name = seq.Name
nc.Dim = ni.Dim
nc.Img = ni.ID
for _, id := range seq.IDs {
nc.Seq = append(nc.Seq, pix.Frame{Pic: picmap[id]})
}
}
}
return res, nil
}