/// <summary> /// Generates a new Block from a BlockStructure, places it at (0,2,0) /// </summary> /// <param name="structure">The Structure to generatoe</param> /// <returns>A new Block</returns> public GameObject GenerateBlock(BlockStructure structure) { Material blockMaterial = new Material(material); blockMaterial.color = structure.BlockColor; GameObject container = new GameObject(); structure.GetCroppedMatrix(); float rowMiddlePoint = (float)(structure.RowsCropped - 1) / 2; float colMiddlePoint = (float)(structure.ColsCropped - 1) / 2; for (int row = 0; row < structure.RowsCropped; row++) { for (int col = 0; col < structure.ColsCropped; col++) { if (structure[row, col] != null) { Vector3 partPosition = new Vector3((rowMiddlePoint - row) * length, 0, (colMiddlePoint - col) * length); GameObject blockPart = Instantiate(partSizes[structure.BlockSize], partPosition, Quaternion.identity, container.transform); blockPart.SetActive(true); } } } GameObject newBlock = CombineTileMeshes(container); newBlock.AddComponent <BlockGeometryScript>().SetStructure(structure); newBlock.GetComponent <MeshRenderer>().material = blockMaterial; newBlock.transform.position = new Vector3(0, 2, 0); newBlock.tag = "Block"; AddPrecurserComponents(newBlock); return(newBlock); }
public void SetStructure(BlockStructure structure) { ClearMatrix(); BlockPart[,] croppedMatrix = structure.GetCroppedMatrix(); if (structure.RowsCropped > Rows) { for (; structure.RowsCropped > Rows;) { AddRow(); handler.UpdateHandlerPosition(Rows, Columns); } } if (structure.ColsCropped > Columns) { for (; structure.ColsCropped > Columns;) { AddCol(); handler.UpdateHandlerPosition(Rows, Columns); } } for (int row = 0; row < structure.RowsCropped; row++) { for (int col = 0; col < structure.ColsCropped; col++) { if (croppedMatrix[row, col] != null) { matrix[row][col].GetComponent <Toggle>().isOn = true; } } } }