From 90b79c7e4977c78ad9304db4b2c367899ad01834 Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Mon, 9 Aug 2021 17:26:28 +0200 Subject: [PATCH] Fixed wobble animation (animation was not stable). - Scaling textures to integer width/height. --- TODO.md | 2 +- animation.go | 12 ++++-------- cmd/tins2021/appcontext.go | 10 +++++----- cmd/tins2021/levelcontroller.go | 6 +++--- cmd/tins2021/textures.go | 4 ++-- 5 files changed, 15 insertions(+), 19 deletions(-) diff --git a/TODO.md b/TODO.md index 9c41956..f876cf6 100644 --- a/TODO.md +++ b/TODO.md @@ -6,5 +6,5 @@ - [ ] Add monster animations (jumping on tile & towards new tile). - [ ] Scale icons (heart & star on right side) when playing. - [ ] Change layout when playing in portrait mode. -- [ ] Fix wobble animation. +- [X] Fix wobble animation. - [ ] Add more unit tests? \ No newline at end of file diff --git a/animation.go b/animation.go index 50ba6c9..2188356 100644 --- a/animation.go +++ b/animation.go @@ -25,14 +25,10 @@ func NewAnimatedTexture(texture ui.Texture, n int) AnimatedTexture { return AnimatedTexture{Texture: texture, Frames: frames} } -func NewSquareAnimatedTexture(texture ui.Texture) AnimatedTexture { - var frames []geom.RectangleF32 - height := float32(texture.Height()) - width := float32(texture.Width()) - for left := float32(0); left < width; left += height { - frames = append(frames, geom.RectF32(left, 0, left+height, height)) - } - return AnimatedTexture{Texture: texture, Frames: frames} +func FitAnimatedTexture(textures *ui.Textures, name string, scale float32, n int) AnimatedTexture { + height := float32(textures.Texture(name).Height()) + scale = geom.Round32(height*scale) / height // clip scale to integer width/height + return NewAnimatedTexture(textures.ScaledByName(name, scale), n) } func (t AnimatedTexture) Scale(scale float32) AnimatedTexture { diff --git a/cmd/tins2021/appcontext.go b/cmd/tins2021/appcontext.go index f2bf637..5d8a09e 100644 --- a/cmd/tins2021/appcontext.go +++ b/cmd/tins2021/appcontext.go @@ -16,10 +16,10 @@ type appContext struct { } func newAppContext(ctx ui.Context, setView func(ui.Control)) *appContext { - newAnimatedTexture(ctx, "star", "images/star.png", func() image.Image { + newAnimatedTexture(ctx, "star", "images/star.png", defaultAnimationFrames, func() image.Image { return tins2021.AnimatePolygon(tins2021.CreateStar(5), tins2021.Yellow, tins2021.NewRotateAnimation(defaultAnimationFrames)) }) - newAnimatedTexture(ctx, "heart", "images/heart.png", func() image.Image { + newAnimatedTexture(ctx, "heart", "images/heart.png", defaultAnimationFrames, func() image.Image { return tins2021.AnimatePolygon(tins2021.CreateHeart(), tins2021.Red, tins2021.NewRotateAnimation(defaultAnimationFrames)) }) @@ -31,13 +31,13 @@ func newAppContext(ctx ui.Context, setView func(ui.Control)) *appContext { tins2021.MonsterTypeChaser: "chasing-monster", }, } - newAnimatedTexture(ctx, app.MonsterTextureNames[tins2021.MonsterTypeStraight], "images/monster-straight.png", func() image.Image { + newAnimatedTexture(ctx, app.MonsterTextureNames[tins2021.MonsterTypeStraight], "images/monster-straight.png", defaultAnimationFrames, func() image.Image { return tins2021.AnimatePolygon(tins2021.CreateHexagon(), tins2021.Green, tins2021.NewWobbleAnimation(defaultAnimationFrames, 30)) }) - newAnimatedTexture(ctx, app.MonsterTextureNames[tins2021.MonsterTypeRandom], "images/monster-random.png", func() image.Image { + newAnimatedTexture(ctx, app.MonsterTextureNames[tins2021.MonsterTypeRandom], "images/monster-random.png", defaultAnimationFrames, func() image.Image { return tins2021.AnimatePolygon(tins2021.CreateHexagon(), tins2021.Blue, tins2021.NewWobbleAnimation(defaultAnimationFrames, 30)) }) - newAnimatedTexture(ctx, app.MonsterTextureNames[tins2021.MonsterTypeChaser], "images/monster-chaser.png", func() image.Image { + newAnimatedTexture(ctx, app.MonsterTextureNames[tins2021.MonsterTypeChaser], "images/monster-chaser.png", defaultAnimationFrames, func() image.Image { return tins2021.AnimatePolygon(tins2021.CreateHexagon(), tins2021.Purple, tins2021.NewWobbleAnimation(defaultAnimationFrames, 30)) }) return app diff --git a/cmd/tins2021/levelcontroller.go b/cmd/tins2021/levelcontroller.go index caecce3..0bd7e06 100644 --- a/cmd/tins2021/levelcontroller.go +++ b/cmd/tins2021/levelcontroller.go @@ -196,11 +196,11 @@ func (r levelController) Render(ctx ui.Context) { cubeHeight := float32(cubes.Normal.Height()) player := ctx.Textures().ScaledByName("dwarf", scale*.6) - star := tins2021.NewAnimatedTexture(ctx.Textures().ScaledByName("star", scale*.4), defaultAnimationFrames) - heart := tins2021.NewAnimatedTexture(ctx.Textures().ScaledByName("heart", scale*.4), defaultAnimationFrames) + star := tins2021.FitAnimatedTexture(textures, "star", scale*.4, defaultAnimationFrames) + heart := tins2021.FitAnimatedTexture(textures, "heart", scale*.4, defaultAnimationFrames) monsterTextures := map[tins2021.MonsterType]tins2021.AnimatedTexture{} for typ, name := range r.app.MonsterTextureNames { - monsterTextures[typ] = tins2021.NewAnimatedTexture(ctx.Textures().ScaledByName(name, scale*.4), defaultAnimationFrames) + monsterTextures[typ] = tins2021.FitAnimatedTexture(textures, name, scale*.4, defaultAnimationFrames) } propOffset := geom.PtF32(-.5*float32(star.Texture.Height()), -.8*float32(star.Texture.Height())) diff --git a/cmd/tins2021/textures.go b/cmd/tins2021/textures.go index 3009d3e..6228cb3 100644 --- a/cmd/tins2021/textures.go +++ b/cmd/tins2021/textures.go @@ -67,7 +67,7 @@ func saveTextureImage(resource string, im image.Image) { png.Encode(out, im) } -func newAnimatedTexture(ctx ui.Context, name, resource string, render animatedTextureRenderFn) tins2021.AnimatedTexture { +func newAnimatedTexture(ctx ui.Context, name, resource string, frames int, render animatedTextureRenderFn) tins2021.AnimatedTexture { raw := loadTextureImage(ctx, resource) if raw == nil { raw = render() @@ -77,7 +77,7 @@ func newAnimatedTexture(ctx ui.Context, name, resource string, render animatedTe if err != nil { panic(err) } - return tins2021.NewSquareAnimatedTexture(texture) + return tins2021.NewAnimatedTexture(texture, frames) } type animatedTextureRenderFn func() image.Image