MARICRUZFERNANDEZ

Untitled

Mar 12th, 2022
1,286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.82 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class Grid : MonoBehaviour
  7. {
  8.   public ShapeStorage shapeStorage;
  9.   public int columns = 0;
  10.   public int rows = 0;
  11.   public float squaresGap = 0.1f;
  12.   public GameObject gridSquare;
  13.   public Vector2 startPosition = new Vector2(0.0f, 0.0f);
  14.   public float squareScale = 0.5f;
  15.   public float everySquareOffset = 0.0f;
  16.   public SquareTextureData squareTextureData;
  17.  
  18.   private Vector2 _offset = new Vector2(0.0f, 0.0f);
  19.   // private List<GameObject> _gridSquares = new List<GameObject>();
  20.   private System.Collections.Generic.List<GameObject> _gridSquares = new System.Collections.Generic.List<GameObject>();
  21.  
  22.   private LineIndicator _lineIndicator;
  23.  
  24.   private Config.SquareColor currentActiveSquareColor_ = Config.SquareColor.NotSet;
  25.   private List<Config.SquareColor> colorsInTheGrid_ = new List<Config.SquareColor>();
  26.  
  27.   private void OnEnable()
  28.   {
  29.     GameEvents.CheckIfShapeCanBePlaced += CheckIfShapeCanBePlaced;
  30.     GameEvents.UpdateSquareColor += OnUpdateSquareColor;
  31.   }
  32.  
  33.   private void OnDisable()
  34.   {
  35.     GameEvents.CheckIfShapeCanBePlaced -= CheckIfShapeCanBePlaced;
  36.     GameEvents.UpdateSquareColor -= OnUpdateSquareColor;
  37.   }
  38.  
  39.   void Start()
  40.   {
  41.     _lineIndicator = GetComponent<LineIndicator>();
  42.     CreateGrid();
  43.     currentActiveSquareColor_ = squareTextureData.activeSquareTextures[0].squareColor;
  44.   }
  45.  
  46.   private void OnUpdateSquareColor(Config.SquareColor color)
  47.   {
  48.     currentActiveSquareColor_ = color;
  49.   }
  50.  
  51.   private List<Config.SquareColor> GetAllSquareColorsInTheGrid()
  52.   {
  53.     var colors = new List<Config.SquareColor>();
  54.  
  55.     foreach (var square in _gridSquares)
  56.     {
  57.       var gridSqaure = square.GetComponent<GridSquare>();
  58.       if (gridSqaure.SquareOccupied)
  59.       {
  60.         var color = gridSqaure.GetCurrentColor();
  61.         if (colors.Contains(color) == false)
  62.         {
  63.           colors.Add(color);
  64.         }
  65.       }
  66.     }
  67.  
  68.     return colors;
  69.   }
  70.   private void CreateGrid()
  71.   {
  72.     SpawnGridSquares();
  73.     SetGridSquaresPositions();
  74.   }
  75.  
  76.   private void SpawnGridSquares()
  77.   {
  78.     int square_index = 0;
  79.  
  80.     for (var row = 0; row < rows; ++row)
  81.     {
  82.       for (var column = 0; column < columns; ++column)
  83.       {
  84.         _gridSquares.Add(Instantiate(gridSquare) as GameObject);
  85.  
  86.         _gridSquares[_gridSquares.Count - 1].GetComponent<GridSquare>().SquareIndex = square_index;
  87.         _gridSquares[_gridSquares.Count - 1].transform.SetParent(this.transform);
  88.         _gridSquares[_gridSquares.Count - 1].transform.localScale = new Vector3(squareScale, squareScale, squareScale);
  89.         //_gridSquares[_gridSquares.Count - 1].GetComponent<GridSquare>().SetImage(square_index % 2 == 0);
  90.         _gridSquares[_gridSquares.Count - 1].GetComponent<GridSquare>().SetImage(_lineIndicator.GetGridSquareIndex(square_index) % 2 == 0);
  91.         square_index++;
  92.       }
  93.     }
  94.   }
  95.  
  96.   private void SetGridSquaresPositions()
  97.   {
  98.     int column_number = 0;
  99.     int row_number = 0;
  100.     Vector2 square_gap_number = new Vector2(0.0f, 0.0f);
  101.     bool row_moved = false;
  102.  
  103.     var square_rect = _gridSquares[0].GetComponent<RectTransform>();
  104.  
  105.     _offset.x = square_rect.rect.width * square_rect.transform.localScale.x + everySquareOffset;
  106.     _offset.y = square_rect.rect.height * square_rect.transform.localScale.y + everySquareOffset;
  107.  
  108.     foreach (GameObject square in _gridSquares)
  109.     {
  110.       if (column_number + 1 > columns)
  111.       {
  112.         square_gap_number.x = 0;
  113.         column_number = 0;
  114.         row_number++;
  115.         row_moved = false;
  116.       }
  117.  
  118.       var pos_x_offset = _offset.x * column_number + (square_gap_number.x * squaresGap);
  119.       var pos_y_offset = _offset.y * row_number + (square_gap_number.y * squaresGap);
  120.  
  121.       if (column_number > 0 && column_number % 3 == 0)
  122.       {
  123.         square_gap_number.x++;
  124.         pos_x_offset += squaresGap;
  125.       }
  126.       if (row_number > 0 && row_number % 3 == 0 && row_moved == false)
  127.       {
  128.         row_moved = true;
  129.         square_gap_number.y++;
  130.         pos_y_offset += squaresGap;
  131.       }
  132.  
  133.       square.GetComponent<RectTransform>().anchoredPosition = new Vector2(startPosition.x + pos_x_offset, startPosition.y - pos_y_offset);
  134.  
  135.       square.GetComponent<RectTransform>().localPosition = new Vector3(startPosition.x + pos_x_offset, startPosition.y - pos_y_offset, 0.0f);
  136.  
  137.       column_number++;
  138.     }
  139.   }
  140.  
  141.   private void CheckIfShapeCanBePlaced()
  142.   {
  143.     var squareIndexes = new List<int>();
  144.     foreach (var square in _gridSquares)
  145.     {
  146.       var gridSquare = square.GetComponent<GridSquare>();
  147.       if (gridSquare.Selected && !gridSquare.SquareOccupied)
  148.       {
  149.         squareIndexes.Add(gridSquare.SquareIndex);
  150.         gridSquare.Selected = false;
  151.         //gridSquare.ActiveSquare();
  152.       }
  153.     }
  154.  
  155.     var currentSelectedShape = shapeStorage.GetCurrentSelectedShape();
  156.     if (currentSelectedShape == null) return;
  157.  
  158.     if (currentSelectedShape.TotalSquareNumber == squareIndexes.Count)
  159.     {
  160.       foreach (var squareIndex in squareIndexes)
  161.       {
  162.         _gridSquares[squareIndex].GetComponent<GridSquare>().PlaceShapeOnBoard(currentActiveSquareColor_);
  163.       }
  164.  
  165.       var shapeLeft = 0;
  166.  
  167.       foreach (var shape in shapeStorage.shapeList)
  168.       {
  169.         if (shape.IsOnStartPosition() && shape.IsAnyOfShapeSquareActive())
  170.         {
  171.           shapeLeft++;
  172.         }
  173.       }
  174.  
  175.       //currentSelectedShape.DeactivateShape();
  176.  
  177.       if (shapeLeft == 0)
  178.       {
  179.         GameEvents.RequestNewShapes();
  180.       }
  181.       else
  182.       {
  183.         GameEvents.SetShapeInactive();
  184.       }
  185.       CheckIfAnyLineIsCompleted();
  186.     }
  187.     else
  188.     {
  189.       GameEvents.MoveShapeToStartPosition();
  190.     }
  191.     //shapeStorage.GetCurrentSelectedShape().DeactivateShape();
  192.   }
  193.  
  194.   // verifica si las lineas estan completas
  195.   void CheckIfAnyLineIsCompleted()
  196.   {
  197.     List<int[]> lines = new List<int[]>();
  198.  
  199.     // columns
  200.     foreach (var column in _lineIndicator.columnIndexes)
  201.     {
  202.       lines.Add(_lineIndicator.GetVerticalLine(column));
  203.     }
  204.  
  205.     // rows
  206.     for (var row = 0; row < 9; row++)
  207.     {
  208.       List<int> data = new List<int>(9);
  209.       for (var index = 0; index < 9; index++)
  210.       {
  211.         data.Add(_lineIndicator.line_data[row, index]);
  212.       }
  213.  
  214.       lines.Add(data.ToArray());
  215.     }
  216.  
  217.     // squares
  218.     for (var sqaure = 0; sqaure < 9; sqaure++)
  219.     {
  220.       List<int> data = new List<int>(9);
  221.       for (var index = 0; index < 9; index++)
  222.       {
  223.         data.Add(_lineIndicator.square_data[sqaure, index]);
  224.       }
  225.  
  226.       lines.Add(data.ToArray());
  227.     }
  228.  
  229.     // Esta funcion necesita ser llamada  antes CheckIfSquaresAreCompleted
  230.     colorsInTheGrid_ = GetAllSquareColorsInTheGrid();
  231.  
  232.     var completedLines = CheckIfSquaresAreCompleted(lines);
  233.  
  234.     if (completedLines >= 2)
  235.     {
  236.       //TODO: Play bonus animation.
  237.       GameEvents.ShowCongratulationWritings();
  238.     }
  239.  
  240.     var totalScores = 10 * completedLines;
  241.     var bonusScores = ShouldPlayColorBonusAnimation();
  242.     GameEvents.AddScores(totalScores + bonusScores);
  243.     CheckIfPlayerLost();
  244.   }
  245.  
  246.   private int ShouldPlayColorBonusAnimation()
  247.   {
  248.     var colorsInTheGridAfterLineRemoved = GetAllSquareColorsInTheGrid();
  249.     Config.SquareColor colorToPlayBonusFor = Config.SquareColor.NotSet;
  250.  
  251.     foreach (var squareColor in colorsInTheGrid_)
  252.     {
  253.       if (colorsInTheGridAfterLineRemoved.Contains(squareColor) == false)
  254.       {
  255.         colorToPlayBonusFor = squareColor;
  256.       }
  257.     }
  258.  
  259.     if ( colorToPlayBonusFor == Config.SquareColor.NotSet)
  260.     {
  261.       Debug.Log("Cannot find Color for bonus");
  262.       return 0;
  263.     }
  264.  
  265.    //nunca debe jugar por el color actual de bonificación
  266.    if ( colorToPlayBonusFor == currentActiveSquareColor_)
  267.    {
  268.      return 0;
  269.    }
  270.  
  271.    GameEvents.ShowBonusScreen(colorToPlayBonusFor);
  272.  
  273.    return 50;
  274.   }
  275.  
  276.   private int CheckIfSquaresAreCompleted(List<int[]> data)
  277.   {
  278.     List<int[]> completedLines = new List<int[]>();
  279.  
  280.     var linesCompleted = 0;
  281.  
  282.     foreach (var line in data)
  283.     {
  284.       var lineCompleted = true;
  285.       foreach (var squareIndex in line)
  286.       {
  287.         var comp = _gridSquares[squareIndex].GetComponent<GridSquare>();
  288.         if (comp.SquareOccupied == false)
  289.         {
  290.           lineCompleted = false;
  291.         }
  292.       }
  293.  
  294.       if (lineCompleted)
  295.       {
  296.         completedLines.Add(line);
  297.       }
  298.     }
  299.  
  300.     foreach (var line in completedLines)
  301.     {
  302.       var completed = false;
  303.  
  304.       foreach (var squareIndex in line)
  305.       {
  306.         var comp = _gridSquares[squareIndex].GetComponent<GridSquare>();
  307.         comp.Deactivate();
  308.         completed = true;
  309.       }
  310.  
  311.       foreach (var squareIndex in line)
  312.       {
  313.         var comp = _gridSquares[squareIndex].GetComponent<GridSquare>();
  314.         comp.ClearOccupied();
  315.       }
  316.  
  317.       if (completed)
  318.       {
  319.         linesCompleted++;
  320.       }
  321.     }
  322.  
  323.     return linesCompleted;
  324.   }
  325.  
  326.   private void CheckIfPlayerLost()
  327.   {
  328.     var validShapes = 0;
  329.     for (var index = 0; index < shapeStorage.shapeList.Count; index++)
  330.     {
  331.       var isShapeActive = shapeStorage.shapeList[index].IsAnyOfShapeSquareActive();
  332.       if (CheckIfShapeCanBePlacedOnGrid(shapeStorage.shapeList[index]) && isShapeActive)
  333.       {
  334.         shapeStorage.shapeList[index]?.ActivateShape();
  335.         validShapes++;
  336.       }
  337.     }
  338.  
  339.     if (validShapes == 0)
  340.     {
  341.       // GAME OVER
  342.       GameEvents.GameOver(false);
  343.       //Debug.Log("GAME OVER ARTISTA...");
  344.     }
  345.   }
  346.  
  347.   private bool CheckIfShapeCanBePlacedOnGrid(Shape currentShape)
  348.   {
  349.     var currentShapeData = currentShape.CurrentShapeData;
  350.     var shapeColumns = currentShapeData.columns;
  351.     var shapeRows = currentShapeData.rows;
  352.  
  353.     // All indexes of filled up squares.
  354.     List<int> originalShapeFilledUpSquares = new List<int>();
  355.     var squareIndex = 0;
  356.  
  357.     for (var rowIndex = 0; rowIndex < shapeRows; rowIndex++)
  358.     {
  359.       for (var columnIndex = 0; columnIndex < shapeColumns; columnIndex++)
  360.       {
  361.         if (currentShapeData.board[rowIndex].column[columnIndex])
  362.         {
  363.           originalShapeFilledUpSquares.Add(squareIndex);
  364.         }
  365.  
  366.         squareIndex++;
  367.       }
  368.     }
  369.  
  370.     if (currentShape.TotalSquareNumber != originalShapeFilledUpSquares.Count)
  371.       Debug.LogError("Number of filled up squares are not the same as the original shape have.");
  372.  
  373.  
  374.     var sqaureList = GetAllSquaresCombination(shapeColumns, shapeRows);
  375.  
  376.     bool canBePlaced = false;
  377.  
  378.     foreach (var number in sqaureList)
  379.     {
  380.       bool shapeCanBePlacedOnTheBoard = true;
  381.       foreach (var squareIndexToCheck in originalShapeFilledUpSquares)
  382.       {
  383.         var comp = _gridSquares[number[squareIndexToCheck]].GetComponent<GridSquare>();
  384.         if (comp.SquareOccupied)
  385.         {
  386.           shapeCanBePlacedOnTheBoard = false;
  387.         }
  388.       }
  389.  
  390.       if (shapeCanBePlacedOnTheBoard)
  391.       {
  392.         canBePlaced = true;
  393.       }
  394.     }
  395.     return canBePlaced;
  396.   }
  397.  
  398.   private List<int[]> GetAllSquaresCombination(int columns, int rows)
  399.   {
  400.     var sqaureList = new List<int[]>();
  401.     var lastColumnIndex = 0;
  402.     var lastRowIndex = 0;
  403.  
  404.     int safeIndex = 0;
  405.  
  406.     while (lastRowIndex + (rows - 1) < 9)
  407.     {
  408.       var rowData = new List<int>();
  409.  
  410.       for (var row = lastRowIndex; row < lastRowIndex + rows; row++)
  411.       {
  412.         for (var column = lastColumnIndex; column < lastColumnIndex + columns; column++)
  413.         {
  414.           rowData.Add(_lineIndicator.line_data[row, column]);
  415.         }
  416.       }
  417.  
  418.       sqaureList.Add(rowData.ToArray());
  419.  
  420.       lastColumnIndex++;
  421.  
  422.       if (lastColumnIndex + (columns - 1) >= 9)
  423.       {
  424.         lastRowIndex++;
  425.         lastColumnIndex = 0;
  426.       }
  427.  
  428.       safeIndex++;
  429.  
  430.       if (safeIndex > 100)
  431.       {
  432.         break;
  433.       }
  434.     }
  435.  
  436.     return sqaureList;
  437.   }
  438. }
  439.  
Advertisement
Add Comment
Please, Sign In to add comment