コード例 #1
0
ファイル: Grid.cs プロジェクト: NobleTeamGames/TAv0.1
    public void GenarateGrid(string Type ,float Difficalt)
    {
       // Background = Instantiate(Resources.Load("Prefab/Forest"), new Vector3(0, 2.5f, 0), Quaternion.identity) as GameObject; 
        _Background = Camera.main.transform.FindChild("Background").GetComponent<Background>();
        //
        float Rand = 0f;                                        //Рандомная величина
        GameObject Now;                                        //Переменная для поиска спаунящегося блока

        

        TextAsset xmlAsset = Resources.Load("LevelStats/" + Type) as TextAsset; //Считываня xml
        XmlDocument xmlDoc = new XmlDocument();
        if (xmlAsset)
            xmlDoc.LoadXml(xmlAsset.text);

        XmlElement Stats = (XmlElement)xmlDoc.DocumentElement.SelectSingleNode("Location");
        Height = Convert.ToInt16(Stats.GetAttribute("height"));
        Widht = Convert.ToInt16(Stats.GetAttribute("width"));

        int CountBack = Convert.ToInt16(xmlDoc.DocumentElement.SelectSingleNode("//Background").Attributes[0].Value);

        //string Str = ;
        _Background.SetUpBackGround(Resources.Load<Sprite>("Sprites/Back/" + Type + "/" + UnityEngine.Random.Range(1, CountBack + 1)));
        List<Vector2> TreesCoords = new List<Vector2>();
        int IndentTree = Convert.ToInt16(xmlDoc.DocumentElement.SelectSingleNode("//Tree").Attributes[0].Value);
        BlockGrid = new Block[Height + 2, Widht];
       // BlockGrid[0, 30] = new Block();
        

        XmlElement Lvl = xmlDoc.GetElementById("1");
        for (int i = -1; i <= Height; i++)
        {
            for (int x = -Widht / 2; x <= Widht / 2 - 1; x++)
            {
                Rand = UnityEngine.Random.value;
                switch (i)
                {
                    case -1:
                        if (UnityEngine.Random.value < Convert.ToSingle(xmlDoc.DocumentElement.SelectSingleNode("//Tree").Attributes[1].Value))
                        {

                            if (x <= 0)
                            {
                                if (TreesCoords.Count > 0)
                                {
                                    if (TreesCoords[TreesCoords.Count - 1].x + IndentTree <= x)
                                        TreesCoords.Add(new Vector2(x, 1));
                                }
                                else
                                    TreesCoords.Add(new Vector2(x, 1));
                            }
                            else
                            {
                                if (UnityEngine.Random.value < 0.5f)
                                    if (TreesCoords.Count > 0)
                                    {
                                        if (TreesCoords[TreesCoords.Count - 1].x + IndentTree <= x)
                                            TreesCoords.Add(new Vector2(x, 1));
                                    }
                                    else
                                        TreesCoords.Add(new Vector2(x, 1));
                            }
                                


                        }
                       
                        break;


                    case 0:
                        Now = Instantiate(Resources.Load("Prefab/GroundUp"), new Vector3(x, -i, 0), Quaternion.identity) as GameObject;
                        BlockGrid[i, x + 15] = Now.GetComponent<Ground>();
                        BlockGrid[i, (x + 15)].gameObject.transform.parent = this.gameObject.transform;
                        break;

                    default:
                        if (UnityEngine.Random.value <= Difficalt)
                        {
                            if (!BlockGrid[i, x + 15])
                            {
                                Now = Instantiate(Resources.Load("Prefab/Ground"), new Vector3(x, -i, 0), Quaternion.identity) as GameObject;
                                BlockGrid[i, x + 15] = Now.GetComponent<Ground>();
                                BlockGrid[i, (x + 15)].gameObject.transform.parent = this.gameObject.transform;
                            }
                        }
                        else
                        {
                            bool Spawn = false;
                            XmlNodeList dataList = xmlDoc.GetElementsByTagName("level")[0].ChildNodes;
                            for (int j = 0; j < dataList.Count; j++)
                            {
                                string[] substrings = Regex.Split(dataList[j].Attributes[1].Value, "(-)");
                                if (i >= Convert.ToInt16(substrings[0]) && i <= Convert.ToInt16(substrings[2]))
                                {
                                    Rand = UnityEngine.Random.value;
                                    if (Rand <= Convert.ToSingle(dataList[j].Attributes[0].Value) && !BlockGrid[i, x + 15])
                                    {
                                        if (!BlockGrid[i, x + 15])
                                            GenerateResourse(i, x, dataList[j].Attributes[2].Value, dataList[j].Name);
                                        Spawn = true;
                                        break;
                                    }
                                }
                            }
                            if (!Spawn)
                            {
                                if (!BlockGrid[i, x + 15])
                                {
                                    Now = Instantiate(Resources.Load("Prefab/Ground"), new Vector3(x, -i, 0), Quaternion.identity) as GameObject;
                                    BlockGrid[i, x + 15] = Now.GetComponent<Ground>();
                                    BlockGrid[i, (x + 15)].gameObject.transform.parent = this.gameObject.transform;
                                }
                            }
                        }
                        break;
                }

            }
            

           
        }
            for (int i = 0; i <= Height; i++)
            {
                for (int x = -Widht / 2; x <= Widht / 2 - 1; x++)
                {
                    if (!BlockGrid[i, x + 15])
                    {
                        Now = Instantiate(Resources.Load("Prefab/Ground"), new Vector3(x, -i, 0), Quaternion.identity) as GameObject;
                        BlockGrid[i, x + 15] = Now.GetComponent<Ground>();
                        BlockGrid[i, (x + 15)].gameObject.transform.parent = this.gameObject.transform;
                    }
                }
            }
        GenerateTree(TreesCoords);
        GenerateZone();
        _Background.gameObject.SetActive(true);
        
    }