Implemented population center generator
This commit is contained in:
@ -5,6 +5,7 @@ using System.Text;
|
||||
using System.Xml.Serialization;
|
||||
using TransportGame.Model.Road;
|
||||
using TransportGame.Utils;
|
||||
using UnityEngine;
|
||||
|
||||
namespace TransportGame.Model
|
||||
{
|
||||
@ -12,7 +13,6 @@ namespace TransportGame.Model
|
||||
public class Map
|
||||
{
|
||||
private float[,] heightmap;
|
||||
private float[,] population;
|
||||
|
||||
#region Properties
|
||||
|
||||
@ -69,48 +69,11 @@ namespace TransportGame.Model
|
||||
public int Height { get { return (heightmap == null) ? 0 : heightmap.GetLength(1); } }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the population map
|
||||
/// Gets or sets 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); } }
|
||||
[XmlArray("populationCenters")]
|
||||
[XmlArrayItem("center")]
|
||||
public List<Point> PopulationCenters { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the articulation road network
|
||||
@ -130,6 +93,7 @@ namespace TransportGame.Model
|
||||
/// </remarks>
|
||||
public Map()
|
||||
{
|
||||
PopulationCenters = new List<Point>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -140,6 +104,7 @@ namespace TransportGame.Model
|
||||
public Map(int width, int height)
|
||||
{
|
||||
heightmap = new float[width, height];
|
||||
PopulationCenters = new List<Point>();
|
||||
}
|
||||
|
||||
#endregion
|
||||
@ -204,5 +169,33 @@ namespace TransportGame.Model
|
||||
|
||||
return dx * dx + dy * dy;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets population using terrain coordinates
|
||||
/// </summary>
|
||||
/// <param name="x">X</param>
|
||||
/// <param name="y">Y</param>
|
||||
/// <returns>Population</returns>
|
||||
public float GetPopulation(int x, int y)
|
||||
{
|
||||
const int maxDistance = 400;
|
||||
float value = 0;
|
||||
|
||||
foreach (var point in PopulationCenters)
|
||||
{
|
||||
int x1 = x - point.X;
|
||||
int y1 = y - point.Y;
|
||||
|
||||
int dist = x1 * x1 + y1 * y1;
|
||||
if (dist < maxDistance * maxDistance)
|
||||
{
|
||||
float influence = 1 - (float)dist / (float)(maxDistance * maxDistance);
|
||||
influence = Mathf.Pow(influence, 3); // Ease
|
||||
value = Mathf.Clamp01(value + influence);
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
23
Game/Assets/Scripts/Model/Point.cs
Normal file
23
Game/Assets/Scripts/Model/Point.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace TransportGame.Model
|
||||
{
|
||||
public class Point
|
||||
{
|
||||
public int X { get; set; }
|
||||
public int Y { get; set; }
|
||||
|
||||
public Point()
|
||||
{
|
||||
}
|
||||
|
||||
public Point(int x, int y)
|
||||
{
|
||||
X = x;
|
||||
Y = y;
|
||||
}
|
||||
}
|
||||
}
|
12
Game/Assets/Scripts/Model/Point.cs.meta
Normal file
12
Game/Assets/Scripts/Model/Point.cs.meta
Normal file
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 94117dad82214b44382141a14825994e
|
||||
timeCreated: 1432281052
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user