feat: better test for animation and the implementation.

This commit is contained in:
Andrey Parhomenko 2024-01-14 00:03:49 +03:00
parent f2d7feb8a4
commit 6994dff47b
3 changed files with 24 additions and 8 deletions

View file

@ -2,7 +2,7 @@ package gg
import ( import (
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
"fmt" //"fmt"
) )
type Animation []*Image type Animation []*Image
@ -45,7 +45,6 @@ func AnimationSetFromImage(
fw, fh := r.Dx()/w, r.Dy()/h fw, fh := r.Dx()/w, r.Dy()/h
for _, define := range defines { for _, define := range defines {
fmt.Println("def-idx-len:", len(define.Indexes))
animation := make(Animation, len(define.Indexes)) animation := make(Animation, len(define.Indexes))
for i := range animation { for i := range animation {
idx := define.Indexes[i] idx := define.Indexes[i]
@ -56,7 +55,6 @@ func AnimationSetFromImage(
}, },
)) ))
} }
fmt.Println("animation-len:", len(animation))
set[define.Id] = animation set[define.Id] = animation
} }
@ -76,6 +74,9 @@ type AnimatedSprite struct {
} }
func (as *AnimatedSprite) Animate(id AnimationId) bool { func (as *AnimatedSprite) Animate(id AnimationId) bool {
if as.AnimationId == id {
return true
}
_, ok := as.Animations[id] _, ok := as.Animations[id]
if ok { if ok {
as.duration = 0 as.duration = 0
@ -87,7 +88,6 @@ func (as *AnimatedSprite) Animate(id AnimationId) bool {
func (as *AnimatedSprite) Draw(c *Context) []EVertex { func (as *AnimatedSprite) Draw(c *Context) []EVertex {
as.duration += c.DrawDt() as.duration += c.DrawDt()
frames := as.Animations[as.AnimationId] frames := as.Animations[as.AnimationId]
fmt.Println("len:", len(frames))
fullTime := Duration(len(frames)) * as.TimeBetweenFrames fullTime := Duration(len(frames)) * as.TimeBetweenFrames
if as.duration > fullTime { if as.duration > fullTime {
as.duration -= fullTime as.duration -= fullTime

View file

@ -55,7 +55,8 @@ func main() {
playerAnimations, _ = gg.AnimationSetFromImage( playerAnimations, _ = gg.AnimationSetFromImage(
playerImg, playerImg,
8, 3, 8, 3,
gg.AD(Walk).DefRow(0, 0, 1, 2, 3, 4), gg.AD(Stand).DefRow(0, 0, 1, 2, 3, 4),
gg.AD(Walk).DefRow(1, 0, 1, 2, 3, 4, 5, 6, 7),
) )
player = NewPlayer() player = NewPlayer()
tri = NewTri() tri = NewTri()

View file

@ -20,11 +20,11 @@ func NewPlayer() *Player {
ret.Transform = gg.T() ret.Transform = gg.T()
ret.Scale = gg.V2(1) ret.Scale = gg.V2(1)
ret.Around = gg.V2(.5) ret.Around = gg.V2(.5)
ret.MoveSpeed = 90. ret.MoveSpeed = 30.
ret.ScaleSpeed = .2 ret.ScaleSpeed = .2
ret.Animations = playerAnimations ret.Animations = playerAnimations
ret.TimeBetweenFrames = time.Second/5 ret.TimeBetweenFrames = time.Second/10
fmt.Println("player-walk", ret.Animate(Walk)) fmt.Println("player-walk", ret.Animate(Stand))
ret.Visible = true ret.Visible = true
ret.Layer = PlayerL ret.Layer = PlayerL
@ -43,6 +43,7 @@ func (p *Player) Update(c *Context) {
cam := c.Camera cam := c.Camera
keys := c.Keys() keys := c.Keys()
walking := false
shift := c.IsPressed(gg.KeyShift) shift := c.IsPressed(gg.KeyShift)
//p.Uniforms["Random"] = any(rand.Float32()) //p.Uniforms["Random"] = any(rand.Float32())
for _, v := range keys { for _, v := range keys {
@ -59,12 +60,22 @@ func (p *Player) Update(c *Context) {
cam.Position.X += p.MoveSpeed * dt cam.Position.X += p.MoveSpeed * dt
case gg.KeyW: case gg.KeyW:
p.Position.Y += p.MoveSpeed * dt p.Position.Y += p.MoveSpeed * dt
walking = true
p.Animate(Walk)
case gg.KeyA: case gg.KeyA:
p.Position.X -= p.MoveSpeed * dt p.Position.X -= p.MoveSpeed * dt
p.Scale.X = -1
walking = true
p.Animate(Walk)
case gg.KeyS: case gg.KeyS:
p.Position.Y -= p.MoveSpeed * dt p.Position.Y -= p.MoveSpeed * dt
walking = true
p.Animate(Walk)
case gg.KeyD: case gg.KeyD:
p.Position.X += p.MoveSpeed * dt p.Position.X += p.MoveSpeed * dt
p.Scale.X = 1
walking = true
p.Animate(Walk)
case gg.KeyR: case gg.KeyR:
cam.Rotation += gg.Pi * p.ScaleSpeed * dt cam.Rotation += gg.Pi * p.ScaleSpeed * dt
case gg.KeyT: case gg.KeyT:
@ -104,6 +115,10 @@ func (p *Player) Update(c *Context) {
} }
} }
if !walking {
p.Animate(Stand)
}
for _, event := range c.Events { for _, event := range c.Events {
switch ec := event.(type) { switch ec := event.(type) {
case *gg.KeyDown: case *gg.KeyDown: