package hud import ( "fmt" "image/color" "github.com/chompy/roguelike_rpg/internal/client/gfx" "github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2/text/v2" ) type BarConfig struct { Width int Height int Color color.Color } type Bar struct { Current int Transition int Max int Config BarConfig } func NewBar(config BarConfig) *Bar { return &Bar{ Config: config, } } func (b *Bar) Draw(x, y int, time float32, screen *ebiten.Image, font *gfx.Font) { w := b.Config.Width h := b.Config.Height if b.Max <= 0 { return } clamped := b.Current if clamped < 0 { clamped = 0 } if clamped > b.Max { clamped = b.Max } filled := float64(clamped) / float64(b.Max) * float64(w) bg := color.NRGBA{0x30, 0x30, 0x30, 0xff} barImg := ebiten.NewImage(w, h) barImg.Fill(bg) fc := b.Config.Color if fc == nil { fc = color.NRGBA{0xff, 0x40, 0x40, 0xff} } fillImg := ebiten.NewImage(int(filled), h) fillImg.Fill(fc) op := &ebiten.DrawImageOptions{} barImg.DrawImage(fillImg, op) if b.Transition >= 0 && b.Transition != b.Current { tClamped := b.Transition if tClamped < 0 { tClamped = 0 } if tClamped > b.Max { tClamped = b.Max } tFilled := float64(tClamped) / float64(b.Max) * float64(w) if tFilled > 0 { overlayImg := ebiten.NewImage(int(tFilled), h) overlayImg.Fill(color.NRGBA{0xff, 0xff, 0xff, 0xa0}) op := &ebiten.DrawImageOptions{} barImg.DrawImage(overlayImg, op) } } op = &ebiten.DrawImageOptions{} op.GeoM.Translate(float64(x), float64(y)) screen.DrawImage(barImg, op) if font != nil { label := fmt.Sprintf("%d/%d", b.Current, b.Max) lw, lh := text.Measure(label, font.Font, 0) op := &text.DrawOptions{} op.GeoM.Translate(float64(x)+float64(w)/2-lw/2, float64(y)+float64(h)/2-lh/2) text.Draw(screen, label, font.Font, op) } }