animation.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package animation
  5. package animation
  6. import "github.com/g3n/engine/util/logger"
  7. // Package logger
  8. var log = logger.New("ANIMATION", logger.Default)
  9. // Animation is a keyframe animation, containing channels.
  10. // Each channel animates a specific property of an object.
  11. // Animations can span multiple objects and properties.
  12. type Animation struct {
  13. name string // Animation name
  14. loop bool // Whether the animation loops
  15. paused bool // Whether the animation is paused
  16. start float32 // Initial time offset value
  17. time float32 // Total running time
  18. minTime float32 // Minimum time value across all channels
  19. maxTime float32 // Maximum time value across all channels
  20. speed float32 // Animation speed multiplier
  21. channels []IChannel // List of channels
  22. }
  23. // NewAnimation creates and returns a pointer to a new Animation object.
  24. func NewAnimation() *Animation {
  25. anim := new(Animation)
  26. anim.speed = 1
  27. return anim
  28. }
  29. // SetName sets the animation name.
  30. func (anim *Animation) SetName(name string) {
  31. anim.name = name
  32. }
  33. // Name returns the animation name.
  34. func (anim *Animation) Name() string {
  35. return anim.name
  36. }
  37. // SetSpeed sets the animation speed.
  38. func (anim *Animation) SetSpeed(speed float32) {
  39. anim.speed = speed
  40. }
  41. // Speed returns the animation speed.
  42. func (anim *Animation) Speed() float32 {
  43. return anim.speed
  44. }
  45. // Reset resets the animation to the beginning.
  46. func (anim *Animation) Reset() {
  47. anim.time = anim.start
  48. // Update all channels
  49. for i := range anim.channels {
  50. ch := anim.channels[i]
  51. ch.Update(anim.start)
  52. }
  53. }
  54. // SetPaused sets whether the animation is paused.
  55. func (anim *Animation) SetPaused(state bool) {
  56. anim.paused = state
  57. }
  58. // Paused returns whether the animation is paused.
  59. func (anim *Animation) Paused() bool {
  60. return anim.paused
  61. }
  62. // SetLoop sets whether the animation is looping.
  63. func (anim *Animation) SetLoop(state bool) {
  64. anim.loop = state
  65. }
  66. // Loop returns whether the animation is looping.
  67. func (anim *Animation) Loop() bool {
  68. return anim.loop
  69. }
  70. // SetStart sets the initial time offset value.
  71. func (anim *Animation) SetStart(v float32) {
  72. anim.start = v
  73. }
  74. // Update interpolates and updates the target values for each channel.
  75. // If the animation is paused, returns false. If the animation is not paused,
  76. // returns true if the input value is inside the key frames ranges or false otherwise.
  77. func (anim *Animation) Update(delta float32) {
  78. // Check if paused
  79. if anim.paused {
  80. return
  81. }
  82. // Check if input is less than minimum
  83. anim.time = anim.time + delta*anim.speed
  84. if anim.time < anim.minTime {
  85. return
  86. }
  87. // Check if input is greater than maximum
  88. if anim.time > anim.maxTime {
  89. if anim.loop {
  90. anim.time = anim.time - anim.maxTime
  91. } else {
  92. anim.time = anim.maxTime - 0.000001
  93. anim.SetPaused(true)
  94. }
  95. }
  96. // Update all channels
  97. for i := range anim.channels {
  98. ch := anim.channels[i]
  99. ch.Update(anim.time)
  100. }
  101. }
  102. // AddChannel adds a channel to the animation.
  103. func (anim *Animation) AddChannel(ch IChannel) {
  104. // TODO (maybe) prevent user from adding two channels of the same type that share target ?
  105. // Add the channel
  106. anim.channels = append(anim.channels, ch)
  107. // Update maxTime and minTime values
  108. kf := ch.Keyframes()
  109. firstTime := kf[0]
  110. if anim.minTime > firstTime {
  111. anim.minTime = firstTime
  112. }
  113. lastTime := kf[len(kf)-1]
  114. if anim.maxTime < lastTime {
  115. anim.maxTime = lastTime
  116. }
  117. }