示例#1
0
    void loadGeometryFile(string filename)
    {
        var sr           = new StreamReader(Application.dataPath + "/" + filename);
        var fileContents = sr.ReadToEnd();

        sr.Close();

        string[] lines = fileContents.Split("\n"[0]);
        foreach (string line in lines)
        {
            string[] v = line.Split(' ');
            if (v.Length >= 3)
            {
                float height          = 0.0f;
                int   indexCorrection = 0;
                if (!float.TryParse(v[v.Length - 1], out height))
                {
                    indexCorrection = 1;
                }

                if ((v.Length + indexCorrection) % 2 == 0)
                {
                    string         name = v[v.Length - 3 + indexCorrection];
                    List <Vector2> list = new List <Vector2>();
                    for (int i = 1; i < v.Length - 4 + indexCorrection; i = i + 2)
                    {
                        float x;
                        float y;
                        if (float.TryParse(v[i], out x) && float.TryParse(v[i + 1], out y))
                        {
                            list.Add(new Vector2(x, y));
                        }
                    }

                    string type = v[v.Length - 2 + indexCorrection];
                    if (type == "wall")
                    {
                        WallExtrudeGeometry.create(name, list, height, -0.2f);
                    }
                    else if (type == "obstacle")
                    {
                        ObstacleExtrudeGeometry.create(name, list, height);
                    }
                    else if (type == "tree")
                    {
                        TreeGeometry.create(name, list[0]);
                    }
                    else
                    {
                        AreaGeometry.create(name, list);
                    }
                }
            }
        }
    }
示例#2
0
    void loadGeometryFile(string filename)
    {
        if (!System.IO.File.Exists(filename))
        {
            Debug.Log("Error: File " + filename + " not found.");
            return;
        }

        XmlDocument xmlDocGeo = new XmlDocument();

        xmlDocGeo.LoadXml(System.IO.File.ReadAllText(filename));

        int wallNr  = 1;    //includes "Walls" and "Obsacles"
        int stairNr = 1;    //includes "Stairs" and "Escalators"
        int floorNr = 1;    //floors are generated for each subroom at this state

        XmlNode geometry = xmlDocGeo.SelectSingleNode("//geometry");

        foreach (XmlElement rooms in geometry.SelectNodes("//rooms"))
        {
            foreach (XmlElement room in rooms.SelectNodes("room"))
            {
                foreach (XmlElement subroom in room.SelectNodes("subroom[@class='subroom'" +
                                                                " or @class = 'Office' " +
                                                                " or @class = 'Not specified' " +
                                                                " or @class='Corridor']"))
                {
                    float height  = 1.0f;
                    float zOffset = TryParseWithDefault.ToSingle(subroom.GetAttribute("C_z"), 0);       //EXCEPTION: sometimes "C_z" is referred to as "C"

                    if (zOffset == 0)
                    {
                        zOffset = TryParseWithDefault.ToSingle(subroom.GetAttribute("C"), 0);
                    }

                    foreach (XmlElement openWall in subroom.SelectNodes("polygon[@caption='wall']"))
                    {
                        WallExtrudeGeometry.create(openWall.GetAttribute("caption") + (" ") + wallNr, parsePoints(openWall), height, 0.2f, zOffset);

                        wallNr++;
                    }
                    foreach (XmlElement obstacle in subroom.SelectNodes("obstacle"))
                    {
                        foreach (XmlElement Wall in obstacle.SelectNodes("polygon"))
                        {
                            ObstacleExtrudeGeometry.create(Wall.GetAttribute("caption") + (" ") + wallNr, parsePoints(Wall), height, zOffset);
                            wallNr++;
                        }
                    }



                    FloorExtrudeGeometry.create("floor " + floorNr, parsePoints_floor(subroom), zOffset);                    //here is creating floor?
                    floorNr++;
                }
                foreach (XmlElement subroom in room.SelectNodes("subroom[@class='stair' or @class ='Stair'"
                                                                + "or @class='idle_escalator']"))
                {
                    float A_x = TryParseWithDefault.ToSingle(subroom.GetAttribute("A_x"), 0);
                    float B_y = TryParseWithDefault.ToSingle(subroom.GetAttribute("B_y"), 0);
                    float C_z = TryParseWithDefault.ToSingle(subroom.GetAttribute("C_z"), 0);  //EXCEPTION: sometimes "C_z" is referred to as "C"
                    if (C_z == 0)
                    {
                        C_z = TryParseWithDefault.ToSingle(subroom.GetAttribute("C"), 0);
                    }

                    float pxUp   = 0;   //Variables need to be assigned
                    float pyUp   = 0;
                    float pxDown = 0;
                    float pyDown = 0;

                    foreach (XmlElement up in subroom.SelectNodes("up"))
                    {
                        pxUp = TryParseWithDefault.ToSingle(up.GetAttribute("px"), 0);
                        pyUp = TryParseWithDefault.ToSingle(up.GetAttribute("py"), 0);
                    }
                    foreach (XmlElement down in subroom.SelectNodes("down"))
                    {
                        pxDown = TryParseWithDefault.ToSingle(down.GetAttribute("px"), 0);
                        pyDown = TryParseWithDefault.ToSingle(down.GetAttribute("py"), 0);
                    }

                    bool           lowerPart   = true;
                    List <Vector2> coordFirst  = new List <Vector2>();
                    List <Vector2> coordSecond = new List <Vector2>();
                    foreach (XmlElement stair in subroom.SelectNodes("polygon[@caption='wall']"))
                    {
                        if (lowerPart == true)
                        {
                            coordFirst = parsePoints(stair);
                            lowerPart  = false;
                        }
                        else
                        {
                            coordSecond = parsePoints(stair);
                            lowerPart   = true;
                        }
                    }


                    StairExtrudeGeometry.create(subroom.GetAttribute("class") + (" ") + stairNr, coordFirst, coordSecond, A_x, B_y, C_z, pxUp, pyUp, pxDown, pyDown);
                    stairNr++;
                }
            }
        }
    }