From 60cd4e9e754cdfbe92bcf786a0815b68cddb4d0c Mon Sep 17 00:00:00 2001 From: surdeus Date: Sat, 13 Jan 2024 21:45:56 +0300 Subject: [PATCH] feat: started implementing animations and also added counters for draw frames and frames. --- animation.go | 34 ++++++++++++++++++++++++++++++++++ cmd/test/debug.go | 6 ++++++ cmd/test/text.go | 17 +++++++++++++++-- engine.go | 27 +++++++++++++++++++++++---- 4 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 animation.go diff --git a/animation.go b/animation.go new file mode 100644 index 0000000..fb4b1be --- /dev/null +++ b/animation.go @@ -0,0 +1,34 @@ +package gg + +import ( + "time" +) + +type Animation struct { + Frames []*Image +} + +type AnimationId int +type AnimationSet map[AnimationId] *Animation + +// The type implements animated sprites. +type AnimatedSprite struct { + Sprite + Animations AnimationSet + AnimationId AnimationId + CurrentFrame int + TimeBetweenFrames time.Duration +} + +func (as *AnimatedSprite) Animate(id AnimationId) bool { + _, ok := as.Animations[id] + if ok { + as.AnimationId = id + } + return ok +} + +func (as *AnimatedSprite) Draw(c *Context) []EVertex { + return nil +} + diff --git a/cmd/test/debug.go b/cmd/test/debug.go index f869971..34669e3 100644 --- a/cmd/test/debug.go +++ b/cmd/test/debug.go @@ -23,6 +23,12 @@ func (d *Debug) Draw(c *Context) []gg.EVertex { keyStrs = append(keyStrs, fmt.Sprintf( "fps: %d", int(c.Fps()), )) + keyStrs = append(keyStrs, fmt.Sprintf( + "dframe: %d", int(c.Dframe()), + )) + keyStrs = append(keyStrs, fmt.Sprintf( + "frame: %d", int(c.Frame()), + )) keyStrs = append(keyStrs, fmt.Sprintf( "relPlayerPos: %v", player.Position, )) diff --git a/cmd/test/text.go b/cmd/test/text.go index 71c98a5..9cca67c 100644 --- a/cmd/test/text.go +++ b/cmd/test/text.go @@ -4,8 +4,21 @@ import ( "vultras.su/core/gg" ) -type Text struct { - gg.Text +type Objecter interface{ } +type Wrap[V Objecter] struct { + O V +} + +//func (w *Wrap) + +type Context2 struct { + *gg.Context +} + +type Text Wrap[struct{ + gg.Text +}] + //func (txt *Text) Update() diff --git a/engine.go b/engine.go index 5b1c82c..77d4648 100644 --- a/engine.go +++ b/engine.go @@ -61,10 +61,12 @@ type Engine struct { // If is set to nil then the engine will panic. Camera *Camera - // The same delta time for all frames - // and all objects. lastTime time.Time - dt, fdt Float + dt Float + + // Frame delta time. + fdt Float + fLastTime time.Time // Temporary stuff keys, prevKeys []Key @@ -77,6 +79,12 @@ type Engine struct { //bufs [LayerBufSize]*Image vertices map[Layer] []ebiten.Vertex //vindices []uint16 + + // Draw frame. + dframe uint + + // Frame. + frame uint } type engine Engine @@ -220,6 +228,7 @@ func (e *Engine) AbsCursorPosition() Vector { func (e *engine) Update() error { eng := (*Engine)(e) + e.fdt = time.Since(e.fLastTime).Seconds() // Buffering the context for faster. @@ -293,7 +302,8 @@ func (e *engine) Update() error { object.Input() <- c } e.wg.Wait() - + e.fLastTime = time.Now() + e.frame++ return nil } @@ -369,6 +379,7 @@ func (e *engine) Draw(img *ebiten.Image) { // Empty the buff to generate it again. eng.Camera.buffered = false e.lastTime = time.Now() + e.dframe++ } func (e *engine) Layout(ow, oh int) (int, int) { @@ -384,11 +395,19 @@ func (e *Engine) Dt() Float { return e.dt } +func (e *Engine) Dframe() uint { + return e.dframe +} + // Return the current fixed delta time. func (e *Engine) Fdt() Float { return 1/60 } +func (e *Engine) Frame() uint { + return e.frame +} + func (e *Engine) Fps() float64 { return ebiten.ActualFPS() }