Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.me.mygdxgame;
- import java.util.Random;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.graphics.Color;
- import com.badlogic.gdx.graphics.OrthographicCamera;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.BitmapFont;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.math.Vector3;
- import com.badlogic.gdx.scenes.scene2d.ui.Image;
- public class Map {
- public Texture empty;
- public Image i_empty;
- public Cell[][] Mapa;
- int map_size, current;
- float cell_scale;
- Cell.Color rand_cell, Next;
- Next_Shape next_shape;
- Cell[][] proximo;
- Float time_left, reset_max;
- BitmapFont f_time_left;
- Vector3 time_text;
- float scale;
- public Map()
- {
- f_time_left = new BitmapFont(Gdx.files.internal("data/Font.fnt"));
- map_size = 10;
- cell_scale = 0.1f;
- current = 0;
- reset_max = 25f;
- time_left = reset_max;
- empty = new Texture(Gdx.files.internal("data/blank.png"));
- i_empty = new Image(empty);
- Mapa = new Cell[map_size][map_size];
- for (int y = 0; y < map_size; y++)
- {
- for(int x = 0; x < map_size; x++)
- {
- Mapa[x][y] = new Cell();
- if (x == 0 || y == 0 || x == map_size-1 || y == map_size-1)
- {
- Mapa[x][y].corner = true;
- }
- else
- {
- Mapa[x][y].color = Cell.Color.white;
- }
- }
- }
- Next = GetColor();
- next_shape = new Next_Shape(Next);
- next_shape.SetNewShape(Next);
- proximo = new Cell[3][3];
- proximo = next_shape.GetCellsMap();
- }
- public void CellClick(OrthographicCamera camera)
- {
- Vector3 pos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
- camera.unproject(pos);
- for (int y = 0; y < map_size; y++)
- {
- for(int x = 0; x < map_size; x++)
- {
- if (IsClicked(x*cell_scale, (y*cell_scale)+cell_scale, cell_scale, cell_scale, pos))
- {
- CallCreatePiece(x, y);
- time_left = reset_max;
- }
- }
- }
- for (int y = 0; y < 3; y++)
- {
- for (int x = 0; x < 3; x++)
- {
- Setcolor(proximo[x][y].color, i_empty, x, y);
- Vector3 pos1 = new Vector3(Gdx.graphics.getWidth(), Gdx.graphics.getHeight() - cell_scale, 0);
- camera.unproject(pos1);
- pos1.x -= 3 * cell_scale;
- pos1.x += x * cell_scale;
- pos1.y += y * cell_scale;
- if (IsClicked(pos1.x, pos1.y, cell_scale, cell_scale, pos))
- {
- next_shape.Rotate();
- }
- }
- }
- }
- public boolean Update()
- {
- if (time_left < 0)
- {
- return false;
- }
- time_left -= Gdx.graphics.getDeltaTime();
- return true;
- }
- public void CallCreatePiece(int x, int y)
- {
- if (CheckFigureValid(x, y))
- {
- MakeFigure(x, y);
- CreatePiece(x, y);
- }
- }
- public void CreatePiece(int x, int y)
- {
- ReclaimPrize(x, y, Next);
- Next = GetColor();
- next_shape.SetNewShape(Next);
- proximo = next_shape.GetCellsMap();
- }
- public void ReclaimPrize(int x, int y, Cell.Color color)
- {
- /*Gdx.app.log("blocks",
- Integer.toString(this.BlockExistCheck(x, y, color)) + " " +
- Integer.toString(this.BlockExistCheck(x-1, y, color)) + " " +
- Integer.toString(this.BlockExistCheck(x, y-1, color)) + " " +
- Integer.toString(this.BlockExistCheck(x+1, y, color)) + " " +
- Integer.toString(this.BlockExistCheck(x, y+1, color)) + " "
- );
- */
- if (this.BlockExistCheck(x, y, Next)!= 0) { this.RecurssivePrize(x, y, Next, 0); }
- if (this.BlockExistCheck(x-1, y, Next)!= 0) { this.RecurssivePrize(x-1, y, Next, 0); }
- if (this.BlockExistCheck(x+1, y, Next)!= 0) { this.RecurssivePrize(x+1, y, Next, 0); }
- if (this.BlockExistCheck(x, y+1, Next)!= 0) { this.RecurssivePrize(x, y+1, Next, 0); }
- if (this.BlockExistCheck(x, y-1, Next)!= 0) { this.RecurssivePrize(x, y-1, Next, 0); }
- if (this.BlockExistCheck(x+1, y+1, Next)!= 0) { this.RecurssivePrize(x+1, y+1, Next, 0); }
- if (this.BlockExistCheck(x-1, y-1, Next)!= 0) { this.RecurssivePrize(x-1, y-1, Next, 0); }
- if (this.BlockExistCheck(x+1, y-1, Next)!= 0) { this.RecurssivePrize(x+1, y-1, Next, 0); }
- if (this.BlockExistCheck(x-1, y+1, Next)!= 0) { this.RecurssivePrize(x-1, y+1, Next, 0); }
- if (this.BlockExistCheck(x-2, y, Next)!= 0) { this.RecurssivePrize(x-2, y, Next, 0); }
- if (this.BlockExistCheck(x+2, y, Next)!= 0) { this.RecurssivePrize(x+2, y, Next, 0); }
- if (this.BlockExistCheck(x, y+2, Next)!= 0) { this.RecurssivePrize(x, y+2, Next, 0); }
- if (this.BlockExistCheck(x, y-2, Next)!= 0) { this.RecurssivePrize(x, y-2, Next, 0); }
- if (this.BlockExistCheck(x+2, y+2, Next)!= 0) { this.RecurssivePrize(x+2, y+2, Next, 0); }
- if (this.BlockExistCheck(x-2, y-2, Next)!= 0) { this.RecurssivePrize(x-2, y-2, Next, 0); }
- if (this.BlockExistCheck(x+2, y-2, Next)!= 0) { this.RecurssivePrize(x+2, y-2, Next, 0); }
- if (this.BlockExistCheck(x-2, y+2, Next)!= 0) { this.RecurssivePrize(x-2, y+2, Next, 0); }
- }
- public void RecurssivePrize(int x, int y, Cell.Color color, int id)
- {
- id++;
- if (id > 9)
- {
- return;
- }
- if (this.BlockExistCheck(x, y, Next) != 0)
- {
- RecurssivePrize(x-1, y, color, id);
- RecurssivePrize(x+1, y, color, id);
- RecurssivePrize(x, y+1, color, id);
- RecurssivePrize(x, y-1, color, id);
- RecurssivePrize(x-1, y-1, color, id);
- RecurssivePrize(x+1, y+1, color, id);
- RecurssivePrize(x-1, y+1, color, id);
- RecurssivePrize(x+1, y-1, color, id);
- RecurssivePrize(x-2, y, color, id);
- RecurssivePrize(x+2, y, color, id);
- RecurssivePrize(x, y+2, color, id);
- RecurssivePrize(x, y-2, color, id);
- RecurssivePrize(x-2, y-2, color, id);
- RecurssivePrize(x+2, y+2, color, id);
- RecurssivePrize(x-2, y+2, color, id);
- RecurssivePrize(x+2, y-2, color, id);
- }
- if (IsValid(x, y))
- {
- Mapa[x][y].color = Cell.Color.white;
- }
- }
- public int BlockExistCheck(int x, int y, Cell.Color color)
- {
- int possibles = 0;
- int horizontals = 0;
- int diagonals = 0;
- if (IsValid(x-1, y) && Mapa[x-1][y].color == color) { horizontals++;}
- if (IsValid(x+1, y) && Mapa[x+1][y].color == color) { horizontals++;}
- if (IsValid(x, y-1) && Mapa[x][y-1].color == color) { horizontals++;}
- if (IsValid(x, y+1) && Mapa[x][y+1].color == color) { horizontals++;}
- if (IsValid(x-1, y-1) && Mapa[x-1][y-1].color == color) { diagonals++;}
- if (IsValid(x+1, y+1) && Mapa[x+1][y+1].color == color) { diagonals++;}
- if (IsValid(x-1, y+1) && Mapa[x-1][y+1].color == color) { diagonals++;}
- if (IsValid(x+1, y-1) && Mapa[x+1][y-1].color == color) { diagonals++;}
- // 1 = horizontal / 2 = diagonals / 3 = both / 0 = nothing
- if (horizontals == 4) {possibles = 1;}
- if (diagonals == 4) {possibles = 2;}
- if (horizontals == 4 && diagonals == 4) {possibles = 3;}
- return possibles;
- }
- public boolean CheckFigureValid(int x, int y)
- {
- int xpos = 0;
- int ypos = 0;
- for (int v = 0; v < 3; v++)
- {
- for (int h = 0; h < 3; h++)
- {
- if (h == 0) { xpos = -1; }
- if (v == 0) { ypos = -1; }
- if (h == 1) { xpos = 0; }
- if (v == 1) { ypos = 0; }
- if (h == 2) { xpos = 1; }
- if (v == 2) { ypos = 1; }
- if (!IsValid(x+xpos, y+ypos))
- {
- return false;
- }
- if (proximo[h][v].color != Cell.Color.white)
- {
- if(Mapa[x+xpos][y+ypos].color != Cell.Color.white)
- {
- return false;
- }
- }
- }
- }
- return true;
- }
- public void MakeFigure(int x, int y)
- {
- int xpos = 0;
- int ypos = 0;
- for (int v = 0; v < 3; v++)
- {
- for (int h = 0; h < 3; h++)
- {
- if (h == 0) { xpos = -1; }
- if (v == 0) { ypos = -1; }
- if (h == 1) { xpos = 0; }
- if (v == 1) { ypos = 0; }
- if (h == 2) { xpos = 1; }
- if (v == 2) { ypos = 1; }
- if (!IsValid(x+xpos, y+ypos))
- {
- return;
- }
- else
- {
- if (Mapa[x+xpos][y+ypos].color == Cell.Color.white)
- {
- Mapa[x+xpos][y+ypos].color = proximo[h][v].color;
- }
- }
- }
- }
- }
- public boolean IsValid(int x, int y)
- {
- if (x < 0 || y < 0)
- {
- return false;
- }
- if (x > map_size-1 || y > map_size-1)
- {
- return false;
- }
- return true;
- }
- public boolean IsClicked(float x, float y, float width, float height, Vector3 pos)
- {
- if (pos.x > x)
- {
- if (pos.x < x + width)
- {
- if (pos.y > y)
- {
- if (pos.y < y + height)
- {
- return true;
- }
- }
- }
- }
- return false;
- }
- public void Draw(SpriteBatch sprite, OrthographicCamera camera)
- {
- for (int y = 0; y < map_size; y++)
- {
- for(int x = 0; x < map_size; x++)
- {
- float new_x = x * cell_scale;
- float new_y = (y * cell_scale) + cell_scale;
- if (Mapa[x][y].corner && Mapa[x][y].color == Cell.Color.white)
- {
- i_empty.setColor(Color.GRAY);
- }
- else
- {
- Setcolor(Mapa[x][y].color, i_empty, x, y);
- }
- i_empty.setBounds(new_x, new_y, cell_scale, cell_scale);
- i_empty.draw(sprite, 1f);
- }
- }
- proximo = next_shape.GetCellsMap();
- for(int y = 0; y < 3; y++)
- {
- for (int x = 0; x < 3; x++)
- {
- if (proximo[x][y] != null && proximo[x][y].color != null)
- {
- Setcolor(proximo[x][y].color, i_empty, x, y);
- }
- Vector3 pos = new Vector3(Gdx.graphics.getWidth(), Gdx.graphics.getHeight() - cell_scale, 0);
- camera.unproject(pos);
- pos.x -= 3 * cell_scale;
- pos.x += x * cell_scale;
- pos.y += y * cell_scale;
- i_empty.setBounds(pos.x, pos.y, cell_scale, cell_scale);
- i_empty.draw(sprite, 1f);
- }
- }
- scale = 0.1f;
- time_text = new Vector3(0, 0, 0);
- camera.unproject(time_text);
- f_time_left.setColor(Color.BLUE);
- f_time_left.setScale(scale);
- int time = Math.round(time_left);
- this.f_time_left.drawMultiLine(sprite, "T:", time_text.x, time_text.y);
- }
- public Cell.Color GetColor()
- {
- Random random = new Random();
- int rand_color = random.nextInt(8);
- switch (rand_color)
- {
- case 1: rand_cell = Cell.Color.blue;
- break;
- case 3: rand_cell = Cell.Color.green;
- break;
- case 4: rand_cell = Cell.Color.orange;
- break;
- case 5: rand_cell = Cell.Color.red;
- break;
- case 7: rand_cell = Cell.Color.yellow;
- break;
- }
- return rand_cell;
- }
- public void Setcolor(Cell.Color color, Image image, int x, int y)
- {
- Cell.Color new_color = color;
- if (new_color == null)
- {
- proximo = next_shape.GetCellsMap();
- new_color = proximo[1][1].color;
- }
- switch(new_color)
- {
- case blue: image.setColor(Color.BLUE);
- break;
- case green: image.setColor(Color.GREEN);
- break;
- case orange: image.setColor(Color.ORANGE);
- break;
- case red: image.setColor(Color.RED);
- break;
- case white: image.setColor(Color.WHITE);
- break;
- case yellow: image.setColor(Color.YELLOW);
- break;
- default: image.setColor(Color.WHITE);
- break;
- }
- }
- public void Dispose()
- {
- empty.dispose();
- f_time_left.dispose();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement