animator.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 texture
  5. import (
  6. "github.com/g3n/engine/gls"
  7. "time"
  8. )
  9. // Animator
  10. type Animator struct {
  11. tex *Texture2D // pointer to texture being displayed
  12. dispTime time.Duration // disply duration of each tile (default = 1.0/30.0)
  13. maxCycles int // maximum number of cycles (default = 0 - continuous)
  14. cycles int // current number of complete cycles
  15. columns int // number of columns
  16. rows int // number of rows
  17. row int // current row
  18. col int // current column
  19. tileTime time.Time // time when tile started to be displayed
  20. }
  21. // NewAnimator creates and returns a texture sheet animator for the specified texture.
  22. func NewAnimator(tex *Texture2D, htiles, vtiles int) *Animator {
  23. a := new(Animator)
  24. a.tex = tex
  25. a.columns = htiles
  26. a.rows = vtiles
  27. a.dispTime = time.Millisecond * 16
  28. a.maxCycles = 0
  29. // Sets texture properties
  30. tex.SetWrapS(gls.REPEAT)
  31. tex.SetWrapT(gls.REPEAT)
  32. tex.SetRepeat(1/float32(a.columns), 1/float32(a.rows))
  33. // Initial state
  34. a.Restart()
  35. return a
  36. }
  37. // SetDispTime sets the display time of each tile in milliseconds.
  38. // The default value is: 1.0/30.0 = 16.6.ms
  39. func (a *Animator) SetDispTime(dtime time.Duration) {
  40. a.dispTime = dtime
  41. }
  42. // SetMaxCycles sets the number of complete cycles to display.
  43. // The default value is: 0 (display continuously)
  44. func (a *Animator) SetMaxCycles(maxCycles int) {
  45. a.maxCycles = maxCycles
  46. }
  47. // Cycles returns the number of complete cycles displayed
  48. func (a *Animator) Cycles() int {
  49. return a.cycles
  50. }
  51. // Restart restart the animator
  52. func (a *Animator) Restart() {
  53. // Time of the currently displayed image
  54. a.tileTime = time.Now()
  55. // Position of current tile to display
  56. a.row = 0
  57. a.col = 0
  58. // Number of cycles displayed
  59. a.cycles = 0
  60. }
  61. // Update prepares the next tile to be rendered.
  62. // Must be called with the current time
  63. func (a *Animator) Update(now time.Time) {
  64. // Checks maximum number of cycles
  65. if a.maxCycles > 0 && a.cycles >= a.maxCycles {
  66. return
  67. }
  68. // If current tile time not reached, do nothing
  69. if now.Sub(a.tileTime) < a.dispTime {
  70. return
  71. }
  72. a.tileTime = now
  73. // Sets the position of the next tile to show
  74. a.tex.SetOffset(float32(a.col)/float32(a.columns), float32(a.row)/float32(a.rows))
  75. a.col += 1
  76. if a.col >= a.columns {
  77. a.col = 0
  78. a.row += 1
  79. if a.row >= a.rows {
  80. a.row = 0
  81. a.cycles += 1
  82. }
  83. }
  84. }