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; }
public Sensor(int id, State state, Signal signal) { this.id = id; this.state = state; this.mySignal = signal; }
private void pbEditor_MouseClick(object sender, MouseEventArgs e) { mousePos = new Vector2(e.X, e.Y); game.ed = this; cornerFlip = 0; isCorner = false; game.setCont(); direction = Direction.Right; if (e.Button == System.Windows.Forms.MouseButtons.Right) { RailRoad r = game.rr; foreach (Track t in r.tracks) { int xxx = t.gfx.Width + Convert.ToInt32(t.position.X); int xxy = Convert.ToInt32(t.position.X); int yyy = t.gfx.Height + Convert.ToInt32(t.position.Y); int yyx = Convert.ToInt32(t.position.Y); if (xxy < e.X && e.X < xxx) { if (yyx < e.Y && e.Y < yyy) { Signal s = new Signal(1, game.Content); if (MathHelper.ToDegrees(t.rotation) == 90) { s.position = new Vector2(t.position.X - 20, t.position.Y + (t.position.Y / 2) ); } else { s.position = new Vector2(t.position.X + (t.gfx.Width / 2) - 20, t.position.Y + t.gfx.Height + 2); } t.signal = s; } } } } //game.setStartPoint(place); }