public static Block Create(KeyValuePair <string, char[, ]> blueprint) { Block block = (new GameObject()).AddComponent <Block>() as Block; block.name = blueprint.Key; Piece.Type[,] pieces = ConvertBlueprintToBlock(blueprint.Value); Vector3 size = new Vector3((float)(pieces.GetLength(0)), (float)(pieces.GetLength(1)), 1.0f); Vector3 positionOffset = size / 2.0f - Vector3.one / 2.0f; for (int i = 0; i < pieces.GetLength(0); ++i) { for (int j = 0; j < pieces.GetLength(1); ++j) { Piece.Type pieceType = pieces[i, j]; if (pieceType == Piece.Type.EMPTY) { continue; } block.AddPiece(new Vector3(i, j, 0) - positionOffset, pieceType); } } block.SetCollider(size); block.InitRenderables(); return(block); }
void ReadLevelXml(TextAsset asset) { // See: http://unitynoobs.blogspot.dk/2011/02/xml-loading-data-from-xml-file.html XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(asset.text); XmlNodeList levelNodelist = xmlDoc.GetElementsByTagName("level"); // TODO: foreach level ... // this time we only read 1 level XmlNodeList blockNodelist = levelNodelist[0].SelectNodes("block"); foreach (XmlNode block in blockNodelist) { Block b = (new GameObject("block")).AddComponent <Block>(); allBlocks.Add(b); XmlNodeList pieceNodelist = block.SelectNodes("piece"); foreach (XmlNode piece in pieceNodelist) { int x = 0; int y = 0; int z = 0; try { x = Int32.Parse(piece.Attributes["x"].Value); y = Int32.Parse(piece.Attributes["y"].Value); z = Int32.Parse(piece.Attributes["z"].Value); b.AddPiece(x, y, z); } catch (FormatException e) { Debug.LogError(e.Message); } } } }
void ReadLevelXml(TextAsset asset) { // See: http://unitynoobs.blogspot.dk/2011/02/xml-loading-data-from-xml-file.html XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(asset.text); XmlNodeList levelNodelist = xmlDoc.GetElementsByTagName("level"); // TODO: foreach level ... // this time we only read 1 level XmlNode l = levelNodelist[0]; //set size try { sizeX = Int32.Parse(l.Attributes["x"].Value); sizeY = Int32.Parse(l.Attributes["y"].Value); sizeZ = Int32.Parse(l.Attributes["z"].Value); } catch (FormatException e) { Debug.LogError(e.Message); } //find all blocks XmlNodeList blockNodelist = l.SelectNodes("block"); foreach (XmlNode block in blockNodelist) { //create block Block b = (new GameObject("block")).AddComponent <Block>(); b.transform.position = GetWorldPosition(0, 0, 0); allBlocks.Add(b); //create material Material mat = new Material(defaultBlockMaterial); mat.color = BlockColor.FromString(block.Attributes["color"].Value); b.Init(mat); //find children (pieces) XmlNodeList pieceNodelist = block.SelectNodes("piece"); foreach (XmlNode piece in pieceNodelist) { int x = 0; int y = 0; int z = 0; try { x = Int32.Parse(piece.Attributes["x"].Value); y = Int32.Parse(piece.Attributes["y"].Value); z = Int32.Parse(piece.Attributes["z"].Value); b.AddPiece(x, y, z); } catch (FormatException e) { Debug.LogError(e.Message); } } } }