PageRenderTime 39ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/graphics/Animation.java

https://github.com/TheCommissar/Map-Editor
Java | 119 lines | 90 code | 26 blank | 3 comment | 13 complexity | 2397fdc8ae285efeea9c98b403a16996 MD5 | raw file
  1. package graphics;
  2. import game.Game;
  3. import java.awt.image.BufferedImage;
  4. import program.Program;
  5. public class Animation
  6. {
  7. private int frameCount; // Counts ticks for change
  8. private int frameDelay; // frame delay 1-12 (You will have to play around with this)
  9. private int currentFrame; // animations current frame
  10. private int animationDirection; // animation direction (i.e counting forward or backward)
  11. private boolean stopped; // has animations stopped
  12. private BufferedImage[] frames; // frames to display
  13. public Animation(String value)
  14. {
  15. String[] lines = value.split("\\r?\\n");
  16. // Get host spritesheet
  17. Sprite s = Program.RESOURCE.getSprite(lines[0]);
  18. // Get frame delay
  19. frameDelay = Integer.parseInt(lines[1]);
  20. // Remainder should be <x, y> for assembling frames
  21. lines = lines[2].split(";");
  22. frames = new BufferedImage[lines.length];
  23. for (int i = 0; i < lines.length; i++)
  24. {
  25. String[] p = lines[i].split(",");
  26. int x = Integer.parseInt(p[0]);
  27. int y = Integer.parseInt(p[1]);
  28. frames[i] = s.getFrame(x, y);
  29. }
  30. stopped = true;
  31. currentFrame = 0;
  32. animationDirection = 1;
  33. }
  34. public void start()
  35. {
  36. if (!stopped)
  37. {
  38. return;
  39. }
  40. if (frames.length == 0)
  41. {
  42. return;
  43. }
  44. stopped = false;
  45. }
  46. public void stop()
  47. {
  48. if (frames.length == 0)
  49. {
  50. return;
  51. }
  52. stopped = true;
  53. }
  54. public void restart()
  55. {
  56. if (frames.length == 0)
  57. {
  58. return;
  59. }
  60. stopped = false;
  61. currentFrame = 0;
  62. }
  63. public void reset()
  64. {
  65. this.stopped = true;
  66. this.frameCount = 0;
  67. this.currentFrame = 0;
  68. }
  69. public BufferedImage getImage()
  70. {
  71. return frames[currentFrame];
  72. }
  73. public void update()
  74. {
  75. if (!stopped)
  76. {
  77. frameCount++;
  78. if (frameCount > frameDelay)
  79. {
  80. frameCount = 0;
  81. currentFrame += animationDirection;
  82. if (currentFrame > frames.length - 1)
  83. {
  84. currentFrame = 0;
  85. }
  86. else if (currentFrame < 0)
  87. {
  88. currentFrame = frames.length - 1;
  89. }
  90. }
  91. }
  92. }
  93. }