Started to implement road generation.

This commit is contained in:
2015-05-20 11:26:46 +03:00
parent 11ca59d8bb
commit 7bd2b7b255
24 changed files with 616 additions and 119 deletions

View File

@ -0,0 +1,147 @@
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;
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: bc333e8a29fbb8242856e43239c57425
timeCreated: 1431612790
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace TransportGame.Model.Road
{
/// <summary>
/// Road node
/// </summary>
[XmlRoot("node")]
public class RoadNode
{
/// <summary>
/// Gets or sets a unique identifier for this node
/// </summary>
[XmlAttribute("id")]
public int Id { get; set; }
/// <summary>
/// Gets or sets the X coordinate of the node
/// </summary>
[XmlAttribute("x")]
public float X { get; set; }
/// <summary>
/// Gets or sets the Y coordinate of the node
/// </summary>
[XmlAttribute("y")]
public float Y { get; set; }
/// <summary>
/// Gets or sets the adjacent articulation segment IDs
/// </summary>
[XmlArray("articulationSegments")]
[XmlArrayItem("id")]
public List<int> ArticulationSegmentIds { get; set; }
/// <summary>
/// Gets or sets the adjacent articulation segment IDs
/// </summary>
[XmlArray("intersectionSegments")]
[XmlArrayItem("id")]
public List<int> IntersectionSegmentIds { get; set; }
/// <summary>
/// Gets or sets the parent network
/// </summary>
[XmlIgnore]
public RoadNetwork ParentNetwork { get; set; }
/// <summary>
/// Gets the adjacent articulation segments
/// </summary>
[XmlIgnore]
public IEnumerable<RoadSegment> ArticulationSegments
{
get
{
return ArticulationSegmentIds.Select(id => ParentNetwork.ArticulationSegments[id]);
}
}
/// <summary>
/// Gets the adjacent setments
/// </summary>
[XmlIgnore]
public IEnumerable<RoadSegment> Segments
{
get
{
return IntersectionSegmentIds.Select(id => ParentNetwork.ArticulationSegments[id]);
}
}
/// <summary>
/// Initializes the node
/// </summary>
public RoadNode()
{
ArticulationSegmentIds = new List<int>();
IntersectionSegmentIds = new List<int>();
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 5be58fb83def956479e9e3af7e96a347
timeCreated: 1431600782
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace TransportGame.Model.Road
{
/// <summary>
/// Represents a road segment
/// </summary>
[XmlRoot("segment")]
public class RoadSegment
{
/// <summary>
/// Gets or sets the id
/// </summary>
[XmlAttribute("id")]
public int Id { get; set; }
/// <summary>
/// Gets or sets the parent network
/// </summary>
[XmlIgnore]
public RoadNetwork ParentNetwork { get; set; }
/// <summary>
/// Gets or sets the id of the first terminal
/// </summary>
[XmlAttribute("term1")]
public int Terminal1Id { get; set; }
/// <summary>
/// Gets or sets the id of the second terminal
/// </summary>
[XmlAttribute("term2")]
public int Terminal2Id { get; set; }
/// <summary>
/// Gets or sets the first terminal
/// </summary>
[XmlIgnore]
public RoadNode Terminal1
{
get
{
return ParentNetwork.Nodes[Terminal1Id];
}
set
{
Terminal1Id = value.Id;
}
}
/// <summary>
/// Gets or sets the second terminal
/// </summary>
[XmlIgnore]
public RoadNode Terminal2
{
get
{
return ParentNetwork.Nodes[Terminal2Id];
}
set
{
Terminal2Id = value.Id;
}
}
/// <summary>
/// Gets or sets the number of lanes going from terminal 1 to terminal 2
/// </summary>
[XmlAttribute("lanesTo2")]
public int LanesTo2 { get; set; }
/// <summary>
/// Gets or sets the number of lanes going form terminal 2 to terminal 1
/// </summary>
[XmlAttribute("lanesTo1")]
public int LanesTo1 { get; set; }
/// <summary>
/// Initializes road segment
/// </summary>
public RoadSegment()
{
LanesTo1 = 1;
LanesTo2 = 1;
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 51c5431eea527e34a8de870e7648758c
timeCreated: 1431600775
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: