progress
This commit is contained in:
parent
7fbf4d7a71
commit
2fc3251c9b
3 changed files with 27 additions and 20 deletions
|
@ -4,25 +4,25 @@ package engine
|
||||||
// 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
|
||||||
}
|
}
|
||||||
|
@ -36,5 +36,5 @@ 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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
14
main.go
|
@ -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,
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in a new issue