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

@ -3,26 +3,26 @@ package engine
// Anim is an "instance" of an AnimDef: an animation being displayed, // Anim is an "instance" of an AnimDef: an animation being displayed,
// together with the current state. // together with the current state.
type Anim struct { type Anim struct {
Def *AnimDef Def *AnimDef
CurrentIndex int Index int
CurrentTicks 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. // Update increments the tick count and advances the frame if necessary.
func (a *Anim) Update() error { func (a *Anim) Update() error {
a.CurrentTicks++ a.Ticks++
if !a.Def.Loop && a.CurrentIndex == len(a.Def.Frames)-1 { if !a.Def.Loop && a.Index == len(a.Def.Frames)-1 {
// on the last frame of a one shot so remain on final frame // on the last frame of a one shot so remain on final frame
return nil return nil
} }
if a.CurrentTicks >= a.Def.Frames[a.CurrentIndex].DurationTicks { if a.Ticks >= a.Def.Frames[a.Index].Duration {
a.CurrentTicks = 0 a.Ticks = 0
a.CurrentIndex++ a.Index++
} }
if a.Def.Loop && a.CurrentIndex >= len(a.Def.Frames) { if a.Def.Loop && a.Index >= len(a.Def.Frames) {
a.CurrentIndex = 0 a.Index = 0
} }
return nil return nil
} }
@ -35,6 +35,6 @@ type AnimDef struct {
// AnimFrame describes a frame in an animation. // AnimFrame describes a frame in an animation.
type AnimFrame struct { type AnimFrame struct {
Frame int `json:"frame"` // show this frame 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 animdef *AnimDef
} }
func (r *AnimRef) Anim() *Anim {
// TODO
return &Anim{
Def: r.animdef,
}
}
// ImageRef loads images from the AssetFS into *ebiten.Image form. // ImageRef loads images from the AssetFS into *ebiten.Image form.
// It is your responsibility to import _ "image/..." for whatever // It is your responsibility to import _ "image/..." for whatever
// format the files are in. // format the files are in.

14
main.go
View file

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