Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Sprite {
- private int X;
- private int Y;
- private float scaleFactor;
- private float faceAngle;
- private PImage sprite;
- private int[] boundingRect;
- public boolean isClicked = false;
- /*
- private float speed = 5.0;
- private float angle = 90.0;
- private float acceleration = 1.0;
- */
- //used for moveMe()
- public int toX;
- public int toY;
- public int curX;
- public int curY;
- public int interval;
- //constructors
- Sprite (int xpos, int ypos, String img) {
- this.scaleFactor = 1.0;
- this.faceAngle = 0;
- this.X = xpos;
- this.Y = ypos;
- try {
- this.sprite = loadImage(img);
- this.boundingRect = makeBoundingRect(this.X, this.Y, this.X + this.sprite.width, this.Y + this.sprite.height);
- if (this.sprite == null) {
- println("[ERROR][Class = Sprite]: failed to load the image: " + img + " please make sure that the file exists and is added to the sketch.");
- exit();
- }
- }
- catch (Exception e) {
- println("[ERROR][Class = Sprite]: failed to load the image: " + img + " please make sure that the file exists and is added to the sketch.");
- e.printStackTrace();
- }
- }
- Sprite (int xpos, int ypos, PImage img) {
- this.X = xpos;
- this.Y = ypos;
- this.scaleFactor = 1.0;
- this.faceAngle = 0;
- this.sprite = img;
- this.boundingRect = makeBoundingRect(this.X, this.Y, this.X + this.sprite.width, this.Y + this.sprite.height);
- }
- Sprite () {
- //empty constructor for getSpriteByName (see bottom)
- }
- //accessors
- int getX() { return this.X;}
- int getY() { return this.Y;}
- float getScale() { return this.scaleFactor;}
- float getAngle() { return this.faceAngle;}
- int getWidth() { return this.sprite.width;}
- int getHeight() { return this.sprite.height;}
- //mutators
- void setX(int xpos) {this.X = xpos;}
- void setY(int ypos) {this.Y = ypos;}
- void setScale(float nScale) {this.scaleFactor = nScale;}
- void setAngle(float deg) {this.faceAngle = deg;}
- public void render() {
- try {
- image(this.sprite, this.X, this.Y);
- this.boundingRect = makeBoundingRect(this.X, this.Y, this.X + this.sprite.width, this.Y + this.sprite.height);
- }
- catch (NullPointerException NPE) {
- println("[ERROR][Class = Sprite]: the loaded image " + this.sprite + " is null & void");
- exit();
- }
- }
- void moveMe(int xtrans, int ytrans) {
- this.X += xtrans;
- this.Y += ytrans;
- }
- void rotateMe(int deg) {
- pushMatrix();
- this.faceAngle += deg;
- //this.faceAngle = constrain(this.faceAngle, 0, 360);
- //translate(this.X + this.sprite.width / 2, this.Y + this.sprite.height / 2);
- rotate(radians(this.faceAngle));
- render();
- popMatrix();
- }
- private void updateXY() {
- curX = this.X;
- curY = this.Y;
- }
- void resizeMe(int w, int h) {
- this.sprite.resize(w, h);
- }
- int[] getBoundingRect() { return this.boundingRect; }
- // void rotateMeTimed()
- // void scaleMe()
- // void scaleMeTimed()
- }
- public class SpriteBatch {
- private ArrayList<Sprite> batch = new ArrayList<Sprite>();
- private int batchIndex = 0;
- private Sprite curSprite;
- private int destinationX;
- private int destinationY;
- private boolean keepX, keepY;
- SpriteBatch() {
- }
- void addSprite(Sprite s) {
- batch.add(batchIndex, s);
- batchIndex++;
- }
- void addNewSprite(String spriteName, int xs, int ys) {
- batch.add(batchIndex, new Sprite(xs, ys, spriteName));
- batchIndex++;
- }
- void removeSprite(int index) {
- batch.remove(index);
- batchIndex--;
- }
- void moveBatchTimed(int desX, int desY, int intrvl, boolean preserveX, boolean preserveY) {
- //preserveX and preserveY are indicated if this batch's X or Y position should be preserved.
- //if both are false then the ALL the sprites within the batch will move to the same location
- //and be rendered on top of each other.
- destinationX = desX;
- destinationY = desY;
- int interval = intrvl;
- if (interval <= 0) {
- interval = 1;
- }
- keepX = preserveX;
- keepY = preserveY;
- //globals are needed here ^ because the inner class "run()" can not reach the parameters passed from the moveBatchTimed() method
- Timer tAnimate = new Timer();
- tAnimate.schedule(new TimerTask() {
- public void run() {
- for (int i = 0; i < batch.size(); i++) {
- curSprite = batch.get(i);
- if (curSprite.getX() < destinationX && !keepX) {
- curSprite.moveMe(1, 0);
- }
- else if (curSprite.getX() > destinationX && !keepX) {
- curSprite.moveMe(-1, 0);
- }
- else if (curSprite.getY() < destinationY && !keepY) {
- curSprite.moveMe(0, 1);
- }
- else if (curSprite.getY() > destinationY && !keepY) {
- curSprite.moveMe(0, -1);
- }
- }
- }
- }
- , 0, interval);
- }
- void render() {
- for (int i = 0; i < batch.size(); i++) {
- batch.get(i).render();
- }
- }
- void moveMe(int xtrans, int ytrans) {
- for (int i = 0; i < batch.size(); i++) {
- batch.get(i).setX(batch.get(i).getX()+xtrans);
- batch.get(i).setY(batch.get(i).getY()+ytrans);
- }
- }
- }
- //deprecated
- /*
- Sprite getSpriteByName(String name) {
- for (MySprite s : level.Sprites) {
- if (s.name.equals(name)) {
- return new Sprite(s.xpos, s.ypos, s.img);
- }
- }
- println("The sprite [" + name + "] does not exist. A default (empty) sprite has been returned instead");
- return new Sprite();
- }
- */
- void renderSprites() {
- for (Sprite s : LoadedSprites) {
- s.rotateMe(5);
- //s.render();
- //drawBoundingRect(s.getBoundingRect());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement