diff --git a/cmd/tins2021/levelcontroller.go b/cmd/tins2021/levelcontroller.go index d935932..2d3848c 100644 --- a/cmd/tins2021/levelcontroller.go +++ b/cmd/tins2021/levelcontroller.go @@ -1,6 +1,7 @@ package main import ( + "image" "image/color" "math/rand" "strconv" @@ -35,17 +36,27 @@ func newLevelControl(ctx ui.Context, level *tins2021.Level) *levelController { "blocked": newCubeTexture(textures, tins2021.Purple), "colored": newCubeTexture(textures, tins2021.Blue), } - newAnimatedTexture(textures, "star", tins2021.CreateStar(5), tins2021.Yellow, tins2021.NewRotateAnimation(defaultAnimationFrames)) - newAnimatedTexture(textures, "heart", tins2021.CreateHeart(), tins2021.Red, tins2021.NewRotateAnimation(defaultAnimationFrames)) + newAnimatedTexture(ctx, "star", "resources/images/star.png", func() image.Image { + return tins2021.AnimatePolygon(tins2021.CreateStar(5), tins2021.Yellow, tins2021.NewRotateAnimation(defaultAnimationFrames)) + }) + newAnimatedTexture(ctx, "heart", "resources/images/heart.png", func() image.Image { + return tins2021.AnimatePolygon(tins2021.CreateHeart(), tins2021.Red, tins2021.NewRotateAnimation(defaultAnimationFrames)) + }) control.MonsterTextureNames = map[tins2021.MonsterType]string{ tins2021.MonsterTypeStraight: "straight-walking-monster", tins2021.MonsterTypeRandom: "random-walking-monster", tins2021.MonsterTypeChaser: "chasing-monster", } - newAnimatedTexture(textures, control.MonsterTextureNames[tins2021.MonsterTypeStraight], tins2021.CreateHexagon(), tins2021.Green, tins2021.NewWobbleAnimation(defaultAnimationFrames, 30)) - newAnimatedTexture(textures, control.MonsterTextureNames[tins2021.MonsterTypeRandom], tins2021.CreateHexagon(), tins2021.Blue, tins2021.NewWobbleAnimation(defaultAnimationFrames, 30)) - newAnimatedTexture(textures, control.MonsterTextureNames[tins2021.MonsterTypeChaser], tins2021.CreateHexagon(), tins2021.Purple, tins2021.NewWobbleAnimation(defaultAnimationFrames, 30)) + newAnimatedTexture(ctx, control.MonsterTextureNames[tins2021.MonsterTypeStraight], "resources/images/monster-straight.png", func() image.Image { + return tins2021.AnimatePolygon(tins2021.CreateHexagon(), tins2021.Green, tins2021.NewWobbleAnimation(defaultAnimationFrames, 30)) + }) + newAnimatedTexture(ctx, control.MonsterTextureNames[tins2021.MonsterTypeRandom], "resources/images/monster-random.png", func() image.Image { + return tins2021.AnimatePolygon(tins2021.CreateHexagon(), tins2021.Blue, tins2021.NewWobbleAnimation(defaultAnimationFrames, 30)) + }) + newAnimatedTexture(ctx, control.MonsterTextureNames[tins2021.MonsterTypeChaser], "resources/images/monster-chaser.png", func() image.Image { + return tins2021.AnimatePolygon(tins2021.CreateHexagon(), tins2021.Purple, tins2021.NewWobbleAnimation(defaultAnimationFrames, 30)) + }) small, err := tins2021.NewBitmapFont(ctx.Renderer(), ctx.Fonts().Font("small"), tins2021.AllCharacters...) if err != nil { diff --git a/cmd/tins2021/resources/images/heart.png b/cmd/tins2021/resources/images/heart.png new file mode 100644 index 0000000..a8a74ec Binary files /dev/null and b/cmd/tins2021/resources/images/heart.png differ diff --git a/cmd/tins2021/resources/images/monster-chaser.png b/cmd/tins2021/resources/images/monster-chaser.png new file mode 100644 index 0000000..2507c85 Binary files /dev/null and b/cmd/tins2021/resources/images/monster-chaser.png differ diff --git a/cmd/tins2021/resources/images/monster-random.png b/cmd/tins2021/resources/images/monster-random.png new file mode 100644 index 0000000..d2fa3c6 Binary files /dev/null and b/cmd/tins2021/resources/images/monster-random.png differ diff --git a/cmd/tins2021/resources/images/monster-straight.png b/cmd/tins2021/resources/images/monster-straight.png new file mode 100644 index 0000000..28cbc9f Binary files /dev/null and b/cmd/tins2021/resources/images/monster-straight.png differ diff --git a/cmd/tins2021/resources/images/star.png b/cmd/tins2021/resources/images/star.png new file mode 100644 index 0000000..0f2c8e4 Binary files /dev/null and b/cmd/tins2021/resources/images/star.png differ diff --git a/cmd/tins2021/textures.go b/cmd/tins2021/textures.go index 03bb555..3009d3e 100644 --- a/cmd/tins2021/textures.go +++ b/cmd/tins2021/textures.go @@ -2,8 +2,9 @@ package main import ( "image" + "image/png" + "os" - "opslag.de/schobers/geom" "opslag.de/schobers/tins2021" "opslag.de/schobers/zntg/ui" ) @@ -44,10 +45,39 @@ func (t cubeTexture) Scaled(textures *ui.Textures, scale float32) cubeTexture { } } -func newAnimatedTexture(textures *ui.Textures, name string, polygon geom.PolygonF, color string, animation tins2021.AnimationRenderer) tins2021.AnimatedTexture { - texture, err := textures.CreateTextureGo(name, tins2021.AnimatePolygon(polygon, color, animation), true) +func loadTextureImage(ctx ui.Context, resource string) image.Image { + in, err := ctx.Resources().OpenResource(resource) + if err != nil { + return nil + } + defer in.Close() + im, err := png.Decode(in) + if err != nil { + return nil + } + return im +} + +func saveTextureImage(resource string, im image.Image) { + out, err := os.Create(resource) + if err != nil { + return + } + defer out.Close() + png.Encode(out, im) +} + +func newAnimatedTexture(ctx ui.Context, name, resource string, render animatedTextureRenderFn) tins2021.AnimatedTexture { + raw := loadTextureImage(ctx, resource) + if raw == nil { + raw = render() + saveTextureImage(resource, raw) + } + texture, err := ctx.Textures().CreateTextureGo(name, raw, true) if err != nil { panic(err) } return tins2021.NewSquareAnimatedTexture(texture) } + +type animatedTextureRenderFn func() image.Image