using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Serialization; namespace TransportGame.Model.Road { [XmlRoot("roadNetwork")] public class RoadNetwork { private int lastNodeId = -1, lastSegmentId = -1; /// /// Gets or sets the road nodes /// [XmlIgnore] public Dictionary Nodes { get; private set; } /// /// Gets or sets the road segments for the articulation graph /// [XmlIgnore] public Dictionary ArticulationSegments { get; private set; } /// /// Gets or sets the road segments for the intersection graph /// [XmlIgnore] public Dictionary IntersectionSegments { get; private set; } /// /// Gets or sets the nodes /// [XmlArray("nodes")] public RoadNode[] NodesArray { get { return Nodes.Values.ToArray(); } set { Nodes.Clear(); foreach (var node in value) Nodes.Add(node.Id, node); } } /// /// Gets or sets the segments /// [XmlArray("articulationGraph")] public RoadSegment[] ArticulationSegmentsArray { get { return ArticulationSegments.Values.ToArray(); } set { ArticulationSegments.Clear(); foreach (var segment in value) ArticulationSegments.Add(segment.Id, segment); } } /// /// Gets or sets the segments /// [XmlArray("intersectionGraph")] public RoadSegment[] IntersectionSegmentsArray { get { return ArticulationSegments.Values.ToArray(); } set { ArticulationSegments.Clear(); foreach (var segment in value) ArticulationSegments.Add(segment.Id, segment); } } /// /// Initializes the road network /// public RoadNetwork() { Nodes = new Dictionary(); ArticulationSegments = new Dictionary(); IntersectionSegments = new Dictionary(); } /// /// Creates a node and returns it /// /// Created node public RoadNode CreateNode() { // Skip IDs that already exist while (Nodes.ContainsKey(++lastNodeId)) ; // Create node RoadNode node = new RoadNode() { Id = lastNodeId, ParentNetwork = this }; Nodes.Add(node.Id, node); return node; } /// /// Creates a segment and returns it /// /// Created segment public RoadSegment CreateArticulationSegment() { // Skip IDs that already exist while (ArticulationSegments.ContainsKey(++lastSegmentId)) ; // Create segment RoadSegment segment = new RoadSegment() { Id = lastSegmentId, ParentNetwork = this }; ArticulationSegments.Add(segment.Id, segment); return segment; } /// /// Creates a segment and returns it /// /// Created segment public RoadSegment CreateIntersectionSegment() { // Skip IDs that already exist while (IntersectionSegments.ContainsKey(++lastSegmentId)) ; // Create segment RoadSegment segment = new RoadSegment() { Id = lastSegmentId, ParentNetwork = this }; IntersectionSegments.Add(segment.Id, segment); return segment; } } }