Advertisement
Guest User

spriteclass

a guest
Oct 12th, 2012
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.73 KB | None | 0 0
  1. public class Sprite {
  2.   private int X;
  3.   private int Y;
  4.   private float scaleFactor;
  5.   private float faceAngle;
  6.   private PImage sprite;
  7.   private int[] boundingRect;
  8.   public boolean isClicked = false;
  9.  
  10.   /*
  11.   private float speed = 5.0;
  12.   private float angle = 90.0;
  13.   private float acceleration = 1.0;
  14.   */
  15.  
  16.  
  17.   //used for moveMe()
  18.   public int toX;
  19.   public int toY;
  20.   public int curX;
  21.   public int curY;
  22.   public int interval;
  23.  
  24.  
  25.   //constructors
  26.   Sprite (int xpos, int ypos, String img) {
  27.    
  28.     this.scaleFactor = 1.0;
  29.     this.faceAngle = 0;
  30.     this.X = xpos;
  31.     this.Y = ypos;
  32.  
  33.     try {
  34.       this.sprite = loadImage(img);
  35.       this.boundingRect = makeBoundingRect(this.X, this.Y, this.X + this.sprite.width, this.Y + this.sprite.height);
  36.       if (this.sprite == null) {
  37.         println("[ERROR][Class = Sprite]: failed to load the image: " + img + " please make sure that the file exists and is added to the sketch.");
  38.         exit();
  39.       }
  40.     }
  41.     catch (Exception e) {
  42.       println("[ERROR][Class = Sprite]: failed to load the image: " + img + " please make sure that the file exists and is added to the sketch.");
  43.       e.printStackTrace();
  44.     }
  45.   }
  46.  
  47.   Sprite (int xpos, int ypos, PImage img) {
  48.     this.X = xpos;
  49.     this.Y = ypos;
  50.     this.scaleFactor = 1.0;
  51.     this.faceAngle = 0;
  52.     this.sprite = img;
  53.     this.boundingRect = makeBoundingRect(this.X, this.Y, this.X + this.sprite.width, this.Y + this.sprite.height);
  54.   }
  55.  
  56.   Sprite () {
  57.     //empty constructor for getSpriteByName (see bottom)
  58.   }
  59.  
  60.   //accessors
  61.   int getX() { return this.X;}
  62.   int getY() { return this.Y;}
  63.   float getScale() { return this.scaleFactor;}
  64.   float getAngle() { return this.faceAngle;}
  65.   int getWidth() { return this.sprite.width;}
  66.   int getHeight() { return this.sprite.height;}
  67.  
  68.  
  69.   //mutators
  70.   void setX(int xpos) {this.X = xpos;}
  71.   void setY(int ypos) {this.Y = ypos;}
  72.   void setScale(float nScale) {this.scaleFactor = nScale;}
  73.   void setAngle(float deg) {this.faceAngle = deg;}
  74.  
  75.   public void render() {
  76.     try {
  77.       image(this.sprite, this.X, this.Y);
  78.       this.boundingRect = makeBoundingRect(this.X, this.Y, this.X + this.sprite.width, this.Y + this.sprite.height);
  79.     }
  80.     catch (NullPointerException NPE) {
  81.       println("[ERROR][Class = Sprite]: the loaded image " + this.sprite + " is null & void");
  82.       exit();
  83.     }
  84.   }
  85.  
  86.   void moveMe(int xtrans, int ytrans) {
  87.     this.X += xtrans;
  88.     this.Y += ytrans;
  89.   }
  90.  
  91.   void rotateMe(int deg) {
  92.     pushMatrix();
  93.     this.faceAngle += deg;
  94.     //this.faceAngle = constrain(this.faceAngle, 0, 360);
  95.     //translate(this.X + this.sprite.width / 2, this.Y + this.sprite.height / 2);
  96.     rotate(radians(this.faceAngle));
  97.     render();
  98.     popMatrix();
  99.   }
  100.  
  101.   private void updateXY() {
  102.     curX = this.X;
  103.     curY = this.Y;
  104.   }
  105.  
  106.   void resizeMe(int w, int h) {
  107.     this.sprite.resize(w, h);
  108.   }
  109.  
  110.   int[] getBoundingRect() { return this.boundingRect; }
  111.   // void rotateMeTimed()
  112.   // void scaleMe()
  113.   // void scaleMeTimed()
  114. }
  115.  
  116. public class SpriteBatch {
  117.   private ArrayList<Sprite> batch = new ArrayList<Sprite>();
  118.   private int batchIndex = 0;
  119.   private Sprite curSprite;
  120.   private int destinationX;
  121.   private int destinationY;
  122.   private boolean keepX, keepY;
  123.  
  124.   SpriteBatch() {
  125.   }
  126.  
  127.   void addSprite(Sprite s) {
  128.     batch.add(batchIndex, s);
  129.     batchIndex++;
  130.   }
  131.  
  132.   void addNewSprite(String spriteName, int xs, int ys) {
  133.     batch.add(batchIndex, new Sprite(xs, ys, spriteName));
  134.     batchIndex++;
  135.   }
  136.  
  137.   void removeSprite(int index) {
  138.     batch.remove(index);
  139.     batchIndex--;
  140.   }
  141.  
  142.   void moveBatchTimed(int desX, int desY, int intrvl, boolean preserveX, boolean preserveY) {
  143.     //preserveX and preserveY are indicated if this batch's X or Y position should be preserved.
  144.     //if both are false then the ALL the sprites within the batch will move to the same location
  145.     //and be rendered on top of each other.
  146.     destinationX = desX;
  147.     destinationY = desY;
  148.     int interval = intrvl;
  149.     if (interval <= 0) {
  150.       interval = 1;
  151.     }
  152.     keepX = preserveX;
  153.     keepY = preserveY;
  154.     //globals are needed here ^ because the inner class "run()" can not reach the parameters passed from the moveBatchTimed() method
  155.  
  156.     Timer tAnimate = new Timer();
  157.     tAnimate.schedule(new TimerTask() {
  158.       public void run() {
  159.         for (int i = 0; i < batch.size(); i++) {
  160.           curSprite = batch.get(i);
  161.           if (curSprite.getX() < destinationX && !keepX) {
  162.             curSprite.moveMe(1, 0);
  163.           }
  164.           else if (curSprite.getX() > destinationX && !keepX) {
  165.             curSprite.moveMe(-1, 0);
  166.           }
  167.           else if (curSprite.getY() < destinationY && !keepY) {
  168.             curSprite.moveMe(0, 1);
  169.           }
  170.           else if (curSprite.getY() > destinationY && !keepY) {
  171.             curSprite.moveMe(0, -1);
  172.           }
  173.         }
  174.       }
  175.     }
  176.     , 0, interval);
  177.   }
  178.  
  179.   void render() {
  180.     for (int i = 0; i < batch.size(); i++) {
  181.       batch.get(i).render();
  182.     }
  183.   }
  184.  
  185.   void moveMe(int xtrans, int ytrans) {
  186.     for (int i = 0; i < batch.size(); i++) {
  187.       batch.get(i).setX(batch.get(i).getX()+xtrans);
  188.       batch.get(i).setY(batch.get(i).getY()+ytrans);
  189.     }
  190.   }
  191. }
  192.  
  193. //deprecated
  194. /*
  195. Sprite getSpriteByName(String name) {
  196.   for (MySprite s : level.Sprites) {
  197.     if (s.name.equals(name)) {
  198.       return new Sprite(s.xpos, s.ypos, s.img);  
  199.     }
  200.   }
  201.   println("The sprite [" + name + "] does not exist. A default (empty) sprite has been returned instead");
  202.   return new Sprite();
  203. }
  204. */
  205.  
  206. void renderSprites() {
  207.     for (Sprite s : LoadedSprites) {
  208.         s.rotateMe(5);
  209.         //s.render();
  210.         //drawBoundingRect(s.getBoundingRect());
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement