Guest User

SimpleActor

a guest
Jan 12th, 2018
617
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.ogurecapps.actors;
  2.  
  3. import com.badlogic.gdx.graphics.Color;
  4. import com.badlogic.gdx.graphics.g2d.Batch;
  5. import com.badlogic.gdx.graphics.g2d.TextureRegion;
  6. import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
  7. import com.badlogic.gdx.math.Polygon;
  8. import com.badlogic.gdx.math.Rectangle;
  9. import com.badlogic.gdx.scenes.scene2d.Actor;
  10. import com.badlogic.gdx.scenes.scene2d.utils.ScissorStack;
  11. import com.badlogic.gdx.utils.viewport.Viewport;
  12.  
  13. /**
  14.  * Created by coder1cv8 on 07.02.2017.
  15.  */
  16.  
  17. public class SimpleActor extends Actor {
  18.  
  19.     public final TextureRegion region;
  20.     private Rectangle clipBounds;
  21.     private boolean drawHitbox;
  22.     private final ShapeRenderer renderer;
  23.  
  24.     public SimpleActor(TextureRegion region) {
  25.         this.region = region;
  26.  
  27.         setSize(region.getRegionWidth(), region.getRegionHeight());
  28.         setBounds(0, 0, getWidth(), getHeight());
  29.  
  30.         renderer = new ShapeRenderer();
  31.     }
  32.  
  33.     public void setDrawHitbox(boolean drawHitbox) {
  34.         this.drawHitbox = drawHitbox;
  35.     }
  36.  
  37.     public void enableClipping(Rectangle clipBounds) {
  38.         this.clipBounds = clipBounds;
  39.     }
  40.  
  41.     @Override
  42.     public void draw(Batch batch, float parentAlpha) {
  43.         Color color = getColor();
  44.         batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
  45.  
  46.         if (clipBounds != null) {
  47.             Rectangle scissors = new Rectangle();
  48.             final Viewport viewport = getStage().getViewport();
  49.             ScissorStack.calculateScissors(getStage().getCamera(), viewport.getScreenX(), viewport.getScreenY(), viewport.getScreenWidth(), viewport.getScreenHeight(), batch.getTransformMatrix(), clipBounds, scissors);
  50.             ScissorStack.pushScissors(scissors);
  51.         }
  52.  
  53.         batch.draw(region, getX(), getY(), getOriginX(), getOriginY(), getWidth(), getHeight(), getScaleX(), getScaleY(), getRotation());
  54.  
  55.         if (clipBounds != null) {
  56.             batch.flush();
  57.             ScissorStack.popScissors();
  58.         }
  59.  
  60.         if (drawHitbox) {
  61.             batch.end();
  62.  
  63.             renderer.setProjectionMatrix(batch.getProjectionMatrix());
  64.             renderer.setTransformMatrix(batch.getTransformMatrix());
  65.             renderer.begin(ShapeRenderer.ShapeType.Line);
  66.             renderer.setColor(1f, 1f, 1f, 1f);
  67.             renderer.polygon(getHitbox().getTransformedVertices());
  68.             renderer.end();
  69.  
  70.             batch.begin();
  71.         }
  72.     }
  73.  
  74.     public Polygon getHitbox() {
  75.         Polygon polygon = new Polygon(new float[]{0, 0, getWidth(), 0, getWidth(), getHeight(), 0, getHeight()});
  76.         polygon.setPosition(getX(), getY());
  77.         polygon.setOrigin(getOriginX(), getOriginY());
  78.         polygon.setScale(getScaleX(), getScaleY());
  79.         polygon.setRotation(getRotation());
  80.  
  81.         return polygon;
  82.     }
  83.  
  84. }
RAW Paste Data