using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using TransportGame.Primitives;
namespace TransportGame.Model.Road
{
///
/// Represents a road segment
///
[XmlRoot("segment")]
public class RoadSegment
{
///
/// Gets or sets the id
///
[XmlAttribute("id")]
public int Id { get; set; }
///
/// Gets or sets the parent network
///
[XmlIgnore]
public RoadNetwork ParentNetwork { get; set; }
///
/// Gets or sets the id of the first terminal
///
[XmlAttribute("term1")]
public int Terminal1Id { get; set; }
///
/// Gets or sets the id of the second terminal
///
[XmlAttribute("term2")]
public int Terminal2Id { get; set; }
///
/// Gets or sets the first terminal
///
[XmlIgnore]
public RoadNode Terminal1
{
get
{
return (Terminal1Id == -1) ? null : ParentNetwork.Nodes[Terminal1Id];
}
set
{
Terminal1Id = (value == null) ? -1 : value.Id;
}
}
///
/// Gets or sets the second terminal
///
[XmlIgnore]
public RoadNode Terminal2
{
get
{
return (Terminal2Id == -1) ? null : ParentNetwork.Nodes[Terminal2Id];
}
set
{
Terminal2Id = (value == null) ? -1 : value.Id;
}
}
///
/// Gets or sets the number of lanes going from terminal 1 to terminal 2
///
[XmlAttribute("lanesTo2")]
public int LanesTo2 { get; set; }
///
/// Gets or sets the number of lanes going form terminal 2 to terminal 1
///
[XmlAttribute("lanesTo1")]
public int LanesTo1 { get; set; }
///
/// Gets the direction vector of this road segment
///
[XmlIgnore]
public Vector2 Direction
{
get
{
if (Terminal1Id == -1 || Terminal2Id == -1 || ParentNetwork == null)
return Vector2.Zero;
return (Terminal2.Position - Terminal1.Position).Normalized;
}
}
///
/// Gets the road segment as a line segment
///
///
public LineSegment AsLineSegment()
{
if (Terminal1Id == -1 || Terminal2Id == -1 || ParentNetwork == null)
throw new InvalidOperationException("Terminals are not initialized.");
return new LineSegment(Terminal1.Position, Terminal2.Position);
}
///
/// Initializes road segment
///
public RoadSegment()
{
LanesTo1 = 1;
LanesTo2 = 1;
Terminal1Id = -1;
Terminal2Id = -1;
}
public override string ToString()
{
return string.Format("(segment id={0}, {1}->{2})", Id, Terminal1, Terminal2);
}
}
}