Exemplo n.º 1
0
    public bool CellHasLowerStudConnection(LXFMLCell cell, int brickId)
    {
        bool      hasStud     = true;
        BrickData brickData   = _bricks[brickId];
        int       brickHeight = LXFMLHelper.GetBrickHeight(brickData.design.id);
        Vector2   brickOrigin = GetBrickOrigin(brickId);

        if (brickHeight == 1)
        {
            if (LXFMLHelper.IsSlopeUp(brickData.design.id))
            {
                if (!MathUtils.NearEqual(brickOrigin, cell.Coordinates))
                {
                    hasStud = false;
                }
            }
        }
        else
        {
            if (!MathUtils.NearEqual(brickOrigin.y, cell.Coordinates.y))
            {
                hasStud = false;
            }
            else
            {
                if (LXFMLHelper.IsSlopeUp(brickData.design.id))
                {
                    if (!MathUtils.NearEqual(brickOrigin.x, cell.Coordinates.x))
                    {
                        hasStud = false;
                    }
                }
            }
        }
        return(hasStud);
    }
Exemplo n.º 2
0
    private void SetWall(string containerName, List <BrickData> edgeBricks, WallPosition position)
    {
        //Create transform to hold the bricks
        var container = new GameObject(containerName).transform;

        container.localScale = new Vector3(1f, 1f, 1f);

        List <LXFMLCell> cellsForDeco = new List <LXFMLCell>();

        //FILTER ALL EDGE BRICKS TO RETRIEVE ELIGIBLE CELLS FOR DECO HOOK ON LOWER PART
        for (int i = 0; i < edgeBricks.Count - 1; i++)
        {
            Vector2 brickOrigin = construction.GetBrickOrigin(edgeBricks[i].id);

            if (LXFMLHelper.IsBrickFlipped(edgeBricks[i]))
            {
                for (int j = (int)brickOrigin.x - LXFMLHelper.GetBrickWidth(edgeBricks[i].design.id) + 1; j < (int)brickOrigin.x; j++)
                {
//					Debug.Log (j);
                    if (!LXFMLHelper.IsSlopeUp(edgeBricks[i].design.id) || j == (int)brickOrigin.x)
                    {
                        cellsForDeco.Add(construction.Grid.GetCellAt((float)j, brickOrigin.y));
                    }
                }
            }
            else
            {
                for (int j = (int)brickOrigin.x; j < (int)brickOrigin.x + LXFMLHelper.GetBrickWidth(edgeBricks[i].design.id); j++)
                {
//					Debug.Log (j);
                    if (!LXFMLHelper.IsSlopeUp(edgeBricks[i].design.id) || j == (int)brickOrigin.x)
                    {
                        cellsForDeco.Add(construction.Grid.GetCellAt((float)j, brickOrigin.y));
                    }
                }
            }
        }

        //FOR EACH ELIGIBLE CELL, FIND BOTTOM CELLS TO EXCLUDE FROM EXTRUSION

        Dictionary <LXFMLCell, List <LXFMLCell> > bricksToRemove       = new Dictionary <LXFMLCell, List <LXFMLCell> >();
        Dictionary <LXFMLCell, List <int> >       availableDecoHeights = new Dictionary <LXFMLCell, List <int> >();

        for (int i = 0; i < cellsForDeco.Count; i++)
        {
//			Debug.Log ("Testing " + cellsForDeco[i].Coordinates.ToString());
            int indexX = (int)cellsForDeco[i].Coordinates.x;
            int indexY = (int)cellsForDeco[i].Coordinates.y;

            bricksToRemove.Add(cellsForDeco[i], new List <LXFMLCell>());
            availableDecoHeights.Add(cellsForDeco[i], new List <int>());

            //if cell is first row, discard
            if (indexY == 0)
            {
                break;
            }

            //Stop lookup at index 1 or maxSize
            int endIndex = indexY - _decoMaxHeight < 0 ? 0 : indexY - _decoMaxHeight;

            int j = indexY - 1;

            for (j = indexY - 1; j >= endIndex; j--)
            {
                LXFMLCell cell = construction.Grid.GetCellAt(indexX, j);
                bricksToRemove[cellsForDeco[i]].Add(cell);
            }

            for (j = 0; j < bricksToRemove[cellsForDeco[i]].Count; j++)
            {
                LXFMLCell currentCell = bricksToRemove[cellsForDeco[i]][j];
                if (j > _decoMinHeight - 1)
                {
                    //DISCARD IF CELL HAS NO STUD TO HOOK THE DECO
                    if (currentCell.Data != null && currentCell.Data.Brick != null && edgeBricks.Contains(currentCell.Data.Brick))
                    {
                        BrickData currentBrick = currentCell.Data.Brick;
                        int       h            = LXFMLHelper.GetBrickHeight(currentBrick.design.id);
                        if (construction.CellHasUpperStudConnection(currentCell, currentBrick.id))
                        {
                            availableDecoHeights[cellsForDeco[i]].Add(j);
                        }
                    }
                }
            }
        }

        Dictionary <int, int> xIndexComparer = new Dictionary <int, int>();

        //RETRIEVE LEFT/RIGHT MOST AVAILABLE CELL FOR DECO
        foreach (KeyValuePair <LXFMLCell, List <int> > kvp in availableDecoHeights)
        {
            if (kvp.Value.Count == 0)
            {
                bricksToRemove.Remove(kvp.Key);
            }
            else
            {
                if (kvp.Key.Data != null && kvp.Key.Data.Brick != null)
                {
                    int id = kvp.Key.Data.Brick.id;
                    if (!xIndexComparer.ContainsKey(id))
                    {
                        if (position == WallPosition.Left)
                        {
                            xIndexComparer.Add(id, int.MaxValue);
                        }
                        else
                        {
                            xIndexComparer.Add(id, int.MinValue);
                        }
                    }
                    int minY = -1;
                    if (position == WallPosition.Left)
                    {
                        minY = Mathf.Min(xIndexComparer[id], (int)kvp.Key.Coordinates.x);
                    }
                    else
                    {
                        minY = Mathf.Max(xIndexComparer[id], (int)kvp.Key.Coordinates.x);
                    }
                    xIndexComparer[id] = minY;

                    for (int k = 0; k < kvp.Value.Count; k++)
                    {
                        Debug.Log("Cell " + kvp.Key.Coordinates.ToString() + " can have " + kvp.Value[k].ToString() + " height deco");
                    }
                }
            }
        }

        Dictionary <LXFMLCell, List <LXFMLCell> > toKeep = new Dictionary <LXFMLCell, List <LXFMLCell> >();

        //TRIM CELLS WHEN NOT LEFTMOST OF BRICK
        foreach (KeyValuePair <LXFMLCell, List <LXFMLCell> > kvp in bricksToRemove)
        {
            if (kvp.Key.Data != null && kvp.Key.Data.Brick != null)
            {
                int comparer = kvp.Key.Data.Brick.id;
                foreach (KeyValuePair <int, int> xIndices in xIndexComparer)
                {
                    if (comparer == xIndices.Key)
                    {
                        if (xIndices.Value == (int)kvp.Key.Coordinates.x)
                        {
                            toKeep.Add(kvp.Key, kvp.Value);
                        }
                    }
                }
            }
        }


        //PICK BEST MATCH FOR DECO
        if (toKeep.Count > 0)
        {
            Debug.Log("Deco avail " + toKeep.Count.ToString());
            //ONLY SUPPORT 1 DECO PER WALL, NEED TO SUPPORT MORE H & V
            int midIndex;
            if (toKeep.Count == 1)
            {
                midIndex = 0;
            }
            else
            {
                midIndex = Mathf.RoundToInt((float)toKeep.Count / 2f) - 1;
            }

            int brickId = 0;

            LXFMLCell cellForDeco = null;

            int index = 0;

            foreach (KeyValuePair <LXFMLCell, List <LXFMLCell> > kvp in toKeep)
            {
                if (index == midIndex)
                {
                    brickId     = kvp.Key.Data.Brick.id;
                    cellForDeco = kvp.Key;
                }
                index++;
            }

            if (cellForDeco != null)
            {
                GameObject deco = ConstructionController.Instance.resourcesProvider.GetDecorationForHeight(availableDecoHeights[cellForDeco], LXFMLDecoration.DecoPosition.Wall);

                if (deco != null)
                {
                    var decoWidth  = deco.GetComponent <LXFMLDecoration>().width;
                    var decoHeight = deco.GetComponent <LXFMLDecoration>().height;
                    int start      = ((_extrusionSize - (int)decoWidth) / 2);
                    int end        = start + (int)decoWidth - 1;

                    for (int i = 0; i < edgeBricks.Count; i++)
                    {
                        float     posZ    = +LXFMLHelper.kBrickSize.x;
                        Transform brickT  = _bricksLookup[edgeBricks[i].id];
                        Vector2   gridPos = construction.GetBrickOrigin(edgeBricks[i].id);
                        for (int j = 0; j < _extrusionSize; j++)
                        {
                            if ((j < start || j > end) || (gridPos.y >= cellForDeco.Coordinates.y || gridPos.y < cellForDeco.Coordinates.y - (decoHeight)))
                            {
                                GameObject copy = Instantiate(brickT.gameObject);
                                copy.transform.localPosition = brickT.localPosition;
                                copy.transform.localRotation = brickT.localRotation;
                                copy.transform.SetParent(container, false);
                                copy.transform.Translate(0f, 0f, posZ, Space.World);
                                extrudedBricksBySize[j].Add(copy);
                            }
                            posZ += LXFMLHelper.kBrickSize.x;
                        }
                    }
                    //construction starting position
                    Vector3 pivotPoint  = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
                    int     offsetIndex = int.MaxValue;

                    for (int i = 0; i < _bricks.Count; i++)
                    {
                        pivotPoint.x = Mathf.Min(pivotPoint.x, _bricks[i].localPosition.x);
                        pivotPoint.y = Mathf.Min(pivotPoint.y, _bricks[i].localPosition.y);
                        pivotPoint.z = Mathf.Min(pivotPoint.z, _bricks[i].localPosition.z);
                        offsetIndex  = Mathf.Min(offsetIndex, (int)_bricks[i].GetComponent <LegoBrickId>().coords.x);
                    }


                    GameObject decoInstance = Instantiate(deco);
                    decoInstance.transform.SetParent(container);

                    var pos = pivotPoint;
                    pos.x += ((cellForDeco.Coordinates.x - (float)offsetIndex) * LXFMLHelper.kBrickSize.x);
                    pos.y += (cellForDeco.Coordinates.y * LXFMLHelper.kBrickSize.y) - (decoHeight * LXFMLHelper.kBrickSize.y);
                    pos.z  = position == WallPosition.Right ? ((float)start + 1f) * LXFMLHelper.kBrickSize.x : ((float)start + decoWidth) * LXFMLHelper.kBrickSize.x;
                    pos.z += pivotPoint.z;


                    decoInstance.transform.localPosition = pos;
                    float rotY = position == WallPosition.Left ? 90f : 270f;
                    decoInstance.transform.Rotate(Vector3.up, rotY);

                    buildingDecorations.Add(decoInstance);
                }
                else
                {
                    ExtrudeNoDeco(edgeBricks, container);
                }
            }
        }
        else
        {
            ExtrudeNoDeco(edgeBricks, container);
        }
        container.SetParent(transform, false);
    }