Anim
This commit is contained in:
parent
3f4da9f6bd
commit
7fbf4d7a71
4 changed files with 72 additions and 30 deletions
40
engine/anim.go
Normal file
40
engine/anim.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
package engine
|
||||
|
||||
// Anim is an "instance" of an AnimDef: an animation being displayed,
|
||||
// together with the current state.
|
||||
type Anim struct {
|
||||
Def *AnimDef
|
||||
CurrentIndex int
|
||||
CurrentTicks int
|
||||
}
|
||||
|
||||
func (a *Anim) CurrentFrame() int { return a.Def.Frames[a.CurrentIndex].Frame }
|
||||
|
||||
// Update increments the tick count and advances the frame if necessary.
|
||||
func (a *Anim) Update() error {
|
||||
a.CurrentTicks++
|
||||
if !a.Def.Loop && a.CurrentIndex == len(a.Def.Frames)-1 {
|
||||
// on the last frame of a one shot so remain on final frame
|
||||
return nil
|
||||
}
|
||||
if a.CurrentTicks >= a.Def.Frames[a.CurrentIndex].DurationTicks {
|
||||
a.CurrentTicks = 0
|
||||
a.CurrentIndex++
|
||||
}
|
||||
if a.Def.Loop && a.CurrentIndex >= len(a.Def.Frames) {
|
||||
a.CurrentIndex = 0
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AnimDef describes an animation (sequence of frames and timings).
|
||||
type AnimDef struct {
|
||||
Frames []AnimFrame `json:"frames"`
|
||||
Loop bool `json:"loop"`
|
||||
}
|
||||
|
||||
// AnimFrame describes a frame in an animation.
|
||||
type AnimFrame struct {
|
||||
Frame int `json:"frame"` // show this frame
|
||||
DurationTicks int `json:"duration"` // for this long
|
||||
}
|
|
@ -14,12 +14,23 @@ var (
|
|||
imageCache = make(map[string]*ebiten.Image)
|
||||
)
|
||||
|
||||
type AnimRef struct {
|
||||
Path string
|
||||
|
||||
animdef *AnimDef
|
||||
}
|
||||
|
||||
// ImageRef loads images from the AssetFS into *ebiten.Image form.
|
||||
// It is your responsibility to import _ "image/..." for whatever
|
||||
// format the files are in.
|
||||
type ImageRef struct {
|
||||
Path string
|
||||
|
||||
image *ebiten.Image
|
||||
}
|
||||
|
||||
// Image returns the image. If it hasn't been loaded yet, it loads.
|
||||
// Multiple distinct ImageRefs can use the same path.
|
||||
func (r *ImageRef) Image() (*ebiten.Image, error) {
|
||||
if r.image != nil {
|
||||
return r.image, nil
|
||||
|
|
|
@ -63,26 +63,7 @@ type StaticTile int
|
|||
func (s StaticTile) TileIndex() int { return int(s) }
|
||||
|
||||
type AnimatedTile struct {
|
||||
Frame int // index into AnimDef
|
||||
DurationTicks int // time spent showing current frame
|
||||
AnimDef []TileAnimFrameDef
|
||||
Anim
|
||||
}
|
||||
|
||||
func (a *AnimatedTile) TileIndex() int { return a.AnimDef[a.Frame].Tile }
|
||||
|
||||
func (a *AnimatedTile) Update() error {
|
||||
a.DurationTicks++
|
||||
if a.DurationTicks >= a.AnimDef[a.Frame].DurationTicks {
|
||||
a.DurationTicks = 0
|
||||
a.Frame++
|
||||
}
|
||||
if a.Frame >= len(a.AnimDef) {
|
||||
a.Frame = 0
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type TileAnimFrameDef struct {
|
||||
Tile int // show this tile
|
||||
DurationTicks int // show it for this long
|
||||
}
|
||||
func (a *AnimatedTile) TileIndex() int { return a.CurrentFrame() }
|
||||
|
|
28
main.go
28
main.go
|
@ -47,18 +47,28 @@ func main() {
|
|||
}
|
||||
|
||||
tiles[4][5] = &engine.AnimatedTile{
|
||||
AnimDef: []engine.TileAnimFrameDef{
|
||||
{Tile: 0, DurationTicks: 16},
|
||||
{Tile: 1, DurationTicks: 16},
|
||||
{Tile: 2, DurationTicks: 16},
|
||||
Anim: engine.Anim{
|
||||
Def: &engine.AnimDef{
|
||||
Frames: []engine.AnimFrame{
|
||||
{Frame: 0, DurationTicks: 16},
|
||||
{Frame: 1, DurationTicks: 16},
|
||||
{Frame: 2, DurationTicks: 16},
|
||||
},
|
||||
Loop: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
tiles[6][7] = &engine.AnimatedTile{
|
||||
AnimDef: []engine.TileAnimFrameDef{
|
||||
{Tile: 3, DurationTicks: 12},
|
||||
{Tile: 4, DurationTicks: 12},
|
||||
{Tile: 5, DurationTicks: 12},
|
||||
{Tile: 6, DurationTicks: 12},
|
||||
Anim: engine.Anim{
|
||||
Def: &engine.AnimDef{
|
||||
Frames: []engine.AnimFrame{
|
||||
{Frame: 3, DurationTicks: 12},
|
||||
{Frame: 4, DurationTicks: 12},
|
||||
{Frame: 5, DurationTicks: 12},
|
||||
{Frame: 6, DurationTicks: 12},
|
||||
},
|
||||
Loop: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue