Fixed wobble animation (animation was not stable).

- Scaling textures to integer width/height.
This commit is contained in:
Sander Schobers 2021-08-09 17:26:28 +02:00
parent 6ce3d84417
commit 90b79c7e49
5 changed files with 15 additions and 19 deletions

View File

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

View File

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

View File

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

View File

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

View File

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