Advertisement
filosofiamanga

java code

Aug 28th, 2015
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.79 KB | None | 0 0
  1. package com.me.mygdxgame;
  2.  
  3. import java.util.Random;
  4.  
  5. import com.badlogic.gdx.Gdx;
  6. import com.badlogic.gdx.graphics.Color;
  7. import com.badlogic.gdx.graphics.OrthographicCamera;
  8. import com.badlogic.gdx.graphics.Texture;
  9. import com.badlogic.gdx.graphics.g2d.BitmapFont;
  10. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  11. import com.badlogic.gdx.math.Vector3;
  12. import com.badlogic.gdx.scenes.scene2d.ui.Image;
  13.  
  14. public class Map {
  15.     public Texture empty;
  16.     public Image i_empty;
  17.     public Cell[][] Mapa;
  18.     int map_size, current;
  19.     float cell_scale;
  20.     Cell.Color rand_cell, Next;
  21.     Next_Shape next_shape;
  22.     Cell[][] proximo;
  23.     Float time_left, reset_max;
  24.     BitmapFont f_time_left;
  25.     Vector3 time_text;
  26.     float scale;
  27.    
  28.     public Map()
  29.     {
  30.         f_time_left = new BitmapFont(Gdx.files.internal("data/Font.fnt"));
  31.         map_size = 10;
  32.         cell_scale = 0.1f;
  33.         current = 0;
  34.         reset_max = 25f;
  35.         time_left = reset_max;
  36.        
  37.        
  38.         empty = new Texture(Gdx.files.internal("data/blank.png"));
  39.         i_empty = new Image(empty);
  40.        
  41.         Mapa = new Cell[map_size][map_size];
  42.         for (int y = 0; y < map_size; y++)
  43.         {
  44.             for(int x = 0; x < map_size; x++)
  45.             {
  46.                 Mapa[x][y] = new Cell();
  47.                
  48.                 if (x == 0 || y == 0 || x == map_size-1 || y == map_size-1)
  49.                 {
  50.                     Mapa[x][y].corner = true;
  51.                 }
  52.                 else
  53.                 {
  54.                     Mapa[x][y].color = Cell.Color.white;
  55.                 }
  56.             }
  57.         }
  58.        
  59.         Next = GetColor();
  60.         next_shape = new Next_Shape(Next);
  61.         next_shape.SetNewShape(Next);
  62.        
  63.         proximo = new Cell[3][3];
  64.         proximo = next_shape.GetCellsMap();
  65.     }
  66.    
  67.     public void CellClick(OrthographicCamera camera)
  68.     {
  69.         Vector3 pos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
  70.         camera.unproject(pos);
  71.        
  72.         for (int y = 0; y < map_size; y++)
  73.         {
  74.             for(int x = 0; x < map_size; x++)
  75.             {
  76.                 if (IsClicked(x*cell_scale, (y*cell_scale)+cell_scale, cell_scale, cell_scale, pos))
  77.                 {
  78.                     CallCreatePiece(x, y);
  79.                     time_left = reset_max;
  80.                 }
  81.             }
  82.         }
  83.        
  84.         for (int y = 0; y < 3; y++)
  85.         {
  86.             for (int x = 0; x < 3; x++)
  87.             {
  88.                 Setcolor(proximo[x][y].color, i_empty, x, y);
  89.                 Vector3 pos1 = new Vector3(Gdx.graphics.getWidth(), Gdx.graphics.getHeight() - cell_scale, 0);
  90.                 camera.unproject(pos1);
  91.                 pos1.x -= 3 * cell_scale;
  92.                 pos1.x += x * cell_scale;
  93.                 pos1.y += y * cell_scale;
  94.                
  95.                 if (IsClicked(pos1.x, pos1.y, cell_scale, cell_scale, pos))
  96.                 {
  97.                     next_shape.Rotate();
  98.                 }
  99.             }
  100.         }
  101.     }
  102.    
  103.     public boolean Update()
  104.     {
  105.         if (time_left < 0)
  106.         {
  107.             return false;
  108.         }
  109.        
  110.         time_left -= Gdx.graphics.getDeltaTime();
  111.        
  112.         return true;
  113.     }
  114.    
  115.     public void CallCreatePiece(int x, int y)
  116.     {  
  117.         if (CheckFigureValid(x, y))
  118.         {
  119.             MakeFigure(x, y);
  120.             CreatePiece(x, y);
  121.         }
  122.     }
  123.    
  124.     public void CreatePiece(int x, int y)
  125.     {
  126.         ReclaimPrize(x, y, Next);
  127.         Next = GetColor();
  128.         next_shape.SetNewShape(Next);
  129.         proximo = next_shape.GetCellsMap();
  130.     }
  131.    
  132.     public void ReclaimPrize(int x, int y, Cell.Color color)
  133.     {
  134.         /*Gdx.app.log("blocks",
  135.                 Integer.toString(this.BlockExistCheck(x, y, color)) + " " +
  136.                 Integer.toString(this.BlockExistCheck(x-1, y, color)) + " " +
  137.                 Integer.toString(this.BlockExistCheck(x, y-1, color)) + " " +
  138.                 Integer.toString(this.BlockExistCheck(x+1, y, color)) + " " +
  139.                 Integer.toString(this.BlockExistCheck(x, y+1, color)) + " "
  140.                 );
  141.         */
  142.        
  143.         if (this.BlockExistCheck(x, y, Next)!= 0) { this.RecurssivePrize(x, y, Next, 0); }
  144.         if (this.BlockExistCheck(x-1, y, Next)!= 0) {   this.RecurssivePrize(x-1, y, Next, 0); }
  145.         if (this.BlockExistCheck(x+1, y, Next)!= 0) {   this.RecurssivePrize(x+1, y, Next, 0); }
  146.         if (this.BlockExistCheck(x, y+1, Next)!= 0) {   this.RecurssivePrize(x, y+1, Next, 0); }
  147.         if (this.BlockExistCheck(x, y-1, Next)!= 0) {   this.RecurssivePrize(x, y-1, Next, 0); }
  148.         if (this.BlockExistCheck(x+1, y+1, Next)!= 0) { this.RecurssivePrize(x+1, y+1, Next, 0); }
  149.         if (this.BlockExistCheck(x-1, y-1, Next)!= 0) { this.RecurssivePrize(x-1, y-1, Next, 0); }
  150.         if (this.BlockExistCheck(x+1, y-1, Next)!= 0) { this.RecurssivePrize(x+1, y-1, Next, 0); }
  151.         if (this.BlockExistCheck(x-1, y+1, Next)!= 0) { this.RecurssivePrize(x-1, y+1, Next, 0); }
  152.        
  153.         if (this.BlockExistCheck(x-2, y, Next)!= 0) {   this.RecurssivePrize(x-2, y, Next, 0); }
  154.         if (this.BlockExistCheck(x+2, y, Next)!= 0) {   this.RecurssivePrize(x+2, y, Next, 0); }
  155.         if (this.BlockExistCheck(x, y+2, Next)!= 0) {   this.RecurssivePrize(x, y+2, Next, 0); }
  156.         if (this.BlockExistCheck(x, y-2, Next)!= 0) {   this.RecurssivePrize(x, y-2, Next, 0); }
  157.         if (this.BlockExistCheck(x+2, y+2, Next)!= 0) { this.RecurssivePrize(x+2, y+2, Next, 0); }
  158.         if (this.BlockExistCheck(x-2, y-2, Next)!= 0) { this.RecurssivePrize(x-2, y-2, Next, 0); }
  159.         if (this.BlockExistCheck(x+2, y-2, Next)!= 0) { this.RecurssivePrize(x+2, y-2, Next, 0); }
  160.         if (this.BlockExistCheck(x-2, y+2, Next)!= 0) { this.RecurssivePrize(x-2, y+2, Next, 0); }
  161.        
  162.     }
  163.    
  164.     public void RecurssivePrize(int x, int y, Cell.Color color, int id)
  165.     {  
  166.         id++;
  167.         if (id > 9)
  168.         {
  169.             return;
  170.         }
  171.        
  172.         if (this.BlockExistCheck(x, y, Next) != 0)
  173.         {
  174.             RecurssivePrize(x-1, y, color, id);
  175.             RecurssivePrize(x+1, y, color, id);
  176.             RecurssivePrize(x, y+1, color, id);
  177.             RecurssivePrize(x, y-1, color, id);
  178.            
  179.             RecurssivePrize(x-1, y-1, color, id);
  180.             RecurssivePrize(x+1, y+1, color, id);
  181.             RecurssivePrize(x-1, y+1, color, id);
  182.             RecurssivePrize(x+1, y-1, color, id);
  183.            
  184.             RecurssivePrize(x-2, y, color, id);
  185.             RecurssivePrize(x+2, y, color, id);
  186.             RecurssivePrize(x, y+2, color, id);
  187.             RecurssivePrize(x, y-2, color, id);
  188.            
  189.             RecurssivePrize(x-2, y-2, color, id);
  190.             RecurssivePrize(x+2, y+2, color, id);
  191.             RecurssivePrize(x-2, y+2, color, id);
  192.             RecurssivePrize(x+2, y-2, color, id);
  193.            
  194.         }
  195.        
  196.         if (IsValid(x, y))
  197.         {
  198.             Mapa[x][y].color = Cell.Color.white;
  199.         }
  200.     }
  201.    
  202.    
  203.     public int BlockExistCheck(int x, int y, Cell.Color color)
  204.     {
  205.         int possibles = 0;
  206.         int horizontals = 0;
  207.         int diagonals = 0;
  208.         if (IsValid(x-1, y) && Mapa[x-1][y].color == color) { horizontals++;}
  209.         if (IsValid(x+1, y) && Mapa[x+1][y].color == color) { horizontals++;}
  210.         if (IsValid(x, y-1) && Mapa[x][y-1].color == color) { horizontals++;}
  211.         if (IsValid(x, y+1) && Mapa[x][y+1].color == color) { horizontals++;}
  212.         if (IsValid(x-1, y-1) && Mapa[x-1][y-1].color == color) { diagonals++;}
  213.         if (IsValid(x+1, y+1) && Mapa[x+1][y+1].color == color) { diagonals++;}
  214.         if (IsValid(x-1, y+1) && Mapa[x-1][y+1].color == color) { diagonals++;}
  215.         if (IsValid(x+1, y-1) && Mapa[x+1][y-1].color == color) { diagonals++;}
  216.        
  217.         // 1 = horizontal / 2 = diagonals / 3 = both / 0 = nothing
  218.         if (horizontals == 4) {possibles = 1;}
  219.         if (diagonals == 4) {possibles = 2;}
  220.         if (horizontals == 4 && diagonals == 4) {possibles = 3;}
  221.        
  222.         return possibles;
  223.     }
  224.    
  225.    
  226.     public boolean CheckFigureValid(int x, int y)
  227.     {
  228.         int xpos = 0;
  229.         int ypos = 0;
  230.         for (int v = 0; v < 3; v++)
  231.         {
  232.             for (int h = 0; h < 3; h++)
  233.             {
  234.                 if (h == 0) { xpos = -1; }
  235.                 if (v == 0) { ypos = -1; }
  236.                 if (h == 1) { xpos = 0; }
  237.                 if (v == 1) { ypos = 0; }
  238.                 if (h == 2) { xpos = 1; }
  239.                 if (v == 2) { ypos = 1; }
  240.                 if (!IsValid(x+xpos, y+ypos))
  241.                 {
  242.                     return false;
  243.                 }
  244.                 if (proximo[h][v].color != Cell.Color.white)
  245.                 {
  246.                     if(Mapa[x+xpos][y+ypos].color != Cell.Color.white)
  247.                     {
  248.                         return false;
  249.                     }
  250.                 }
  251.             }
  252.         }
  253.        
  254.         return true;
  255.     }
  256.    
  257.     public void MakeFigure(int x, int y)
  258.     {
  259.         int xpos = 0;
  260.         int ypos = 0;
  261.         for (int v = 0; v < 3; v++)
  262.         {
  263.             for (int h = 0; h < 3; h++)
  264.             {
  265.                 if (h == 0) { xpos = -1; }
  266.                 if (v == 0) { ypos = -1; }
  267.                 if (h == 1) { xpos = 0; }
  268.                 if (v == 1) { ypos = 0; }
  269.                 if (h == 2) { xpos = 1; }
  270.                 if (v == 2) { ypos = 1; }
  271.                 if (!IsValid(x+xpos, y+ypos))
  272.                 {
  273.                     return;
  274.                 }
  275.                 else
  276.                 {
  277.                     if (Mapa[x+xpos][y+ypos].color == Cell.Color.white)
  278.                     {
  279.                         Mapa[x+xpos][y+ypos].color = proximo[h][v].color;
  280.                     }
  281.                 }
  282.             }
  283.         }
  284.     }
  285.    
  286.     public boolean IsValid(int x, int y)
  287.     {
  288.         if (x < 0 || y < 0)
  289.         {
  290.             return false;
  291.         }
  292.         if (x > map_size-1 || y > map_size-1)
  293.         {
  294.             return false;
  295.         }
  296.         return true;
  297.     }
  298.    
  299.    
  300.     public boolean IsClicked(float x, float y, float width, float height, Vector3 pos)
  301.     {
  302.         if (pos.x > x)
  303.         {
  304.             if (pos.x < x + width)
  305.             {
  306.                 if (pos.y > y)
  307.                 {
  308.                     if (pos.y < y + height)
  309.                     {
  310.                         return true;
  311.                     }
  312.                 }
  313.             }
  314.         }
  315.        
  316.         return false;
  317.     }
  318.    
  319.     public void Draw(SpriteBatch sprite, OrthographicCamera camera)
  320.     {
  321.        
  322.         for (int y = 0; y < map_size; y++)
  323.         {
  324.             for(int x = 0; x < map_size; x++)
  325.             {
  326.                 float new_x = x * cell_scale;
  327.                 float new_y = (y * cell_scale) + cell_scale;
  328.                
  329.                 if (Mapa[x][y].corner && Mapa[x][y].color == Cell.Color.white)
  330.                 {
  331.                     i_empty.setColor(Color.GRAY);
  332.                 }
  333.                 else
  334.                 {
  335.                     Setcolor(Mapa[x][y].color, i_empty, x, y);
  336.                 }
  337.                 i_empty.setBounds(new_x, new_y, cell_scale, cell_scale);
  338.                 i_empty.draw(sprite, 1f);
  339.             }
  340.         }
  341.        
  342.         proximo = next_shape.GetCellsMap();
  343.        
  344.         for(int y = 0; y < 3; y++)
  345.         {
  346.             for (int x = 0; x < 3; x++)
  347.             {
  348.                 if (proximo[x][y] != null && proximo[x][y].color != null)
  349.                 {
  350.                     Setcolor(proximo[x][y].color, i_empty, x, y);
  351.                 }
  352.                 Vector3 pos = new Vector3(Gdx.graphics.getWidth(), Gdx.graphics.getHeight() - cell_scale, 0);
  353.                 camera.unproject(pos);
  354.                 pos.x -= 3 * cell_scale;
  355.                 pos.x += x * cell_scale;
  356.                 pos.y += y * cell_scale;
  357.                 i_empty.setBounds(pos.x, pos.y, cell_scale, cell_scale);
  358.                 i_empty.draw(sprite, 1f);
  359.             }
  360.         }
  361.        
  362.         scale = 0.1f;
  363.        
  364.         time_text = new Vector3(0, 0, 0);
  365.         camera.unproject(time_text);
  366.         f_time_left.setColor(Color.BLUE);
  367.         f_time_left.setScale(scale);
  368.         int time = Math.round(time_left);
  369.         this.f_time_left.drawMultiLine(sprite, "T:", time_text.x, time_text.y);
  370.     }
  371.    
  372.    
  373.     public Cell.Color GetColor()
  374.     {
  375.         Random random = new Random();
  376.         int rand_color = random.nextInt(8);
  377.         switch (rand_color)
  378.         {
  379.         case 1: rand_cell = Cell.Color.blue;
  380.         break;
  381.         case 3: rand_cell = Cell.Color.green;
  382.         break;
  383.         case 4: rand_cell = Cell.Color.orange;
  384.         break;
  385.         case 5: rand_cell = Cell.Color.red;
  386.         break;
  387.         case 7: rand_cell = Cell.Color.yellow;
  388.         break;
  389.         }
  390.         return rand_cell;
  391.     }
  392.    
  393.  
  394.     public void Setcolor(Cell.Color color, Image image, int x, int y)
  395.     {
  396.         Cell.Color new_color = color;
  397.         if (new_color == null)
  398.         {
  399.             proximo = next_shape.GetCellsMap();
  400.             new_color = proximo[1][1].color;
  401.         }
  402.         switch(new_color)
  403.         {
  404.         case blue: image.setColor(Color.BLUE);
  405.             break;
  406.         case green: image.setColor(Color.GREEN);
  407.             break;
  408.         case orange: image.setColor(Color.ORANGE);
  409.             break;
  410.         case red: image.setColor(Color.RED);
  411.             break;
  412.         case white: image.setColor(Color.WHITE);
  413.             break;
  414.         case yellow: image.setColor(Color.YELLOW);
  415.             break;
  416.         default: image.setColor(Color.WHITE);
  417.             break;
  418.         }
  419.        
  420.     }
  421.  
  422.    
  423.     public void Dispose()
  424.     {
  425.         empty.dispose();
  426.         f_time_left.dispose();
  427.     }
  428. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement