Skip to content

Commit

Permalink
Merge pull request #290 from JiepengTan/builder#604playing_sound_on_a…
Browse files Browse the repository at this point in the history
…nimation

#builder #604 adjust sound-playing behavior for animation
  • Loading branch information
xushiwei authored Jun 20, 2024
2 parents 55a67ad + e509a47 commit 03f76f4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ type aniConfig struct {
AniType aniTypeEnum `json:"anitype"`
OnStart *actionConfig `json:"onStart"` //start
OnPlay *actionConfig `json:"onPlay"` //play
IsLoop bool `json:"isLoop"`
//OnEnd *actionConfig `json:"onEnd"` //stop
}

Expand Down
20 changes: 12 additions & 8 deletions internal/anim/anim.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ type Animation struct {
currentFrame int
preFrame int

preRepeatCount int
//playing
playingCallback func(*Animation, int, interface{})
playingCallback func(*Animation, int, bool, interface{})
//stop
stopCallback func(*Animation)
//error
Expand Down Expand Up @@ -149,7 +150,7 @@ func (this *Animation) SetEasingFunction(easingFunc tools.IEasingFunction) {
this.easingFunction = easingFunc
}

func (this *Animation) SetOnPlayingListener(playfuc func(*Animation, int, interface{})) {
func (this *Animation) SetOnPlayingListener(playfuc func(*Animation, int, bool, interface{})) {
this.playingCallback = playfuc
}

Expand All @@ -168,7 +169,7 @@ func (this *Animation) Init(name string, framePerSecond float64, dataType int, l
this.DataType = dataType
this.currentFrame = math.MaxInt32
this.preFrame = math.MinInt32

this.preRepeatCount = 0
globalAnimId++

if loopMode == -1 {
Expand Down Expand Up @@ -341,12 +342,15 @@ func (this *Animation) Animate(target IAnimationTarget, delay float64, from int,
// Compute ratio
rangeval := float64(to + 1 - from)
ratio := delay * float64(this.FramePerSecond*speedRatio) / 1000.0

repeatCount := int(ratio/rangeval) >> 0
isReplay := repeatCount != this.preRepeatCount
if isReplay {
this.preRepeatCount = repeatCount
}
if ratio >= rangeval && !loop { // If we are out of range and not looping get back to caller

//add compete
if this.playingCallback != nil && this.preFrame != to && len(this.keys) > 1 {
this.playingCallback(this, to, this.keys[len(this.keys)-1].Value)
this.playingCallback(this, to, isReplay, this.keys[len(this.keys)-1].Value)
}

//stop callback
Expand All @@ -355,7 +359,7 @@ func (this *Animation) Animate(target IAnimationTarget, delay float64, from int,
}
return false
}
repeatCount := int(ratio/rangeval) >> 0

this.currentFrame = from
if rangeval != 0 {
this.currentFrame = from + int(ratio)%int(rangeval)
Expand Down Expand Up @@ -412,7 +416,7 @@ func (this *Animation) Animate(target IAnimationTarget, delay float64, from int,
currentValue := this._interpolate(this.currentFrame, repeatCount, this.LoopMode, offsetValue, highLimitValue)

if this.playingCallback != nil {
this.playingCallback(this, this.currentFrame, currentValue)
this.playingCallback(this, this.currentFrame, isReplay, currentValue)
}

// // Set value
Expand Down
9 changes: 4 additions & 5 deletions internal/anim/animapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Anim struct {
keyframelist []*AnimationKeyFrame

//playing
playingCallback func(int, interface{})
playingCallback func(int, bool, interface{})
//stop
stopCallback func()
}
Expand All @@ -43,9 +43,9 @@ func NewAnim(name string, valtype ANIMVALTYPE, fps float64, totalframe int) *Ani
}
a.animation = NewAnimation(name, fps, (int)(valtype), ANIMATIONLOOPMODE_CYCLE)
a.Id = int(a.animation.GetAnimId())
a.animation.SetOnPlayingListener(func(an *Animation, currframe int, currval interface{}) {
a.animation.SetOnPlayingListener(func(an *Animation, currframe int, isReplay bool, currval interface{}) {
if a.playingCallback != nil {
a.playingCallback(currframe, currval)
a.playingCallback(currframe, isReplay, currval)
}
})
a.animation.SetOnStopingListener(func(an *Animation) {
Expand Down Expand Up @@ -82,7 +82,7 @@ func (a *Anim) SetLoop(isloop bool) *Anim {
return a
}

func (a *Anim) SetOnPlayingListener(playfuc func(int, interface{})) *Anim {
func (a *Anim) SetOnPlayingListener(playfuc func(int, bool, interface{})) *Anim {
a.playingCallback = playfuc
return a
}
Expand All @@ -108,7 +108,6 @@ func (a *Anim) Stop() *Anim {
return a
}

//
func (a *Anim) Update(delay float64) bool {
if a.status == AnimstatusStop {
return false
Expand Down
23 changes: 16 additions & 7 deletions sprite.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ type Sprite struct {
hasOnCloned bool
hasOnTouched bool

gamer reflect.Value
gamer reflect.Value
lastAnim *anim.Anim
}

func (p *Sprite) SetDying() { // dying: visible but can't be touched
Expand Down Expand Up @@ -521,13 +522,14 @@ func (p *Sprite) getFromAnToForAni(anitype aniTypeEnum, from interface{}, to int
}

func (p *Sprite) goAnimate(name string, ani *aniConfig) {
if p.lastAnim != nil {
p.lastAnim.Stop()
}
var animwg sync.WaitGroup
animwg.Add(1)

if ani.OnStart != nil {
if ani.OnStart.Play != "" {
p.g.Play__3(ani.OnStart.Play)
}
if ani.OnStart != nil && ani.OnStart.Play != "" {
p.g.Play__3(ani.OnStart.Play)
}

//anim frame
Expand Down Expand Up @@ -556,14 +558,20 @@ func (p *Sprite) goAnimate(name string, ani *aniConfig) {
pre_y := p.y
pre_direction := p.direction //turn p.direction

an := anim.NewAnim(name, animtype, fps, framenum).AddKeyFrame(0, fromval).AddKeyFrame(framenum, toval).SetLoop(false)
an := anim.NewAnim(name, animtype, fps, framenum).AddKeyFrame(0, fromval).AddKeyFrame(framenum, toval).SetLoop(ani.IsLoop)
p.lastAnim = an
if debugInstr {
log.Printf("New anim [name %s id %d] from:%v to:%v framenum:%d fps:%f", an.Name, an.Id, fromval, toval, framenum, fps)
}
an.SetOnPlayingListener(func(currframe int, currval interface{}) {
an.SetOnPlayingListener(func(currframe int, isReplay bool, currval interface{}) {
if debugInstr {
log.Printf("playing anim [name %s id %d] currframe %d, val %v", an.Name, an.Id, currframe, currval)
}
if isReplay && ani.IsLoop {
if ani.OnStart != nil && ani.OnStart.Play != "" {
p.g.Play__3(ani.OnStart.Play)
}
}

switch ani.AniType {
case aniTypeFrame:
Expand Down Expand Up @@ -601,6 +609,7 @@ func (p *Sprite) goAnimate(name string, ani *aniConfig) {
log.Printf("stop anim [name %s id %d] ", an.Name, an.Id)
}
animwg.Done()
p.lastAnim = nil
})

var h *tickHandler
Expand Down

0 comments on commit 03f76f4

Please sign in to comment.