Android Open Source - tetris-android Factory Shape






From Project

Back to project page tetris-android.

License

The source code is released under:

MIT License

If you think the Android project tetris-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.ultimate39.android.games.tetris;
/*ww w . ja  v a  2  s. c o m*/
import java.util.Random;


import com.badlogic.androidgames.framework.gl.TextureRegion;
import com.badlogic.androidgames.framework.helper.Logger;

public class FactoryShape {

  static Logger log = new Logger();
  static Random rand = new Random();
  // ??? ?????????? ????? ??? ??????? ?????????? ?????? "?????" ??? Shape2
  static int positionLine = 1;

  public static Shape newShape(int shapeType,int color) {
    
    switch (shapeType) {
    case 0:
      return new Shape1(color);
    case 1:
      return new Shape2(color);
    case 2:
      return new Shape3(color);
    case 3:
      return new Shape4(color);
    case 4:
      return new Shape5(color);
    case 5:
      return new Shape6(color);
    case 6:
      return new Shape7(color);
    }
    

    return new Shape1(color);
  }

  public static TextureRegion setColor(int color) {

    switch (color) {
    case 0:
      return Assets.blue;
    case 1:
      return Assets.green;
    case 2:
      return Assets.red;
    case 3:
      return Assets.blueLight;
    }
    return null;
  }

  public static void turnShapeLine(Shape shape) {
    Square square[] = shape.getArray();

    if (positionLine == 1) {
      turnSquare(square[1], square[2]);
      turnSquare(square[3], square[2]);
      turnAroundSquareLine(square[0], square[2]);
    } else {
      turnSquare(square[0], square[1]);
      turnSquare(square[2], square[1]);
      turnAroundSquareLine(square[3], square[1]);
    }

  }

  public static void turnShape(Shape shape, Square field[][]) {
    Square[] square = shape.getArray();
    Square squareCent = square[1];
    int type = shape.getType();
    boolean disable = false;
    if (disable != true) {
      switch (type) {
      case 1:
        return;
      case 2:
        if (positionLine == 1) {
          if (isNearSquare(square[1], field) == false
              && isNearSquare(square[3], field) == false
              && isNearAroundSquareLine(square[0], field) == false) {
            moveIfNearWallLine(shape);
            turnShapeLine(shape);
            positionLine = 2;
            return;
          }
        }
        if (positionLine == 2) {
          if (isNearSquare(square[0], field) == false
              && isNearSquare(square[2], field) == false
              && isNearAroundSquareLine(square[3], field) == false) {

            moveIfNearWallLine(shape);
            turnShapeLine(shape);
            positionLine = 1;
            return;
          }
        }

        return;
      case 3:
        if (isNearSquare(square[0], field) == false
            && isNearSquare(square[2], field) == false
            && isNearAroundSquare(square[3], field) == false) {
          moveIfNearWall(shape, 1);
          turnSquare(square[0], squareCent);
          turnSquare(square[2], squareCent);
          turnAroundSquare(square[3], squareCent);
        }
        return;
      case 4:
        if (isNearSquare(square[0], field) == false
            && isNearSquare(square[2], field) == false
            && isNearAroundSquare(square[3], field) == false) {
          moveIfNearWall(shape, 1);
          turnSquare(square[0], squareCent);
          turnSquare(square[2], squareCent);
          turnAroundSquare(square[3], squareCent);
        }
        return;
      case 5:
        if (isNearSquare(square[0], field) == false
            && isNearSquare(square[2], field) == false
            && isNearAroundSquare(square[3], field) == false) {
          moveIfNearWall(shape, 1);
          turnSquare(square[0], squareCent);
          turnSquare(square[2], squareCent);
          turnAroundSquare(square[3], squareCent);
        }
        return;
      case 6:

        if (isNearSquare(square[0], field) == false
            && isNearSquare(square[2], field) == false
            && isNearAroundSquare(square[3], field) == false) {
          moveIfNearWall(shape, 1);
          turnSquare(square[0], squareCent);
          turnSquare(square[2], squareCent);
          turnSquare(square[3], squareCent);
        }
        return;
      case 7:

        if (isNearSquare(square[0], field) == false
            && isNearSquare(square[2], field) == false
            && isNearAroundSquare(square[3], field) == false) {
          moveIfNearWall(shape, 1);
          turnSquare(square[0], squareCent);
          turnSquare(square[2], squareCent);
          turnAroundSquare(square[3], squareCent);
        }
        return;
      }
    }

  }

  static void turnSquare(Square square, Square squareCent) {
    if (square.newFieldX - 1 == squareCent.newFieldX
        && square.newFieldY == squareCent.newFieldY) {
      square.newFieldX -= 1;
      square.newFieldY -= 1;
      square.position.add(-1, -1);
    } else if (square.newFieldX == squareCent.newFieldX
        && square.newFieldY + 1 == squareCent.newFieldY) {
      square.newFieldX -= 1;
      square.newFieldY += 1;
      square.position.add(-1, +1);
    } else if (square.newFieldX + 1 == squareCent.newFieldX
        && square.newFieldY == squareCent.newFieldY) {
      square.newFieldX += 1;
      square.newFieldY += 1;
      square.position.add(+1, +1);
    } else if (square.newFieldX == squareCent.newFieldX
        && square.newFieldY - 1 == squareCent.newFieldY) {
      square.newFieldX += 1;
      square.newFieldY -= 1;
      square.position.add(+1, -1);
    }

  }

  static void turnAroundSquare(Square square, Square squareCent) {
    // ???????????? 4 ???????
    if (square.newFieldX + 1 == squareCent.newFieldX
        && square.newFieldY - 1 == squareCent.newFieldY) {
      square.newFieldX += 2;
      square.newFieldY -= 0;
      square.position.add(+2, 0);
    } else if (square.newFieldX - 1 == squareCent.newFieldX
        && square.newFieldY - 1 == squareCent.newFieldY) {
      square.newFieldX -= 0;
      square.newFieldY -= 2;
      square.position.add(0, -2);
    } else if (square.newFieldX - 1 == squareCent.newFieldX
        && square.newFieldY + 1 == squareCent.newFieldY) {
      square.newFieldX -= 2;
      square.newFieldY += 0;
      square.position.add(-2, 0);
    } else if (square.newFieldX + 1 == squareCent.newFieldX
        && square.newFieldY + 1 == squareCent.newFieldY) {
      square.newFieldX += 0;
      square.newFieldY += 2;
      square.position.add(0, +2);
    }

  }

  static void turnAroundSquareLine(Square square, Square squareCent) {
    if (square.newFieldX - 2 == squareCent.newFieldX
        && square.newFieldY == squareCent.newFieldY) {
      square.newFieldX -= 2;
      square.newFieldY -= 2;
      square.position.add(-2, -2);
    } else if (square.newFieldX == squareCent.newFieldX
        && square.newFieldY + 2 == squareCent.newFieldY) {
      square.newFieldX -= 2;
      square.newFieldY += 2;
      square.position.add(-2, +2);
    } else if (square.newFieldX + 2 == squareCent.newFieldX
        && square.newFieldY == squareCent.newFieldY) {
      square.newFieldX += 2;
      square.newFieldY += 2;
      square.position.add(+2, +2);
    } else if (square.newFieldX == squareCent.newFieldX
        && square.newFieldY - 2 == squareCent.newFieldY) {
      square.newFieldX += 2;
      square.newFieldY -= 2;
      square.position.add(+2, -2);
    }

  }

  static boolean isNearAroundSquare(Square square, Square field[][]) {
    synchronized (square) {
      if (field[square.newFieldY + 2][square.newFieldX - 0] != null) {
        return true;
      } else if (field[square.newFieldY - 0][square.newFieldX - 2] != null) {
        return true;
      } else if (field[square.newFieldY - 2][square.newFieldX] != null) {
        return true;
      } else if (field[square.newFieldY][square.newFieldX - 2] != null) {
        return true;
      }
      return false;
    }
  }

  static boolean isNearAroundSquareLine(Square square, Square field[][]) {
    synchronized (square) {
      if (field[square.newFieldY - 2][square.newFieldX - 2] != null) {
        return true;
      } else if (field[square.newFieldY - 2][square.newFieldX + 2] != null) {
        return true;
      } else if (field[square.newFieldY + 2][square.newFieldX + 2] != null) {
        return true;
      } else if (field[square.newFieldY + 2][square.newFieldX - 2] != null) {
        return true;
      }

      return false;
    }
  }

  static boolean isNearSquare(Square square, Square field[][]) {
    synchronized (square) {
      if (field[square.newFieldY - 1][square.newFieldX - 1] != null) {
        return true;
      }
      if (field[square.newFieldY - 1][square.newFieldX + 1] != null) {
        return true;
      }
      if (field[square.newFieldY + 1][square.newFieldX + 1] != null) {
        return true;
      }
      if (field[square.newFieldY + 1][square.newFieldX - 1] != null) {
        return true;
      }
      return false;
    }
  }

  static void moveLeftShape(Shape shape, Square field[][]) {
    synchronized (shape) {
      Square square[] = shape.getArray();
      for (Square element : square) {
        if (element.newFieldX == 2) {
          return;
        } else if (field[element.newFieldY][element.newFieldX - 1] != null) {
          return;
        }
      }
      shape.update(-1, 0);
    }
  }

  static void moveRightShape(Shape shape, Square field[][]) {
    synchronized (shape) {
      Square square[] = shape.getArray();
      for (Square element : square) {
        if (element.newFieldX == 14) {
          return;
        } else if (field[element.newFieldY][element.newFieldX + 1] != null) {
          return;
        }
      }
      shape.update(1, 0);
    }
  }

  static void moveIfNearWall(Shape shape, int x) {
    Square square[] = shape.getArray();
    if (square[1].newFieldX == 2) {
      shape.update(x, 0);
    }
    if (square[1].newFieldX == 14) {
      shape.update(-x, 0);
    }
  }

    
  static void moveIfNearWallLine(Shape shape) {

    Square square[] = shape.getArray();

    if (square[2].newFieldX == 2) {
      shape.update(2, 0);
    }
    if (square[2].newFieldX == 3) {
      shape.update(1, 0);
    }
    if (square[2].newFieldX == 14) {
      shape.update(-2, 0);
    }
    if (square[2].newFieldX == 13) {
      shape.update(-1, 0);
    }
  }

  
}




Java Source Code List

com.badlogic.androidgames.framework.Audio.java
com.badlogic.androidgames.framework.Color.java
com.badlogic.androidgames.framework.DynamicGameObject.java
com.badlogic.androidgames.framework.FileIOInternal.java
com.badlogic.androidgames.framework.FileIO.java
com.badlogic.androidgames.framework.GameObject.java
com.badlogic.androidgames.framework.Game.java
com.badlogic.androidgames.framework.Graphics.java
com.badlogic.androidgames.framework.Input.java
com.badlogic.androidgames.framework.Music.java
com.badlogic.androidgames.framework.Pixmap.java
com.badlogic.androidgames.framework.Pool.java
com.badlogic.androidgames.framework.Screen.java
com.badlogic.androidgames.framework.Sound.java
com.badlogic.androidgames.framework.TestScreen.java
com.badlogic.androidgames.framework.gl.Animation.java
com.badlogic.androidgames.framework.gl.BTMPFont.java
com.badlogic.androidgames.framework.gl.Camera2D.java
com.badlogic.androidgames.framework.gl.Font.java
com.badlogic.androidgames.framework.gl.SpatialHashGrid.java
com.badlogic.androidgames.framework.gl.SpriteBatcher.java
com.badlogic.androidgames.framework.gl.TextureRegion.java
com.badlogic.androidgames.framework.gl.Texture.java
com.badlogic.androidgames.framework.gl.Vertices.java
com.badlogic.androidgames.framework.helper.DebugDraw.java
com.badlogic.androidgames.framework.helper.FPSCounter.java
com.badlogic.androidgames.framework.helper.Logger.java
com.badlogic.androidgames.framework.impl.AccelerometerHandler.java
com.badlogic.androidgames.framework.impl.AndroidAudio.java
com.badlogic.androidgames.framework.impl.AndroidFastRenderView.java
com.badlogic.androidgames.framework.impl.AndroidFileIOInternal.java
com.badlogic.androidgames.framework.impl.AndroidFileIO.java
com.badlogic.androidgames.framework.impl.AndroidGame.java
com.badlogic.androidgames.framework.impl.AndroidGraphics.java
com.badlogic.androidgames.framework.impl.AndroidInput.java
com.badlogic.androidgames.framework.impl.AndroidMusic.java
com.badlogic.androidgames.framework.impl.AndroidPixmap.java
com.badlogic.androidgames.framework.impl.AndroidSound.java
com.badlogic.androidgames.framework.impl.GLGame.java
com.badlogic.androidgames.framework.impl.GLGraphics.java
com.badlogic.androidgames.framework.impl.GLScreen.java
com.badlogic.androidgames.framework.impl.KeyboardHandler.java
com.badlogic.androidgames.framework.impl.MultiTouchHandler.java
com.badlogic.androidgames.framework.impl.SingleTouchHandler.java
com.badlogic.androidgames.framework.impl.TouchHandler.java
com.badlogic.androidgames.framework.math.Circle.java
com.badlogic.androidgames.framework.math.OverlapTester.java
com.badlogic.androidgames.framework.math.Rectangle.java
com.badlogic.androidgames.framework.math.Vector2.java
com.ultimate39.android.games.tetris.Assets.java
com.ultimate39.android.games.tetris.FactoryShape.java
com.ultimate39.android.games.tetris.GameScreen.java
com.ultimate39.android.games.tetris.MainMenuScreen.java
com.ultimate39.android.games.tetris.Settings.java
com.ultimate39.android.games.tetris.Shape.java
com.ultimate39.android.games.tetris.Square.java
com.ultimate39.android.games.tetris.Tetris.java
com.ultimate39.android.games.tetris.World.java