Initial attempt at road generation

This commit is contained in:
2015-05-26 19:36:44 +03:00
parent b6b2dce32e
commit 22f1905b1b
10 changed files with 239 additions and 10 deletions

View File

@ -13,7 +13,8 @@ namespace TransportGame.MapViewer
None = 0,
Elevation = 1,
Population = 2,
All = Elevation | Population
RoadArticulations = 3,
All = Elevation | Population | RoadArticulations
};
/// <summary>
@ -57,9 +58,13 @@ namespace TransportGame.MapViewer
// Create texture on which to draw
Bitmap24 bitmap = new Bitmap24(Convert.ToInt32(map.Width * Scale), Convert.ToInt32(map.Height * Scale));
// First layer - cells
// Elevation, population
DrawPixels(bitmap, map, (layers & Layers.Elevation) > 0, (layers & Layers.Population) > 0);
// Roads
if ((layers & Layers.RoadArticulations) > 0)
DrawRoads(bitmap, map);
// Done
return bitmap;
}
@ -83,6 +88,12 @@ namespace TransportGame.MapViewer
int mapX = Convert.ToInt32(x / Scale);
int mapY = Convert.ToInt32(y / Scale);
if (mapX >= map.Width)
mapX = map.Width - 1;
if (mapY >= map.Height)
mapY = map.Height - 1;
// Draw water
if (map.IsWater(mapX, mapY))
bitmap[x, y] = WaterColor;
@ -108,5 +119,18 @@ namespace TransportGame.MapViewer
}
}
}
private void DrawRoads(Bitmap24 bitmap, Map map)
{
// Draw road segments
foreach (var pair in map.RoadNetwork.ArticulationSegments)
{
bitmap.DrawLine(Convert.ToInt32(pair.Value.Terminal1.X * Scale),
Convert.ToInt32(pair.Value.Terminal1.Y * Scale),
Convert.ToInt32(pair.Value.Terminal2.X * Scale),
Convert.ToInt32(pair.Value.Terminal2.Y * Scale),
Colors.Black);
}
}
}
}

View File

@ -62,6 +62,7 @@
<CheckBox IsChecked="{Binding LayersAll}">(All)</CheckBox>
<CheckBox IsChecked="{Binding LayerElevation}">Elevation</CheckBox>
<CheckBox IsChecked="{Binding LayerPopulation}">Population</CheckBox>
<CheckBox IsChecked="{Binding LayerRoadArticulations}">Roads (articulation map)</CheckBox>
<Button Name="buttonRender" Grid.Column="1" Content="Render" Click="buttonRender_Click" />

View File

@ -109,10 +109,10 @@ namespace TransportGame.MapViewer
{
get
{
if (LayerElevation && LayerPopulation)
if (LayerElevation && LayerPopulation && LayerRoadArticulations)
return true;
if (!LayerElevation && !LayerPopulation)
if (!LayerElevation && !LayerPopulation && !LayerRoadArticulations)
return false;
return null;
@ -171,6 +171,28 @@ namespace TransportGame.MapViewer
}
}
/// <summary>
/// Gets or sets the population layer flag
/// </summary>
public bool LayerRoadArticulations
{
get
{
return _layers.HasFlag(MapRenderer.Layers.RoadArticulations);
}
set
{
if (value) _layers |= MapRenderer.Layers.RoadArticulations;
else _layers &= ~MapRenderer.Layers.RoadArticulations;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("LayerRoadArticulations"));
PropertyChanged(this, new PropertyChangedEventArgs("LayersAll"));
}
}
}
#endregion
/// <summary>

View File

@ -68,5 +68,35 @@ namespace TransportGame.MapViewer.Model
return 3 * (x + y * Width);
}
public void DrawLine(int x0, int y0, int x1, int y1, Color color)
{
int dx = Math.Abs(x1 - x0);
int sx = x0 < x1 ? 1 : -1;
int dy = Math.Abs(y1 - y0);
int sy = y0 < y1 ? 1 : -1;
int err = (dx > dy ? dx : -dy) / 2, e2;
for (; ; )
{
this[x0, y0] = color;
if (x0 == x1 && y0 == y1)
break;
e2 = err;
if (e2 > -dx)
{
err -= dy;
x0 += sx;
}
if (e2 < dy)
{
err += dx;
y0 += sy;
}
}
}
}
}

View File

@ -14,7 +14,20 @@ namespace TransportGame.MapViewer.Storage
{
public static Map Read(string file)
{
return SerializationHelper.DeserializeXml<Map>(file);
Map map = SerializationHelper.DeserializeXml<Map>(file);
// Fix road network issue
foreach (var pair in map.RoadNetwork.ArticulationSegments)
pair.Value.ParentNetwork = map.RoadNetwork;
foreach (var pair in map.RoadNetwork.IntersectionSegments)
pair.Value.ParentNetwork = map.RoadNetwork;
foreach (var pair in map.RoadNetwork.Nodes)
pair.Value.ParentNetwork = map.RoadNetwork;
// Done
return map;
}
}
}