ichigo/engine/animref.go

51 lines
963 B
Go
Raw Normal View History

2021-08-23 20:28:49 +10:00
package engine
import (
"encoding/gob"
"io/fs"
)
var (
animCache = make(map[assetKey]Anim)
2021-08-27 11:49:11 +10:00
_ interface {
Animer
Loader
} = &AnimRef{}
2021-08-23 20:28:49 +10:00
)
func init() {
2021-08-25 15:04:38 +10:00
gob.Register(&AnimRef{})
2021-08-23 20:28:49 +10:00
}
// AnimRef manages an Anim using a premade AnimDef from the cache.
type AnimRef struct {
Path string
anim Anim
}
func (r *AnimRef) Load(assets fs.FS) error {
// Fast path: set r.anim to a copy
anim, found := animCache[assetKey{assets, r.Path}]
if found {
r.anim = anim
return nil
}
// Slow path: load from gobz file
2021-08-26 11:31:39 +10:00
if err := LoadGobz(&r.anim, assets, r.Path); err != nil {
2021-08-23 20:28:49 +10:00
return err
}
animCache[assetKey{assets, r.Path}] = r.anim
return nil
}
// CurrentFrame returns the value of CurrentFrame from r.anim.
func (r *AnimRef) CurrentFrame() int { return r.anim.CurrentFrame() }
// Reset calls Reset on r.anim.
func (r *AnimRef) Reset() { r.anim.Reset() }
// Update calls Update on r.anim.
func (r *AnimRef) Update() error { return r.anim.Update() }