createLevelFromTmx unmarshalls and unpacks tmx data into a Level
(r io.Reader, tmxURL string, root string)
| 14 | |
| 15 | // createLevelFromTmx unmarshalls and unpacks tmx data into a Level |
| 16 | func createLevelFromTmx(r io.Reader, tmxURL string, root string) (*Level, error) { |
| 17 | if root == "" { |
| 18 | return nil, errors.New("createLevelFromTmx should be called with a real root") |
| 19 | } |
| 20 | tmx.TMXURL = filepath.Join(root, tmxURL) |
| 21 | tmxLevel, err := tmx.Parse(r) |
| 22 | if err != nil { |
| 23 | return nil, err |
| 24 | } |
| 25 | level := &Level{} |
| 26 | level.Orientation = orth |
| 27 | level.resourceMap = make(map[uint32]Texture) |
| 28 | level.pointMap = make(map[mapPoint]*Tile) |
| 29 | level.framesMap = make(map[uint32][]uint32) |
| 30 | |
| 31 | // get a map of the gids to textures from the tilesets |
| 32 | for _, ts := range tmxLevel.Tilesets { |
| 33 | for _, g := range ts.Grid { |
| 34 | level.Orientation = g.Orientation |
| 35 | } |
| 36 | for _, t := range ts.Tiles { |
| 37 | for _, i := range t.Image { |
| 38 | if i.Source != "" { |
| 39 | tex, err := LoadedSprite(path.Join(path.Dir(tmxURL), i.Source)) |
| 40 | if err != nil { |
| 41 | if strings.HasPrefix(err.Error(), "resource not loaded") { |
| 42 | err = engo.Files.Load(path.Join(path.Dir(tmxURL), i.Source)) |
| 43 | if err != nil { |
| 44 | return nil, err |
| 45 | } |
| 46 | tex, err = LoadedSprite(path.Join(path.Dir(tmxURL), i.Source)) |
| 47 | } else { |
| 48 | return nil, err |
| 49 | } |
| 50 | } |
| 51 | level.resourceMap[ts.FirstGID+t.ID] = *tex |
| 52 | } |
| 53 | } |
| 54 | frames := []uint32{} |
| 55 | for _, f := range t.AnimationFrames { |
| 56 | frames = append(frames, ts.FirstGID+f.TileID) |
| 57 | } |
| 58 | level.framesMap[ts.FirstGID+t.ID] = frames |
| 59 | } |
| 60 | for _, i := range ts.Image { |
| 61 | if i.Source != "" { |
| 62 | _, err := LoadedSprite(path.Join(path.Dir(tmxURL), i.Source)) |
| 63 | if err != nil { |
| 64 | if strings.HasPrefix(err.Error(), "resource not loaded") { |
| 65 | err = engo.Files.Load(path.Join(path.Dir(tmxURL), i.Source)) |
| 66 | if err != nil { |
| 67 | return nil, err |
| 68 | } |
| 69 | _, err = LoadedSprite(path.Join(path.Dir(tmxURL), i.Source)) |
| 70 | } else { |
| 71 | return nil, err |
| 72 | } |
| 73 | } |
no test coverage detected