Exemplo n.º 1
0
    void PaseXml(XmlDocument xml)
    {
        //得到objects节点下的所有子节点
        XmlNodeList xmlNodeList = xml.SelectSingleNode("map/Enenty").ChildNodes;

        //遍历所有子节点
        foreach (XmlElement xl1 in xmlNodeList)
        {
            foreach (XmlElement xl2 in xl1.ChildNodes)
            {
                string    type      = (xl2.GetAttribute("type") + xl2.InnerText);
                BlockType blocktype = BlockType.Null;
                switch (type)
                {
                case "Road":
                    blocktype = BlockType.Road;

                    //PlayerPanel._Instance.SetText(type);
                    break;

                case "Build":
                    blocktype = BlockType.Build;
                    break;

                case "Normal":

                    blocktype = BlockType.Normal;
                    break;

                default:
                    blocktype = BlockType.Null;
                    break;
                }


                int id     = int.Parse(xl2.GetAttribute("id") + xl2.InnerText);
                int x      = int.Parse(xl2.GetAttribute("x") + xl2.InnerText);
                int y      = int.Parse(xl2.GetAttribute("y") + xl2.InnerText);
                int rotate = int.Parse(xl2.GetAttribute("rotate") + xl2.InnerText);

                //x += 30;
                //y += 30;

                int height = 30;

                GameObject go     = null;
                GameObject prefeb = null;


                if (blocktype == BlockType.Road)
                {
                    prefeb = Resources.Load("Prefebs/Road/" + "roadTile_" + string.Format("{0:000}", id)) as GameObject;

                    newroadPoint = GameObject.Instantiate(roadPointPrefeb, new Vector3(x - 1.5f, 0.25f, y - 1.5f), Quaternion.identity) as GameObject;
                    newroadPoint.transform.parent = GameObject.Find("RoadPoints").transform;

                    //roadpointList.Add(newroadPoint.GetComponent<RoadPoint>());
                }
                else if (blocktype == BlockType.Build)
                {
                    prefeb = Resources.Load("Prefebs/Building/" + "modularBuildings_" + string.Format("{0:000}", id)) as GameObject;
                }
                else if (blocktype == BlockType.Normal)
                {
                    prefeb = Resources.Load("Prefebs/Road/" + "roadTile_" + string.Format("{0:000}", id)) as GameObject;
                }


                if (rotate == 0)
                {
                    go = GameObject.Instantiate(prefeb, new Vector3(x, height, y), Quaternion.identity) as GameObject;
                }
                else if (rotate == 90)
                {
                    go = GameObject.Instantiate(prefeb, new Vector3(x, height, y - 3), Quaternion.Euler(0, rotate, 0)) as GameObject;
                }
                else if (rotate == 180)
                {
                    go = GameObject.Instantiate(prefeb, new Vector3(x - 3, height, y - 3), Quaternion.Euler(0, rotate, 0)) as GameObject;
                }
                else if (rotate == 270)
                {
                    go = GameObject.Instantiate(prefeb, new Vector3(x - 3, height, y), Quaternion.Euler(0, rotate, 0)) as GameObject;
                }

                if (blocktype == BlockType.Road)
                {
                    go.gameObject.name = "Road-" + x / 3 + "-" + y / 3;
                }
                else if (blocktype == BlockType.Build)
                {
                    go.gameObject.name = "Building-" + x / 3 + "-" + y / 3;
                }
                else if (blocktype == BlockType.Normal)
                {
                    go.gameObject.name = "Normal-" + x / 3 + "-" + y / 3;
                }



                go.transform.parent = GameObject.Find("Terrain").transform;


                MapBlock block = go.GetComponent <MapBlock>();
                if (newroadPoint != null)
                {
                    newroadPoint.GetComponent <RoadPoint>().MapBlock = block;
                }
                newroadPoint = null;
                //Debug.Log(rotate);
                block.InitBlock(blocktype, id, x, y, blocksList.Count, rotate);

                blocksList.Add(block);
            }
        }

        isXMLReadComplete = true;
    }