Преглед изворни кода

improved animation framework

Daniel Salvadori пре 7 година
родитељ
комит
4008c7c85e
2 измењених фајлова са 23 додато и 10 уклоњено
  1. 22 4
      animation/animation.go
  2. 1 6
      animation/channel.go

+ 22 - 4
animation/animation.go

@@ -16,6 +16,7 @@ type Animation struct {
 	time     float32    // Total running time
 	minTime  float32    // Minimum time value across all channels
 	maxTime  float32    // Maximum time value across all channels
+	speed    float32    // Animation speed multiplier
 	channels []IChannel // List of channels
 }
 
@@ -23,6 +24,7 @@ type Animation struct {
 func NewAnimation() *Animation {
 
 	anim := new(Animation)
+	anim.speed = 1
 	return anim
 }
 
@@ -38,12 +40,28 @@ func (anim *Animation) Name() string {
 	return anim.name
 }
 
+// SetSpeed sets the animation speed.
+func (anim *Animation) SetSpeed(speed float32) {
+
+	anim.speed = speed
+}
+
+// Speed returns the animation speed.
+func (anim *Animation) Speed() float32 {
+
+	return anim.speed
+}
+
 // Reset resets the animation to the beginning.
 func (anim *Animation) Reset() {
 
 	anim.time = anim.start
-	anim.paused = false
-	// TODO reset channels?
+
+	// Update all channels
+	for i := range anim.channels {
+		ch := anim.channels[i]
+		ch.Update(anim.start)
+	}
 }
 
 // SetPaused sets whether the animation is paused.
@@ -87,7 +105,7 @@ func (anim *Animation) Update(delta float32) {
 	}
 
 	// Check if input is less than minimum
-	anim.time = anim.time + delta
+	anim.time = anim.time + delta * anim.speed
 	if anim.time < anim.minTime {
 		return
 	}
@@ -103,7 +121,7 @@ func (anim *Animation) Update(delta float32) {
 	}
 
 	// Update all channels
-	for i := 0; i < len(anim.channels); i++ {
+	for i := range anim.channels {
 		ch := anim.channels[i]
 		ch.Update(anim.time)
 	}

+ 1 - 6
animation/channel.go

@@ -83,7 +83,7 @@ func (c *Channel) Update(time float32) {
 		return
 	}
 
-	// Find key frame interval
+	// Find keyframe interval
 	var idx int
 	for idx = 0; idx < len(c.keyframes)-1; idx++ {
 		if time >= c.keyframes[idx] && time < c.keyframes[idx+1] {
@@ -91,11 +91,6 @@ func (c *Channel) Update(time float32) {
 		}
 	}
 
-	// Check if last keyframe
-	if idx >= len(c.keyframes)-1 {
-		return
-	}
-
 	// Interpolate and update
 	relativeDelta := (time-c.keyframes[idx])/(c.keyframes[idx+1]-c.keyframes[idx])
 	c.interpAction(idx, relativeDelta)