Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.85 KB | None | 0 0
  1. package com.my1010.game;
  2. /*esta clase se encargara de la parte grafica de todas las piezas, es decir, se encargara de pintar todas las piezas por pantalla */
  3.  
  4. import java.util.Random;
  5. import com.badlogic.gdx.audio.Sound;
  6. import com.badlogic.gdx.graphics.g2d.*;
  7. import com.badlogic.gdx.graphics.*;
  8. import com.badlogic.gdx.Gdx;
  9. import com.badlogic.gdx.math.Vector3;
  10.  
  11. public class Piece {
  12. public Texture img;
  13. public static final int CONSTANT_OUTLINE = 50;
  14. private float x;
  15. private float y;
  16. private String[] color;
  17. boolean touchPiece;
  18. // private Sound soundTouch;
  19. // private Sound soundRelease;
  20. private Sprite image;
  21. private OrthographicCamera camera;
  22. private Vector3 touchPosition, blockWidthHeight;
  23.  
  24.  
  25. public Piece(float x , float y, OrthographicCamera camera){
  26. //genero un color al azar para cada pieza
  27. Random randomNumber = new Random();
  28. int randomColor = (int)(randomNumber.nextDouble() *8);
  29. this.color = new String[8];
  30.  
  31. color[0] = "green.png";
  32. color[1] = "blue.png";
  33. color[2] = "blue2.png";
  34. color[3] = "green2.png";
  35. color[4] = "yellow.png";
  36. color[5] = "orange.png";
  37. color[6] = "purple.png";
  38. color[7] = "red.png";
  39. this.x = x;
  40. this.y = y;
  41. // soundTouch = Gdx.audio.newSound(Gdx.files.internal("touch.mp3"));
  42. //soundRelease = Gdx.audio.newSound(Gdx.files.internal("release.mp3"));
  43.  
  44. img = new Texture(color[randomColor]);
  45. image = new Sprite(img);
  46. image.setOrigin(0,0);
  47. image.setSize(My1010Game.HEIGHT / 20, My1010Game.HEIGHT / 20);
  48. //image.setPosition(-image.getWidth()/2,-image.getHeight()/2);
  49. blockWidthHeight = camera.unproject(new Vector3(image.getWidth(), image.getHeight(), 0));
  50.  
  51. this.camera = camera;
  52.  
  53. }
  54.  
  55.  
  56. public boolean outlinePiece(float sizePiece) {
  57. /*metodo que me definira el contorno o las limitaciones de la pieza para que esta sea transladada*/
  58. float dx, dy;
  59. dx = Gdx.input.getX();
  60. dy = Gdx.input.getY();
  61. touchPosition = camera.unproject(new Vector3(dx, dy, 0));
  62. 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)){
  63. //soundTouch.play(0.5f);
  64. return (true);
  65. }
  66. else return (false);
  67.  
  68. }
  69.  
  70. public void positionXY(){
  71. float moveX = Gdx.input.getX() - blockWidthHeight.x/2; //resto la dimension del bloque
  72. float moveY = Gdx.input.getY() + blockWidthHeight.y - My1010Game.HEIGHT_REST*1.2f ;
  73. touchPosition = camera.unproject(new Vector3(moveX, moveY, 0));
  74.  
  75. }
  76.  
  77. public void printLeftL(SpriteBatch batch , int SIZE) {
  78. final float SIZE_PIECE = blockWidthHeight.x / 1.5f;
  79. float row = 0;
  80. int column = 0;
  81. int boxGrapherCounter = 0;
  82. int separationX = 15;
  83. boolean verifyWay = false;
  84. //variables que me sirven para dibujar la pieza;
  85.  
  86.  
  87. if (Gdx.input.justTouched()) {
  88. if (outlinePiece(SIZE_PIECE)) touchPiece = true;
  89. else touchPiece = false;
  90. }
  91.  
  92. // solo un ejemplo de como se generaria una pieza;
  93. if ((Gdx.input.isTouched()) && (touchPiece)) {
  94. while (boxGrapherCounter < SIZE) {
  95. positionXY(); //este metodo es quien determina donde se pinta a partir de donde tocas
  96. //positionBoxToAdd(touchPosition);
  97.  
  98. if (!verifyWay) {
  99. image.setPosition(touchPosition.x + image.getWidth(), touchPosition.y + column);
  100. image.draw(batch);
  101. column += image.getHeight();
  102. } else {
  103. image.setPosition(touchPosition.x + row, touchPosition.y + column - image.getHeight());
  104. image.draw(batch);
  105. row -= image.getWidth();
  106. }
  107.  
  108. if (boxGrapherCounter == SIZE / 2) verifyWay = true;
  109. boxGrapherCounter++;
  110. }
  111. }
  112. else {
  113. while (boxGrapherCounter < SIZE) {
  114. if (verifyWay) {
  115. batch.draw(image, x + row, y + column, SIZE_PIECE, SIZE_PIECE);
  116. column -= image.getHeight() - separationX;//error de pintado, cuando se esta estatico, se debe corregir
  117. }
  118. else {
  119. batch.draw(image, x + row, y + column, SIZE_PIECE, SIZE_PIECE);
  120. row += image.getWidth() - separationX;
  121. }
  122.  
  123. 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
  124. boxGrapherCounter++;
  125.  
  126. }
  127. }
  128. /*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*/
  129. }
  130.  
  131. public void printL(SpriteBatch batch , int SIZE){
  132. final float SIZE_PIECE = blockWidthHeight.x / 1.5f;
  133. float row = 0;
  134. int column = 0;
  135. int boxGrapherCounter = 0;
  136. int separationX = 15;
  137. boolean verifyWay = false;
  138.  
  139. // solo un ejemplo de como se generaria una pieza;
  140.  
  141. if (Gdx.input.justTouched()) {
  142. if (outlinePiece(SIZE_PIECE)) touchPiece = true;
  143. else touchPiece = false;
  144. }
  145.  
  146. if ((Gdx.input.isTouched()) && (touchPiece)) {
  147. while (boxGrapherCounter < SIZE) {
  148. positionXY(); //este metodo es quien determina donde se pinta a partir de donde tocas
  149. //positionBoxToAdd(touchPosition);
  150.  
  151. if (!verifyWay) {
  152. image.setPosition(touchPosition.x, touchPosition.y + column);
  153. image.draw(batch);
  154. column += image.getHeight();
  155. } else {
  156. row += image.getWidth();
  157. image.setPosition(touchPosition.x + row, touchPosition.y + column - image.getHeight());
  158. image.draw(batch);
  159.  
  160. }
  161.  
  162. if (boxGrapherCounter == (SIZE / 2)) verifyWay = true;
  163. boxGrapherCounter++;
  164. }
  165. }
  166. else {
  167.  
  168. while (boxGrapherCounter < SIZE) {
  169. if (!verifyWay) {
  170. batch.draw(image, x, y + column, SIZE_PIECE, SIZE_PIECE);
  171. column += image.getHeight() - separationX;//error de pintado, cuando se esta estatico, se debe corregir
  172. } else {
  173. batch.draw(image, x + row, y + column, SIZE_PIECE, SIZE_PIECE);
  174. row += image.getWidth() - separationX;
  175. }
  176. 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
  177. boxGrapherCounter++;
  178. }
  179. }
  180. }
  181.  
  182. public void printInvestedL(SpriteBatch batch , int SIZE){
  183. //uso el mismo nombre de las variables internas para mantener nombres constantes en mi codigo
  184. final float SIZE_PIECE = blockWidthHeight.x / 1.5f;
  185. float row = 0;
  186. int column = 0;
  187. int boxGrapherCounter = 0;
  188. int separationX = 15;
  189. boolean verifyWay = false;
  190.  
  191. // solo un ejemplo de como se generaria una pieza;
  192.  
  193. if (Gdx.input.justTouched()) {
  194. if (outlinePiece(SIZE_PIECE)) touchPiece = true;
  195. else touchPiece = false;
  196. }
  197.  
  198.  
  199. if ((Gdx.input.isTouched()) && (touchPiece)) {
  200. while (boxGrapherCounter < SIZE) {
  201. positionXY(); //este metodo es quien determina donde se pinta a partir de donde tocas
  202. //positionBoxToAdd(touchPosition);
  203.  
  204. if (!verifyWay) {
  205. image.setPosition(touchPosition.x , touchPosition.y + column);
  206. image.draw(batch);
  207. column -= image.getHeight();
  208. } else {
  209. image.setPosition(touchPosition.x + row, touchPosition.y + column);
  210. image.draw(batch);
  211. row += image.getWidth();
  212. }
  213.  
  214. if (boxGrapherCounter == (SIZE / 2) - 1) verifyWay = true;
  215. boxGrapherCounter++;
  216. }
  217. }
  218. else {
  219.  
  220. while (boxGrapherCounter < SIZE) {
  221. if (!verifyWay) {
  222. batch.draw(image, x, y + column, SIZE_PIECE, SIZE_PIECE);
  223. column -= image.getHeight() - separationX;//error de pintado, cuando se esta estatico, se debe corregir
  224. } else {
  225. batch.draw(image, x + row, y + column , SIZE_PIECE, SIZE_PIECE);
  226. row += image.getWidth() - separationX;
  227. }
  228. 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
  229. boxGrapherCounter++;
  230. }
  231. }
  232. }
  233.  
  234. public void printInvestedLeftL(SpriteBatch batch , int SIZE){
  235. //uso el mismo nombre de las variables internas para mantener nombres constantes en mi codigo
  236. final float SIZE_PIECE = blockWidthHeight.x / 1.5f;
  237. float row = 0;
  238. int column = 0;
  239. int boxGrapherCounter = 0;
  240. int separationX = 15;
  241. boolean verifyWay = false;
  242.  
  243. // solo un ejemplo de como se generaria una pieza;
  244.  
  245. if (Gdx.input.justTouched()) {
  246. if (outlinePiece(SIZE_PIECE)) touchPiece = true;
  247. else touchPiece = false;
  248. }
  249.  
  250.  
  251. if ((Gdx.input.isTouched()) && (touchPiece)) {
  252. while (boxGrapherCounter < SIZE) {
  253. positionXY(); //este metodo es quien determina donde se pinta a partir de donde tocas
  254. //positionBoxToAdd(touchPosition);
  255.  
  256. if (verifyWay) {
  257. image.setPosition(touchPosition.x + row , touchPosition.y + column);
  258. image.draw(batch);
  259. column += image.getHeight();
  260. } else {
  261. image.setPosition(touchPosition.x + row, touchPosition.y );
  262. image.draw(batch);
  263. row += image.getWidth();
  264. }
  265.  
  266. if (boxGrapherCounter == (SIZE / 2) - 1) verifyWay = true;
  267. boxGrapherCounter++;
  268. }
  269. }
  270. else {
  271.  
  272. while (boxGrapherCounter < SIZE) {
  273. if (verifyWay) {
  274. batch.draw(image, x + row, y + column, SIZE_PIECE, SIZE_PIECE);
  275. column += image.getHeight() - separationX;//error de pintado, cuando se esta estatico, se debe corregir
  276. } else {
  277. batch.draw(image, x + row, y , SIZE_PIECE, SIZE_PIECE);
  278. row += image.getWidth() - separationX;
  279. }
  280. 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
  281. boxGrapherCounter++;
  282. }
  283. }
  284. }
  285.  
  286. public void printSquare(SpriteBatch batch, int SIZE){
  287. final float SIZE_PIECE = blockWidthHeight.x / 1.5f;
  288. float row = 0;
  289. int column = 0;
  290. int squarePivot = 0;
  291. int boxGrapherCounter = 0;
  292. int separationX = 15;
  293.  
  294. if (Gdx.input.justTouched()) {
  295. if (outlinePiece(SIZE_PIECE)) touchPiece = true;
  296. else touchPiece = false;
  297. }
  298.  
  299. if ((Gdx.input.isTouched()) && (touchPiece)) {
  300. while (boxGrapherCounter < SIZE*SIZE){
  301. positionXY();
  302. if (squarePivot < SIZE){
  303. image.setPosition(touchPosition.x + row , touchPosition.y + column );
  304. image.draw(batch);
  305. row += image.getWidth();
  306.  
  307. }
  308. else{
  309. column += image.getHeight();
  310. row = 0;
  311. image.setPosition(touchPosition.x + row , touchPosition.y + column );
  312. image.draw(batch);
  313. row += image.getWidth();
  314. squarePivot = 0;
  315. }
  316. squarePivot++;
  317. boxGrapherCounter++;
  318. }
  319. }
  320. else {
  321. while(boxGrapherCounter < SIZE*SIZE){
  322. if (squarePivot < SIZE){
  323. batch.draw(image, x + row, y + column, SIZE_PIECE, SIZE_PIECE);
  324. row += image.getWidth() - separationX;
  325. }
  326. else {
  327. column += image.getHeight() - separationX;
  328. row = 0;
  329. batch.draw(image, x + row, y + column, SIZE_PIECE, SIZE_PIECE);
  330. row += image.getWidth() - separationX;
  331. squarePivot = 0;
  332. }
  333. squarePivot++;
  334. boxGrapherCounter++;
  335. }
  336.  
  337. }
  338.  
  339. }
  340.  
  341. public void printLine(SpriteBatch batch, int SIZE, int answer){
  342. final float SIZE_PIECE = blockWidthHeight.x / 1.5f;
  343. int boxGrapherCounter = 0;
  344. int box = 0;
  345. int separationX = 15;
  346.  
  347.  
  348. if (Gdx.input.justTouched()) {
  349. if (outlinePiece(SIZE_PIECE)) touchPiece = true;
  350. else touchPiece = false;
  351. }
  352.  
  353. if ((Gdx.input.isTouched()) && (touchPiece)) {
  354. while (boxGrapherCounter < SIZE){
  355. positionXY();//metodo de importancia
  356. if (answer == 0){
  357. image.setPosition(touchPosition.x + box , touchPosition.y);
  358. image.draw(batch);
  359. box += image.getWidth();
  360. }
  361. else {
  362. image.setPosition(touchPosition.x , touchPosition.y + box);
  363. image.draw(batch);
  364. box -= image.getHeight();
  365. }
  366. boxGrapherCounter++;
  367. }
  368. }
  369. else {
  370. while(boxGrapherCounter < SIZE){
  371. if (answer == 0){
  372. batch.draw(image, x + box, y, SIZE_PIECE, SIZE_PIECE);
  373. box += image.getWidth() - separationX;
  374. }
  375. else {
  376. batch.draw(image, x, y + box, SIZE_PIECE, SIZE_PIECE);
  377. box -= image.getHeight() - separationX;
  378. }
  379. boxGrapherCounter++;
  380. }
  381.  
  382. }
  383.  
  384.  
  385. }
  386.  
  387. }
  388.  
  389. /*public void printL(SpriteBatch batch) {
  390. final float SIZE_PIECE = blockWidthHeight.x / 1.5f;
  391. if (Gdx.input.justTouched()) {
  392. if (outlinePiece(SIZE_PIECE)) touchPiece = true;
  393. else touchPiece = false;
  394. }
  395.  
  396. //variables que me sirven para dibujar la pieza;
  397. final int SIZE = 3;//este es un valor de prueba, el size sera el size piece ya programado en la logica.
  398. float row = 0;
  399. int column = 0;
  400. int cont = 0;
  401. int separationX = 10;
  402. // solo un ejemplo de como se generaria una pieza;
  403. while (cont < SIZE){
  404. if ((Gdx.input.isTouched()) && (touchPiece)){
  405. positionXY(); //este metodo es quien determina donde se pinta a partir de donde tocas
  406. //positionBoxToAdd(touchPosition);
  407. if(cont == 0){
  408. image.setPosition(touchPosition.x + image.getWidth(), touchPosition.y);
  409. image.draw(batch);
  410. column += image.getHeight();
  411. }
  412. else{
  413. image.setPosition(touchPosition.x + row, touchPosition.y + column);
  414. image.draw(batch);
  415. row += image.getWidth();
  416. }
  417. cont ++;
  418. }
  419. else{
  420. if(cont == 0){
  421. batch.draw(image, x + image.getWidth()- separationX , y, SIZE_PIECE, SIZE_PIECE);
  422. column += image.getHeight() - separationX;
  423. }
  424. else{
  425. batch.draw(image, x + row , y + column, SIZE_PIECE, SIZE_PIECE);
  426. row += image.getWidth() - separationX;
  427. }
  428. cont ++;
  429. }
  430. }
  431. }*/
  432.  
  433. /*este codigo me dibuja una l invertida
  434. *
  435. * public void ShowPiece(SpriteBatch batch) {
  436.  
  437.  
  438. if (Gdx.input.justTouched()) {
  439. if (outlinePiece()) touchPiece = true;
  440. else touchPiece = false;
  441. }
  442.  
  443. final int SIZE = 3;//este es un valor de prueba, el size sera el size piece ya programado en la logica.
  444. float row = 0;
  445. float column = 50;
  446. int cont = 0;
  447.  
  448. while (cont <= SIZE){
  449. if ((Gdx.input.isTouched()) && (touchPiece)){
  450.  
  451. if (cont < SIZE) batch.draw(image, Gdx.input.getX() + row , Gdx.graphics.getHeight() - Gdx.input.getY(), sizePiece + 5, sizePiece + 5);
  452. else batch.draw(image, Gdx.input.getX(), Gdx.graphics.getHeight() - Gdx.input.getY() - column, sizePiece + 5, sizePiece + 5);
  453. row += 50;
  454. cont++;
  455. }
  456. else{
  457. column = 45;
  458. if (cont < SIZE )batch.draw(image, x + row , y, sizePiece, sizePiece);
  459. else batch.draw(image, x , y - column, sizePiece, sizePiece);
  460. row += 45;
  461. cont++;
  462. }
  463. }
  464.  
  465. }
  466.  
  467. *
  468. *
  469. *este codigo me dibuja una linea horizonbtal
  470. *
  471. * if (Gdx.input.justTouched()) {
  472. if (outlinePiece()) touchPiece = true;
  473. else touchPiece = false;
  474. }
  475.  
  476. final int SIZE = 5;//este es un valor de prueba, el size sera el size piece ya programado en la logica.
  477. float row = 0;
  478. int cont = 0;
  479.  
  480. while (cont < SIZE){
  481.  
  482. if ((Gdx.input.isTouched()) && (touchPiece)){
  483.  
  484. batch.draw(image, Gdx.input.getX() + row , Gdx.graphics.getHeight() - Gdx.input.getY(), sizePiece + 5, sizePiece + 5);
  485. row += 50;
  486. cont++;
  487. }
  488. else{
  489.  
  490. batch.draw(image, x + row , y, sizePiece, sizePiece);
  491. row += 45;
  492. cont++;
  493. }
  494. }
  495.  
  496. *
  497. *
  498. *este algoritmo me dibuja una pieza vertical
  499. *
  500. *
  501. * if (Gdx.input.justTouched()) {
  502. if (outlinePiece()) touchPiece = true;
  503. else touchPiece = false;
  504. }
  505.  
  506. final int SIZE = 5;//este es un valor de prueba, el size sera el size piece ya programado en la logica.
  507. float row = 0;
  508. int cont = 0;
  509.  
  510. while (cont < SIZE){
  511.  
  512. if ((Gdx.input.isTouched()) && (touchPiece)){
  513.  
  514. batch.draw(image, Gdx.input.getX() , Gdx.graphics.getHeight() - Gdx.input.getY() - row, sizePiece + 5, sizePiece + 5);
  515. row += 50;
  516. cont++;
  517. }
  518. else{
  519.  
  520. batch.draw(image, x , y - row, sizePiece, sizePiece);
  521. row += 45;
  522. cont++;
  523. }
  524. }
  525.  
  526. *
  527. * */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement