PageRenderTime 192ms CodeModel.GetById 28ms RepoModel.GetById 5ms app.codeStats 0ms

/org/rygal/graphic/Animation.hx

https://github.com/riflum/Rygal
Haxe | 97 lines | 19 code | 13 blank | 65 comment | 0 complexity | 2aaade2b908fbca3adec45b7395e9dac MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
  1. // Copyright (C) 2012 Robert Böhm
  2. //
  3. // This file is part of Rygal.
  4. //
  5. // Rygal is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Lesser General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // Rygal is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  13. // more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public License
  16. // along with Rygal. If not, see: <http://www.gnu.org/licenses/>.
  17. package org.rygal.graphic;
  18. /**
  19. * <h2>Description</h2>
  20. * <p>
  21. * An animation that is based on a texture sequence and a specified delay
  22. * between frames.
  23. * </p>
  24. *
  25. * <h2>Example</h2>
  26. * <code>
  27. * var spritesheet:Spritesheet = new Spritesheet(<br />
  28. * &nbsp;&nbsp;Texture.fromAssets("assets/walkAnimation.png"), 4, 1);<br />
  29. * var animation:Animation = Animation.fromSpritesheet(250, spritesheet)<br />
  30. * <br />
  31. * var sprite:AnimatedSprite = new AnimatedSprite();<br />
  32. * sprite.registerAnimation("walk", animation);<br />
  33. * sprite.loop("walk");
  34. * </code>
  35. *
  36. * @author Robert Böhm
  37. */
  38. class Animation {
  39. /** The delay between multiple frames. */
  40. public var frameDelay:Int;
  41. /** The texture sequence this animation is based on. */
  42. public var sequence:TextureSequence;
  43. /**
  44. * Creates a new animation based on a frame delay and a given texture
  45. * sequence.
  46. *
  47. * @param frameDelay The delay between frames in milliseconds.
  48. * @param sequence The texture sequence this animation is based on.
  49. */
  50. public function new(frameDelay:Int, sequence:TextureSequence) {
  51. this.frameDelay = frameDelay;
  52. this.sequence = sequence;
  53. }
  54. /**
  55. * Creates a new animation based on a spritesheet.
  56. *
  57. * @param frameDelay The delay between frames in milliseconds.
  58. * @param spritesheet The spritesheet this animation is based on.
  59. * @param start The first ID to use (Inclusive).
  60. * @param end The last ID to use (Exclusive). Use -1 to make it
  61. * last until the (inclusive) last tile.
  62. * @param loop Shall the animation be looping? (Forward and at the
  63. * end reverse direction)
  64. * @return The new animation based on the given parameters.
  65. */
  66. public static function fromSpritesheet(frameDelay:Int, spritesheet:Spritesheet,
  67. start:Int = 0, end:Int = -1, loop:Bool = false):Animation {
  68. return new Animation(frameDelay,
  69. TextureSequence.fromSpritesheet(spritesheet, start, end, loop));
  70. }
  71. /**
  72. * Creates a new animation based on a TextureAtlas.
  73. *
  74. * @param frameDelay The delay between frames in milliseconds.
  75. * @param textureAtlas The TextureAtlas this animation is based on.
  76. * @param names The names of the sprites this animation will use.
  77. * @return The new animation based on the given parameters.
  78. */
  79. public static function fromTextureAtlas(frameDelay:Int,
  80. textureAtlas:TextureAtlas, names:Array<String>):Animation {
  81. return new Animation(frameDelay,
  82. TextureSequence.fromTextureAtlas(textureAtlas, names));
  83. }
  84. }