2020-03-19 22:24:21 +00:00
|
|
|
package assetstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
2020-03-20 00:02:27 +00:00
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten"
|
2020-03-19 22:24:21 +00:00
|
|
|
|
|
|
|
"code.ur.gs/lupine/ordoor/internal/maps"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Map struct {
|
|
|
|
assets *AssetStore
|
|
|
|
set *Set
|
|
|
|
Rect image.Rectangle
|
|
|
|
|
|
|
|
raw *maps.GameMap
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map loads a game map with the given name (e.g. "Chapter01")
|
|
|
|
func (a *AssetStore) Map(name string) (*Map, error) {
|
|
|
|
name = canonical(name)
|
|
|
|
|
|
|
|
if m, ok := a.maps[name]; ok {
|
|
|
|
return m, nil
|
|
|
|
}
|
2020-03-20 00:02:27 +00:00
|
|
|
log.Printf("Loading map %v", name)
|
2020-03-19 22:24:21 +00:00
|
|
|
|
|
|
|
mapFile, err := a.lookup(name, "map", "Maps", "MultiMaps")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
txtFile, err := a.lookup(name, "txt", "Maps", "MultiMaps")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
raw, err := maps.LoadGameMapByFiles(mapFile, txtFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// The set for a map is small and frequently referenced, so load it here
|
|
|
|
set, err := a.Set(raw.MapSetName())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m := &Map{
|
|
|
|
Rect: image.Rect(
|
|
|
|
int(raw.MinWidth),
|
|
|
|
int(raw.MinLength),
|
|
|
|
int(raw.MaxWidth),
|
|
|
|
int(raw.MaxLength),
|
|
|
|
),
|
|
|
|
assets: a,
|
|
|
|
raw: raw,
|
|
|
|
set: set,
|
|
|
|
}
|
|
|
|
|
|
|
|
a.maps[canonical(name)] = m
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ImagesForCell returns the sprites needed to correctly render this cell.
|
|
|
|
// They should be rendered from first to last to get the correct ordering
|
|
|
|
func (m *Map) ImagesForCell(x, y, z int) ([]*ebiten.Image, error) {
|
|
|
|
cell := m.raw.At(x, y, z)
|
|
|
|
images := make([]*ebiten.Image, 0, 4)
|
|
|
|
|
2020-03-20 00:02:27 +00:00
|
|
|
for _, ref := range []maps.ObjRef{cell.Surface, cell.Right, cell.Left, cell.Center} {
|
2020-03-19 22:24:21 +00:00
|
|
|
if !ref.IsActive() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
obj, err := m.set.Object(ref.Index())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
img, err := obj.Image(ref.Sprite())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
images = append(images, img)
|
|
|
|
}
|
|
|
|
|
|
|
|
return images, nil
|
|
|
|
}
|