You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.4 KiB

  1. package rendering;
  2. import gameobjects.GameObject;
  3. import playground.Animation;
  4. import java.awt.Graphics2D;
  5. import java.awt.image.BufferedImage;
  6. import java.util.LinkedList;
  7. import org.apache.logging.log4j.Logger;
  8. import org.apache.logging.log4j.LogManager;
  9. public class AnimationArtist extends Artist {
  10. protected LinkedList<BufferedImage> imageArray;
  11. protected LinkedList<Double> showtime;
  12. protected double t0;
  13. protected int loopFrame = 0;
  14. protected double w = 0, h = 0, scale = 0;
  15. protected String playmode = ""; // can be loop, forward, backward
  16. private static Logger logger = LogManager.getLogger(AnimationArtist.class);
  17. public AnimationArtist(GameObject go, Animation anim, double t0, String playmode, double scale) {
  18. super(go);
  19. this.imageArray = anim.getImageList();
  20. this.showtime = anim.getShowtimeList();
  21. this.t0 = t0;
  22. logger.debug("AnimationArtist start = ") ;
  23. this.loopFrame = 0;
  24. for (int i = 0; i < imageArray.size(); i++) {
  25. logger.trace("ANIMRAWWH="+imageArray.get(i).getWidth()) ;
  26. this.w = Math.max(this.w, imageArray.get(i).getWidth());
  27. this.h = Math.max(imageArray.get(i).getHeight(), this.h);
  28. }
  29. logger.debug("AnimationArtist RAW WH = "+this.w+"/"+this.h) ;
  30. this.playmode = playmode;
  31. this.w *= scale;
  32. this.h *= scale;
  33. }
  34. public double getW() {
  35. return w;
  36. }
  37. public double getH() {
  38. return h;
  39. }
  40. @Override
  41. public void draw(Graphics2D g) {
  42. double i2 = this.getGameTime();
  43. double elapsedTime = i2 - t0;
  44. // Diff chance from sec to millis
  45. double mseconds = (double) elapsedTime;
  46. logger.debug("showtime= "+showtime.get(loopFrame)+" elapsed= "+elapsedTime+" i2= "+i2);
  47. if (playmode.equals("loop")) {
  48. if (mseconds >= showtime.get(loopFrame)) {
  49. loopFrame++;
  50. if (loopFrame >= imageArray.size())
  51. loopFrame = 0;
  52. t0 = i2;
  53. }
  54. }
  55. if (playmode.equals("forward")) {
  56. if (mseconds >= showtime.get(loopFrame) && loopFrame < imageArray.size() - 1) {
  57. loopFrame++;
  58. t0 = i2;
  59. }
  60. }
  61. if (playmode.equals("backward")) {
  62. if (mseconds >= showtime.get(loopFrame) && loopFrame > 1) {
  63. loopFrame--;
  64. t0 = i2;
  65. }
  66. }
  67. g.drawImage(imageArray.get(loopFrame), (int) Math.round(this.getX() - w / 2.),
  68. (int) Math.round(this.getY() - h / 2.), (int) w, (int) h, null);
  69. }
  70. }