Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. package mx.itesm.juegodelavida;
  2.  
  3. import com.badlogic.gdx.Gdx;
  4. import com.badlogic.gdx.graphics.Pixmap;
  5. import com.badlogic.gdx.graphics.Texture;
  6. import com.badlogic.gdx.math.MathUtils;
  7.  
  8. /**
  9. * Created by roberto on 17/04/17.
  10. */
  11.  
  12. class JuegoVida extends Pantalla
  13. {
  14. // Dimensiones del mundo
  15. private final int COLUMNAS = 64;
  16. private final int RENGLONES = 40;
  17. private final int TAMANO_CELDA = 20; // 64x20 = 1280, 40x20 = 800
  18. private final Principal juego;
  19.  
  20. // Matriz para representar el mundo
  21. private Estado[][] mundo;
  22. private Estado[][] temporal; // Calcula siguiente generación
  23.  
  24. // Texturas
  25. private Texture celdaViva;
  26. private Texture celdaMuerta;
  27.  
  28. public JuegoVida(Principal principal) {
  29. this.juego = principal;
  30. }
  31.  
  32. @Override
  33. public void show() {
  34. generarMundo();
  35. celdaViva = crearCelda(Estado.VIVA);
  36. celdaMuerta = crearCelda(Estado.MUERTA);
  37. }
  38.  
  39. private void generarMundo() {
  40. // El mundo
  41. mundo = new Estado[RENGLONES][COLUMNAS];
  42. // Inicializa el mundo de manera aleatoria
  43. for (int i=0; i<mundo.length; i++){
  44. for (int j=0; j<mundo[i].length; j++) {
  45. if (i==0 || j==0 || i==mundo.length-1 || j==mundo[i].length-1) {
  46. mundo[i][j] = Estado.MUERTA; // en el marco no hay vidas
  47. } else {
  48. Estado estado = Estado.MUERTA;
  49. if (MathUtils.random(1f) < 0.1f) { // 10% vivas
  50. estado = Estado.VIVA;
  51. }
  52. mundo[i][j] = estado;
  53. }
  54. }
  55. }
  56. // Mundo temporal para siguiente generación
  57. temporal = new Estado[RENGLONES][COLUMNAS];
  58. }
  59.  
  60. @Override
  61. public void render(float delta) {
  62. borrarPantalla(0,0.2f,0);
  63. // Dibujar
  64. batch.setProjectionMatrix(camara.combined);
  65. batch.begin();
  66. dibujarMundo();
  67. batch.end();
  68.  
  69. calcularSiguienteGeneracion();
  70.  
  71. juego.logger.log();
  72. }
  73.  
  74. private void calcularSiguienteGeneracion() {
  75. // No recorre el marco
  76. for (int i=1; i<mundo.length-1; i++) {
  77. for (int j=1; j < mundo[i].length-1; j++) {
  78. int vecinos = contarVecinos(i, j);
  79. if (mundo[i][j]==Estado.VIVA) {
  80. if (vecinos==2 || vecinos==3) {
  81. temporal[i][j] = Estado.VIVA; // 1. Sobrevive
  82. } else {
  83. temporal[i][j] = Estado.MUERTA; // 2. Fallece
  84. }
  85. } else {
  86. if (vecinos==3) {
  87. temporal[i][j] = Estado.VIVA; // 3. Nace
  88. } else {
  89. temporal[i][j] = Estado.MUERTA;
  90. }
  91. }
  92. }
  93. }
  94. // Copia el mundo temporal en el mundo
  95. long inicio = System.nanoTime();
  96. for (int i=0; i<mundo.length; i+=2) {
  97. for (int j = 0; j < mundo[i].length; j+=2) {
  98. mundo[i][j] = temporal[i][j];
  99. mundo[i+1][j+1] = temporal[i+1][j+1];
  100. mundo[i][j+1] = temporal[i][j+1];
  101. mundo[i+1][j] = temporal[i+1][j];
  102. }
  103. }
  104. long fin = System.nanoTime();
  105. Gdx.app.log("copiando","Tiempo: " + (fin-inicio)/1000);
  106. }
  107.  
  108. private int contarVecinos(int i, int j) {
  109. return esVecino(i-1,j-1) + esVecino(i-1,j) + esVecino(i-1,j+1)
  110. + esVecino(i,j-1) + esVecino(i,j+1)
  111. + esVecino(i+1,j-1) + esVecino(i+1,j) + esVecino(i+1,j+1);
  112. }
  113.  
  114. private int esVecino(int i, int j) {
  115. //if (i>=0 && i<mundo.length-1) {
  116. //if (j>=0 && j<mundo[i].length-1) {
  117. if (mundo[i][j]==Estado.VIVA) {
  118. return 1;
  119. }
  120. //}
  121. //}
  122. return 0;
  123. }
  124.  
  125. private void dibujarMundo() {
  126. // Dibuja el estado actual del mundo
  127. long inicio = System.nanoTime();
  128. int x, y=0;
  129. for (int i=0; i<mundo.length; i++){
  130. x = 0;
  131. for (int j=0; j<mundo[i].length; j++) {
  132. if (mundo[i][j] == Estado.VIVA) {
  133. batch.draw(celdaViva, x, y);
  134. } else {
  135. batch.draw(celdaMuerta, x, y);
  136. }
  137. x += TAMANO_CELDA;
  138. }
  139. y += TAMANO_CELDA;
  140. }
  141. long fin = System.nanoTime();
  142. Gdx.app.log("dibujandoMundo","Tiempo: " + (fin-inicio)/1000);
  143. }
  144.  
  145. private Texture crearCelda(Estado estado) {
  146.  
  147. Pixmap pixmap = new Pixmap(TAMANO_CELDA, TAMANO_CELDA, Pixmap.Format.RGBA8888);
  148. if (estado==Estado.VIVA) {
  149. pixmap.setColor(1,1,1,1);
  150. } else {
  151. pixmap.setColor(0,0,0,1);
  152. }
  153. pixmap.fillRectangle(0, 0, pixmap.getWidth(), pixmap.getHeight());
  154. Texture textura = new Texture(pixmap);
  155. pixmap.dispose();
  156. return textura;
  157. }
  158.  
  159. @Override
  160. public void pause() {
  161.  
  162. }
  163.  
  164. @Override
  165. public void resume() {
  166.  
  167. }
  168.  
  169. @Override
  170. public void dispose() {
  171.  
  172. }
  173.  
  174. public enum Estado {
  175. VIVA,
  176. MUERTA
  177. }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement