Finished road generator
This commit is contained in:
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml.Serialization;
|
||||
using TransportGame.Utils;
|
||||
|
||||
namespace TransportGame.Model.Road
|
||||
{
|
||||
@ -12,20 +13,19 @@ namespace TransportGame.Model.Road
|
||||
private int lastNodeId = -1, lastSegmentId = -1;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the road nodes
|
||||
/// Gets the road nodes
|
||||
/// </summary>
|
||||
[XmlIgnore]
|
||||
public Dictionary<int, RoadNode> Nodes { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the road segments for the articulation graph
|
||||
/// Gets 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
|
||||
/// Gets the road segments for the intersection graph
|
||||
/// </summary>
|
||||
[XmlIgnore]
|
||||
public Dictionary<int, RoadSegment> IntersectionSegments { get; private set; }
|
||||
@ -44,7 +44,10 @@ namespace TransportGame.Model.Road
|
||||
{
|
||||
Nodes.Clear();
|
||||
foreach (var node in value)
|
||||
{
|
||||
node.ParentNetwork = this;
|
||||
Nodes.Add(node.Id, node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -62,7 +65,10 @@ namespace TransportGame.Model.Road
|
||||
{
|
||||
ArticulationSegments.Clear();
|
||||
foreach (var segment in value)
|
||||
{
|
||||
segment.ParentNetwork = this;
|
||||
ArticulationSegments.Add(segment.Id, segment);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -80,7 +86,10 @@ namespace TransportGame.Model.Road
|
||||
{
|
||||
ArticulationSegments.Clear();
|
||||
foreach (var segment in value)
|
||||
{
|
||||
segment.ParentNetwork = this;
|
||||
ArticulationSegments.Add(segment.Id, segment);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -97,8 +106,9 @@ namespace TransportGame.Model.Road
|
||||
/// <summary>
|
||||
/// Creates a node and returns it
|
||||
/// </summary>
|
||||
/// <returns>Created node</returns>
|
||||
public RoadNode CreateNode()
|
||||
/// <param name="pos">Position</param>
|
||||
/// <returns></returns>
|
||||
public RoadNode CreateNode(Vector2 pos)
|
||||
{
|
||||
// Skip IDs that already exist
|
||||
while (Nodes.ContainsKey(++lastNodeId)) ;
|
||||
@ -107,18 +117,39 @@ namespace TransportGame.Model.Road
|
||||
RoadNode node = new RoadNode()
|
||||
{
|
||||
Id = lastNodeId,
|
||||
ParentNetwork = this
|
||||
ParentNetwork = this,
|
||||
Position = pos
|
||||
};
|
||||
|
||||
Nodes.Add(node.Id, node);
|
||||
return node;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a node and returns it
|
||||
/// </summary>
|
||||
/// <returns>Created node</returns>
|
||||
public RoadNode CreateNode()
|
||||
{
|
||||
return CreateNode(Vector2.Zero);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a segment and returns it
|
||||
/// </summary>
|
||||
/// <returns>Created segment</returns>
|
||||
public RoadSegment CreateArticulationSegment()
|
||||
{
|
||||
return CreateArticulationSegment(null, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a segment and returns it
|
||||
/// </summary>
|
||||
/// <param name="term1">First terminal</param>
|
||||
/// <param name="term2">Second terminal</param>
|
||||
/// <returns>Created segment</returns>
|
||||
public RoadSegment CreateArticulationSegment(RoadNode term1, RoadNode term2)
|
||||
{
|
||||
// Skip IDs that already exist
|
||||
while (ArticulationSegments.ContainsKey(++lastSegmentId)) ;
|
||||
@ -127,13 +158,59 @@ namespace TransportGame.Model.Road
|
||||
RoadSegment segment = new RoadSegment()
|
||||
{
|
||||
Id = lastSegmentId,
|
||||
ParentNetwork = this
|
||||
ParentNetwork = this,
|
||||
Terminal1 = term1,
|
||||
Terminal2 = term2
|
||||
};
|
||||
|
||||
// Set links
|
||||
if (term1 != null)
|
||||
term1.ArticulationSegmentIds.Add(segment.Id);
|
||||
|
||||
if (term2 != null)
|
||||
term2.ArticulationSegmentIds.Add(segment.Id);
|
||||
|
||||
ArticulationSegments.Add(segment.Id, segment);
|
||||
return segment;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an articulation segment
|
||||
/// </summary>
|
||||
/// <param name="term1">First terminal</param>
|
||||
/// <param name="term2pos">Position of second terminal</param>
|
||||
/// <returns>Road segment</returns>
|
||||
public RoadSegment CreateArticulationSegment(RoadNode term1, Vector2 term2pos)
|
||||
{
|
||||
var term2 = CreateNode(term2pos);
|
||||
return CreateArticulationSegment(term1, term2);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an articulation segment
|
||||
/// </summary>
|
||||
/// <param name="term1pos">Position of first terminal</param>
|
||||
/// <param name="term2">Second terminal</param>
|
||||
/// <returns>Road segment</returns>
|
||||
public RoadSegment CreateArticulationSegment(Vector2 term1pos, RoadNode term2)
|
||||
{
|
||||
var term1 = CreateNode(term1pos);
|
||||
return CreateArticulationSegment(term1, term2);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an articulation segment
|
||||
/// </summary>
|
||||
/// <param name="term1pos">Position of first terminal</param>
|
||||
/// <param name="term2pos">Position of second terminal</param>
|
||||
/// <returns>Road segment</returns>
|
||||
public RoadSegment CreateArticulationSegment(Vector2 term1pos, Vector2 term2pos)
|
||||
{
|
||||
var term1 = CreateNode(term1pos);
|
||||
var term2 = CreateNode(term2pos);
|
||||
return CreateArticulationSegment(term1, term2);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a segment and returns it
|
||||
/// </summary>
|
||||
@ -153,5 +230,35 @@ namespace TransportGame.Model.Road
|
||||
IntersectionSegments.Add(segment.Id, segment);
|
||||
return segment;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Splits an articulation segment in two segments
|
||||
/// </summary>
|
||||
/// <param name="segment"></param>
|
||||
/// <param name="point"></param>
|
||||
/// <returns>Newly created road node</returns>
|
||||
public RoadNode SplitArticulationSegment(RoadSegment segment, Vector2 point)
|
||||
{
|
||||
// Get current terminals
|
||||
var term1 = segment.Terminal1;
|
||||
var term2 = segment.Terminal2;
|
||||
int l1 = segment.LanesTo1, l2 = segment.LanesTo2;
|
||||
|
||||
// Create new terminal
|
||||
var newTerm = CreateNode(point);
|
||||
|
||||
// Delete exinsting segment
|
||||
term1.ArticulationSegmentIds.Remove(segment.Id);
|
||||
term2.ArticulationSegmentIds.Remove(segment.Id);
|
||||
ArticulationSegments.Remove(segment.Id);
|
||||
|
||||
// Create split segments
|
||||
var seg1 = CreateArticulationSegment(term1, newTerm);
|
||||
var seg2 = CreateArticulationSegment(newTerm, term2);
|
||||
seg1.LanesTo1 = seg2.LanesTo1 = l1;
|
||||
seg1.LanesTo2 = seg2.LanesTo2 = l2;
|
||||
|
||||
return newTerm;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ namespace TransportGame.Model.Road
|
||||
/// Road node
|
||||
/// </summary>
|
||||
[XmlRoot("node")]
|
||||
public class RoadNode
|
||||
public class RoadNode : IPositionable
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets a unique identifier for this node
|
||||
@ -63,7 +63,7 @@ namespace TransportGame.Model.Road
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the adjacent setments
|
||||
/// Gets the adjacent segments
|
||||
/// </summary>
|
||||
[XmlIgnore]
|
||||
public IEnumerable<RoadSegment> Segments
|
||||
@ -82,5 +82,27 @@ namespace TransportGame.Model.Road
|
||||
ArticulationSegmentIds = new List<int>();
|
||||
IntersectionSegmentIds = new List<int>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the position
|
||||
/// </summary>
|
||||
[XmlIgnore]
|
||||
public Vector2 Position
|
||||
{
|
||||
get
|
||||
{
|
||||
return new Vector2(X, Y);
|
||||
}
|
||||
set
|
||||
{
|
||||
X = value.X;
|
||||
Y = value.Y;
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return string.Format("(node id={0}, {1})", Id, Position);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -44,11 +44,11 @@ namespace TransportGame.Model.Road
|
||||
{
|
||||
get
|
||||
{
|
||||
return ParentNetwork.Nodes[Terminal1Id];
|
||||
return (Terminal1Id == -1) ? null : ParentNetwork.Nodes[Terminal1Id];
|
||||
}
|
||||
set
|
||||
{
|
||||
Terminal1Id = value.Id;
|
||||
Terminal1Id = (value == null) ? -1 : value.Id;
|
||||
}
|
||||
}
|
||||
|
||||
@ -60,11 +60,11 @@ namespace TransportGame.Model.Road
|
||||
{
|
||||
get
|
||||
{
|
||||
return ParentNetwork.Nodes[Terminal2Id];
|
||||
return (Terminal2Id == -1) ? null : ParentNetwork.Nodes[Terminal2Id];
|
||||
}
|
||||
set
|
||||
{
|
||||
Terminal2Id = value.Id;
|
||||
Terminal2Id = (value == null) ? -1 : value.Id;
|
||||
}
|
||||
}
|
||||
|
||||
@ -79,7 +79,7 @@ namespace TransportGame.Model.Road
|
||||
/// </summary>
|
||||
[XmlAttribute("lanesTo1")]
|
||||
public int LanesTo1 { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes road segment
|
||||
/// </summary>
|
||||
@ -87,6 +87,13 @@ namespace TransportGame.Model.Road
|
||||
{
|
||||
LanesTo1 = 1;
|
||||
LanesTo2 = 1;
|
||||
Terminal1Id = -1;
|
||||
Terminal2Id = -1;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return string.Format("(segment id={0}, {1}->{2})", Id, Terminal1, Terminal2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user