Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package southgrove.game.board.core;
- import android.opengl.GLES20;
- import android.opengl.Matrix;
- import southgrove.droidgl.DroidGL;
- import southgrove.droidgl.core.Material;
- import southgrove.droidgl.core.Node;
- import southgrove.droidgl.core.NodeBase;
- import southgrove.droidgl.math.MathUtils;
- public class Tetromino extends NodeBase implements Node
- {
- public Tetromino(int type, int gridPosX, int gridPosY, int gridPosZ, int gridRotation, Material material)
- {
- super(material);
- this.type = type;
- this.gridPosX = gridPosX;
- this.gridPosY = gridPosY;
- this.gridPosZ = gridPosZ;
- this.gridRotation = (int) MathUtils.wrap(gridRotation, 0, 4);
- this.points = getPoints(type, gridRotation);
- }
- @Override
- protected void onCreated()
- {
- // THIS WILL NEVER HAPPEN
- }
- @Override
- protected void onDraw(float timeFactor)
- {
- final TetrominoMesh mesh = TetrominoMesh.getMesh(type, gridRotation);
- rotation.x = 0f;
- rotation.y = 0f;
- rotation.z = gridRotation * GRID_ROTATION_DEGREES;
- /*
- * GRID POSITION
- */
- positionHandle = GLES20.glGetUniformLocation(material.getShader(), "uPosition");
- checkGlError("glGetUniformLocation uPosition");
- //positionHandle = GLES20.glGetAttribLocation(material.getShader(), "aPosition");
- //checkGlError("glGetAttribLocation aPosition");
- if (positionHandle == GL_ERROR)
- {
- throw new RuntimeException("Could not get uniform location for uPosition");
- }
- GLES20.glUniform3f(positionHandle, position.x, position.y, position.z);
- checkGlError("glUniform3f positionHandle");
- //GLES20.glVertexAttrib3f(positionHandle, position.x, position.y, position.z);
- //checkGlError("glVertexAttrib3f positionHandle");
- /*
- * VERTICES
- */
- vertexHandle = GLES20.glGetAttribLocation(material.getShader(), "aVertex");
- checkGlError("glGetAttribLocation aVertex");
- if (vertexHandle == GL_ERROR)
- {
- throw new RuntimeException("Could not get attrib location for aVertex");
- }
- GLES20.glEnableVertexAttribArray(vertexHandle);
- checkGlError("glEnableVertexAttribArray vertexHandle");
- mesh.getVertexBuffer().position(TRIANGLE_VERTICES_DATA_POS_OFFSET);
- GLES20.glVertexAttribPointer(vertexHandle, 3, GLES20.GL_FLOAT, false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mesh.getVertexBuffer());
- checkGlError("glVertexAttribPointer vertexHandle");
- /*
- * TEXTURE COORDS
- */
- textureHandle = GLES20.glGetAttribLocation(material.getShader(), "aTextureCoord");
- checkGlError("glGetAttribLocation aTextureCoord");
- if (textureHandle == GL_ERROR)
- {
- throw new RuntimeException("Could not get attrib location for aTextureCoord");
- }
- GLES20.glEnableVertexAttribArray(textureHandle);
- checkGlError("glEnableVertexAttribArray textureHandle");
- mesh.getVertexBuffer().position(TRIANGLE_VERTICES_DATA_UV_OFFSET);
- GLES20.glVertexAttribPointer(textureHandle, 2, GLES20.GL_FLOAT, false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mesh.getVertexBuffer());
- checkGlError("glVertexAttribPointer textureHandle");
- /*
- * MATRICES
- */
- mvpMatrixHandle = GLES20.glGetUniformLocation(material.getShader(), "uMVPMatrix");
- checkGlError("glGetUniformLocation uMVPMatrix");
- if (mvpMatrixHandle == GL_ERROR)
- {
- throw new RuntimeException("Could not get attrib location for uMVPMatrix");
- }
- Matrix.setIdentityM(mMatrix, 0);
- GLES20.glUniformMatrix4fv(mvpMatrixHandle, 1, false, DroidGL.getActiveCamera().getModelViewProjectionMatrix(mMatrix), 0);
- /*
- * DRAW
- */
- GLES20.glDrawElements(GLES20.GL_TRIANGLES, mesh.getNumIndices(), GLES20.GL_UNSIGNED_SHORT, mesh.getIndexBuffer());
- }
- public void addToGrid(Grid grid)
- {
- for (int[] point : points)
- {
- final Cell c = grid.cell(gridPosX + point[0], gridPosY + point[1], gridPosZ);
- c.setIsTetrominoCenter(point[0] == 0 && point[1] == 0);
- c.setTetromino(this);
- }
- }
- public void removeFromGrid(Grid grid)
- {
- for (int[] point : points)
- {
- final Cell c = grid.cell(gridPosX + point[0], gridPosY + point[1], gridPosZ);
- c.setIsTetrominoCenter(false);
- c.setTetromino(null);
- }
- }
- public int getType()
- {
- return type;
- }
- public int getGridPosX()
- {
- return gridPosX;
- }
- public void setGridPosX(int gridPosX)
- {
- this.gridPosX = gridPosX;
- }
- public int getGridPosY()
- {
- return gridPosY;
- }
- public void setGridPosY(int gridPosY)
- {
- this.gridPosY = gridPosY;
- }
- public int getGridPosZ()
- {
- return gridPosZ;
- }
- public void setGridPosZ(int gridPosZ)
- {
- this.gridPosZ = gridPosZ;
- }
- public int getGridRotation()
- {
- return gridRotation;
- }
- private int type;
- private int gridPosX = 0;
- private int gridPosY = 0;
- private int gridPosZ = 0;
- private int gridRotation = 0;
- private int[][] points;
- private float[] mMatrix = new float[16];
- private int vertexHandle;
- private int positionHandle;
- private int textureHandle;
- private int mvpMatrixHandle;
- private static final float GRID_ROTATION_DEGREES = 90f;
- private static final int FLOAT_SIZE_BYTES = 4;
- private static final int TRIANGLE_VERTICES_DATA_STRIDE_BYTES = 5 * FLOAT_SIZE_BYTES;
- private static final int TRIANGLE_VERTICES_DATA_POS_OFFSET = 0;
- private static final int TRIANGLE_VERTICES_DATA_UV_OFFSET = 3;
- private static final int GL_ERROR = -1;
- public static final int TYPE_I = 0;
- public static final int TYPE_J = 1;
- public static final int TYPE_L = 2;
- public static final int TYPE_O = 3;
- public static final int TYPE_S = 4;
- public static final int TYPE_Z = 5;
- public static final int TYPE_T = 6;
- public static final int NUM_TYPES = 7;
- public static Boolean fitsOnGrid(Grid grid, int type, int gridPosX, int gridPosY, int gridPosZ, int gridRotation)
- {
- int[][] points = getPoints(type, gridRotation);
- for (int[] point : points)
- if (grid.cell(gridPosX + point[0], gridPosY + point[1], gridPosZ).getTetromino() != null)
- return false;
- return true;
- }
- protected static int[][] getPoints(int type, int rotation)
- {
- switch (type)
- {
- case TYPE_I:
- return I[(int) MathUtils.wrap(rotation, 0, 2)];
- case TYPE_J:
- return J[(int) MathUtils.wrap(rotation, 0, 4)];
- case TYPE_L:
- return L[(int) MathUtils.wrap(rotation, 0, 4)];
- case TYPE_O:
- return O[0];
- case TYPE_S:
- return S[(int) MathUtils.wrap(rotation, 0, 2)];
- case TYPE_Z:
- return Z[(int) MathUtils.wrap(rotation, 0, 2)];
- case TYPE_T:
- return T[(int) MathUtils.wrap(rotation, 0, 4)];
- default:
- return null;
- }
- }
- private final static int[][][] I = new int[][][] {
- { { 0, 1 }, { 0, 0 }, { 0, -1 }, { 0, -2 } },
- { { -1, 0 }, { 0, 0 }, { 1, 0 }, { 2, 0 } }
- };
- private final static int[][][] J = new int[][][] {
- { { 0, 1 }, { 0, 0 }, { -1, -1 }, { 0, -1 } },
- { { -1, 1 }, { -1, 0 }, { 0, 0 }, { 1, 0 } },
- { { 0, 1 }, { 1, 1 }, { 0, 0 }, { 0, -1 } },
- { { -1, 0 }, { 0, 0 }, { 1, 0 }, { 1, -1 } },
- };
- private final static int[][][] L = new int[][][] {
- { { 0, 1 }, { 0, 0 }, { 0, -1 }, { 1, -1 } },
- { { -1, 0 }, { 0, 0 }, { 1, 0 }, { -1, -1 } },
- { { -1, 1 }, { 0, 1 }, { 0, 0 }, { 0, -1 } },
- { { 1, 1 }, { -1, 0 }, { 0, 0 }, { 1, 0 } },
- };
- private final static int[][][] O = new int[][][] {
- { { 0, 0 }, { 1, 0 }, { 0, -1 }, { 1, -1 } }
- };
- private final static int[][][] S = new int[][][] {
- { { 0, 1 }, { 0, 0 }, { 1, 0 }, { 1, -1 } },
- { { 0, 1 }, { 1, 1 }, { -1, 0 }, { 0, 0 } }
- };
- private final static int[][][] Z = new int[][][] {
- { { 0, 1 }, { 0, 0 }, { -1, 0 }, { -1, -1 } },
- { { -1, 0 }, { 0, 0 }, { 0, -1 }, { 1, -1 } }
- };
- private final static int[][][] T = new int[][][] {
- { { -1, 0 }, { 0, 0 }, { 1, 0 }, { 0, -1 } },
- { { 0, 1 }, { -1, 0 }, { 0, 0 }, { 0, -1 } },
- { { 0, 1 }, { -1, 0 }, { 0, 0 }, { 1, 0 } },
- { { 0, 1 }, { 0, 0 }, { 1, 0 }, { 0, -1 } },
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement