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); }
void UpdateAvailableColors(LegoBrickId selected) { if (selected) { _modelUI.UpdateColorsList(LXFMLHelper.GetColors(selected.designId), _builder.construction.GetBrick(selected.id).materialId); } }
IEnumerator TweenInvalidBrick(Transform t) { Vector3 vel = Vector3.zero; _isTweening = true; while (!MathUtils.NearEqual(t.position, _originalPos)) { t.position = Vector3.SmoothDamp(t.position, _originalPos, ref vel, 0.1f); yield return(0); } if (_newBrickCreated) { Destroy(t.gameObject); _dragCellData = null; _newBrickCreated = false; selectedPart = null; UpdateAvailableParts(selectedPart); UpdateAvailableColors(selectedPart); } else { t.position = _originalPos; t.parent = constructionTarget; if (_dragCellData != null) { _dragCellData.Brick.position = _originalPos; _builder.construction.AddBrick(_dragCellData.Brick); _validPosition = true; } } _isTweening = false; }
void Start() { _modelUI = GetComponent <ConstructionEditorUI>(); _builder = ConstructionController.Instance.SetConstruction(constructionTarget); if (_builder == null) { _builder = ConstructionController.Instance.SetConstruction(xml.text, constructionTarget); } _builder.Init(ConstructionBuilder.BuilderType.Edit); foreach (Transform child in constructionTarget.transform) { LegoBrickId legoBrick = child.GetComponent <LegoBrickId>(); if (legoBrick != null) { _bricks.Add(legoBrick.id, legoBrick); } } selectedPart = null; UpdateAvailableParts(null); UpdateAvailableColors(null); }
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 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")); }
public void Unselect() { _modelUI.UpdateColorsList(new int[0], 0); _dragCellData = null; selectedPart = null; draggingScenePart = null; _newBrickCreated = false; }
public void DestroyBrick(LegoBrickId brick) { int brickId = brick.id; _bricks.Remove(brick.transform); _bricksLookup.Remove(brickId); construction.RemoveBrick(brickId); Destroy(brick.gameObject); }
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); } }
public void DestroyPart() { if (selectedPart) { int partId = selectedPart.id; _builder.DestroyBrick(selectedPart); _bricks.Remove(partId); selectedPart = null; UpdateAvailableParts(null); UpdateAvailableColors(null); } }
public void StopBricksAnimation() { StopAllCoroutines(); _numBricksAnimateComplete = _numBricksToAnimate; for (int i = 0; i < _bricks.Count; ++i) { var renderer = _bricks [i].GetComponentInChildren <MeshRenderer>(); LegoBrickId legoBrick = _bricks [i].GetComponent <LegoBrickId> (); BrickData bd = construction.GetBrick(legoBrick.id); renderer.sharedMaterial = ConstructionController.Instance.resourcesProvider.GetMaterial(bd.materialId); } }
public void CheckSelectedBrick() { if (!_isTweening) { #if UNITY_EDITOR Vector2 mousePos = Input.mousePosition; #else Vector2 mousePos = Input.touchCount > 0 ? Input.GetTouch(0).position : Vector2.zero; #endif Vector2 gridPos = screenPointToGridPosition(mousePos); LXFMLCell cell = _builder.construction.Grid.GetCellAt(gridPos); if (cell != null && cell.Data != null) { selectedPart = draggingScenePart = _bricks[cell.Data.Brick.id]; _startDragPosition = selectedPart.transform.position; _startDragCell = gridPos; UpdateAvailableParts(selectedPart); UpdateAvailableColors(selectedPart); _dragCellData = cell.Data; _originalPos = selectedPart.transform.position; Vector2 blockOrigin = _builder.construction.GetBrickCell(selectedPart.id).Coordinates; if (cell.Data.Brick.isFlipped) { blockOrigin.x -= cell.Data.Brick.design.width - 1; } _cellOffset.x = gridPos.x - blockOrigin.x; _cellOffset.y = gridPos.y - blockOrigin.y; _builder.construction.RemoveBrick(cell.Data.Brick.id); } else { _modelUI.UpdateColorsList(new int[0], 0); selectedPart = null; UpdateAvailableParts(null); UpdateAvailableColors(null); } } }
Transform CreateBrick(BrickData brick, int designId, Transform container) { var prefabRef = ConstructionController.Instance.resourcesProvider.GetPrefabForDesign(designId); if (prefabRef == null) { Debug.LogWarning(string.Format("No brick prefab was found for ID {0}. Skipping.", brick.design.id)); return(null); } var prefab = Instantiate <Transform>(prefabRef.transform); prefab.SetParent(container, false); prefab.localPosition = construction.TransformGridCoordinates(construction.GetBrickOrigin(brick.id)); prefab.localScale = Vector3.one; LegoBrickId brickId = prefab.GetComponent <LegoBrickId>(); if (brickId == null) { brickId = prefab.gameObject.AddComponent <LegoBrickId>(); brickId.designId = designId; brickId.coords = construction.GetBrickOrigin(brick.id); } brickId.id = brick.id; // Resetting rotation because it's not 0,0,0 on the prefab for some reason prefab.localRotation = Quaternion.identity; // LXFML inverts y rotation; that's why we rotate around "down" prefab.Rotate(Vector3.down, brick.rotation.eulerAngles.y, Space.Self); var renderer = prefab.GetComponentInChildren <MeshRenderer>(); if (renderer != null) { renderer.sharedMaterial = ConstructionController.Instance.resourcesProvider.GetMaterial(brick.materialId); } return(prefab); }
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); } }
public void SetConstructionPivot(ConstructionPivot pivot) { Rect bounds = construction.Grid.GetRect(); pivotOffset.x = 0f; pivotOffset.y = 0f; pivotOffset.z = LXFMLHelper.kBrickSize.x / 2f; switch (pivot) { case ConstructionPivot.LowerLeft: pivotOffset.x = -((bounds.x * LXFMLHelper.kBrickSize.x) - (LXFMLHelper.kBrickSize.x / 2f)); pivotOffset.y = -(bounds.y * LXFMLHelper.kBrickSize.y); break; case ConstructionPivot.LowerRight: pivotOffset.x = -((bounds.x * LXFMLHelper.kBrickSize.x) + (bounds.width * LXFMLHelper.kBrickSize.x) - (LXFMLHelper.kBrickSize.x / 2f)); pivotOffset.y = -(bounds.y * LXFMLHelper.kBrickSize.y); break; case ConstructionPivot.LowerCenter: pivotOffset.x = -((bounds.x * LXFMLHelper.kBrickSize.x) + (bounds.width * LXFMLHelper.kBrickSize.x / 2f) - (LXFMLHelper.kBrickSize.x / 2f)); pivotOffset.y = -(bounds.y * LXFMLHelper.kBrickSize.y); break; case ConstructionPivot.UpperLeft: pivotOffset.x = -((bounds.x * LXFMLHelper.kBrickSize.x) - (LXFMLHelper.kBrickSize.x / 2f)); pivotOffset.y = -((bounds.y * LXFMLHelper.kBrickSize.y) + (bounds.height * LXFMLHelper.kBrickSize.y)); break; case ConstructionPivot.UpperRight: pivotOffset.x = -((bounds.x * LXFMLHelper.kBrickSize.x) + (bounds.width * LXFMLHelper.kBrickSize.x) - (LXFMLHelper.kBrickSize.x / 2f)); pivotOffset.y = -((bounds.y * LXFMLHelper.kBrickSize.y) + (bounds.height * LXFMLHelper.kBrickSize.y)); break; case ConstructionPivot.UpperCenter: pivotOffset.x = -((bounds.x * LXFMLHelper.kBrickSize.x) + (bounds.width * LXFMLHelper.kBrickSize.x / 2f) - (LXFMLHelper.kBrickSize.x / 2f)); pivotOffset.y = -((bounds.y * LXFMLHelper.kBrickSize.y) + (bounds.height * LXFMLHelper.kBrickSize.y)); break; case ConstructionPivot.MiddleLeft: pivotOffset.x = -((bounds.x * LXFMLHelper.kBrickSize.x) - (LXFMLHelper.kBrickSize.x / 2f)); pivotOffset.y = -((bounds.y * LXFMLHelper.kBrickSize.y) + (bounds.height * LXFMLHelper.kBrickSize.y / 2f)); break; case ConstructionPivot.MiddleRight: pivotOffset.x = -((bounds.x * LXFMLHelper.kBrickSize.x) + (bounds.width * LXFMLHelper.kBrickSize.x) - (LXFMLHelper.kBrickSize.x / 2f)); pivotOffset.y = -((bounds.y * LXFMLHelper.kBrickSize.y) + (bounds.height * LXFMLHelper.kBrickSize.y / 2f)); break; case ConstructionPivot.MiddleCenter: pivotOffset.x = -((bounds.x * LXFMLHelper.kBrickSize.x) + (bounds.width * LXFMLHelper.kBrickSize.x / 2f) - (LXFMLHelper.kBrickSize.x / 2f)); pivotOffset.y = -((bounds.y * LXFMLHelper.kBrickSize.y) + (bounds.height * LXFMLHelper.kBrickSize.y / 2f)); break; } for (int i = 0; i < _bricks.Count; ++i) { LegoBrickId legoBrick = _bricks[i].GetComponent <LegoBrickId>(); BrickData bd = construction.GetBrick(legoBrick.id); _bricks[i].localPosition = bd.position; _bricks[i].Translate(pivotOffset, Space.World); } }