Advertisement
Guest User

Pasha

a guest
Jan 23rd, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.92 KB | None | 0 0
  1. package org.home.game.org.home.game.sprite;
  2.  
  3. import com.badlogic.gdx.graphics.Color;
  4. import com.badlogic.gdx.graphics.g2d.Batch;
  5. import com.badlogic.gdx.graphics.g2d.TextureAtlas;
  6. import com.badlogic.gdx.math.MathUtils;
  7. import com.badlogic.gdx.math.Rectangle;
  8. import com.badlogic.gdx.scenes.scene2d.Actor;
  9. import com.badlogic.gdx.scenes.scene2d.actions.Actions;
  10.  
  11. import org.home.game.org.home.util.Constans;
  12. import org.home.game.org.home.util.Direction;
  13.  
  14.  
  15.  
  16. public class Box extends Actor{
  17.  
  18.     private TextureAtlas.AtlasRegion boxTexture;
  19.     private Rectangle bounds;
  20.     private Direction direction;
  21.     private float toX, toY;
  22.     public boolean isMoving;
  23.     public int speed = 1;
  24.  
  25.     public Box(int direction) {
  26.         boxTexture = new TextureAtlas.AtlasRegion(TextureManager.getRegionByName("box"));
  27.  
  28.         switch (direction) {
  29.             case 0:
  30.                 this.direction = Direction.RIGHT;
  31.                 setPosition(Constans.BOX_RIGHT_X, Constans.BOX_RIGHT_Y);
  32.                 break;
  33.             case 1:
  34.                 this.direction = Direction.LEFT;
  35.                 setPosition(Constans.BOX_LEFT_X, Constans.BOX_LEFT_Y);
  36.                 break;
  37.             default:
  38.                 this.direction = Direction.RIGHT;
  39.                 setPosition(Constans.BOX_RIGHT_X, Constans.BOX_RIGHT_Y);
  40.                 break;
  41.         }
  42.  
  43.         toX = MathUtils.random(0, Constans.BOX_COUNT_INLINE) * 64;
  44.         toY = 0;
  45.  
  46.         bounds = new Rectangle(getX(), getY(), Constans.BOX_WIDTH, Constans.BOX_HEIGHT);
  47.  
  48.         isMoving = false;
  49.     }
  50.  
  51.     @Override
  52.     public void draw(Batch batch, float parentAlpha) {
  53.         Color color = this.getColor();
  54.         batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
  55.         batch.draw(boxTexture, getX(), getY(), Constans.BOX_WIDTH, Constans.BOX_HEIGHT);
  56.         super.draw(batch, batch.getColor().a);
  57.     }
  58.  
  59.     public void update(){
  60.         check();
  61.     }
  62.  
  63.     private void moveY() {
  64.         if(!isMoving){
  65.             this.addAction(Actions.moveTo(getX() + speed, Constans.HEIGHT));
  66. //            setY(getY() - speed);
  67.             this.isMoving = true;
  68.         }
  69.     }
  70.  
  71.  
  72.     private void moveX() {
  73.         if(!isMoving){
  74.             this.addAction(Actions.moveTo(getX() + speed, 0));
  75. //            setX(getX() + speed);
  76.             this.isMoving = true;
  77.         }
  78.     }
  79.  
  80.     private void check() {
  81.         if (toX != getX()) {
  82.             moveX();
  83.             this.isMoving = false;
  84.             this.addAction(Actions.fadeOut(speed * 0.025f));
  85.         } else if (toY != getY()) {
  86.             moveY();
  87.             this.isMoving = false;
  88.             this.addAction(Actions.fadeOut(speed * 0.025f));
  89.         }
  90.     }
  91.  
  92.     public Rectangle getBounds() {
  93.         return bounds;
  94.     }
  95.  
  96.     public void setToY(int toY) {
  97.         this.toY = toY;
  98.     }
  99.  
  100.     public void setToX(int toX) {
  101.         this.toX = toX;
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement