Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Animation {
- boolean canPlay = true;
- boolean oscillate = true;
- boolean repeat = true;
- int current;
- int dir = 1;
- int interval;
- int iterations;
- int maxIterations = 1;
- int length;
- PVector angle = new PVector();
- PVector pivot;
- PVector position;
- PVector scale;
- PVector uvOff = new PVector();
- PVector uvScl = new PVector(1, 1);
- PImage[] frames;
- Animation(float posX, float posY, float posZ,
- float sclX, float sclY, float sclZ,
- float pvtX, float pvtY, float pvtZ,
- int intvl, String folderPath) {
- this(new PVector(posX, posY, posZ),
- new PVector(sclX, sclY, sclZ),
- new PVector(pvtX, pvtY, pvtZ),
- intvl, loadTextures(folderPath));
- }
- Animation(PVector pos, PVector scl, PVector pvt,
- int intvl, String folderPath) {
- this(pos, scl, pvt, intvl,
- loadTextures(folderPath));
- }
- Animation(PVector pos, PVector scl, PVector pvt,
- int intvl, PImage... frs) {
- position = pos;
- scale = scl;
- float halfW = scale.x * 0.5;
- float halfH = scale.y * 0.5;
- pivot = new PVector(
- map(pvt.x, 0, 1, -halfW, halfW),
- map(pvt.y, 0, 1, -halfH, halfH));
- interval = intvl;
- frames = frs;
- length = frames.length;
- }
- void draw() {
- advance();
- show();
- }
- void advance() {
- canPlay = repeat || iterations < maxIterations;
- if (canPlay) {
- if (frameCount % interval == 0) {
- // Choose sequencing.
- if (!oscillate) {
- current = (current + 1) % length;
- } else {
- current += dir;
- if (current <= 0
- || current >= length - 1) {
- dir *= -1;
- }
- }
- // Count iterations.
- if (current == length - 1) {
- iterations++;
- }
- }
- }
- }
- void reset() {
- current = 0;
- dir = 1;
- iterations = 0;
- }
- void show() {
- float halfW = scale.x * 0.5;
- float halfH = scale.y * 0.5;
- PVector cmlt = PVector.add(position, pivot);
- pushStyle();
- noStroke();
- pushMatrix();
- translate(cmlt.x, cmlt.y, cmlt.z);
- rotateY(angle.y);
- rotateZ(angle.z);
- rotateX(angle.x);
- beginShape();
- texture(frames[current]);
- vertex(-pivot.x - halfW, -pivot.y - halfH, -pivot.z,
- uvOff.x, uvOff.y);
- vertex(-pivot.x + halfW, -pivot.y - halfH, -pivot.z,
- uvOff.x + uvScl.x, uvOff.y);
- vertex(-pivot.x + halfW, -pivot.y + halfH, -pivot.z,
- uvOff.x + uvScl.x, uvOff.y + uvScl.y);
- vertex(-pivot.x - halfW, -pivot.y + halfH, -pivot.z,
- uvOff.x, uvOff.y + uvScl.y);
- endShape(CLOSE);
- // For diagnostic purposes only.
- //strokeWeight(10);
- //stroke(0xff00ff7f);
- //point(0, 0, 0);
- //stroke(0xffff7f00);
- //point(-pivot.x, -pivot.y, -pivot.z);
- popMatrix();
- popStyle();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement