animation.nim 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # author: Ethosa
  2. type
  3. AnimationObj*[T] = object
  4. frame*: int
  5. speed*, current*: float
  6. name*: string
  7. frames*: seq[T]
  8. AnimationRef*[T] = ref AnimationObj[T]
  9. AnimationArray*[T] = seq[AnimationRef[T]]
  10. proc Animation*[T](name: string, speed: float = 1f): AnimationRef[T] =
  11. ## Creates a new Animation object.
  12. ##
  13. ## Arguments:
  14. ## - `name` is an animation name.
  15. ## - `speed` is an animation speed.
  16. runnableExamples:
  17. var
  18. anim = Animation[int]("animation", 10f)
  19. AnimationRef[T](name: name, speed: speed, current: 0f, frames: @[], frame: 0)
  20. proc addFrame*[T](anim: AnimationRef[T], frame: T) =
  21. ## Adds a new frame in the animation.
  22. anim.frames.add(frame)
  23. # --- Operators --- #
  24. proc `==`*[T](x, y: AnimationRef[T]): bool =
  25. x.name == y.name and x.speed == y.speed and x.frames == y.frames
  26. proc `[]`*[T](arr: AnimationArray[T], index: string): AnimationRef[T] =
  27. for elem in arr:
  28. if elem.name == index:
  29. return elem
  30. proc contains*[T](arr: AnimationArray[T], animation: AnimationRef[T]): bool =
  31. result = false
  32. for elem in arr:
  33. if elem.name == animation.name:
  34. result = true
  35. break
  36. proc contains*[T](arr: AnimationArray[T], name: string): bool =
  37. result = false
  38. for elem in arr:
  39. if elem.name == name:
  40. result = true
  41. break