This commit is contained in:
Josh Deprez 2021-07-31 17:17:26 +10:00 committed by Josh Deprez
parent 7fbf4d7a71
commit 2fc3251c9b
3 changed files with 27 additions and 20 deletions

View file

@ -4,25 +4,25 @@ package engine
// together with the current state.
type Anim struct {
Def *AnimDef
CurrentIndex int
CurrentTicks int
Index int
Ticks int
}
func (a *Anim) CurrentFrame() int { return a.Def.Frames[a.CurrentIndex].Frame }
func (a *Anim) CurrentFrame() int { return a.Def.Frames[a.Index].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 {
a.Ticks++
if !a.Def.Loop && a.Index == 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.Ticks >= a.Def.Frames[a.Index].Duration {
a.Ticks = 0
a.Index++
}
if a.Def.Loop && a.CurrentIndex >= len(a.Def.Frames) {
a.CurrentIndex = 0
if a.Def.Loop && a.Index >= len(a.Def.Frames) {
a.Index = 0
}
return nil
}
@ -36,5 +36,5 @@ type AnimDef struct {
// AnimFrame describes a frame in an animation.
type AnimFrame struct {
Frame int `json:"frame"` // show this frame
DurationTicks int `json:"duration"` // for this long
Duration int `json:"duration"` // for this long, in ticks
}

View file

@ -20,6 +20,13 @@ type AnimRef struct {
animdef *AnimDef
}
func (r *AnimRef) Anim() *Anim {
// TODO
return &Anim{
Def: r.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.

14
main.go
View file

@ -50,9 +50,9 @@ func main() {
Anim: engine.Anim{
Def: &engine.AnimDef{
Frames: []engine.AnimFrame{
{Frame: 0, DurationTicks: 16},
{Frame: 1, DurationTicks: 16},
{Frame: 2, DurationTicks: 16},
{Frame: 0, Duration: 16},
{Frame: 1, Duration: 16},
{Frame: 2, Duration: 16},
},
Loop: true,
},
@ -62,10 +62,10 @@ func main() {
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},
{Frame: 3, Duration: 12},
{Frame: 4, Duration: 12},
{Frame: 5, Duration: 12},
{Frame: 6, Duration: 12},
},
Loop: true,
},