city-generation/Game/Assets/Scripts/Model/Road/RoadNetwork.cs

148 lines
4.0 KiB
C#
Raw Normal View History

2015-05-20 08:26:46 +00:00
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;
/// <summary>
/// Gets or sets the road nodes
/// </summary>
[XmlIgnore]
public Dictionary<int, RoadNode> Nodes { get; private set; }
/// <summary>
/// Gets or sets the road segments for the articulation graph
/// </summary>
[XmlIgnore]
public Dictionary<int, RoadSegment> ArticulationSegments { get; private set; }
/// <summary>
/// Gets or sets the road segments for the intersection graph
/// </summary>
[XmlIgnore]
public Dictionary<int, RoadSegment> IntersectionSegments { get; private set; }
/// <summary>
/// Gets or sets the nodes
/// </summary>
[XmlArray("nodes")]
public RoadNode[] NodesArray
{
get
{
return Nodes.Values.ToArray();
}
set
{
Nodes.Clear();
foreach (var node in value)
Nodes.Add(node.Id, node);
}
}
/// <summary>
/// Gets or sets the segments
/// </summary>
[XmlArray("articulationGraph")]
public RoadSegment[] ArticulationSegmentsArray
{
get
{
return ArticulationSegments.Values.ToArray();
}
set
{
ArticulationSegments.Clear();
foreach (var segment in value)
ArticulationSegments.Add(segment.Id, segment);
}
}
/// <summary>
/// Gets or sets the segments
/// </summary>
[XmlArray("intersectionGraph")]
public RoadSegment[] IntersectionSegmentsArray
{
get
{
return ArticulationSegments.Values.ToArray();
}
set
{
ArticulationSegments.Clear();
foreach (var segment in value)
ArticulationSegments.Add(segment.Id, segment);
}
}
/// <summary>
/// Creates a node and returns it
/// </summary>
/// <returns>Created node</returns>
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;
}
/// <summary>
/// Creates a segment and returns it
/// </summary>
/// <returns>Created segment</returns>
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;
}
/// <summary>
/// Creates a segment and returns it
/// </summary>
/// <returns>Created segment</returns>
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;
}
}
}