animator.go 2.5 KB

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