コード例 #1
0
        public RailRoad loadXML(string path, ContentManager content)
        {
            XmlDocument doc = new XmlDocument();

            //doc.Load(@"C:\Users\LasseSoerensen\Documents\Visual Studio 2010\Projects\RailRoadXML\RailRoadXML\RailRoad.xml");
            doc.Load(path);

            XmlNodeList nodeList = doc.SelectNodes("RailRoad/Track");

            RailRoad rr = new RailRoad();

            foreach (XmlNode n in nodeList)
            {
                string type = n.Attributes.GetNamedItem("Type").Value.ToString();

                Track t = new StraightTrack();
                t = new StraightTrack();
                t.gfx = content.Load<Texture2D>("StraightNB");
                t.direction = Convert.ToBoolean(n.Attributes.GetNamedItem("Direction").Value.ToString());
                if (type.Equals("CornerTrack"))
                {
                    t = new CornerTrack();
                    t.direction = Convert.ToBoolean(n.Attributes.GetNamedItem("Direction").Value.ToString());
                    //if (t.direction)
                    //{
                    t.gfx = content.Load<Texture2D>("rightTurnNB");
                    //}
                    //else
                    //{
                    //    t.gfx = content.Load<Texture2D>("leftTurnNB");
                    //}
                }

                if (type.Equals("SwitchRight"))
                {
                    t = new SwitchRight();
                    t.direction = Convert.ToBoolean(n.Attributes.GetNamedItem("Direction").Value.ToString());
                    t.turn = Convert.ToBoolean(n.Attributes.GetNamedItem("Switch").Value.ToString());

                    t.gfx = content.Load<Texture2D>("switchRight");
                }

                if (type.Equals("SwitchLeft"))
                {
                    t = new SwitchLeft();
                    t.direction = Convert.ToBoolean(n.Attributes.GetNamedItem("Direction").Value.ToString());
                    t.turn = Convert.ToBoolean(n.Attributes.GetNamedItem("Switch").Value.ToString());

                    t.gfx = content.Load<Texture2D>("switchLeft");
                }

                t.rotation = MathHelper.ToRadians(Convert.ToInt32(n.Attributes.GetNamedItem("Rotation").Value));

                t.position = new Vector2(Convert.ToInt32(n.Attributes.GetNamedItem("X").Value), Convert.ToInt32(n.Attributes.GetNamedItem("Y").Value));
                t.id = Convert.ToInt32(n.Attributes.GetNamedItem("ID").Value);

                //foreach (XmlNode nn in n.ChildNodes)
                //{
                //if (nn.Name == "Signal")
                //{
                //    Signal s = new Signal(Convert.ToInt32(nn.Attributes.GetNamedItem("ID").Value), content);
                //    s.position = new Vector2(Convert.ToInt32(nn.Attributes.GetNamedItem("X").Value), Convert.ToInt32(nn.Attributes.GetNamedItem("Y").Value));
                //    switch (nn.Attributes.GetNamedItem("InitialState").Value)
                //    {
                //        case "Stop": s.state = Signal.State.Stop; break;
                //        case "Go": s.state = Signal.State.Go; break;
                //        case "Off": s.state = Signal.State.Off; break;
                //    }

                //    t.signal = s;
                //    System.Diagnostics.Debug.WriteLine("Signal added");
                //}
                //else
                //if (nn.Name == "Sensor")
                //{
                //    Sensor s = new Sensor();
                //    s.id = Convert.ToInt32(nn.Attributes.GetNamedItem("ID").Value);
                //    s.mySignal = rr.findSignal(Convert.ToInt32(nn.Attributes.GetNamedItem("SignalID").Value));

                //    switch (nn.Attributes.GetNamedItem("InitialState").Value)
                //    {
                //        case "On": s.state = Sensor.State.On; break;
                //        case "Off": s.state = Sensor.State.Off; break;
                //    }
                //    t.sensor = s;
                //}

                //}

                rr.tracks.Add(t);
            }
            foreach (XmlNode xml in nodeList)
            {
                int id = Convert.ToInt32(xml.Attributes.GetNamedItem("ID").Value);
                foreach (Track t in rr.tracks)
                {
                    if (t.id == id)
                    {
                        t.prevTrack = rr.findTrack(Convert.ToInt32(xml.Attributes.GetNamedItem("PreviousTrack").Value));
                        t.nextTrack = rr.findTrack(Convert.ToInt32(xml.Attributes.GetNamedItem("NextTrack").Value));

                        if (t is SwitchLeft || t is SwitchRight)
                        {
                            t.switchTrack = rr.findTrack(Convert.ToInt32(xml.Attributes.GetNamedItem("SwitchTrackID").Value));
                        }
                        foreach (XmlNode nn in xml.ChildNodes)
                        {
                            if (nn.Name == "Signal")
                            {
                                Signal s = new Signal(Convert.ToInt32(nn.Attributes.GetNamedItem("ID").Value), content);
                                s.position = new Vector2(Convert.ToInt32(nn.Attributes.GetNamedItem("X").Value), Convert.ToInt32(nn.Attributes.GetNamedItem("Y").Value));
                                switch (nn.Attributes.GetNamedItem("InitialState").Value)
                                {
                                    case "Stop": s.state = Signal.State.Stop; break;
                                    case "Go": s.state = Signal.State.Go; break;
                                    case "Off": s.state = Signal.State.Off; break;
                                }

                                t.signal = s;
                                System.Diagnostics.Debug.WriteLine("Signal added");
                            }
                        }
                    }
                }
            }

            foreach (XmlNode xml in nodeList)
            {
                foreach (Track t in rr.tracks)
                {
                    if (t.id == Convert.ToInt32(xml.Attributes.GetNamedItem("ID").Value))
                    {
                        foreach (XmlNode nn in xml.ChildNodes)
                        {
                            if (nn.Name == "Sensor")
                            {
                                Sensor s = new Sensor();
                                s.id = Convert.ToInt32(nn.Attributes.GetNamedItem("ID").Value);
                                s.mySignal = rr.findSignal(Convert.ToInt32(nn.Attributes.GetNamedItem("SignalID").Value));

                                switch (nn.Attributes.GetNamedItem("InitialState").Value)
                                {
                                    case "On": s.state = Sensor.State.On; break;
                                    case "Off": s.state = Sensor.State.Off; break;
                                }
                                t.sensor = s;
                            }
                        }
                    }
                }
            }

            return rr;
        }
コード例 #2
0
        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {

                if (game.rr.tracks.Count() == 0)
                {
                    Track t = new StraightTrack();
                    t.gfx = content.Load<Texture2D>("StraightNB");
                    t.position = mousePos;
                    t.rotation = 0;
                    t.id = 1;
                    t.direction = true;
                    game.addTrack(t);
                }
                else
                {
                    Track oldT = game.rr.tracks[game.rr.tracks.Count() - 1];
                    Track t = new StraightTrack();
                    t.gfx = content.Load<Texture2D>("StraightNB");
                    t.direction = true;
                    if (direction == Direction.Right)
                    {
                        if (isCorner)
                        {
                            if (MathHelper.ToDegrees(oldT.rotation) == 180)
                            {
                                t.position = new Vector2(oldT.position.X, oldT.position.Y - 18);
                            }
                            else if (MathHelper.ToDegrees(oldT.rotation) == 270)
                                t.position = new Vector2(oldT.position.X + 59, oldT.position.Y - 59);
                        }
                        else
                        {
                            t.position = new Vector2(oldT.position.X + 50, oldT.position.Y);
                        }
                    }
                    if (direction == Direction.Left)
                    {
                        t.direction = false;
                        if (isCorner)
                        {
                            if (MathHelper.ToDegrees(oldT.rotation) == 0)
                            {
                                t.position = new Vector2(oldT.position.X - 50, oldT.position.Y);
                            }
                            else if (MathHelper.ToDegrees(oldT.rotation) == 90)
                                t.position = new Vector2(oldT.position.X - 109, oldT.position.Y + 41);
                        }
                        else
                        {
                            t.position = new Vector2(oldT.position.X - 50, oldT.position.Y);
                        }
                    }

                    if (direction == Direction.Down)
                    {
                        t.rotation = MathHelper.ToRadians(90);
                        if (isCorner)
                        {
                            if (MathHelper.ToDegrees(oldT.rotation) == 0)
                            {
                                t.position = new Vector2(oldT.position.X + 59, oldT.position.Y + 59);
                            }
                            else if (MathHelper.ToDegrees(oldT.rotation) == 270)
                                t.position = new Vector2(oldT.position.X + 18, oldT.position.Y);
                        }
                        else
                        {
                            t.position = new Vector2(oldT.position.X, oldT.position.Y + 50);
                        }
                    }
                    if (direction == Direction.Up)
                    {
                        t.direction = false;
                        t.rotation = MathHelper.ToRadians(90);
                        if (isCorner)
                        {
                            if (MathHelper.ToDegrees(oldT.rotation) == 90)
                            {
                                t.position = new Vector2(oldT.position.X, oldT.position.Y - 50);
                            }
                            else if (MathHelper.ToDegrees(oldT.rotation) == 180)
                                t.position = new Vector2(oldT.position.X- 41, oldT.position.Y - 109);
                        }
                        else
                        {
                            t.position = new Vector2(oldT.position.X, oldT.position.Y - 50);
                        }
                    }
                    t.prevTrack = game.rr.tracks[game.rr.tracks.Count() - 1];
                    t.id = oldT.id + 1;
                    game.rr.tracks[game.rr.tracks.Count() - 1].nextTrack = t;
                    game.addTrack(t);
                    isCorner = false;
                }
            }
        }