public void OneDiagonalAround(Vector2Int key, bool[] diaDirectionCheck, int loop)
    {
        RoadTypeList currentRoadType = RoadTypeList.ALL_DIA_CONNECTION;
        Vector2Int   offset          = Vector2Int.zero;

        if (diaDirectionCheck[(int)DiagonalDirection.TOP_LEFT])
        {
            currentRoadType = RoadTypeList.DIA_TL_ONLY_CONNECTION;
            offset          = TOP_LEFT_VECTOR;
        }
        else if (diaDirectionCheck[(int)DiagonalDirection.TOP_RIGHT])
        {
            currentRoadType = RoadTypeList.DIA_TR_ONLY_CONNECTION;
            offset          = TOP_RIGHT_VECTOR;
        }
        else if (diaDirectionCheck[(int)DiagonalDirection.BOTTOM_LEFT])
        {
            currentRoadType = RoadTypeList.DIA_BL_ONLY_CONNECTION;
            offset          = BOTTOM_LEFT_VECTOR;
        }
        else //bottom right
        {
            currentRoadType = RoadTypeList.DIA_BR_ONLY_CONNECTION;
            offset          = BOTTOM_RIGHT_VECTOR;
        }

        SetRoadSprite(key, currentRoadType);
        CheckAndChangeRoadDirection(key + offset, loop + 1);
    }
    public void SaveFileWasLoaded(BuildingsOnMap[] saveFileBuildingsData, RoadsOnMap[] saveFileRoadsData)
    {
        // Removes existing buildings first
        RemoveAllBuildingsFromMap_Quietly();

        BaseBuildingsClass savedBuilding = null;
        Vector3            savedPosition = Vector3.zero;

        for (int i = 0; i < saveFileBuildingsData.Length; ++i)
        {
            savedPosition.x = saveFileBuildingsData[i].worldPosX;
            savedPosition.y = saveFileBuildingsData[i].worldPosY;
            savedBuilding   = Instantiate(BuildingDataBase.GetInstance().GetBaseBuildingGO(), savedPosition, Quaternion.identity).GetComponent <BaseBuildingsClass>();
            savedBuilding.SetNewBuildingType(BuildingDataBase.GetInstance().GetBuildingData((BuildingDataBase.BUILDINGS)saveFileBuildingsData[i].buildingType));
            PlaceBuildingToGrid(ref savedBuilding);
        }

        //do the same for the roads
        for (int i = 0; i < saveFileRoadsData.Length; ++i)
        {
            savedPosition.x = saveFileRoadsData[i].worldPosX;
            savedPosition.y = saveFileRoadsData[i].worldPosY;
            savedBuilding   = Instantiate(BuildingDataBase.GetInstance().GetBaseBuildingGO(), savedPosition, Quaternion.identity).GetComponent <BaseBuildingsClass>();
            savedBuilding.SetNewBuildingType(BuildingDataBase.GetInstance().GetBuildingData(BuildingDataBase.BUILDINGS.B_ROAD));

            //set to the correct road direction sprite
            RoadTypeList roadType = (RoadTypeList)saveFileRoadsData[i].roadType;
            savedBuilding.SetSprite(BuildingDataBase.GetInstance().GetRoadSprite(roadType));
            StoreLoadedRoads(savedBuilding, roadType);
        }
    }
    public Sprite GetRoadSprite(RoadTypeList roadType)
    {
        if (m_DictRoadTypeSprites.ContainsKey(roadType))
        {
            return(m_DictRoadTypeSprites[roadType]);
        }

        return(null);
    }
    public void TwoDiagonalAround(Vector2Int key, bool[] diaDirectionCheck, int loop)
    {
        RoadTypeList currentRoadType = RoadTypeList.ALL_DIA_CONNECTION;

        Vector2Int[] offsets = new Vector2Int[2];

        if (diaDirectionCheck[(int)DiagonalDirection.TOP_LEFT])
        {
            offsets[0] = TOP_LEFT_VECTOR;

            if (diaDirectionCheck[(int)DiagonalDirection.TOP_RIGHT]) //top left and top right
            {
                offsets[1]      = TOP_RIGHT_VECTOR;
                currentRoadType = RoadTypeList.DIA_TL_TR_ONLY_CONNECTION;
            }
            else if (diaDirectionCheck[(int)DiagonalDirection.BOTTOM_LEFT]) //top left and bottom left
            {
                offsets[1]      = BOTTOM_LEFT_VECTOR;
                currentRoadType = RoadTypeList.DIA_TL_BL_ONLY_CONNECTION;
            }
            else //assume its top left and bottom right
            {
                offsets[1]      = BOTTOM_RIGHT_VECTOR;
                currentRoadType = RoadTypeList.DIA_TL_BR_ONLY_CONNECTION;
            }
        }
        else if (diaDirectionCheck[(int)DiagonalDirection.TOP_RIGHT])
        {
            offsets[0] = TOP_RIGHT_VECTOR;

            if (diaDirectionCheck[(int)DiagonalDirection.BOTTOM_LEFT]) //top right and bottom left
            {
                offsets[1]      = BOTTOM_LEFT_VECTOR;
                currentRoadType = RoadTypeList.DIA_TR_BL_ONLY_CONNECTION;
            }
            else //assume its top right and bottom right
            {
                offsets[1]      = BOTTOM_RIGHT_VECTOR;
                currentRoadType = RoadTypeList.DIA_TR_BR_ONLY_CONNECTION;
            }
        }
        else //assume its bottom left and bottom right
        {
            offsets[0] = BOTTOM_RIGHT_VECTOR;
            offsets[1] = BOTTOM_LEFT_VECTOR;

            currentRoadType = RoadTypeList.DIA_BL_BR_ONLY_CONNECTION;
        }

        SetRoadSprite(key, currentRoadType);

        //check the 2 diagonal roads and update accordingly
        foreach (Vector2Int offset in offsets)
        {
            CheckAndChangeRoadDirection(key + offset, loop + 1);
        }
    }
    public void ThreeRoadsAround(Vector2Int key, bool[] directionChecks, int loop)
    {
        RoadTypeList currentRoadType = RoadTypeList.NO_CONNECTION;

        Vector2Int[] offsets = new Vector2Int[3];

        if (directionChecks[(int)Direction.UP])
        {
            offsets[0] = UP_VECTOR;

            if (directionChecks[(int)Direction.DOWN])
            {
                offsets[1] = DOWN_VECTOR;

                if (directionChecks[(int)Direction.RIGHT]) //up, down and right got tiles
                {
                    currentRoadType = RoadTypeList.U_D_R_ONLY_CONNECTION;
                    offsets[2]      = RIGHT_VECTOR;
                }
                else //up, down and left got tiles
                {
                    currentRoadType = RoadTypeList.U_D_L_ONLY_CONNECTION;
                    offsets[2]      = LEFT_VECTOR;
                }
            }
            else //assume its up, right and left
            {
                offsets[1]      = RIGHT_VECTOR;
                offsets[2]      = LEFT_VECTOR;
                currentRoadType = RoadTypeList.U_R_L_ONLY_CONNECTION;
            }
        }
        else //assume its down, right and left
        {
            offsets[0]      = DOWN_VECTOR;
            offsets[1]      = RIGHT_VECTOR;
            offsets[2]      = LEFT_VECTOR;
            currentRoadType = RoadTypeList.D_R_L_ONLY_CONNECTION;
        }

        //check diagonal
        Vector2Int diagonalOffset = Vector2Int.zero;

        ThreeRoadsDiagonal(key, ref currentRoadType, loop);

        //change the current one
        SetRoadSprite(key, currentRoadType);

        foreach (Vector2Int offset in offsets)
        {
            CheckAndChangeRoadDirection(new Vector2Int(key.x + offset.x, key.y + offset.y), loop + 1);
        }
    }
    public void StoreLoadedInRoads(Vector2Int key, BaseBuildingsClass roadInfo, RoadTypeList roadType)
    {
        if (CheckMapAvailability(key)) //if key exists, do early return
        {
            return;
        }

        m_RoadSpriteRendererMap.Add(key, roadInfo);

        int indexConverted = Convert2DToIntIndex(key);

        m_RoadMap.Add(indexConverted, roadType);
    }
    public void SetRoadSprite(Vector2Int key, RoadTypeList type)
    {
        BaseBuildingsClass road = m_RoadSpriteRendererMap[key];

        road.SetSprite(BuildingDataBase.GetInstance().GetRoadSprite(type));

        //change and store the road type
        int indexConverted = Convert2DToIntIndex(key);

        if (m_RoadMap.ContainsKey(indexConverted))
        {
            m_RoadMap[indexConverted] = type;
        }
    }
    //to store the loaded roads from the save file into the roadManager
    public void StoreLoadedRoads(BaseBuildingsClass roadBuilding, RoadTypeList roadType)
    {
        if (m_RoadManager == null)
        {
            return;
        }

        Vector3    buildingBottomLeftWorldPos = roadBuilding.GetBottomLeftGridPosition();
        Vector2Int buildingSize = roadBuilding.GetBuildingSizeOnMap();

        // Set all the grids taken by new building to true
        SetGridTakenArray(buildingBottomLeftWorldPos, buildingSize, true);

        Vector2Int key = (Vector2Int)m_GridGO.WorldToCell(roadBuilding.GetBottomLeftGridPosition());

        roadBuilding.BuildingPlaced();

        m_RoadManager.StoreLoadedInRoads(key, roadBuilding, roadType);
    }
    public void OneRoadAround(Vector2Int key, bool[] directionChecks, int loop) //only one road around the latest road placed
    {
        Vector2Int   offset          = Vector2Int.zero;
        RoadTypeList currentRoadType = RoadTypeList.NO_CONNECTION; //the dir for the road player is placing

        //get the offset on where the other road is
        if (directionChecks[(int)Direction.UP])
        {
            offset          = UP_VECTOR;
            currentRoadType = RoadTypeList.U_ONLY_CONNECTION;
        }
        else if (directionChecks[(int)Direction.DOWN]) //other road on its left
        {
            offset          = DOWN_VECTOR;
            currentRoadType = RoadTypeList.D_ONLY_CONNECTION;
        }
        else if (directionChecks[(int)Direction.RIGHT]) //other road is on its right
        {
            offset          = RIGHT_VECTOR;
            currentRoadType = RoadTypeList.R_ONLY_CONNECTION;
        }
        else
        {
            offset          = LEFT_VECTOR;
            currentRoadType = RoadTypeList.L_ONLY_CONNECTION;
        }

        //change the current one
        SetRoadSprite(key, currentRoadType);

        //check and change the surrounding roads accordingly
        CheckAndChangeRoadDirection(new Vector2Int(key.x + offset.x, key.y + offset.y), loop + 1);

        //TODO, IF NO CHANGE THAT MEANS EVERYTHING OKAY, EARLY RETURN DONT NEED RECRUSIVE

        return;
    }
    public void ThreeRoadsDiagonal(Vector2Int key, ref RoadTypeList currentRoadType, int loop)
    {
        Vector2Int diagonalOffset = Vector2Int.zero;

        //check possible combination of diagonals and see if theres any roads there
        switch (currentRoadType)
        {
        case RoadTypeList.U_D_L_ONLY_CONNECTION:
        {
            bool bottomLeft, topLeft;
            bottomLeft = topLeft = false;

            diagonalOffset = new Vector2Int(-1, -1);     //check bottom left first
            if (CheckMapAvailability(diagonalOffset + key))
            {
                currentRoadType = RoadTypeList.U_D_L_DIA_BL_ONLY_CONNECTION;
                bottomLeft      = true;
                CheckAndChangeRoadDirection(key + diagonalOffset, loop + 1);
            }

            diagonalOffset = new Vector2Int(-1, 1);     //check top left
            if (CheckMapAvailability(diagonalOffset + key))
            {
                currentRoadType = RoadTypeList.U_D_L_DIA_TL_ONLY_CONNECTION;
                topLeft         = true;
                CheckAndChangeRoadDirection(key + diagonalOffset, loop + 1);
            }

            if (topLeft && bottomLeft)     //if diagonally both sides have
            {
                currentRoadType = RoadTypeList.U_D_L_DIA_BL_TL_ONLY_CONNECTION;
            }
        }
        break;

        case RoadTypeList.U_D_R_ONLY_CONNECTION:
        {
            bool bottomRight, topRight;
            bottomRight = topRight = false;

            diagonalOffset = new Vector2Int(1, -1);     //check bottom right first
            if (CheckMapAvailability(diagonalOffset + key))
            {
                currentRoadType = RoadTypeList.U_D_R_DIA_BR_ONLY_CONNECTION;
                bottomRight     = true;
                CheckAndChangeRoadDirection(key + diagonalOffset, loop + 1);
            }

            diagonalOffset = new Vector2Int(1, 1);     //check top right
            if (CheckMapAvailability(diagonalOffset + key))
            {
                currentRoadType = RoadTypeList.U_D_R_DIA_TR_ONLY_CONNECTION;
                topRight        = true;
                CheckAndChangeRoadDirection(key + diagonalOffset, loop + 1);
            }

            if (topRight && bottomRight)     //if diagonally both sides have
            {
                currentRoadType = RoadTypeList.U_D_R_DIA_BR_TR_ONLY_CONNECTION;
            }
        }
        break;

        case RoadTypeList.U_R_L_ONLY_CONNECTION:
        {
            bool topLeft, topRight;
            topLeft = topRight = false;

            diagonalOffset = new Vector2Int(1, 1);     //check top right first
            if (CheckMapAvailability(diagonalOffset + key))
            {
                currentRoadType = RoadTypeList.U_R_L_DIA_TR_ONLY_CONNECTION;
                topRight        = true;
                CheckAndChangeRoadDirection(key + diagonalOffset, loop + 1);
            }

            diagonalOffset = new Vector2Int(-1, 1);     //check top left
            if (CheckMapAvailability(diagonalOffset + key))
            {
                currentRoadType = RoadTypeList.U_R_L_DIA_TL_ONLY_CONNECTION;
                topLeft         = true;
                CheckAndChangeRoadDirection(key + diagonalOffset, loop + 1);
            }

            if (topRight && topLeft)     //if diagonally both sides have
            {
                currentRoadType = RoadTypeList.U_R_L_DIA_TR_TL_ONLY_CONNECTION;
            }
        }
        break;

        case RoadTypeList.D_R_L_ONLY_CONNECTION:
        {
            bool bottomLeft, bottomRight;
            bottomLeft = bottomRight = false;

            diagonalOffset = new Vector2Int(1, -1);     //check bottom right first
            if (CheckMapAvailability(diagonalOffset + key))
            {
                currentRoadType = RoadTypeList.D_R_L_DIA_BR_ONLY_CONNECTION;
                bottomRight     = true;
                CheckAndChangeRoadDirection(key + diagonalOffset, loop + 1);     //change the corners accordingly
            }

            diagonalOffset = new Vector2Int(-1, -1);     //check bottom left
            if (CheckMapAvailability(diagonalOffset + key))
            {
                currentRoadType = RoadTypeList.D_R_L_DIA_BL_ONLY_CONNECTION;
                bottomLeft      = true;
                CheckAndChangeRoadDirection(key + diagonalOffset, loop + 1);
            }

            if (bottomLeft && bottomRight)     //if diagonally both sides have
            {
                currentRoadType = RoadTypeList.D_R_L_DIA_BR_BL_ONLY_CONNECTION;
            }
        }
        break;
        }
    }
    public void TwoRoadsAround(Vector2Int key, bool[] directionChecks, int loop)
    {
        RoadTypeList currentRoadType = RoadTypeList.NO_CONNECTION;
        bool         diagonal        = false;

        Vector2Int [] offsets = new Vector2Int[3];
        for (int i = 0; i < 3; ++i)
        {
            offsets[i] = Vector2Int.zero;
        }

        if (directionChecks[(int)Direction.UP])
        {
            offsets[0] = UP_VECTOR;

            if (directionChecks[(int)Direction.DOWN]) //one above, other below
            {
                currentRoadType = RoadTypeList.U_D_ONLY_CONNECTION;
                offsets[1]      = DOWN_VECTOR;
            }
            else if (directionChecks[(int)Direction.RIGHT])
            {
                offsets[1]      = RIGHT_VECTOR;
                currentRoadType = RoadTypeList.U_R_ONLY_CONNECTION;
            }
            else if (directionChecks[(int)Direction.LEFT])
            {
                currentRoadType = RoadTypeList.U_L_ONLY_CONNECTION;
                offsets[1]      = LEFT_VECTOR;
            }
        }
        else if (directionChecks[(int)Direction.DOWN])
        {
            offsets[0] = DOWN_VECTOR;

            if (directionChecks[(int)Direction.RIGHT])
            {
                currentRoadType = RoadTypeList.D_R_ONLY_CONNECTION;
                offsets[1]      = RIGHT_VECTOR;
            }
            else if (directionChecks[(int)Direction.LEFT])
            {
                currentRoadType = RoadTypeList.D_L_ONLY_CONNECTION;
                offsets[1]      = LEFT_VECTOR;
            }
        }
        else //assume one on the right, other on the left
        {
            currentRoadType = RoadTypeList.R_L_ONLY_CONNECTION;
            offsets[0]      = RIGHT_VECTOR;
            offsets[1]      = LEFT_VECTOR;
        }

        //check if theres anything diagonal of the road
        offsets[2] = offsets[0] + offsets[1];
        if (CheckMapAvailability(key + offsets[2]) && offsets[2] != Vector2Int.zero)
        {
            diagonal        = true;
            currentRoadType = currentRoadType + (RoadTypeList.U_R_DIA_ONLY_CONNECTION - RoadTypeList.U_R_ONLY_CONNECTION);
        }

        //change the current one
        SetRoadSprite(key, currentRoadType);

        CheckAndChangeRoadDirection(new Vector2Int(key.x + offsets[0].x, key.y + offsets[0].y), loop + 1);
        CheckAndChangeRoadDirection(new Vector2Int(key.x + offsets[1].x, key.y + offsets[1].y), loop + 1);

        if (diagonal)
        {
            CheckAndChangeRoadDirection(new Vector2Int(key.x + offsets[2].x, key.y + offsets[2].y), loop + 1);
        }
    }