Started to implement road generation.
This commit is contained in:
@ -3,6 +3,8 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml.Serialization;
|
||||
using TransportGame.Model.Road;
|
||||
using TransportGame.Utils;
|
||||
|
||||
namespace TransportGame.Model
|
||||
{
|
||||
@ -10,6 +12,15 @@ namespace TransportGame.Model
|
||||
public class Map
|
||||
{
|
||||
private float[,] grid;
|
||||
private float[,] population;
|
||||
|
||||
#region Properties
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the biome
|
||||
/// </summary>
|
||||
[XmlElement("biome")]
|
||||
public Biome Biome { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the water level
|
||||
@ -17,17 +28,11 @@ namespace TransportGame.Model
|
||||
[XmlElement("waterLevel")]
|
||||
public float WaterLevel { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the biome
|
||||
/// </summary>
|
||||
[XmlElement("biome")]
|
||||
public Biome Biome { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the heights array in range [0,1]
|
||||
/// </summary>
|
||||
[XmlIgnore()]
|
||||
public float[,] Heights
|
||||
public float[,] Heightmap
|
||||
{
|
||||
get
|
||||
{
|
||||
@ -38,49 +43,85 @@ namespace TransportGame.Model
|
||||
/// <summary>
|
||||
/// Gets or sets the heights as raw bytes
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Bytes are stored as such:
|
||||
///
|
||||
/// Offset Size Content
|
||||
/// ------------------------
|
||||
/// 0 4 Width
|
||||
/// 4 8 Height
|
||||
/// 8 var 32bit floating point values
|
||||
///
|
||||
/// </remarks>
|
||||
[XmlElement("heights")]
|
||||
public byte[] HeightsRaw
|
||||
[XmlElement("heightmap")]
|
||||
public byte[] HeightmapRaw
|
||||
{
|
||||
get
|
||||
{
|
||||
List<byte> bytes = new List<byte>();
|
||||
|
||||
bytes.AddRange(BitConverter.GetBytes(Width));
|
||||
bytes.AddRange(BitConverter.GetBytes(Height));
|
||||
|
||||
for (int x = 0; x < Width; x++)
|
||||
for (int y = 0; y < Height; y++)
|
||||
bytes.AddRange(BitConverter.GetBytes(grid[x, y]));
|
||||
|
||||
return bytes.ToArray();
|
||||
return grid.ToByteArray();
|
||||
}
|
||||
set
|
||||
{
|
||||
int pos = 0;
|
||||
int w = BitConverter.ToInt32(value, pos); pos += sizeof(int);
|
||||
int h = BitConverter.ToInt32(value, pos); pos += sizeof(int);
|
||||
|
||||
grid = new float[w, h];
|
||||
|
||||
for (int x = 0; x < w; x++)
|
||||
for (int y = 0; y < h; y++)
|
||||
{
|
||||
grid[x, y] = BitConverter.ToSingle(value, pos);
|
||||
pos += sizeof(float);
|
||||
}
|
||||
grid = value.GetFloatMatrix();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets width of heightmap
|
||||
/// </summary>
|
||||
[XmlIgnore]
|
||||
public int Width { get { return (grid == null) ? 0 : grid.GetLength(0); } }
|
||||
|
||||
/// <summary>
|
||||
/// Gets height of heightmap
|
||||
/// </summary>
|
||||
[XmlIgnore]
|
||||
public int Height { get { return (grid == null) ? 0 : grid.GetLength(1); } }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the population map
|
||||
/// </summary>
|
||||
[XmlIgnore()]
|
||||
public float[,] Population
|
||||
{
|
||||
get
|
||||
{
|
||||
return population;
|
||||
}
|
||||
set
|
||||
{
|
||||
population = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the population as raw bytes
|
||||
/// </summary>
|
||||
[XmlElement("population")]
|
||||
public byte[] PopulationRaw
|
||||
{
|
||||
get
|
||||
{
|
||||
return population.ToByteArray();
|
||||
}
|
||||
set
|
||||
{
|
||||
population = value.GetFloatMatrix();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets width of population map
|
||||
/// </summary>
|
||||
[XmlIgnore]
|
||||
public int PopulationWidth { get { return (population == null) ? 0 : population.GetLength(0); } }
|
||||
|
||||
/// <summary>
|
||||
/// Gets height of population map
|
||||
/// </summary>
|
||||
[XmlIgnore]
|
||||
public int PopulationHeight { get { return (population == null) ? 0 : population.GetLength(1); } }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the articulation road network
|
||||
/// </summary>
|
||||
[XmlElement("roadNetwork")]
|
||||
public RoadNetwork RoadNetwork { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the map
|
||||
/// </summary>
|
||||
@ -94,43 +135,36 @@ namespace TransportGame.Model
|
||||
/// <summary>
|
||||
/// Initializes the map
|
||||
/// </summary>
|
||||
/// <param name="width"></param>
|
||||
/// <param name="height"></param>
|
||||
/// <param name="width">Width</param>
|
||||
/// <param name="height">Height</param>
|
||||
public Map(int width, int height)
|
||||
{
|
||||
grid = new float[width, height];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the cell at specified position in range [0, Biome.Height]
|
||||
/// Gets the cell at specified position in range [0, Biome.Height]
|
||||
/// </summary>
|
||||
/// <param name="x">X</param>
|
||||
/// <param name="y">Y</param>
|
||||
/// <returns>Cell</returns>
|
||||
[XmlIgnore]
|
||||
public float this[int x, int y]
|
||||
/// <returns>Value</returns>
|
||||
public float GetHeight(int x, int y)
|
||||
{
|
||||
get
|
||||
{
|
||||
return grid[x, y] * Biome.Height;
|
||||
}
|
||||
set
|
||||
{
|
||||
grid[x, y] = value / Biome.Height;
|
||||
}
|
||||
return grid[x, y] * Biome.Height;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets width of map
|
||||
/// Sets the height at specified position in range [0, Biome.Height]
|
||||
/// </summary>
|
||||
[XmlIgnore]
|
||||
public int Width { get { return grid.GetLength(0); } }
|
||||
|
||||
/// <summary>
|
||||
/// Gets height of map
|
||||
/// </summary>
|
||||
[XmlIgnore]
|
||||
public int Height { get { return grid.GetLength(1); } }
|
||||
/// <param name="x">X</param>
|
||||
/// <param name="y">Y</param>
|
||||
/// <param name="value">Value</param>
|
||||
public void SetHeight(int x, int y, float value)
|
||||
{
|
||||
grid[x, y] = value / Biome.Height;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if specified cell is a water cell
|
||||
@ -140,7 +174,7 @@ namespace TransportGame.Model
|
||||
/// <returns></returns>
|
||||
public bool IsWater(int x, int y)
|
||||
{
|
||||
return this[x, y] <= WaterLevel;
|
||||
return GetHeight(x, y) <= WaterLevel;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
9
Game/Assets/Scripts/Model/Road.meta
Normal file
9
Game/Assets/Scripts/Model/Road.meta
Normal file
@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dc46a301e57a8af41872e48f0b2cbd61
|
||||
folderAsset: yes
|
||||
timeCreated: 1431600774
|
||||
licenseType: Free
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
147
Game/Assets/Scripts/Model/Road/RoadNetwork.cs
Normal file
147
Game/Assets/Scripts/Model/Road/RoadNetwork.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
12
Game/Assets/Scripts/Model/Road/RoadNetwork.cs.meta
Normal file
12
Game/Assets/Scripts/Model/Road/RoadNetwork.cs.meta
Normal 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:
|
86
Game/Assets/Scripts/Model/Road/RoadNode.cs
Normal file
86
Game/Assets/Scripts/Model/Road/RoadNode.cs
Normal 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>();
|
||||
}
|
||||
}
|
||||
}
|
12
Game/Assets/Scripts/Model/Road/RoadNode.cs.meta
Normal file
12
Game/Assets/Scripts/Model/Road/RoadNode.cs.meta
Normal 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:
|
92
Game/Assets/Scripts/Model/Road/RoadSegment.cs
Normal file
92
Game/Assets/Scripts/Model/Road/RoadSegment.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
12
Game/Assets/Scripts/Model/Road/RoadSegment.cs.meta
Normal file
12
Game/Assets/Scripts/Model/Road/RoadSegment.cs.meta
Normal 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:
|
Reference in New Issue
Block a user