Пример #1
0
    private GameObject InstantiateChip(Grid.ChipType chip, int x, int y)
    {
        float newX = new float();
        float newY = new float();

        newX = -2.0f + x * 0.571f;
        newY = -4.2f + y * 0.97f;
        GameObject resultGO;

        switch (chip)
        {
        case Grid.ChipType.R:
            resultGO = Instantiate(redPrefab, new Vector2(newX, newY), Quaternion.identity);
            break;

        case Grid.ChipType.G:
            resultGO = Instantiate(greenPrefab, new Vector2(newX, newY), Quaternion.identity);
            break;

        case Grid.ChipType.B:
            resultGO = Instantiate(bluePrefab, new Vector2(newX, newY), Quaternion.identity);
            break;

        case Grid.ChipType.P:
            resultGO = Instantiate(pinkPrefab, new Vector2(newX, newY), Quaternion.identity);
            break;

        default:
            resultGO = null;
            break;
        }
        return(resultGO);
    }
Пример #2
0
    private void UserAction()
    {
        RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);

        if (hit)
        {
            Vector2Int chipPosition = view.GetChipPosition(hit.transform.gameObject);
            if (!isChipStored)
            {
                storedChip     = grid.GetChip(chipPosition.x, chipPosition.y);
                storedPosition = chipPosition;
                isChipStored   = true;
            }
            else
            {
                if (Mathf.Abs(chipPosition.x - storedPosition.x) == 1 && Mathf.Abs(chipPosition.y - storedPosition.y) == 0 ||
                    Mathf.Abs(chipPosition.x - storedPosition.x) == 0 && Mathf.Abs(chipPosition.y - storedPosition.y) == 1)
                {
                    SwapChips(chipPosition, storedPosition);
                    isChipStored = false;
                    if (chipPosition.x == storedPosition.x)
                    {
                        CheckAndClearLine(storedPosition.x, true);
                        CheckAndClearLine(storedPosition.y, false);
                        CheckAndClearLine(chipPosition.y, false);
                    }
                    else
                    {
                        CheckAndClearLine(storedPosition.x, true);
                        CheckAndClearLine(storedPosition.y, false);
                        CheckAndClearLine(chipPosition.x, true);
                    }
                    Cascade();
                    view.EraseGrid();
                    view.DrawGrid(grid);
                }
                else
                {
                    isChipStored = false;
                }
            }
        }
    }
Пример #3
0
    private void CheckAndClearLine(int line, bool vertical)
    {
        uint repeatCounter = 0;

        Grid.ChipType     previousChip = Grid.ChipType.NULLCHIP;
        List <Vector2Int> clearList    = new List <Vector2Int>();

        if (vertical)
        {
            for (int heightCount = 0; heightCount < height; heightCount++)
            {
                if (grid.GetChip(line, heightCount) == previousChip)
                {
                    repeatCounter++;
                    if (repeatCounter > 1)
                    {
                        clearList.Add(new Vector2Int(line, heightCount));

                        if (repeatCounter == 2)
                        {
                            clearList.Add(new Vector2Int(line, heightCount - 1));
                            clearList.Add(new Vector2Int(line, heightCount - 2));
                        }
                    }
                }
                else
                {
                    repeatCounter = 0;
                    previousChip  = grid.GetChip(line, heightCount);
                }
            }
        }
        else
        {
            for (int widthCount = 0; widthCount < width; widthCount++)
            {
                if (grid.GetChip(widthCount, line) == previousChip)
                {
                    repeatCounter++;
                    if (repeatCounter > 1)
                    {
                        if (!clearList.Contains(new Vector2Int(widthCount, line)))
                        {
                            clearList.Add(new Vector2Int(widthCount, line));
                        }

                        if (repeatCounter == 2)
                        {
                            if (!clearList.Contains(new Vector2Int(widthCount - 1, line)))
                            {
                                clearList.Add(new Vector2Int(widthCount - 1, line));
                            }

                            if (!clearList.Contains(new Vector2Int(widthCount - 2, line)))
                            {
                                clearList.Add(new Vector2Int(widthCount - 2, line));
                            }
                        }
                    }
                }
                else
                {
                    repeatCounter = 0;
                    previousChip  = grid.GetChip(widthCount, line);
                }
            }
        }

        //clear repeats
        foreach (Vector2Int chipPosition in clearList)
        {
            if (chipPosition.x >= 0 && chipPosition.x < width)
            {
                if (chipPosition.y >= 0 && chipPosition.y < height)
                {
                    grid.SetChip(Grid.ChipType.NULLCHIP, chipPosition.x, chipPosition.y);
                    nullPositions.Add(new Vector2Int(chipPosition.x, chipPosition.y));
                }
            }
        }
    }
Пример #4
0
 private void SwapChips(Vector2Int chipPosition1, Vector2Int chipPosition2)
 {
     Grid.ChipType auxChip = grid.GetChip(chipPosition2.x, chipPosition2.y);
     grid.SetChip(grid.GetChip(chipPosition1.x, chipPosition1.y), chipPosition2.x, chipPosition2.y);
     grid.SetChip(auxChip, chipPosition1.x, chipPosition1.y);
 }
Пример #5
0
    private bool SpawnCheck()
    {
        List <Vector2Int> rerollList = new List <Vector2Int>();
        uint repeatCounter           = 0;
        bool result = false;

        Grid.ChipType previousChip = Grid.ChipType.NULLCHIP;

        //check vertical matches
        for (int widthCount = 0; widthCount < width; widthCount++)
        {
            for (int heightCount = 0; heightCount < height; heightCount++)
            {
                if (grid.GetChip(widthCount, heightCount) == previousChip)
                {
                    repeatCounter++;
                    if (repeatCounter > 1)
                    {
                        rerollList.Add(new Vector2Int(widthCount, heightCount));
                        result = true;

                        if (repeatCounter == 3)
                        {
                            rerollList.Add(new Vector2Int(widthCount, heightCount - 1));
                            rerollList.Add(new Vector2Int(widthCount, heightCount - 2));
                        }
                    }
                }
                else
                {
                    repeatCounter = 0;
                    previousChip  = grid.GetChip(widthCount, heightCount);
                }
            }
            previousChip  = Grid.ChipType.NULLCHIP;
            repeatCounter = 0;
        }

        //check horizontal matches
        for (int heightCount = 0; heightCount < height; heightCount++)
        {
            for (int widthCount = 0; widthCount < width; widthCount++)
            {
                if (grid.GetChip(widthCount, heightCount) == previousChip)
                {
                    repeatCounter++;
                    if (repeatCounter > 1)
                    {
                        if (!rerollList.Contains(new Vector2Int(widthCount, heightCount)))
                        {
                            rerollList.Add(new Vector2Int(widthCount, heightCount));
                        }

                        result = true;

                        if (repeatCounter == 3)
                        {
                            if (!rerollList.Contains(new Vector2Int(widthCount - 1, heightCount)))
                            {
                                rerollList.Add(new Vector2Int(widthCount - 1, heightCount));
                            }

                            if (!rerollList.Contains(new Vector2Int(widthCount - 2, heightCount)))
                            {
                                rerollList.Add(new Vector2Int(widthCount - 2, heightCount));
                            }
                        }
                    }
                }
                else
                {
                    repeatCounter = 0;
                    previousChip  = grid.GetChip(widthCount, heightCount);
                }
            }
            previousChip  = Grid.ChipType.NULLCHIP;
            repeatCounter = 0;
        }


        //reroll matching chips
        foreach (Vector2Int chipPosition in rerollList)
        {
            previousChip = grid.GetChip(chipPosition.x, chipPosition.y);
            while (previousChip == grid.GetChip(chipPosition.x, chipPosition.y))
            {
                grid.SetChip((Grid.ChipType)Random.Range((int)0, (int)4), chipPosition.x, chipPosition.y);
            }
        }
        return(result);
    }