Files
pobounty/flame.go

70 lines
1.3 KiB
Go
Raw Normal View History

2023-09-17 13:34:03 -04:00
package main
import (
"bytes"
_ "embed"
"image"
_ "image/png"
"log"
"github.com/hajimehoshi/ebiten/v2"
)
const (
FLAME_ANIMATION_CYCLES = 8
FLAME_WIDTH = 24
FLAME_HEIGHT = 32
)
var (
//go:embed resources/images/hot.png
flameAssets_png []byte
assetsFlame *ebiten.Image
)
type Flame struct {
Sprite *ebiten.Image //our principle sprite representing the flame
cycle int //current animation cycle for the character
}
func NewFlame() *Flame {
return &Flame{
cycle: 0,
Sprite: ebiten.NewImage(FLAME_WIDTH, FLAME_HEIGHT),
}
}
func (f *Flame) CycleUpdate() {
f.cycle = (f.cycle + 1) % CHARACTER_ANIMATION_CYCLES
}
func (f *Flame) Animate() {
//compute start and end location of asset to use for animation frame cycle
sx := FLAME_WIDTH * f.cycle
sy := 0
ex := FLAME_WIDTH * (f.cycle + 1)
ey := FLAME_HEIGHT
f.Sprite.Clear()
f.Sprite.DrawImage(assetsFlame.SubImage(image.Rect(sx, sy, ex, ey)).(*ebiten.Image), nil)
f.CycleUpdate()
}
func (f *Flame) GetWidth() int {
return f.Sprite.Bounds().Dx()
}
func (f *Flame) GetHeight() int {
return f.Sprite.Bounds().Dy()
}
func init() {
img, _, err := image.Decode(bytes.NewReader(flameAssets_png))
if err != nil {
log.Fatal(err)
}
assetsFlame = ebiten.NewImageFromImage(img)
}