public List <LXFMLCell> GetBrickCells(int brickId)
    {
        List <LXFMLCell> cells     = new List <LXFMLCell>();
        BrickData        brickData = _bricks[brickId];
        int     brickWidth         = LXFMLHelper.GetBrickWidth(brickData.design.id);
        int     brickHeight        = LXFMLHelper.GetBrickHeight(brickData.design.id);
        Vector2 brickOrigin        = GetBrickOrigin(brickId);

        int originX = !LXFMLHelper.IsBrickFlipped(brickData) ? (int)brickOrigin.x : (int)brickOrigin.x - brickWidth + 1;
        int originY = (int)brickOrigin.y;

//		Debug.Log ("brickId = " + brickId.ToString());
//		Debug.Log (brickOrigin);
//		Debug.Log (originX);

//		Debug.Log("***");

        for (int i = originX; i < originX + brickWidth; i++)
        {
            for (int j = originY; j < originY + brickHeight; j++)
            {
//				Debug.Log (i.ToString() + " - " + j.ToString());
                cells.Add(_grid.GetCellAt(i, j));
            }
        }
//		Debug.Log("-------------------------");
        return(cells);
    }
Example #2
0
    void CreateNewBrickData()
    {
        BrickData brickData = new BrickData();

        brickData.design = new BrickDesignData();

        brickData.id            = _builder.construction.GetNextBrickId();
        brickData.materialId    = LXFMLHelper.GetColors(draggingScenePart.designId)[0];
        brickData.design.id     = draggingScenePart.designId;
        brickData.design.width  = LXFMLHelper.GetBrickWidth(draggingScenePart.designId);
        brickData.design.height = LXFMLHelper.GetBrickHeight(draggingScenePart.designId);
        brickData.design.type   = LXFMLHelper.GetBrickType(draggingScenePart.designId);

        Vector3 position = draggingScenePart.transform.position;

        //WEIRD HACK...I HAVE TO OFFSET BY HALF A BRICK TO GET THE CORRECT POSITION
        position.z -= LXFMLHelper.kBrickSize.x / 2f;

        brickData.position = position;
        brickData.scale    = Vector3.one;
        brickData.rotation = Quaternion.Euler(0f, -draggingScenePart.transform.rotation.eulerAngles.y, 0f);

        brickData.isFlipped = LXFMLHelper.IsBrickFlipped(brickData);

        _builder.AddBrick(_bricks[-1].transform, brickData);

        draggingScenePart.id = brickData.id;



        _bricks.Remove(-1);

        if (_bricks.ContainsKey(draggingScenePart.id))
        {
            _bricks.Remove(draggingScenePart.id);
        }

        _bricks.Add(draggingScenePart.id, draggingScenePart);
    }
Example #3
0
    public static LXFMLConstruction ParseConstruction(string lxfmlData, ConstructionType pType)
    {
        var construction = GetConstructionByType(pType);

        XmlDocument xmlDoc = new XmlDocument();

        xmlDoc.LoadXml(lxfmlData);

        XmlNodeList nodes = xmlDoc.SelectNodes("LXFML/Bricks/Brick/Part");
        BrickData   brick;

        float minX = float.MaxValue;
        float minY = float.MaxValue;

        List <BrickData> list_tmp = new List <BrickData>();

        foreach (XmlElement node in nodes)
        {
            brick        = new BrickData();
            brick.design = new BrickDesignData();

            brick.id            = int.Parse(node.GetAttribute(kTokenBrickId));
            brick.materialId    = int.Parse(node.GetAttribute(kTokenMaterials).Split(',')[0]);
            brick.design.id     = int.Parse(node.GetAttribute(kTokenDesign));
            brick.design.width  = LXFMLHelper.GetBrickWidth(brick.design.id);
            brick.design.height = LXFMLHelper.GetBrickHeight(brick.design.id);
            brick.design.type   = LXFMLHelper.GetBrickType(brick.design.id);

            Matrix4x4 matrix = LXFMLHelper.GetBrickMatrix((node.FirstChild as XmlElement).GetAttribute(kTokenBrickMatrix));
            brick.position = LXFMLHelper.GetBrickPosition(matrix);

            minX = Mathf.Min(minX, brick.position.x);
            minY = Mathf.Min(minY, brick.position.y);

            brick.scale    = LXFMLHelper.GetBrickScale(matrix);
            brick.rotation = LXFMLHelper.GetBrickRotation(matrix);

            brick.isFlipped = LXFMLHelper.IsBrickFlipped(brick);

            list_tmp.Add(brick);
        }

        if (minX > 0)
        {
            minX = 0;
        }

        if (minY > 0)
        {
            minY = 0;
        }

        //Offset position
        foreach (BrickData b in list_tmp)
        {
//			try
//			{
            var pos = b.position;
            pos.x     -= minX;
            pos.y     -= minY;
            b.position = pos;
            construction.AddBrick(b);
//			}
//			catch(Exception e)
//			{
//				UnityEngine.Debug.LogError(b.position + " " + minX + " " + minY);
//			}
        }

        return(construction);
    }
Example #4
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);
    }