protected void DumpLine(LXFMLGrid grid, int row) { var s = new StringBuilder(); s.Append("<size=22><b>"); for (int x = 0; x < grid.Width; ++x) { var cell = grid.GetCellAt(x, row); if (cell.IsEmpty) { s.Append("<color=grey>[O]</color>"); } else { Color32 color = (Color32)LXFMLHelper.GetBrickColor(cell.Data.Brick.materialId); var colorString = color.r.ToString("X2") + color.g.ToString("X2") + color.b.ToString("X2"); var str = string.Format("<color=#{0}>[O]</color>", colorString); s.Append(str); } } s.Append("</b></size>"); Debug.Log(s.ToString()); }
void UpdateAvailableColors(LegoBrickId selected) { if (selected) { _modelUI.UpdateColorsList(LXFMLHelper.GetColors(selected.designId), _builder.construction.GetBrick(selected.id).materialId); } }
void UpdateAvailableParts(LegoBrickId selected) { List <int> list = new List <int> (); int selectedDesignId = -1; if (selected) { for (int i = 0; i < LXFMLHelper.brickSet.Length; i++) { if (LXFMLHelper.GetBrickHeight(LXFMLHelper.brickSet[i]) <= LXFMLHelper.GetBrickHeight(selected.designId) && Mathf.Max(LXFMLHelper.UpperSlots(LXFMLHelper.brickSet[i]), LXFMLHelper.LowerSlots(LXFMLHelper.brickSet[i])) <= Mathf.Max(LXFMLHelper.UpperSlots(selected.designId), LXFMLHelper.LowerSlots(selected.designId))) { list.Add(LXFMLHelper.brickSet[i]); } } selectedDesignId = selected.designId; } else { for (int i = 0; i < LXFMLHelper.brickSet.Length; i++) { list.Add(LXFMLHelper.brickSet[i]); } } _modelUI.UpdatePartsList(list, selected != null, selectedDesignId); }
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); }
public void UpdateColorsList(int[] colors, int selectedColor) { foreach (Transform tr in colorsListUI) { tr.gameObject.GetComponent <Button>().onClick.RemoveAllListeners(); Destroy(tr.gameObject); } for (int i = 0; i < colors.Length; i++) { GameObject btn = Instantiate(colorButtonPrefab) as GameObject; Image img = btn.transform.GetChild(0).GetComponent <Image> (); img.color = LXFMLHelper.GetBrickColor(colors[i]); if (img.color.r + img.color.g + img.color.b < 0.1f) { img.color += new Color(.1f, .1f, .1f, 1); } btn.name = colors[i].ToString(); btn.transform.SetParent(colorsListUI, false); int matId = colors[i]; var button = btn.GetComponent <Button>(); button.onClick.AddListener(() => { _modelEditor.UpdateColorPart(matId); }); ColorBlock colorBlock = button.colors; colorBlock.pressedColor = LXFMLHelper.GetBrickColor(colors[i]); if (colors[i] == selectedColor) { button.interactable = false; colorBlock.disabledColor = LXFMLHelper.GetBrickColor(colors[i]); } button.colors = colorBlock; } }
public IEnumerator AnimateBrickScanRandom() { _numBricksToAnimate = _bricks.Count; _bricks.Sort(SortByYAscending); for (int i = 0; i < _bricks.Count; ++i) { _bricks[i].gameObject.SetLayerRecursively(8); _bricks[i].gameObject.SetActive(false); } for (int i = 0; i < _bricks.Count; ++i) { yield return(new WaitForEndOfFrame()); yield return(new WaitForEndOfFrame()); _bricks[i].gameObject.SetActive(true); LegoBrickId legoBrick = _bricks[i].GetComponent <LegoBrickId>(); BrickData bd = construction.GetBrick(legoBrick.id); int[] availColors = LXFMLHelper.GetColors(legoBrick.designId); int numIterations = Random.Range(15, 20); StartCoroutine(AnimateBrickColors(LXFMLHelper.GetColors(legoBrick.designId), bd.materialId, _bricks[i].gameObject, numIterations)); } yield return(StartCoroutine("WaitForAnimComplete")); }
void UnselectDragBrick() { if (draggingScenePart) { if (_validPosition) { if (_dragCellData != null) { 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; _dragCellData.Brick.position = position; _builder.construction.AddBrick(_dragCellData.Brick); } else { CreateNewBrickData(); } _modelUI.UpdateColorsList(LXFMLHelper.GetColors(draggingScenePart.designId), _builder.construction.GetBrick(draggingScenePart.id).materialId); _dragCellData = null; _bricks[draggingScenePart.id].transform.parent = constructionTarget; draggingScenePart = null; _newBrickCreated = false; } else { StartCoroutine(TweenInvalidBrick(_bricks[draggingScenePart.id].transform)); draggingScenePart = null; _bricks.Remove(-1); } } }
public void StartDragPart(int designId, bool reverse, Vector2 touchPos) { if (!_isTweening) { GameObject newBrick = Instantiate(ConstructionController.Instance.resourcesProvider.GetPrefabForDesign(designId)); newBrick.transform.SetParent(constructionTarget); LegoBrickId newOne = newBrick.GetComponent <LegoBrickId>(); newOne.id = -1; Vector2 gridPos = screenPointToGridPosition((Vector3)touchPos); newOne.transform.position = new Vector3(0, gridPos.y * LXFMLHelper.kBrickSize.y, gridPos.x * LXFMLHelper.kBrickSize.x); _originalPos = new Vector3(gridPos.x * LXFMLHelper.kBrickSize.x, gridPos.y * LXFMLHelper.kBrickSize.y, 0f); float add = reverse?180:0; if (LXFMLHelper.GetBrickOrientation(newOne.designId) == LXFMLHelper.BrickOrientation.NegativeX) { newOne.transform.localEulerAngles = new Vector3(0, 0f + add, 0); } else if (LXFMLHelper.GetBrickOrientation(newOne.designId) == LXFMLHelper.BrickOrientation.PositiveX) { newOne.transform.localEulerAngles = new Vector3(0, 180f + add, 0); } else if (LXFMLHelper.GetBrickOrientation(newOne.designId) == LXFMLHelper.BrickOrientation.NegativeZ) { newOne.transform.localEulerAngles = new Vector3(0, 90f + add, 0); } else if (LXFMLHelper.GetBrickOrientation(newOne.designId) == LXFMLHelper.BrickOrientation.PositiveZ) { newOne.transform.localEulerAngles = new Vector3(0, 270f + add, 0); } var renderer = newBrick.GetComponentInChildren <MeshRenderer>(); renderer.sharedMaterial = ConstructionController.Instance.resourcesProvider.GetMaterial(LXFMLHelper.GetColors(designId)[0]); selectedPart = draggingScenePart = newOne; _cellOffset.x = reverse ? LXFMLHelper.GetBrickWidth(newOne.designId) - 1 : 0; _cellOffset.y = 0; _startDragCell.x = 0f; _startDragCell.y = 0f; _startDragPosition = Vector3.zero; _newBrickCreated = true; if (_bricks.ContainsKey(-1)) { _bricks.Remove(-1); } _bricks.Add(-1, newOne); } }
void RecurseNeighbourPowerBricks(BrickData brick, List <BrickData> visited) { if (visited.Contains(brick) || !LXFMLHelper.IsPowerBrick(brick.design.id, brick.materialId)) { return; } else { visited.Add(brick); } foreach (BrickData connected in brick.NeighbourBricks) { RecurseNeighbourPowerBricks(connected, visited); } }
public void Render(BrickData brick) { Reset(); if (brick.design.type == BrickType.Normal) { RenderNormal(brick); } else { RenderSlope(brick); } _fullImage.color = LXFMLHelper.GetBrickColor(brick.materialId); _slopeImage.color = LXFMLHelper.GetBrickColor(brick.materialId); }
// public GameObject GetDecorationForHeight(float height){ // List<GameObject> decos = new List<GameObject>(); // for(int i = 0 ; i < _decorations.Length ; i++){ // LXFMLDecoration deco = _decorations[i].GetComponent<LXFMLDecoration>(); // if(deco.height <= height ){ // decos.Add(_decorations[i]); // } // } // if(decos.Count == 0) // return null; // return decos[Random.Range(0,decos.Count)]; // } public List <int> GetAvailableBricksForFootprint(Vector4 footprint, int pDesignID) { List <int> availBricks = new List <int>(); int width = (int)(footprint.x + footprint.y) + 1; int height = (int)(footprint.z + footprint.w) + 1; foreach (KeyValuePair <int, GameObject> brick in _prefabsLookup) { int designId = brick.Key; if (LXFMLHelper.GetBrickHeight(designId) <= height && LXFMLHelper.GetBrickWidth(designId) <= width && designId != pDesignID && !(LXFMLHelper.IsSpecial2By1Brick(designId)) && !(LXFMLHelper.Is2By1Brick(designId) && LXFMLHelper.Is2By1Brick(pDesignID))) { availBricks.Add(brick.Key); } } return(availBricks); }
void Test() { var brick = new BrickData(); brick.design = new BrickDesignData(); brick.id = -1; brick.materialId = 102; brick.design.id = 60481; brick.design.width = LXFMLHelper.GetBrickWidth(brick.design.id); brick.design.height = LXFMLHelper.GetBrickHeight(brick.design.id); brick.design.type = LXFMLHelper.GetBrickType(brick.design.id); brick.position = Vector3.zero; brick.scale = Vector3.one; brick.rotation = Quaternion.identity; brick.isFlipped = true; Render(brick); }
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); }
public bool CellHasUpperStudConnection(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.IsSlopeDown(brickData.design.id)) { if (!MathUtils.NearEqual(brickOrigin, cell.Coordinates)) { hasStud = false; } } } else { float upperY = cell.Coordinates.y + brickHeight - 1; if (!MathUtils.NearEqual(upperY, cell.Coordinates.y)) { hasStud = false; } else { if (LXFMLHelper.IsSlopeDown(brickData.design.id)) { if (!MathUtils.NearEqual(cell.Coordinates.x, cell.Coordinates.x)) { hasStud = false; } } } } return(hasStud); }
public void UpdatePartsList(List <int> list, bool showRemove, int selectedDesignId) { removeBrickButton.interactable = showRemove; foreach (Transform tr in partsListUI) { Destroy(tr.gameObject); } for (int i = 0; i < list.Count; i++) { if (list[i] != selectedDesignId) { GameObject btn = Instantiate(partButtonPrefab) as GameObject; Image img = btn.transform.GetChild(0).GetComponent <Image> (); btn.name = list [i].ToString(); img.sprite = Resources.Load <Sprite> ("BrickTextures/" + list [i].ToString()); BrickPartDragHandler dragHandler = btn.GetComponent <BrickPartDragHandler>(); dragHandler.designId = list[i]; dragHandler.reversedPart = false; btn.transform.SetParent(partsListUI, false); } if (LXFMLHelper.IsSlope(list[i]) || LXFMLHelper.IsCurved(list[i])) { GameObject btn_reverse = Instantiate(partButtonPrefab) as GameObject; Image img_reverse = btn_reverse.transform.GetChild(0).GetComponent <Image> (); btn_reverse.name = list [i].ToString() + "_reverse"; img_reverse.sprite = Resources.Load <Sprite> ("BrickTextures/" + list [i].ToString()); img_reverse.transform.localScale = new Vector3(-1, 1, 1); BrickPartDragHandler dragHandler_reverse = btn_reverse.GetComponent <BrickPartDragHandler>(); dragHandler_reverse.reversedPart = true; dragHandler_reverse.designId = list[i]; btn_reverse.transform.SetParent(partsListUI, false); } } }
void TestValidPosition(Vector2 gridPos) { int width = LXFMLHelper.GetBrickWidth(draggingScenePart.designId); int heigth = LXFMLHelper.GetBrickHeight(draggingScenePart.designId); int xStartTest = Mathf.RoundToInt(gridPos.x - _cellOffset.x); int xEndTest = Mathf.RoundToInt(gridPos.x - _cellOffset.x + width); int yStartTest = Mathf.RoundToInt(gridPos.y - _cellOffset.y); int yEndTest = Mathf.RoundToInt(gridPos.y - _cellOffset.y + heigth); _validPosition = true; //TEST GRID LIMITS if (xStartTest < 0 || xEndTest > LXFMLHelper.kGridSize || yStartTest < 0 || yEndTest > LXFMLHelper.kGridSize) { _validPosition = false; } //TEST OCCUPIED CELLS if (_validPosition) { for (int i = xStartTest; i < xEndTest; i++) { for (int j = yStartTest; j < yEndTest; j++) { LXFMLCell cell = _builder.construction.Grid.GetCellAt(i, j); if (cell != null && cell.Data != null) { _validPosition = false; break; } } } } //TEST FLOATING BRICK if (_validPosition) { int numContacts = 0; for (int i = xStartTest; i < xEndTest; i++) { LXFMLCell cellBottom = _builder.construction.Grid.GetCellAt(i, yStartTest - 1); if (cellBottom != null && cellBottom.Data != null) { if (cellBottom.Data.Brick.design.type == BrickType.Normal || cellBottom.Data.Brick.design.type == BrickType.SlopeUp || cellBottom.Data.Brick.design.type == BrickType.CurveIn) { numContacts++; } else { if (cellBottom.Data.IsOrigin) { numContacts++; } else { if (i == _builder.construction.GetBrickOrigin(cellBottom.Data.Brick.id).x) { numContacts++; } } } } LXFMLCell cellTop = _builder.construction.Grid.GetCellAt(i, yEndTest); if (cellTop != null && cellTop.Data != null) { if (cellTop.Data.Brick.design.type == BrickType.Normal || cellTop.Data.Brick.design.type == BrickType.SlopeDown || cellTop.Data.Brick.design.type == BrickType.CurveOut) { numContacts++; } else { if (cellTop.Data.IsOrigin) { numContacts++; } } } } _validPosition = numContacts != 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); }
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); }
void AddLasers(List <BrickData> bricksList, int direction) { for (int i = 0; i < bricksList.Count; i++) { if (LXFMLHelper.IsPowerBrick(bricksList[i].design.id, bricksList[i].materialId)) { //Check upper and lower bricks int numUpperSlots = LXFMLHelper.UpperSlots(bricksList[i].design.id); int numLowerSlots = LXFMLHelper.LowerSlots(bricksList[i].design.id); Vector2 brickOrigin = GetBrickOrigin(bricksList[i].id); int inc = 0; int j = Mathf.RoundToInt(brickOrigin.x); int numUpperBricks = 0; while (inc < numUpperSlots) { LXFMLCell upperCell = _grid.GetCellAt(j, (int)brickOrigin.y + bricksList[i].design.height); if (upperCell != null && !upperCell.IsEmpty) { if (upperCell.Data.Brick.design.type == BrickType.Normal || upperCell.Data.Brick.design.type == BrickType.SlopeDown || upperCell.Data.Brick.design.type == BrickType.CurveOut) { numUpperBricks++; } else { if (upperCell.Data.IsOrigin) { numUpperBricks++; } } } j += bricksList[i].Orientation; inc++; } inc = 0; j = Mathf.RoundToInt(brickOrigin.x); int numLowerBricks = 0; while (inc < numLowerSlots) { LXFMLCell lowerCell = _grid.GetCellAt(j, (int)brickOrigin.y - 1); if (lowerCell != null && !lowerCell.IsEmpty) { if (lowerCell.Data.Brick.design.type == BrickType.Normal || lowerCell.Data.Brick.design.type == BrickType.SlopeUp || lowerCell.Data.Brick.design.type == BrickType.CurveIn) { numLowerBricks++; } else { if (lowerCell.Data.IsOrigin) { numLowerBricks++; } else { if (j == GetBrickOrigin(lowerCell.Data.Brick.id).x) { numLowerBricks++; } } } } j += bricksList[i].Orientation; inc++; } if (numLowerBricks == numLowerSlots && numUpperBricks == numUpperSlots) { CreateLaser(bricksList[i], direction); } } } }
private void SetConnectedBricks(BrickData brick, Vector2 brickOrigin) { int numUpperSlots = LXFMLHelper.UpperSlots(brick.design.id); int numLowerSlots = LXFMLHelper.LowerSlots(brick.design.id); int inc = 0; int i = Mathf.RoundToInt(brickOrigin.x); while (inc < numUpperSlots) { LXFMLCell upperCell = _grid.GetCellAt(i, (int)brickOrigin.y + brick.design.height); if (upperCell != null && !upperCell.IsEmpty) { if (upperCell.Data.Brick.design.type == BrickType.Normal || upperCell.Data.Brick.design.type == BrickType.SlopeDown || upperCell.Data.Brick.design.type == BrickType.CurveOut) { ConnectBricks(brick, upperCell.Data.Brick); } else { if (upperCell.Data.IsOrigin) { ConnectBricks(brick, upperCell.Data.Brick); } } } i += brick.Orientation; inc++; } inc = 0; i = Mathf.RoundToInt(brickOrigin.x); while (inc < numLowerSlots) { LXFMLCell lowerCell = _grid.GetCellAt(i, (int)brickOrigin.y - 1); if (lowerCell != null && !lowerCell.IsEmpty) { if (lowerCell.Data.Brick.design.type == BrickType.Normal || lowerCell.Data.Brick.design.type == BrickType.SlopeUp || lowerCell.Data.Brick.design.type == BrickType.CurveIn) { ConnectBricks(brick, lowerCell.Data.Brick); } else { if (lowerCell.Data.IsOrigin) { ConnectBricks(brick, lowerCell.Data.Brick); } else { if (i == GetBrickOrigin(lowerCell.Data.Brick.id).x) { ConnectBricks(brick, lowerCell.Data.Brick); } } } } i += brick.Orientation; inc++; } var leftCell = _grid.GetCellAt(brickOrigin.x - 1f, brickOrigin.y); var rightCell = _grid.GetCellAt(brickOrigin.x + brick.design.width, brickOrigin.y); if (brick.Orientation < 0) { leftCell = _grid.GetCellAt(brickOrigin.x - brick.design.width, brickOrigin.y); rightCell = _grid.GetCellAt(brickOrigin.x + 1, brickOrigin.y); } if (leftCell != null && !leftCell.IsEmpty) { brick.NeighbourBricks.Add(leftCell.Data.Brick); leftCell.Data.Brick.NeighbourBricks.Add(brick); } if (rightCell != null && !rightCell.IsEmpty) { brick.NeighbourBricks.Add(rightCell.Data.Brick); rightCell.Data.Brick.NeighbourBricks.Add(brick); } }
public void ReplacePart(int designId, bool reverse) { if (selectedPart != null) { bool currentReversed = !MathUtils.NearEqual(selectedPart.transform.localRotation.eulerAngles.y, LXFMLHelper.GetDefaultRotation(selectedPart.designId)); //(_construction.GetBrick(selectedPart.id)); int brickId = selectedPart.id; //Destroy old one Vector3 oldPosition = selectedPart.transform.position; _builder.DestroyBrick(selectedPart); _bricks.Remove(brickId); //Create new one BrickData brickData = new BrickData(); brickData.design = new BrickDesignData(); brickData.id = _builder.construction.GetNextBrickId(); brickData.materialId = LXFMLHelper.GetColors(designId)[0]; brickData.design.id = designId; brickData.design.width = LXFMLHelper.GetBrickWidth(designId); brickData.design.height = LXFMLHelper.GetBrickHeight(designId); brickData.design.type = LXFMLHelper.GetBrickType(designId); brickData.scale = Vector3.one; GameObject newBrick = Instantiate(ConstructionController.Instance.resourcesProvider.GetPrefabForDesign(designId)); newBrick.transform.SetParent(constructionTarget); LegoBrickId newOne = newBrick.GetComponent <LegoBrickId>(); newOne.id = brickData.id; newOne.transform.position = oldPosition; Vector3 position = oldPosition; //WEIRD HACK...I HAVE TO OFFSET BY HALF A BRICK TO GET THE CORRECT POSITION position.z -= LXFMLHelper.kBrickSize.x / 2f; brickData.position = position; float add = reverse?180:0; if (LXFMLHelper.GetBrickOrientation(newOne.designId) == LXFMLHelper.BrickOrientation.NegativeX) { newOne.transform.localEulerAngles = new Vector3(0, 0f + add, 0); } else if (LXFMLHelper.GetBrickOrientation(newOne.designId) == LXFMLHelper.BrickOrientation.PositiveX) { newOne.transform.localEulerAngles = new Vector3(0, 180f + add, 0); } else if (LXFMLHelper.GetBrickOrientation(newOne.designId) == LXFMLHelper.BrickOrientation.NegativeZ) { newOne.transform.localEulerAngles = new Vector3(0, 90f + add, 0); } else if (LXFMLHelper.GetBrickOrientation(newOne.designId) == LXFMLHelper.BrickOrientation.PositiveZ) { newOne.transform.localEulerAngles = new Vector3(0, 270f + add, 0); } //TODO : Fix Orientation stuff, it's getting messy... if (reverse) { if (!currentReversed) { float xOffset = (brickData.design.width - 1) * LXFMLHelper.kBrickSize.x; if (LXFMLHelper.GetBrickOrientation(newOne.designId) == LXFMLHelper.BrickOrientation.PositiveZ) { newOne.transform.Translate(0f, 0f, xOffset); } else { newOne.transform.Translate(-xOffset, 0f, 0f); } brickData.position = newOne.transform.position; } } else { if (currentReversed) { float xOffset = (brickData.design.width - 1) * LXFMLHelper.kBrickSize.x; if (LXFMLHelper.GetBrickOrientation(newOne.designId) == LXFMLHelper.BrickOrientation.PositiveZ) { newOne.transform.Translate(0f, 0f, xOffset); } else { newOne.transform.Translate(-xOffset, 0f, 0f); } brickData.position = newOne.transform.position; } } brickData.isFlipped = reverse; brickData.rotation = Quaternion.Euler(0f, -newOne.transform.localEulerAngles.y, 0f); var renderer = newBrick.GetComponentInChildren <MeshRenderer>(); renderer.sharedMaterial = ConstructionController.Instance.resourcesProvider.GetMaterial(brickData.materialId); selectedPart = newOne; _bricks.Add(newOne.id, newOne); _builder.AddBrick(_bricks[newOne.id].transform, brickData); UpdateAvailableParts(selectedPart); UpdateAvailableColors(selectedPart); } }