Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.my1010.game;
- /*esta clase se encargara de la parte grafica de todas las piezas, es decir, se encargara de pintar todas las piezas por pantalla */
- import java.util.Random;
- import com.badlogic.gdx.audio.Sound;
- import com.badlogic.gdx.graphics.g2d.*;
- import com.badlogic.gdx.graphics.*;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.math.Vector3;
- public class Piece {
- public Texture img;
- public static final int CONSTANT_OUTLINE = 50;
- private float x;
- private float y;
- private String[] color;
- boolean touchPiece;
- // private Sound soundTouch;
- // private Sound soundRelease;
- private Sprite image;
- private OrthographicCamera camera;
- private Vector3 touchPosition, blockWidthHeight;
- public Piece(float x , float y, OrthographicCamera camera){
- //genero un color al azar para cada pieza
- Random randomNumber = new Random();
- int randomColor = (int)(randomNumber.nextDouble() *8);
- this.color = new String[8];
- color[0] = "green.png";
- color[1] = "blue.png";
- color[2] = "blue2.png";
- color[3] = "green2.png";
- color[4] = "yellow.png";
- color[5] = "orange.png";
- color[6] = "purple.png";
- color[7] = "red.png";
- this.x = x;
- this.y = y;
- // soundTouch = Gdx.audio.newSound(Gdx.files.internal("touch.mp3"));
- //soundRelease = Gdx.audio.newSound(Gdx.files.internal("release.mp3"));
- img = new Texture(color[randomColor]);
- image = new Sprite(img);
- image.setOrigin(0,0);
- image.setSize(My1010Game.HEIGHT / 20, My1010Game.HEIGHT / 20);
- //image.setPosition(-image.getWidth()/2,-image.getHeight()/2);
- blockWidthHeight = camera.unproject(new Vector3(image.getWidth(), image.getHeight(), 0));
- this.camera = camera;
- }
- public boolean outlinePiece(float sizePiece) {
- /*metodo que me definira el contorno o las limitaciones de la pieza para que esta sea transladada*/
- float dx, dy;
- dx = Gdx.input.getX();
- dy = Gdx.input.getY();
- touchPosition = camera.unproject(new Vector3(dx, dy, 0));
- if ((touchPosition.x <= (x + sizePiece + this.CONSTANT_OUTLINE)) && (touchPosition.x >= x - this.CONSTANT_OUTLINE) && (touchPosition.y <= y + sizePiece + this.CONSTANT_OUTLINE) && (touchPosition.y >= y - this.CONSTANT_OUTLINE)){
- //soundTouch.play(0.5f);
- return (true);
- }
- else return (false);
- }
- public void positionXY(){
- float moveX = Gdx.input.getX() - blockWidthHeight.x/2; //resto la dimension del bloque
- float moveY = Gdx.input.getY() + blockWidthHeight.y - My1010Game.HEIGHT_REST*1.2f ;
- touchPosition = camera.unproject(new Vector3(moveX, moveY, 0));
- }
- public void printLeftL(SpriteBatch batch , int SIZE) {
- final float SIZE_PIECE = blockWidthHeight.x / 1.5f;
- float row = 0;
- int column = 0;
- int boxGrapherCounter = 0;
- int separationX = 15;
- boolean verifyWay = false;
- //variables que me sirven para dibujar la pieza;
- if (Gdx.input.justTouched()) {
- if (outlinePiece(SIZE_PIECE)) touchPiece = true;
- else touchPiece = false;
- }
- // solo un ejemplo de como se generaria una pieza;
- if ((Gdx.input.isTouched()) && (touchPiece)) {
- while (boxGrapherCounter < SIZE) {
- positionXY(); //este metodo es quien determina donde se pinta a partir de donde tocas
- //positionBoxToAdd(touchPosition);
- if (!verifyWay) {
- image.setPosition(touchPosition.x + image.getWidth(), touchPosition.y + column);
- image.draw(batch);
- column += image.getHeight();
- } else {
- image.setPosition(touchPosition.x + row, touchPosition.y + column - image.getHeight());
- image.draw(batch);
- row -= image.getWidth();
- }
- if (boxGrapherCounter == SIZE / 2) verifyWay = true;
- boxGrapherCounter++;
- }
- }
- else {
- while (boxGrapherCounter < SIZE) {
- if (verifyWay) {
- batch.draw(image, x + row, y + column, SIZE_PIECE, SIZE_PIECE);
- column -= image.getHeight() - separationX;//error de pintado, cuando se esta estatico, se debe corregir
- }
- else {
- batch.draw(image, x + row, y + column, SIZE_PIECE, SIZE_PIECE);
- row += image.getWidth() - separationX;
- }
- if (boxGrapherCounter == (SIZE / 2) - 1) verifyWay = true;//el menos uno es debido a que boxgrapher empieza en 0, y es esto lo que hace necesario el menos 1
- boxGrapherCounter++;
- }
- }
- /*los ciclos hechos en este metodo fueron hechos separados por comodidad de verlos mejor, pero si se hace un solo while funciona de la misma manera*/
- }
- public void printL(SpriteBatch batch , int SIZE){
- final float SIZE_PIECE = blockWidthHeight.x / 1.5f;
- float row = 0;
- int column = 0;
- int boxGrapherCounter = 0;
- int separationX = 15;
- boolean verifyWay = false;
- // solo un ejemplo de como se generaria una pieza;
- if (Gdx.input.justTouched()) {
- if (outlinePiece(SIZE_PIECE)) touchPiece = true;
- else touchPiece = false;
- }
- if ((Gdx.input.isTouched()) && (touchPiece)) {
- while (boxGrapherCounter < SIZE) {
- positionXY(); //este metodo es quien determina donde se pinta a partir de donde tocas
- //positionBoxToAdd(touchPosition);
- if (!verifyWay) {
- image.setPosition(touchPosition.x, touchPosition.y + column);
- image.draw(batch);
- column += image.getHeight();
- } else {
- row += image.getWidth();
- image.setPosition(touchPosition.x + row, touchPosition.y + column - image.getHeight());
- image.draw(batch);
- }
- if (boxGrapherCounter == (SIZE / 2)) verifyWay = true;
- boxGrapherCounter++;
- }
- }
- else {
- while (boxGrapherCounter < SIZE) {
- if (!verifyWay) {
- batch.draw(image, x, y + column, SIZE_PIECE, SIZE_PIECE);
- column += image.getHeight() - separationX;//error de pintado, cuando se esta estatico, se debe corregir
- } else {
- batch.draw(image, x + row, y + column, SIZE_PIECE, SIZE_PIECE);
- row += image.getWidth() - separationX;
- }
- if (boxGrapherCounter == (SIZE / 2) - 1) verifyWay = true;//el menos uno es debido a que boxgrapher empieza en 0, y es esto lo que hace necesario el menos 1
- boxGrapherCounter++;
- }
- }
- }
- public void printInvestedL(SpriteBatch batch , int SIZE){
- //uso el mismo nombre de las variables internas para mantener nombres constantes en mi codigo
- final float SIZE_PIECE = blockWidthHeight.x / 1.5f;
- float row = 0;
- int column = 0;
- int boxGrapherCounter = 0;
- int separationX = 15;
- boolean verifyWay = false;
- // solo un ejemplo de como se generaria una pieza;
- if (Gdx.input.justTouched()) {
- if (outlinePiece(SIZE_PIECE)) touchPiece = true;
- else touchPiece = false;
- }
- if ((Gdx.input.isTouched()) && (touchPiece)) {
- while (boxGrapherCounter < SIZE) {
- positionXY(); //este metodo es quien determina donde se pinta a partir de donde tocas
- //positionBoxToAdd(touchPosition);
- if (!verifyWay) {
- image.setPosition(touchPosition.x , touchPosition.y + column);
- image.draw(batch);
- column -= image.getHeight();
- } else {
- image.setPosition(touchPosition.x + row, touchPosition.y + column);
- image.draw(batch);
- row += image.getWidth();
- }
- if (boxGrapherCounter == (SIZE / 2) - 1) verifyWay = true;
- boxGrapherCounter++;
- }
- }
- else {
- while (boxGrapherCounter < SIZE) {
- if (!verifyWay) {
- batch.draw(image, x, y + column, SIZE_PIECE, SIZE_PIECE);
- column -= image.getHeight() - separationX;//error de pintado, cuando se esta estatico, se debe corregir
- } else {
- batch.draw(image, x + row, y + column , SIZE_PIECE, SIZE_PIECE);
- row += image.getWidth() - separationX;
- }
- if (boxGrapherCounter == (SIZE / 2) - 1) verifyWay = true;//el menos uno es debido a que boxgrapher empieza en 0, y es esto lo que hace necesario el menos 1
- boxGrapherCounter++;
- }
- }
- }
- public void printInvestedLeftL(SpriteBatch batch , int SIZE){
- //uso el mismo nombre de las variables internas para mantener nombres constantes en mi codigo
- final float SIZE_PIECE = blockWidthHeight.x / 1.5f;
- float row = 0;
- int column = 0;
- int boxGrapherCounter = 0;
- int separationX = 15;
- boolean verifyWay = false;
- // solo un ejemplo de como se generaria una pieza;
- if (Gdx.input.justTouched()) {
- if (outlinePiece(SIZE_PIECE)) touchPiece = true;
- else touchPiece = false;
- }
- if ((Gdx.input.isTouched()) && (touchPiece)) {
- while (boxGrapherCounter < SIZE) {
- positionXY(); //este metodo es quien determina donde se pinta a partir de donde tocas
- //positionBoxToAdd(touchPosition);
- if (verifyWay) {
- image.setPosition(touchPosition.x + row , touchPosition.y + column);
- image.draw(batch);
- column += image.getHeight();
- } else {
- image.setPosition(touchPosition.x + row, touchPosition.y );
- image.draw(batch);
- row += image.getWidth();
- }
- if (boxGrapherCounter == (SIZE / 2) - 1) verifyWay = true;
- boxGrapherCounter++;
- }
- }
- else {
- while (boxGrapherCounter < SIZE) {
- if (verifyWay) {
- batch.draw(image, x + row, y + column, SIZE_PIECE, SIZE_PIECE);
- column += image.getHeight() - separationX;//error de pintado, cuando se esta estatico, se debe corregir
- } else {
- batch.draw(image, x + row, y , SIZE_PIECE, SIZE_PIECE);
- row += image.getWidth() - separationX;
- }
- if (boxGrapherCounter == (SIZE / 2) - 1) verifyWay = true;//el menos uno es debido a que boxgrapher empieza en 0, y es esto lo que hace necesario el menos 1
- boxGrapherCounter++;
- }
- }
- }
- public void printSquare(SpriteBatch batch, int SIZE){
- final float SIZE_PIECE = blockWidthHeight.x / 1.5f;
- float row = 0;
- int column = 0;
- int squarePivot = 0;
- int boxGrapherCounter = 0;
- int separationX = 15;
- if (Gdx.input.justTouched()) {
- if (outlinePiece(SIZE_PIECE)) touchPiece = true;
- else touchPiece = false;
- }
- if ((Gdx.input.isTouched()) && (touchPiece)) {
- while (boxGrapherCounter < SIZE*SIZE){
- positionXY();
- if (squarePivot < SIZE){
- image.setPosition(touchPosition.x + row , touchPosition.y + column );
- image.draw(batch);
- row += image.getWidth();
- }
- else{
- column += image.getHeight();
- row = 0;
- image.setPosition(touchPosition.x + row , touchPosition.y + column );
- image.draw(batch);
- row += image.getWidth();
- squarePivot = 0;
- }
- squarePivot++;
- boxGrapherCounter++;
- }
- }
- else {
- while(boxGrapherCounter < SIZE*SIZE){
- if (squarePivot < SIZE){
- batch.draw(image, x + row, y + column, SIZE_PIECE, SIZE_PIECE);
- row += image.getWidth() - separationX;
- }
- else {
- column += image.getHeight() - separationX;
- row = 0;
- batch.draw(image, x + row, y + column, SIZE_PIECE, SIZE_PIECE);
- row += image.getWidth() - separationX;
- squarePivot = 0;
- }
- squarePivot++;
- boxGrapherCounter++;
- }
- }
- }
- public void printLine(SpriteBatch batch, int SIZE, int answer){
- final float SIZE_PIECE = blockWidthHeight.x / 1.5f;
- int boxGrapherCounter = 0;
- int box = 0;
- int separationX = 15;
- if (Gdx.input.justTouched()) {
- if (outlinePiece(SIZE_PIECE)) touchPiece = true;
- else touchPiece = false;
- }
- if ((Gdx.input.isTouched()) && (touchPiece)) {
- while (boxGrapherCounter < SIZE){
- positionXY();//metodo de importancia
- if (answer == 0){
- image.setPosition(touchPosition.x + box , touchPosition.y);
- image.draw(batch);
- box += image.getWidth();
- }
- else {
- image.setPosition(touchPosition.x , touchPosition.y + box);
- image.draw(batch);
- box -= image.getHeight();
- }
- boxGrapherCounter++;
- }
- }
- else {
- while(boxGrapherCounter < SIZE){
- if (answer == 0){
- batch.draw(image, x + box, y, SIZE_PIECE, SIZE_PIECE);
- box += image.getWidth() - separationX;
- }
- else {
- batch.draw(image, x, y + box, SIZE_PIECE, SIZE_PIECE);
- box -= image.getHeight() - separationX;
- }
- boxGrapherCounter++;
- }
- }
- }
- }
- /*public void printL(SpriteBatch batch) {
- final float SIZE_PIECE = blockWidthHeight.x / 1.5f;
- if (Gdx.input.justTouched()) {
- if (outlinePiece(SIZE_PIECE)) touchPiece = true;
- else touchPiece = false;
- }
- //variables que me sirven para dibujar la pieza;
- final int SIZE = 3;//este es un valor de prueba, el size sera el size piece ya programado en la logica.
- float row = 0;
- int column = 0;
- int cont = 0;
- int separationX = 10;
- // solo un ejemplo de como se generaria una pieza;
- while (cont < SIZE){
- if ((Gdx.input.isTouched()) && (touchPiece)){
- positionXY(); //este metodo es quien determina donde se pinta a partir de donde tocas
- //positionBoxToAdd(touchPosition);
- if(cont == 0){
- image.setPosition(touchPosition.x + image.getWidth(), touchPosition.y);
- image.draw(batch);
- column += image.getHeight();
- }
- else{
- image.setPosition(touchPosition.x + row, touchPosition.y + column);
- image.draw(batch);
- row += image.getWidth();
- }
- cont ++;
- }
- else{
- if(cont == 0){
- batch.draw(image, x + image.getWidth()- separationX , y, SIZE_PIECE, SIZE_PIECE);
- column += image.getHeight() - separationX;
- }
- else{
- batch.draw(image, x + row , y + column, SIZE_PIECE, SIZE_PIECE);
- row += image.getWidth() - separationX;
- }
- cont ++;
- }
- }
- }*/
- /*este codigo me dibuja una l invertida
- *
- * public void ShowPiece(SpriteBatch batch) {
- if (Gdx.input.justTouched()) {
- if (outlinePiece()) touchPiece = true;
- else touchPiece = false;
- }
- final int SIZE = 3;//este es un valor de prueba, el size sera el size piece ya programado en la logica.
- float row = 0;
- float column = 50;
- int cont = 0;
- while (cont <= SIZE){
- if ((Gdx.input.isTouched()) && (touchPiece)){
- if (cont < SIZE) batch.draw(image, Gdx.input.getX() + row , Gdx.graphics.getHeight() - Gdx.input.getY(), sizePiece + 5, sizePiece + 5);
- else batch.draw(image, Gdx.input.getX(), Gdx.graphics.getHeight() - Gdx.input.getY() - column, sizePiece + 5, sizePiece + 5);
- row += 50;
- cont++;
- }
- else{
- column = 45;
- if (cont < SIZE )batch.draw(image, x + row , y, sizePiece, sizePiece);
- else batch.draw(image, x , y - column, sizePiece, sizePiece);
- row += 45;
- cont++;
- }
- }
- }
- *
- *
- *este codigo me dibuja una linea horizonbtal
- *
- * if (Gdx.input.justTouched()) {
- if (outlinePiece()) touchPiece = true;
- else touchPiece = false;
- }
- final int SIZE = 5;//este es un valor de prueba, el size sera el size piece ya programado en la logica.
- float row = 0;
- int cont = 0;
- while (cont < SIZE){
- if ((Gdx.input.isTouched()) && (touchPiece)){
- batch.draw(image, Gdx.input.getX() + row , Gdx.graphics.getHeight() - Gdx.input.getY(), sizePiece + 5, sizePiece + 5);
- row += 50;
- cont++;
- }
- else{
- batch.draw(image, x + row , y, sizePiece, sizePiece);
- row += 45;
- cont++;
- }
- }
- *
- *
- *este algoritmo me dibuja una pieza vertical
- *
- *
- * if (Gdx.input.justTouched()) {
- if (outlinePiece()) touchPiece = true;
- else touchPiece = false;
- }
- final int SIZE = 5;//este es un valor de prueba, el size sera el size piece ya programado en la logica.
- float row = 0;
- int cont = 0;
- while (cont < SIZE){
- if ((Gdx.input.isTouched()) && (touchPiece)){
- batch.draw(image, Gdx.input.getX() , Gdx.graphics.getHeight() - Gdx.input.getY() - row, sizePiece + 5, sizePiece + 5);
- row += 50;
- cont++;
- }
- else{
- batch.draw(image, x , y - row, sizePiece, sizePiece);
- row += 45;
- cont++;
- }
- }
- *
- * */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement