Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.16 KB | None | 0 0
  1. package southgrove.game.board.core;
  2.  
  3. import android.opengl.GLES20;
  4. import android.opengl.Matrix;
  5. import southgrove.droidgl.DroidGL;
  6. import southgrove.droidgl.core.Material;
  7. import southgrove.droidgl.core.Node;
  8. import southgrove.droidgl.core.NodeBase;
  9. import southgrove.droidgl.math.MathUtils;
  10.  
  11. public class Tetromino extends NodeBase implements Node
  12. {
  13.  
  14.     public Tetromino(int type, int gridPosX, int gridPosY, int gridPosZ, int gridRotation, Material material)
  15.     {
  16.         super(material);
  17.  
  18.         this.type = type;
  19.         this.gridPosX = gridPosX;
  20.         this.gridPosY = gridPosY;
  21.         this.gridPosZ = gridPosZ;
  22.         this.gridRotation = (int) MathUtils.wrap(gridRotation, 0, 4);
  23.         this.points = getPoints(type, gridRotation);
  24.     }
  25.  
  26.     @Override
  27.     protected void onCreated()
  28.     {
  29.         // THIS WILL NEVER HAPPEN
  30.     }
  31.  
  32.     @Override
  33.     protected void onDraw(float timeFactor)
  34.     {
  35.         final TetrominoMesh mesh = TetrominoMesh.getMesh(type, gridRotation);
  36.  
  37.         rotation.x = 0f;
  38.         rotation.y = 0f;
  39.         rotation.z = gridRotation * GRID_ROTATION_DEGREES;
  40.  
  41.         /*
  42.          * GRID POSITION
  43.          */
  44.  
  45.         positionHandle = GLES20.glGetUniformLocation(material.getShader(), "uPosition");
  46.         checkGlError("glGetUniformLocation uPosition");
  47.  
  48.         //positionHandle = GLES20.glGetAttribLocation(material.getShader(), "aPosition");
  49.         //checkGlError("glGetAttribLocation aPosition");
  50.        
  51.         if (positionHandle == GL_ERROR)
  52.         {
  53.             throw new RuntimeException("Could not get uniform location for uPosition");
  54.         }
  55.  
  56.         GLES20.glUniform3f(positionHandle, position.x, position.y, position.z);
  57.         checkGlError("glUniform3f positionHandle");
  58.        
  59.         //GLES20.glVertexAttrib3f(positionHandle, position.x, position.y, position.z);
  60.         //checkGlError("glVertexAttrib3f positionHandle");
  61.  
  62.         /*
  63.          * VERTICES
  64.          */
  65.  
  66.         vertexHandle = GLES20.glGetAttribLocation(material.getShader(), "aVertex");
  67.         checkGlError("glGetAttribLocation aVertex");
  68.         if (vertexHandle == GL_ERROR)
  69.         {
  70.             throw new RuntimeException("Could not get attrib location for aVertex");
  71.         }
  72.  
  73.         GLES20.glEnableVertexAttribArray(vertexHandle);
  74.         checkGlError("glEnableVertexAttribArray vertexHandle");
  75.  
  76.         mesh.getVertexBuffer().position(TRIANGLE_VERTICES_DATA_POS_OFFSET);
  77.         GLES20.glVertexAttribPointer(vertexHandle, 3, GLES20.GL_FLOAT, false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mesh.getVertexBuffer());
  78.         checkGlError("glVertexAttribPointer vertexHandle");
  79.  
  80.         /*
  81.          * TEXTURE COORDS
  82.          */
  83.  
  84.         textureHandle = GLES20.glGetAttribLocation(material.getShader(), "aTextureCoord");
  85.         checkGlError("glGetAttribLocation aTextureCoord");
  86.         if (textureHandle == GL_ERROR)
  87.         {
  88.             throw new RuntimeException("Could not get attrib location for aTextureCoord");
  89.         }
  90.  
  91.         GLES20.glEnableVertexAttribArray(textureHandle);
  92.         checkGlError("glEnableVertexAttribArray textureHandle");
  93.  
  94.         mesh.getVertexBuffer().position(TRIANGLE_VERTICES_DATA_UV_OFFSET);
  95.         GLES20.glVertexAttribPointer(textureHandle, 2, GLES20.GL_FLOAT, false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mesh.getVertexBuffer());
  96.         checkGlError("glVertexAttribPointer textureHandle");
  97.  
  98.         /*
  99.          * MATRICES
  100.          */
  101.  
  102.         mvpMatrixHandle = GLES20.glGetUniformLocation(material.getShader(), "uMVPMatrix");
  103.         checkGlError("glGetUniformLocation uMVPMatrix");
  104.         if (mvpMatrixHandle == GL_ERROR)
  105.         {
  106.             throw new RuntimeException("Could not get attrib location for uMVPMatrix");
  107.         }
  108.  
  109.         Matrix.setIdentityM(mMatrix, 0);
  110.  
  111.         GLES20.glUniformMatrix4fv(mvpMatrixHandle, 1, false, DroidGL.getActiveCamera().getModelViewProjectionMatrix(mMatrix), 0);
  112.  
  113.         /*
  114.          * DRAW
  115.          */
  116.  
  117.         GLES20.glDrawElements(GLES20.GL_TRIANGLES, mesh.getNumIndices(), GLES20.GL_UNSIGNED_SHORT, mesh.getIndexBuffer());
  118.     }
  119.  
  120.     public void addToGrid(Grid grid)
  121.     {
  122.         for (int[] point : points)
  123.         {
  124.             final Cell c = grid.cell(gridPosX + point[0], gridPosY + point[1], gridPosZ);
  125.  
  126.             c.setIsTetrominoCenter(point[0] == 0 && point[1] == 0);
  127.             c.setTetromino(this);
  128.         }
  129.     }
  130.  
  131.     public void removeFromGrid(Grid grid)
  132.     {
  133.         for (int[] point : points)
  134.         {
  135.             final Cell c = grid.cell(gridPosX + point[0], gridPosY + point[1], gridPosZ);
  136.  
  137.             c.setIsTetrominoCenter(false);
  138.             c.setTetromino(null);
  139.         }
  140.     }
  141.  
  142.     public int getType()
  143.     {
  144.         return type;
  145.     }
  146.  
  147.     public int getGridPosX()
  148.     {
  149.         return gridPosX;
  150.     }
  151.  
  152.     public void setGridPosX(int gridPosX)
  153.     {
  154.         this.gridPosX = gridPosX;
  155.     }
  156.  
  157.     public int getGridPosY()
  158.     {
  159.         return gridPosY;
  160.     }
  161.  
  162.     public void setGridPosY(int gridPosY)
  163.     {
  164.         this.gridPosY = gridPosY;
  165.     }
  166.  
  167.     public int getGridPosZ()
  168.     {
  169.         return gridPosZ;
  170.     }
  171.  
  172.     public void setGridPosZ(int gridPosZ)
  173.     {
  174.         this.gridPosZ = gridPosZ;
  175.     }
  176.  
  177.     public int getGridRotation()
  178.     {
  179.         return gridRotation;
  180.     }
  181.  
  182.     private int                 type;
  183.     private int                 gridPosX                            = 0;
  184.     private int                 gridPosY                            = 0;
  185.     private int                 gridPosZ                            = 0;
  186.     private int                 gridRotation                        = 0;
  187.     private int[][]             points;
  188.  
  189.     private float[]             mMatrix                             = new float[16];
  190.  
  191.     private int                 vertexHandle;
  192.     private int                 positionHandle;
  193.     private int                 textureHandle;
  194.     private int                 mvpMatrixHandle;
  195.  
  196.     private static final float  GRID_ROTATION_DEGREES               = 90f;
  197.  
  198.     private static final int    FLOAT_SIZE_BYTES                    = 4;
  199.     private static final int    TRIANGLE_VERTICES_DATA_STRIDE_BYTES = 5 * FLOAT_SIZE_BYTES;
  200.     private static final int    TRIANGLE_VERTICES_DATA_POS_OFFSET   = 0;
  201.     private static final int    TRIANGLE_VERTICES_DATA_UV_OFFSET    = 3;
  202.  
  203.     private static final int    GL_ERROR                            = -1;
  204.  
  205.     public static final int     TYPE_I                              = 0;
  206.     public static final int     TYPE_J                              = 1;
  207.     public static final int     TYPE_L                              = 2;
  208.     public static final int     TYPE_O                              = 3;
  209.     public static final int     TYPE_S                              = 4;
  210.     public static final int     TYPE_Z                              = 5;
  211.     public static final int     TYPE_T                              = 6;
  212.     public static final int     NUM_TYPES                           = 7;
  213.  
  214.     public static Boolean fitsOnGrid(Grid grid, int type, int gridPosX, int gridPosY, int gridPosZ, int gridRotation)
  215.     {
  216.         int[][] points = getPoints(type, gridRotation);
  217.  
  218.         for (int[] point : points)
  219.             if (grid.cell(gridPosX + point[0], gridPosY + point[1], gridPosZ).getTetromino() != null)
  220.                 return false;
  221.  
  222.         return true;
  223.     }
  224.  
  225.     protected static int[][] getPoints(int type, int rotation)
  226.     {
  227.         switch (type)
  228.         {
  229.             case TYPE_I:
  230.                 return I[(int) MathUtils.wrap(rotation, 0, 2)];
  231.  
  232.             case TYPE_J:
  233.                 return J[(int) MathUtils.wrap(rotation, 0, 4)];
  234.  
  235.             case TYPE_L:
  236.                 return L[(int) MathUtils.wrap(rotation, 0, 4)];
  237.  
  238.             case TYPE_O:
  239.                 return O[0];
  240.  
  241.             case TYPE_S:
  242.                 return S[(int) MathUtils.wrap(rotation, 0, 2)];
  243.  
  244.             case TYPE_Z:
  245.                 return Z[(int) MathUtils.wrap(rotation, 0, 2)];
  246.  
  247.             case TYPE_T:
  248.                 return T[(int) MathUtils.wrap(rotation, 0, 4)];
  249.  
  250.             default:
  251.                 return null;
  252.  
  253.         }
  254.     }
  255.  
  256.     private final static int[][][]  I   = new int[][][] {
  257.                                         { { 0, 1 }, { 0, 0 }, { 0, -1 }, { 0, -2 } },
  258.                                         { { -1, 0 }, { 0, 0 }, { 1, 0 }, { 2, 0 } }
  259.                                         };
  260.  
  261.     private final static int[][][]  J   = new int[][][] {
  262.                                         { { 0, 1 }, { 0, 0 }, { -1, -1 }, { 0, -1 } },
  263.                                         { { -1, 1 }, { -1, 0 }, { 0, 0 }, { 1, 0 } },
  264.                                         { { 0, 1 }, { 1, 1 }, { 0, 0 }, { 0, -1 } },
  265.                                         { { -1, 0 }, { 0, 0 }, { 1, 0 }, { 1, -1 } },
  266.                                         };
  267.  
  268.     private final static int[][][]  L   = new int[][][] {
  269.                                         { { 0, 1 }, { 0, 0 }, { 0, -1 }, { 1, -1 } },
  270.                                         { { -1, 0 }, { 0, 0 }, { 1, 0 }, { -1, -1 } },
  271.                                         { { -1, 1 }, { 0, 1 }, { 0, 0 }, { 0, -1 } },
  272.                                         { { 1, 1 }, { -1, 0 }, { 0, 0 }, { 1, 0 } },
  273.                                         };
  274.  
  275.     private final static int[][][]  O   = new int[][][] {
  276.                                         { { 0, 0 }, { 1, 0 }, { 0, -1 }, { 1, -1 } }
  277.                                         };
  278.  
  279.     private final static int[][][]  S   = new int[][][] {
  280.                                         { { 0, 1 }, { 0, 0 }, { 1, 0 }, { 1, -1 } },
  281.                                         { { 0, 1 }, { 1, 1 }, { -1, 0 }, { 0, 0 } }
  282.                                         };
  283.  
  284.     private final static int[][][]  Z   = new int[][][] {
  285.                                         { { 0, 1 }, { 0, 0 }, { -1, 0 }, { -1, -1 } },
  286.                                         { { -1, 0 }, { 0, 0 }, { 0, -1 }, { 1, -1 } }
  287.                                         };
  288.  
  289.     private final static int[][][]  T   = new int[][][] {
  290.                                         { { -1, 0 }, { 0, 0 }, { 1, 0 }, { 0, -1 } },
  291.                                         { { 0, 1 }, { -1, 0 }, { 0, 0 }, { 0, -1 } },
  292.                                         { { 0, 1 }, { -1, 0 }, { 0, 0 }, { 1, 0 } },
  293.                                         { { 0, 1 }, { 0, 0 }, { 1, 0 }, { 0, -1 } },
  294.                                         };
  295.  
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement