Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. class Animation {
  2. boolean canPlay = true;
  3. boolean oscillate = true;
  4. boolean repeat = true;
  5.  
  6. int current;
  7. int dir = 1;
  8. int interval;
  9. int iterations;
  10. int maxIterations = 1;
  11. int length;
  12.  
  13. PVector angle = new PVector();
  14. PVector pivot;
  15. PVector position;
  16. PVector scale;
  17. PVector uvOff = new PVector();
  18. PVector uvScl = new PVector(1, 1);
  19.  
  20. PImage[] frames;
  21.  
  22. Animation(float posX, float posY, float posZ,
  23. float sclX, float sclY, float sclZ,
  24. float pvtX, float pvtY, float pvtZ,
  25. int intvl, String folderPath) {
  26. this(new PVector(posX, posY, posZ),
  27. new PVector(sclX, sclY, sclZ),
  28. new PVector(pvtX, pvtY, pvtZ),
  29. intvl, loadTextures(folderPath));
  30. }
  31.  
  32. Animation(PVector pos, PVector scl, PVector pvt,
  33. int intvl, String folderPath) {
  34. this(pos, scl, pvt, intvl,
  35. loadTextures(folderPath));
  36. }
  37.  
  38. Animation(PVector pos, PVector scl, PVector pvt,
  39. int intvl, PImage... frs) {
  40. position = pos;
  41. scale = scl;
  42. float halfW = scale.x * 0.5;
  43. float halfH = scale.y * 0.5;
  44. pivot = new PVector(
  45. map(pvt.x, 0, 1, -halfW, halfW),
  46. map(pvt.y, 0, 1, -halfH, halfH));
  47. interval = intvl;
  48. frames = frs;
  49. length = frames.length;
  50. }
  51.  
  52. void draw() {
  53. advance();
  54. show();
  55. }
  56.  
  57. void advance() {
  58. canPlay = repeat || iterations < maxIterations;
  59.  
  60. if (canPlay) {
  61. if (frameCount % interval == 0) {
  62.  
  63. // Choose sequencing.
  64. if (!oscillate) {
  65. current = (current + 1) % length;
  66. } else {
  67. current += dir;
  68. if (current <= 0
  69. || current >= length - 1) {
  70. dir *= -1;
  71. }
  72. }
  73.  
  74. // Count iterations.
  75. if (current == length - 1) {
  76. iterations++;
  77. }
  78. }
  79. }
  80. }
  81.  
  82. void reset() {
  83. current = 0;
  84. dir = 1;
  85. iterations = 0;
  86. }
  87.  
  88. void show() {
  89. float halfW = scale.x * 0.5;
  90. float halfH = scale.y * 0.5;
  91. PVector cmlt = PVector.add(position, pivot);
  92.  
  93. pushStyle();
  94. noStroke();
  95.  
  96. pushMatrix();
  97. translate(cmlt.x, cmlt.y, cmlt.z);
  98. rotateY(angle.y);
  99. rotateZ(angle.z);
  100. rotateX(angle.x);
  101.  
  102. beginShape();
  103. texture(frames[current]);
  104. vertex(-pivot.x - halfW, -pivot.y - halfH, -pivot.z,
  105. uvOff.x, uvOff.y);
  106. vertex(-pivot.x + halfW, -pivot.y - halfH, -pivot.z,
  107. uvOff.x + uvScl.x, uvOff.y);
  108. vertex(-pivot.x + halfW, -pivot.y + halfH, -pivot.z,
  109. uvOff.x + uvScl.x, uvOff.y + uvScl.y);
  110. vertex(-pivot.x - halfW, -pivot.y + halfH, -pivot.z,
  111. uvOff.x, uvOff.y + uvScl.y);
  112. endShape(CLOSE);
  113.  
  114. // For diagnostic purposes only.
  115. //strokeWeight(10);
  116. //stroke(0xff00ff7f);
  117. //point(0, 0, 0);
  118. //stroke(0xffff7f00);
  119. //point(-pivot.x, -pivot.y, -pivot.z);
  120.  
  121. popMatrix();
  122. popStyle();
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement