Advertisement
reisi007

Drawable class for LibGDX

Feb 19th, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.77 KB | None | 0 0
  1. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  2. import com.badlogic.gdx.graphics.g2d.TextureRegion;
  3. import com.badlogic.gdx.math.Rectangle;
  4. import com.badlogic.gdx.math.Vector2;
  5.  
  6. /*
  7. A class made for easyfied drawing
  8.  */
  9. public class Drawable implements IIntersectable, IGameObject {
  10.     public static TextureRegion blackDebug = null;
  11.     public static boolean DEBUG = false;
  12.     private TextureRegion textureRegion;
  13.     protected float x, y, w, h;
  14.     private Rectangle bounds;
  15.  
  16.     public Drawable(TextureRegion textureRegion, Vector2 position, Anchor anchor) {
  17.         this(textureRegion, position, anchor, textureRegion.getRegionWidth());
  18.     }
  19.  
  20.     public Drawable(TextureRegion textureRegion, Vector2 position, Anchor anchor, float setWidth) {
  21.         this(textureRegion, position, anchor, setWidth, true, false, false);
  22.     }
  23.  
  24.     public Drawable(TextureRegion textureRegion, Vector2 position, Anchor anchor, float setWidth, boolean flipV) {
  25.         this(textureRegion, position, anchor, setWidth, true, false, flipV);
  26.     }
  27.  
  28.     public Drawable(TextureRegion textureRegion, Vector2 position, Anchor anchor, float setSide, boolean setWidth, boolean flipH, boolean flipV) {
  29.         this.textureRegion = textureRegion;
  30.         w = textureRegion.getRegionWidth();
  31.         h = textureRegion.getRegionHeight();
  32.         setPosition(anchor, position.x, position.y);
  33.         setScale(setSide / (setWidth ? w : h));
  34.         this.textureRegion.flip(flipV, flipH);
  35.         UpdateRectangle();
  36.     }
  37.  
  38.     public void UpdateRectangle() {
  39.         bounds = new Rectangle(x, y, w, h);
  40.     }
  41.  
  42.     @Override
  43.     public void Update(GameTime.GameTimeArgs gameTimeArgs) {
  44.         UpdateRectangle();
  45.     }
  46.  
  47.     public void Draw(SpriteBatch spriteBatch) {
  48.         if (DEBUG) {
  49.             spriteBatch.draw(blackDebug, bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight());
  50.         }
  51.         spriteBatch.draw(textureRegion, x, y, w, h);
  52.     }
  53.  
  54.     public float rightMost() {
  55.         return x + w;
  56.     }
  57.  
  58.     @Override
  59.     public void setScale(float newScale) {
  60.         setScale(newScale, newScale);
  61.     }
  62.  
  63.     public void setScale(float sx, float sy) {
  64.         w *= sx;
  65.         h *= sy;
  66.     }
  67.  
  68.     public static void setSclae(float newScale, IGameObject object) {
  69.         object.setScale(newScale);
  70.     }
  71.  
  72.     public void setPosition(Anchor a, float x, float y) {
  73.         // Adapt x coordinate
  74.         switch (a) {
  75.             case TopMiddle:
  76.             case MiddleMiddle:
  77.             case LowMiddle:
  78.                 x -= w / 2;
  79.                 break;
  80.             case TopRight:
  81.             case MiddleRight:
  82.             case LowRight:
  83.                 x -= w;
  84.                 break;
  85.             default: //*L
  86.                 break;
  87.         }
  88.         //Adpat y coordinate
  89.         switch (a) {
  90.             case TopLeft:
  91.             case TopMiddle:
  92.             case TopRight:
  93.                 y -= h;
  94.                 break;
  95.             case MiddleLeft:
  96.             case MiddleMiddle:
  97.             case MiddleRight:
  98.                 y -= h / 2;
  99.                 break;
  100.             default: //L*
  101.                 break;
  102.         }
  103.         this.x = x;
  104.         this.y = y;
  105.     }
  106.  
  107.     public TextureRegion getTextureRegion() {
  108.         return textureRegion;
  109.     }
  110.  
  111.  
  112.     public boolean Intersects(Rectangle[] o) {
  113.         Rectangle[] origin = getBounds();
  114.         for (int j = 0; j < origin.length; j++)
  115.             for (int i = 0; i < o.length; i++)
  116.                 if (origin[j].overlaps(o[i]))
  117.                     return true;
  118.         return false;
  119.  
  120.     }
  121.  
  122.     public Rectangle[] getBounds() {
  123.         return new Rectangle[]{bounds};
  124.     }
  125.  
  126.     public String toString() {
  127.         return "X; " + x + " Y: " + y + " W: " + w + "H: " + h;
  128.     }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement