feat: started implementing animations and also added counters for draw frames and frames.

This commit is contained in:
Andrey Parhomenko 2024-01-13 21:45:56 +03:00
parent ab406b1249
commit 60cd4e9e75
4 changed files with 78 additions and 6 deletions

34
animation.go Normal file
View file

@ -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
}

View file

@ -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,
))

View file

@ -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()

View file

@ -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()
}