Skip to content

Commit

Permalink
Merge pull request #289 from JiepengTan/builder#603_Animation_binding
Browse files Browse the repository at this point in the history
#builder #603 add default animation & animation binding
  • Loading branch information
xushiwei authored Jun 25, 2024
2 parents 8285bd8 + 5e4b01d commit ea894f5
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 15 deletions.
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ type spriteConfig struct {
Visible bool `json:"visible"`
IsDraggable bool `json:"isDraggable"`
Pivot math32.Vector2 `json:"pivot"`
DefaultAnimation string `json:"defaultAnimation"`
AnimBindings map[string]string `json:"animBindings"`
}

func (p *spriteConfig) getCostumeIndex() int {
Expand Down
21 changes: 19 additions & 2 deletions game.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,19 @@ func (p *Game) loadSprite(sprite Spriter, name string, gamer reflect.Value) erro
}

func spriteOf(sprite Spriter) *Sprite {
vSpr := reflect.ValueOf(sprite).Elem()
return vSpr.Field(0).Addr().Interface().(*Sprite)
vSpr := reflect.ValueOf(sprite)
if vSpr.Kind() != reflect.Ptr {
return nil
}
vSpr = vSpr.Elem()
if vSpr.Kind() != reflect.Struct || vSpr.NumField() < 1 {
return nil
}
spriteField := vSpr.Field(0)
if spriteField.Type() != reflect.TypeOf(Sprite{}) {
return nil
}
return spriteField.Addr().Interface().(*Sprite)
}

func (p *Game) loadIndex(g reflect.Value, proj *projConfig) (err error) {
Expand Down Expand Up @@ -408,6 +419,12 @@ func (p *Game) loadIndex(g reflect.Value, proj *projConfig) (err error) {
}
}
for _, ini := range inits {
spr := spriteOf(ini)
if spr != nil {
spr.OnStart(func() {
spr.awake()
})
}
ini.Main()
}

Expand Down
56 changes: 43 additions & 13 deletions sprite.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ const (
EdgeRight specialObj = touchingScreenRight
EdgeBottom specialObj = touchingScreenBottom
)
const (
StateDie string = "die"
StateTurn string = "turn"
StateGlide string = "glide"
)

type Sprite struct {
baseObj
Expand All @@ -63,10 +68,12 @@ type Sprite struct {
rRect *math32.RotatedRect
pivot math32.Vector2

sayObj *sayOrThinker
quoteObj *quoter
animations map[string]*aniConfig
greffUniforms map[string]interface{} // graphic effects
sayObj *sayOrThinker
quoteObj *quoter
animations map[string]*aniConfig
greffUniforms map[string]interface{} // graphic effects
animBindings map[string]string
defaultAnimation string

penColor color.RGBA
penShade float64
Expand Down Expand Up @@ -113,8 +120,13 @@ func (p *Sprite) init(
p.isVisible = sprite.Visible
p.pivot = sprite.Pivot

p.animations = make(map[string]*aniConfig)
p.animBindings = make(map[string]string)
for key, val := range sprite.AnimBindings {
p.animBindings[key] = val
}

p.defaultAnimation = sprite.DefaultAnimation
p.animations = make(map[string]*aniConfig)
for key, val := range sprite.FAnimations {
var ani = val
ani.AniType = aniTypeFrame
Expand Down Expand Up @@ -148,7 +160,13 @@ func (p *Sprite) init(
p.animations[key] = ani
}
}

func (p *Sprite) awake() {
if p.defaultAnimation != "" {
if p.isVisible {
p.Animate(p.defaultAnimation)
}
}
}
func (p *Sprite) InitFrom(src *Sprite) {
p.baseObj.initFrom(&src.baseObj)
p.eventSinks.initFrom(&src.eventSinks, p)
Expand Down Expand Up @@ -237,6 +255,9 @@ func cloneSprite(out reflect.Value, outPtr Spriter, in reflect.Value, v specsp)
if v != nil { // in loadSprite
applySpriteProps(dest, v)
} else { // in sprite.Clone
dest.OnCloned__1(func() {
dest.awake()
})
outPtr.Main()
}
return dest
Expand Down Expand Up @@ -403,7 +424,7 @@ func (p *Sprite) OnTurning__1(onTurning func()) {
}

func (p *Sprite) Die() { // prototype sprite can't be destroyed, but can die
const aniName = "die"
aniName := p.getStateAnimName(StateDie)
p.SetDying()
if ani, ok := p.animations[aniName]; ok {
p.goAnimate(aniName, ani)
Expand Down Expand Up @@ -522,6 +543,13 @@ func (p *Sprite) getFromAnToForAni(anitype aniTypeEnum, from interface{}, to int

}

func (p *Sprite) getStateAnimName(stateName string) string {
if bindingName, ok := p.animBindings[stateName]; ok {
return bindingName
}
return stateName
}

func (p *Sprite) goAnimate(name string, ani *aniConfig) {
if p.lastAnim != nil {
p.lastAnim.Stop()
Expand Down Expand Up @@ -785,7 +813,8 @@ func (p *Sprite) Glide__0(x, y float64, secs float64) {
To: math32.NewVector2(x, y),
AniType: aniTypeGlide,
}
p.goAnimate("glide", ani)
animName := p.getStateAnimName(StateGlide)
p.goAnimate(animName, ani)
}

func (p *Sprite) Glide__1(obj interface{}, secs float64) {
Expand Down Expand Up @@ -880,13 +909,13 @@ func (p *Sprite) Turn(val interface{}) {
default:
panic("Turn: unexpected input")
}

if ani, ok := p.animations["turn"]; ok {
animName := p.getStateAnimName(StateTurn)
if ani, ok := p.animations[animName]; ok {
anicopy := *ani
anicopy.From = p.direction
anicopy.To = p.direction + delta
anicopy.Duration = ani.Duration / 360.0 * math.Abs(delta)
p.goAnimate("turn", &anicopy)
p.goAnimate(animName, &anicopy)
return
}
if p.setDirection(delta, true) && debugInstr {
Expand Down Expand Up @@ -920,7 +949,8 @@ func (p *Sprite) TurnTo(obj interface{}) {
angle = 90 - math.Atan2(dy, dx)*180/math.Pi
}

if ani, ok := p.animations["turn"]; ok {
animName := p.getStateAnimName(StateTurn)
if ani, ok := p.animations[animName]; ok {
fromangle := math.Mod(p.direction+360.0, 360.0)
toangle := math.Mod(angle+360.0, 360.0)
if toangle-fromangle > 180.0 {
Expand All @@ -934,7 +964,7 @@ func (p *Sprite) TurnTo(obj interface{}) {
anicopy.From = fromangle
anicopy.To = toangle
anicopy.Duration = ani.Duration / 360.0 * math.Abs(delta)
p.goAnimate("turn", &anicopy)
p.goAnimate(animName, &anicopy)
return
}
if p.setDirection(angle, false) && debugInstr {
Expand Down

0 comments on commit ea894f5

Please sign in to comment.