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

View file

@ -55,7 +55,8 @@ func main() {
playerAnimations, _ = gg.AnimationSetFromImage(
playerImg,
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()
tri = NewTri()

View file

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