okay new ux idea

This commit is contained in:
2025-08-27 23:22:39 -04:00
parent 8b1bfc8a01
commit 573396054f
8 changed files with 134 additions and 210 deletions

View File

@@ -3,7 +3,7 @@ package sprites
import (
"image"
"git.vezzani.net/ben/games/common/window/v1"
"git.vezzani.net/ben/games/common/ux/v1"
"github.com/hajimehoshi/ebiten/v2"
)
@@ -16,9 +16,9 @@ func Update() {
}
type animation struct {
RowNumber uint8
FrameCount uint8
Scale float32
RowNumber uint8
TickCount uint8
Scale float32
}
type subImager interface {
@@ -31,7 +31,12 @@ type Sprite struct {
imgData subImager
animations map[string]animation
baseAnim *animation
baseAnim *animation
currentAnim *animation
animationStartedAt int
animationStopAt int
animateOnce bool
}
func (s *Sprite) baseAnimation() animation {
@@ -50,22 +55,17 @@ func (s *Sprite) baseAnimation() animation {
return animation{}
}
func (s *Sprite) getAnimation(id string) animation {
if id == "" {
return s.baseAnimation()
func (s *Sprite) getAnimation() animation {
if s.currentAnim != nil {
return *s.currentAnim
}
if anim, ok := s.animations[id]; ok {
return anim
}
return s.baseAnimation()
}
func (s *Sprite) Image(ops imageOptions) image.Image {
anim := s.getAnimation(ops.animation)
anim := s.getAnimation()
xOffset := updateCount % int(anim.FrameCount) * s.width
xOffset := updateCount % int(anim.TickCount) * s.width
yOffset := int(anim.RowNumber) * s.height
r := image.Rect(xOffset, yOffset, s.width, s.height).Intersect(s.imgData.Bounds())
@@ -74,9 +74,12 @@ func (s *Sprite) Image(ops imageOptions) image.Image {
}
func (s *Sprite) Draw(screen *ebiten.Image, options ...ImageOption) {
if s.getAnimation().RowNumber == 0 {
}
ops := imageOptions{
scaleX: window.Scale,
scaleY: window.Scale,
scaleX: ux.Scale,
scaleY: ux.Scale,
}
for _, o := range options {
@@ -98,11 +101,28 @@ func (s *Sprite) Draw(screen *ebiten.Image, options ...ImageOption) {
)
}
func (s *Sprite) StartAnimation(id string, once bool) {
if a, ok := s.animations[id]; ok {
s.currentAnim = &a
}
if once {
s.animateOnce = true
s.animationStopAt = updateCount + int(s.currentAnim.TickCount)
}
s.animationStartedAt = updateCount
}
func (s *Sprite) StopAnimation() {
s.currentAnim = nil
s.animationStartedAt = 0
s.animationStopAt = 0
s.animateOnce = false
}
type imageOptions struct {
x, y float64
scaleX, scaleY float64
rotateTheta float64
animation string
}
type ImageOption func(options *imageOptions)
@@ -114,12 +134,6 @@ func ToScale(x, y float64) ImageOption {
}
}
func Animation(name string) ImageOption {
return func(o *imageOptions) {
o.animation = name
}
}
func AtPosition(x, y float64) ImageOption {
return func(o *imageOptions) {
o.x, o.y = x, y