Exemplo n.º 1
0
    /**
     * Read a level from an XML document.
     */
    private PipeElement[,] instanciateLevelFromXml(TextAsset document)
    {
        XmlDocument xml = new XmlDocument();

        PipeElement[,] grid = new PipeElement[GRID_SIZE, GRID_SIZE];

        // load the document and get the level(s)
        xml.LoadXml(document.text);
        XmlNodeList levelNodes = xml.GetElementsByTagName("level");

        XmlAttribute levelEstimatedTime = levelNodes[0].Attributes["estimatedTime"];

        if (levelEstimatedTime != null)
        {
            // use it as the base time for countdown timer
            timebar.maxTime = int.Parse(levelEstimatedTime.Value);
        }
        // start and destination pipes
        // TODO init

        XmlNodeList pipeNodes = levelNodes[0].ChildNodes;

        foreach (XmlNode curNode in pipeNodes)
        {
            if (curNode.Name == "pipe")
            {
                int x = 0;
                int y = 0;
                PipeElement.Orientation orientation = PipeElement.Orientation.NORTH;
                PipeElement.Type        type        = PipeElement.Type.PIPE_I;

                // add the pipe if possible
                x = int.Parse(curNode.Attributes["x"].Value);
                y = int.Parse(curNode.Attributes["y"].Value);

                switch (curNode.Attributes["type"].Value)
                {
                case "L":
                    type = PipeElement.Type.PIPE_L;
                    break;

                case "I":
                    type = PipeElement.Type.PIPE_I;
                    break;

                case "in":
                    type = PipeElement.Type.PIPE_IN;
                    break;

                case "out":
                    type = PipeElement.Type.PIPE_OUT;
                    break;
                }

                XmlAttribute dirAttribute = curNode.Attributes["dir"];
                if (dirAttribute == null)
                {
                    orientation = OrientationMethods.randomOrientation();
                }
                else
                {
                    orientation = OrientationMethods.fromString(dirAttribute.Value);
                }


                if (x >= 0 && x < GRID_SIZE && y >= 0 && y < GRID_SIZE)
                {
                    // add the new pipe to the grid, and set its neighbors
                    PipeElement toAdd = new PipeElement(type, orientation);
                    grid[x, y] = toAdd;

                    if (toAdd.type == PipeElement.Type.PIPE_IN)
                    {
                        inputX           = x;
                        inputY           = y;
                        inputOrientation = orientation;
                    }
                    else if (toAdd.type == PipeElement.Type.PIPE_OUT)
                    {
                        outputX           = x;
                        outputY           = y;
                        outputOrientation = orientation;
                    }

                    // set neighbors

                    // TODO neighbors
                }
            }
            else
            {
            }
        }

        return(grid);
    }