Refractoring
This commit is contained in:
parent
670f260a4c
commit
6b008cfa9a
@ -58,29 +58,29 @@
|
||||
<Compile Include="Assets\Scripts\Model\Biome.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Building.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\BuildingLot.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\CityMap.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Config\BuildingGeneratorConfig.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Config\RoadGeneratorConfig.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Config\TerrainGeneratorConfig.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\IPositionable.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\LineSegment.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Map.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Polygon.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Range.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Rectangle.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Road\RoadNetwork.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Road\RoadNode.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Road\RoadSegment.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Texture.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Vector2.cs" />
|
||||
<Compile Include="Assets\Scripts\Primitives\IPositionable.cs" />
|
||||
<Compile Include="Assets\Scripts\Primitives\Interval.cs" />
|
||||
<Compile Include="Assets\Scripts\Primitives\LineSegment.cs" />
|
||||
<Compile Include="Assets\Scripts\Primitives\Polygon.cs" />
|
||||
<Compile Include="Assets\Scripts\Primitives\Rectangle.cs" />
|
||||
<Compile Include="Assets\Scripts\Primitives\Vector2.cs" />
|
||||
<Compile Include="Assets\Scripts\Unity\BuildingMeshGenerator.cs" />
|
||||
<Compile Include="Assets\Scripts\Unity\InitializeScript.cs" />
|
||||
<Compile Include="Assets\Scripts\Unity\RoadMeshGenerator.cs" />
|
||||
<Compile Include="Assets\Scripts\Unity\TerrainGeneratorScript.cs" />
|
||||
<Compile Include="Assets\Scripts\Utils\Algorithms.cs" />
|
||||
<Compile Include="Assets\Scripts\Utils\Algorithms\GridTraverseAlgorithm.cs" />
|
||||
<Compile Include="Assets\Scripts\Utils\ColorHelper.cs" />
|
||||
<Compile Include="Assets\Scripts\Utils\Expression.cs" />
|
||||
<Compile Include="Assets\Scripts\Utils\Logger.cs" />
|
||||
<Compile Include="Assets\Scripts\Utils\MathHelper.cs" />
|
||||
<Compile Include="Assets\Scripts\Utils\QuadTree.cs" />
|
||||
<Compile Include="Assets\Scripts\Utils\RandomExtensions.cs" />
|
||||
<Compile Include="Assets\Scripts\Utils\SerializationHelper.cs" />
|
||||
|
@ -58,29 +58,29 @@
|
||||
<Compile Include="Assets\Scripts\Model\Biome.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Building.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\BuildingLot.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\CityMap.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Config\BuildingGeneratorConfig.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Config\RoadGeneratorConfig.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Config\TerrainGeneratorConfig.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\IPositionable.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\LineSegment.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Map.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Polygon.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Range.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Rectangle.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Road\RoadNetwork.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Road\RoadNode.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Road\RoadSegment.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Texture.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Vector2.cs" />
|
||||
<Compile Include="Assets\Scripts\Primitives\IPositionable.cs" />
|
||||
<Compile Include="Assets\Scripts\Primitives\Interval.cs" />
|
||||
<Compile Include="Assets\Scripts\Primitives\LineSegment.cs" />
|
||||
<Compile Include="Assets\Scripts\Primitives\Polygon.cs" />
|
||||
<Compile Include="Assets\Scripts\Primitives\Rectangle.cs" />
|
||||
<Compile Include="Assets\Scripts\Primitives\Vector2.cs" />
|
||||
<Compile Include="Assets\Scripts\Unity\BuildingMeshGenerator.cs" />
|
||||
<Compile Include="Assets\Scripts\Unity\InitializeScript.cs" />
|
||||
<Compile Include="Assets\Scripts\Unity\RoadMeshGenerator.cs" />
|
||||
<Compile Include="Assets\Scripts\Unity\TerrainGeneratorScript.cs" />
|
||||
<Compile Include="Assets\Scripts\Utils\Algorithms.cs" />
|
||||
<Compile Include="Assets\Scripts\Utils\Algorithms\GridTraverseAlgorithm.cs" />
|
||||
<Compile Include="Assets\Scripts\Utils\ColorHelper.cs" />
|
||||
<Compile Include="Assets\Scripts\Utils\Expression.cs" />
|
||||
<Compile Include="Assets\Scripts\Utils\Logger.cs" />
|
||||
<Compile Include="Assets\Scripts\Utils\MathHelper.cs" />
|
||||
<Compile Include="Assets\Scripts\Utils\QuadTree.cs" />
|
||||
<Compile Include="Assets\Scripts\Utils\RandomExtensions.cs" />
|
||||
<Compile Include="Assets\Scripts\Utils\SerializationHelper.cs" />
|
||||
|
@ -4,6 +4,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using TransportGame.Business;
|
||||
using TransportGame.Model;
|
||||
using TransportGame.Primitives;
|
||||
using TransportGame.Model.Road;
|
||||
using TransportGame.Utils;
|
||||
|
||||
@ -112,7 +113,7 @@ namespace TransportGame.Generator
|
||||
|
||||
foreach (var lot in lotTree.Query(lotArea))
|
||||
{
|
||||
if (BuildingLot.DoesIntersect(lot0, lot))
|
||||
if (Polygon.Intersect(lot0, lot))
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using TransportGame.Model;
|
||||
using TransportGame.Noise;
|
||||
using TransportGame.Primitives;
|
||||
|
||||
namespace TransportGame.Generator
|
||||
{
|
||||
|
@ -5,8 +5,9 @@ using System.Text;
|
||||
using TransportGame.Business;
|
||||
using TransportGame.Model;
|
||||
using TransportGame.Model.Road;
|
||||
using TransportGame.Primitives;
|
||||
using TransportGame.Utils;
|
||||
using Vector2 = TransportGame.Model.Vector2;
|
||||
using Vector2 = TransportGame.Primitives.Vector2;
|
||||
|
||||
namespace TransportGame.Generator
|
||||
{
|
||||
@ -259,8 +260,7 @@ namespace TransportGame.Generator
|
||||
}
|
||||
|
||||
// Filter & sort the segments by distance
|
||||
segmentIds = segmentIds.Distinct().OrderBy(id =>
|
||||
LineSegment.Distance(map.RoadNetwork.ArticulationSegments[id].AsLineSegment(), segment.Terminal2Pos));
|
||||
segmentIds = segmentIds.Distinct().OrderBy(id => map.RoadNetwork.ArticulationSegments[id].AsLineSegment().Distance(segment.Terminal2Pos));
|
||||
|
||||
foreach (var segmentId in segmentIds)
|
||||
{
|
||||
|
@ -47,7 +47,7 @@ namespace TransportGame.Generator
|
||||
GenerateElevation(map);
|
||||
|
||||
// Generate water level
|
||||
float waterAmount = random.NextSingle(map.Biome.Moisture.Minimum, map.Biome.Moisture.Maximum);
|
||||
float waterAmount = random.NextSingle(map.Biome.Moisture.Min, map.Biome.Moisture.Max);
|
||||
map.WaterLevel = Mathf.Pow(waterAmount, ConfigManager.Tergen.WaterNonLinearPower) * map.Biome.Height;
|
||||
|
||||
return map;
|
||||
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml.Serialization;
|
||||
using TransportGame.Primitives;
|
||||
using TransportGame.Utils;
|
||||
|
||||
namespace TransportGame.Model
|
||||
@ -33,13 +34,13 @@ namespace TransportGame.Model
|
||||
/// Value is a probability, should be between 0 and 1.
|
||||
/// </remarks>
|
||||
[XmlElement("moisture")]
|
||||
public Range Moisture { get; set; }
|
||||
public Interval Moisture { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the vegetation density of the biome
|
||||
/// </summary>
|
||||
[XmlElement("vegetationDensity")]
|
||||
public Range VegetationDensity { get; set; }
|
||||
public Interval VegetationDensity { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets an array of textures to use
|
||||
|
@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using TransportGame.Primitives;
|
||||
|
||||
namespace TransportGame.Model
|
||||
{
|
||||
|
@ -3,17 +3,16 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using TransportGame.Business;
|
||||
using TransportGame.Primitives;
|
||||
using TransportGame.Utils;
|
||||
|
||||
namespace TransportGame.Model
|
||||
{
|
||||
public class BuildingLot : IPositionable
|
||||
/// <summary>
|
||||
/// Building lot
|
||||
/// </summary>
|
||||
public class BuildingLot : Polygon
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the list of points
|
||||
/// </summary>
|
||||
public Vector2[] Points { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the size of the lot
|
||||
/// </summary>
|
||||
@ -23,8 +22,8 @@ namespace TransportGame.Model
|
||||
/// Initializes the building lot
|
||||
/// </summary>
|
||||
public BuildingLot()
|
||||
: base(new Vector2[4])
|
||||
{
|
||||
Points = new Vector2[4];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -32,8 +31,8 @@ namespace TransportGame.Model
|
||||
/// </summary>
|
||||
/// <param name="size">size</param>
|
||||
public BuildingLot(float size)
|
||||
: base(new Vector2[4])
|
||||
{
|
||||
Points = new Vector2[4];
|
||||
Size = size;
|
||||
}
|
||||
|
||||
@ -43,8 +42,8 @@ namespace TransportGame.Model
|
||||
/// <param name="size">Size</param>
|
||||
/// <param name="points">Points</param>
|
||||
public BuildingLot(float size, params Vector2[] points)
|
||||
: base(points)
|
||||
{
|
||||
Points = points;
|
||||
Size = size;
|
||||
}
|
||||
|
||||
@ -54,30 +53,11 @@ namespace TransportGame.Model
|
||||
/// <param name="size">Size</param>
|
||||
/// <param name="points">Points</param>
|
||||
public BuildingLot(float size, IEnumerable<Vector2> points)
|
||||
: base(points)
|
||||
{
|
||||
Points = points.ToArray();
|
||||
Size = size;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the lot position
|
||||
/// </summary>
|
||||
public Vector2 Position
|
||||
{
|
||||
get { return Points.Aggregate((x, y) => x + y) / Points.Length; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests if two building lots intersect
|
||||
/// </summary>
|
||||
/// <param name="a">First lot</param>
|
||||
/// <param name="b">Second lot</param>
|
||||
/// <returns></returns>
|
||||
public static bool DoesIntersect(BuildingLot a, BuildingLot b)
|
||||
{
|
||||
return Algorithmss.DoPolygonsIntersect(a.Points, b.Points);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests if a building lot intersects a line segment (such as a road segment)
|
||||
/// </summary>
|
||||
|
@ -4,8 +4,8 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml.Serialization;
|
||||
using TransportGame.Model.Road;
|
||||
using TransportGame.Primitives;
|
||||
using TransportGame.Utils;
|
||||
using UnityEngine;
|
||||
|
||||
namespace TransportGame.Model
|
||||
{
|
||||
@ -256,8 +256,8 @@ namespace TransportGame.Model
|
||||
if (dist < PopulationCenterRange * PopulationCenterRange)
|
||||
{
|
||||
float influence = 1 - (float)dist / (float)(PopulationCenterRange * PopulationCenterRange);
|
||||
influence = Mathf.Pow(influence, 6) * 0.7f; // Ease
|
||||
value = Mathf.Clamp01(value + influence);
|
||||
influence = (float)Math.Pow(influence, 6) * 0.7f; // Ease
|
||||
value = MathHelper.Clamp01(value + influence);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,12 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace TransportGame.Model
|
||||
{
|
||||
public interface IPositionable
|
||||
{
|
||||
Vector2 Position { get; }
|
||||
}
|
||||
}
|
@ -1,108 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace TransportGame.Model
|
||||
{
|
||||
public struct LineSegment
|
||||
{
|
||||
public float X0 { get; private set; }
|
||||
public float Y0 { get; private set; }
|
||||
public float X1 { get; private set; }
|
||||
public float Y1 { get; private set; }
|
||||
|
||||
public Vector2 P0
|
||||
{
|
||||
get
|
||||
{
|
||||
return new Vector2(X0, Y0);
|
||||
}
|
||||
}
|
||||
public Vector2 P1
|
||||
{
|
||||
get
|
||||
{
|
||||
return new Vector2(X1, Y1);
|
||||
}
|
||||
}
|
||||
|
||||
public float Length
|
||||
{
|
||||
get
|
||||
{
|
||||
return (P1 - P0).Length;
|
||||
}
|
||||
}
|
||||
|
||||
public float LengthSq
|
||||
{
|
||||
get
|
||||
{
|
||||
return (P1 - P0).LengthSq;
|
||||
}
|
||||
}
|
||||
|
||||
public Vector2 Direction
|
||||
{
|
||||
get
|
||||
{
|
||||
return (P1 - P0).Normalized;
|
||||
}
|
||||
}
|
||||
|
||||
public LineSegment(Vector2 p0, Vector2 p1)
|
||||
: this()
|
||||
{
|
||||
X0 = p0.X; Y0 = p0.Y;
|
||||
X1 = p1.X; Y1 = p1.Y;
|
||||
}
|
||||
|
||||
public LineSegment(float x0, float y0, float x1, float y1)
|
||||
: this()
|
||||
{
|
||||
X0 = x0; Y0 = y0;
|
||||
X1 = x1; Y1 = y1;
|
||||
}
|
||||
|
||||
public static Vector2? Intersect(LineSegment a, LineSegment b)
|
||||
{
|
||||
float s1x = a.X1 - a.X0, s1y = a.Y1 - a.Y0;
|
||||
float s2x = b.X1 - b.X0, s2y = b.Y1 - b.Y0;
|
||||
|
||||
float det = (-s2x * s1y + s1x * s2y);
|
||||
|
||||
// Avoid division by zero
|
||||
// Note: this is an edge case, the vectors might be parallel or colliniar
|
||||
if (det == 0) return null;
|
||||
|
||||
float s = (-s1y * (a.X0 - b.X0) + s1x * (a.Y0 - b.Y0)) / det;
|
||||
float t = (s2x * (a.Y0 - b.Y0) - s2y * (a.X0 - b.X0)) / det;
|
||||
|
||||
// Collision detected
|
||||
if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
|
||||
return new Vector2(a.X0 + t * s1x, a.Y0 + t * s1y);
|
||||
|
||||
// No collision
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the distance from point p to line segment
|
||||
/// </summary>
|
||||
/// <param name="line">Line</param>
|
||||
/// <param name="p">Point</param>
|
||||
/// <returns>Distance</returns>
|
||||
public static float Distance(LineSegment line, Vector2 p)
|
||||
{
|
||||
float det = (line.Y1 - line.Y0) * p.X - (line.X1 - line.X0) * p.Y
|
||||
+ line.X1 * line.Y0 - line.Y1 * line.X0;
|
||||
return Math.Abs(det) / line.Length;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return string.Format("({0}, {1})->({2}, {3})", X0, Y0, X1, Y1);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace TransportGame.Model
|
||||
{
|
||||
public class Range
|
||||
{
|
||||
[XmlAttribute("min")]
|
||||
public float Minimum { get; set; }
|
||||
|
||||
[XmlAttribute("max")]
|
||||
public float Maximum { get; set; }
|
||||
|
||||
public Range()
|
||||
{
|
||||
Minimum = 0;
|
||||
Maximum = 1;
|
||||
}
|
||||
|
||||
public Range(float min, float max)
|
||||
{
|
||||
Minimum = min;
|
||||
Maximum = max;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return String.Format("[{0}, {1}]", Minimum, Maximum);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,77 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace TransportGame.Model
|
||||
{
|
||||
public struct Rectangle
|
||||
{
|
||||
public float Left { get; set; }
|
||||
public float Top { get; set; }
|
||||
public float Right { get; set; }
|
||||
public float Bottom { get; set; }
|
||||
|
||||
public Rectangle(float left, float top, float right, float bottom)
|
||||
: this()
|
||||
{
|
||||
Left = left;
|
||||
Top = top;
|
||||
Right = right;
|
||||
Bottom = bottom;
|
||||
|
||||
if (left > right)
|
||||
throw new ArgumentException("Left must be smaller than right.");
|
||||
|
||||
if (top > bottom)
|
||||
throw new ArgumentException("Top must be smaller than bottom.");
|
||||
}
|
||||
|
||||
public float Width
|
||||
{
|
||||
get
|
||||
{
|
||||
return Right - Left;
|
||||
}
|
||||
set
|
||||
{
|
||||
Right = Left + value;
|
||||
}
|
||||
}
|
||||
|
||||
public float Height
|
||||
{
|
||||
get
|
||||
{
|
||||
return Bottom - Top;
|
||||
}
|
||||
set
|
||||
{
|
||||
Bottom = Top + value;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Contains(float x, float y)
|
||||
{
|
||||
return x >= Left && x <= Right && y >= Top && y <= Bottom;
|
||||
}
|
||||
|
||||
public bool Contains(Vector2 p)
|
||||
{
|
||||
return Contains(p.X, p.Y);
|
||||
}
|
||||
|
||||
public static bool Intersects (Rectangle a, Rectangle b)
|
||||
{
|
||||
return !(b.Left > a.Right ||
|
||||
b.Right < a.Left ||
|
||||
b.Top > a.Bottom ||
|
||||
b.Bottom < a.Top);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return string.Format("({0}, {1}, {2}, {3})", Left, Top, Right, Bottom);
|
||||
}
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml.Serialization;
|
||||
using TransportGame.Primitives;
|
||||
using TransportGame.Utils;
|
||||
|
||||
namespace TransportGame.Model.Road
|
||||
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml.Serialization;
|
||||
using TransportGame.Primitives;
|
||||
|
||||
namespace TransportGame.Model.Road
|
||||
{
|
||||
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml.Serialization;
|
||||
using TransportGame.Primitives;
|
||||
|
||||
namespace TransportGame.Model.Road
|
||||
{
|
||||
|
9
Game/Assets/Scripts/Primitives.meta
Normal file
9
Game/Assets/Scripts/Primitives.meta
Normal file
@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 38c9355be2e20d242ab7047cac952101
|
||||
folderAsset: yes
|
||||
timeCreated: 1434538689
|
||||
licenseType: Free
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
14
Game/Assets/Scripts/Primitives/IPositionable.cs
Normal file
14
Game/Assets/Scripts/Primitives/IPositionable.cs
Normal file
@ -0,0 +1,14 @@
|
||||
|
||||
namespace TransportGame.Primitives
|
||||
{
|
||||
/// <summary>
|
||||
/// Object can be positioned using a point
|
||||
/// </summary>
|
||||
public interface IPositionable
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the position of the object
|
||||
/// </summary>
|
||||
Vector2 Position { get; }
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cdecfb69b7ca68446910d0a607e934e6
|
||||
timeCreated: 1432824088
|
||||
guid: b1f8e20ab1416e64aa689620b275d16e
|
||||
timeCreated: 1434538689
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
98
Game/Assets/Scripts/Primitives/Interval.cs
Normal file
98
Game/Assets/Scripts/Primitives/Interval.cs
Normal file
@ -0,0 +1,98 @@
|
||||
using System;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace TransportGame.Primitives
|
||||
{
|
||||
/// <summary>
|
||||
/// Interval
|
||||
/// </summary>
|
||||
public class Interval
|
||||
{
|
||||
#region Properties
|
||||
|
||||
/// <summary>
|
||||
/// Gets the minimum value of the interval
|
||||
/// </summary>
|
||||
[XmlAttribute("min")]
|
||||
public float Min { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the maximum value of the interval
|
||||
/// </summary>
|
||||
[XmlAttribute("max")]
|
||||
public float Max { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Other properties
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the length of the interval
|
||||
/// </summary>
|
||||
[XmlIgnore]
|
||||
public float Length
|
||||
{
|
||||
get
|
||||
{
|
||||
return Max - Min;
|
||||
}
|
||||
set
|
||||
{
|
||||
Max = Min + value;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes interval
|
||||
/// </summary>
|
||||
public Interval()
|
||||
{
|
||||
Min = 0;
|
||||
Max = 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes interval
|
||||
/// </summary>
|
||||
/// <param name="min">Minimum value</param>
|
||||
/// <param name="max">Maximum value</param>
|
||||
public Interval(float min, float max)
|
||||
{
|
||||
Min = min;
|
||||
Max = max;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Operations
|
||||
|
||||
/// <summary>
|
||||
/// Tests if interval contains value
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public bool Contains(float value)
|
||||
{
|
||||
return (value >= Min && value <= Max);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Object overrides
|
||||
|
||||
/// <summary>
|
||||
/// Gets string representation
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override string ToString()
|
||||
{
|
||||
return String.Format("[{0}, {1}]", Min, Max);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -1,8 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 67f24adfb8b21234184d0caf81f424ea
|
||||
guid: e3c02b6aafb292640a692c9ea354adb1
|
||||
timeCreated: 1434538689
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
178
Game/Assets/Scripts/Primitives/LineSegment.cs
Normal file
178
Game/Assets/Scripts/Primitives/LineSegment.cs
Normal file
@ -0,0 +1,178 @@
|
||||
using System;
|
||||
|
||||
namespace TransportGame.Primitives
|
||||
{
|
||||
/// <summary>
|
||||
/// A line segment
|
||||
/// </summary>
|
||||
public struct LineSegment
|
||||
{
|
||||
#region Properties
|
||||
|
||||
/// <summary>
|
||||
/// X coordinate of first point
|
||||
/// </summary>
|
||||
public float X0 { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Y coordinate of first point
|
||||
/// </summary>
|
||||
public float Y0 { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// X coordinate of second point
|
||||
/// </summary>
|
||||
public float X1 { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Y coordinate of second point
|
||||
/// </summary>
|
||||
public float Y1 { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the first point
|
||||
/// </summary>
|
||||
public Vector2 P0
|
||||
{
|
||||
get
|
||||
{
|
||||
return new Vector2(X0, Y0);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the second point
|
||||
/// </summary>
|
||||
public Vector2 P1
|
||||
{
|
||||
get
|
||||
{
|
||||
return new Vector2(X1, Y1);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the length of the segment
|
||||
/// </summary>
|
||||
public float Length
|
||||
{
|
||||
get
|
||||
{
|
||||
return (P1 - P0).Length;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the length squared of the segment
|
||||
/// </summary>
|
||||
public float LengthSq
|
||||
{
|
||||
get
|
||||
{
|
||||
return (P1 - P0).LengthSq;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the direction vector of the segment
|
||||
/// </summary>
|
||||
public Vector2 Direction
|
||||
{
|
||||
get
|
||||
{
|
||||
return (P1 - P0).Normalized;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes line segment
|
||||
/// </summary>
|
||||
/// <param name="p0">First point</param>
|
||||
/// <param name="p1">Second point</param>
|
||||
public LineSegment(Vector2 p0, Vector2 p1)
|
||||
: this()
|
||||
{
|
||||
X0 = p0.X; Y0 = p0.Y;
|
||||
X1 = p1.X; Y1 = p1.Y;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes line segment
|
||||
/// </summary>
|
||||
/// <param name="x0">X coordinate of first segment</param>
|
||||
/// <param name="y0">Y coordinate of first segment</param>
|
||||
/// <param name="x1">X coordinate of second segment</param>
|
||||
/// <param name="y1">Y coordinate of second segment</param>
|
||||
public LineSegment(float x0, float y0, float x1, float y1)
|
||||
: this()
|
||||
{
|
||||
X0 = x0; Y0 = y0;
|
||||
X1 = x1; Y1 = y1;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Operations
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the intersection between two line segments.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Ignores case where intersection is a line.
|
||||
/// </remarks>
|
||||
/// <param name="a">First line segment</param>
|
||||
/// <param name="b">Second line segment</param>
|
||||
/// <returns>Intersection point, or null if segments don't intersect.</returns>
|
||||
public static Vector2? Intersect(LineSegment a, LineSegment b)
|
||||
{
|
||||
float s1x = a.X1 - a.X0, s1y = a.Y1 - a.Y0;
|
||||
float s2x = b.X1 - b.X0, s2y = b.Y1 - b.Y0;
|
||||
|
||||
float det = (-s2x * s1y + s1x * s2y);
|
||||
|
||||
// Avoid division by zero
|
||||
// Note: this is an edge case, the vectors might be parallel or colliniar
|
||||
if (det == 0) return null;
|
||||
|
||||
float s = (-s1y * (a.X0 - b.X0) + s1x * (a.Y0 - b.Y0)) / det;
|
||||
float t = (s2x * (a.Y0 - b.Y0) - s2y * (a.X0 - b.X0)) / det;
|
||||
|
||||
// Collision detected
|
||||
if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
|
||||
return new Vector2(a.X0 + t * s1x, a.Y0 + t * s1y);
|
||||
|
||||
// No collision
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the distance from a point p to a line segment
|
||||
/// </summary>
|
||||
/// <param name="p">Point</param>
|
||||
/// <returns>Distance</returns>
|
||||
public float Distance(Vector2 p)
|
||||
{
|
||||
float det = (Y1 - Y0) * p.X - (X1 - X0) * p.Y + X1 * Y0 - Y1 * X0;
|
||||
return Math.Abs(det) / Length;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ToString
|
||||
|
||||
/// <summary>
|
||||
/// Gets string representation of line segment
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override string ToString()
|
||||
{
|
||||
return string.Format("({0}, {1})->({2}, {3})", X0, Y0, X1, Y1);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
fileFormatVersion: 2
|
||||
guid: eaabdeeb068e1dc46a7a9da6fd47f393
|
||||
timeCreated: 1432847695
|
||||
guid: a6b34c7e4ed62b04fbb44b61fbb7bc23
|
||||
timeCreated: 1434538689
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
@ -1,17 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using TransportGame.Utils;
|
||||
|
||||
namespace TransportGame.Model
|
||||
namespace TransportGame.Primitives
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a polygon
|
||||
/// </summary>
|
||||
public class Polygon : IPositionable
|
||||
{
|
||||
#region Private types
|
||||
#region Private data types
|
||||
|
||||
private struct Edge
|
||||
{
|
||||
@ -26,11 +24,17 @@ namespace TransportGame.Model
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the points that define the polygon
|
||||
/// </summary>
|
||||
public Vector2[] Points { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Other properties
|
||||
|
||||
/// <summary>
|
||||
/// Gets the gravitational center
|
||||
/// </summary>
|
||||
@ -39,6 +43,10 @@ namespace TransportGame.Model
|
||||
get { return Points.Aggregate((x, y) => x + y) / Points.Length; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes polygon
|
||||
/// </summary>
|
||||
@ -65,28 +73,9 @@ namespace TransportGame.Model
|
||||
Points = points;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compares angle between two NORMALIZED vectors.
|
||||
/// </summary>
|
||||
/// <param name="a"></param>
|
||||
/// <param name="b"></param>
|
||||
/// <returns></returns>
|
||||
private static float CompareAngle(Vector2 a, Vector2 b)
|
||||
{
|
||||
float sin = Vector2.Cross(a, b);
|
||||
float cos = Vector2.Dot(a, b);
|
||||
#endregion
|
||||
|
||||
if (sin >= 0 && cos >= 0)
|
||||
return sin;
|
||||
|
||||
else if (sin >= 0 && cos <= 0)
|
||||
return 1 + Math.Abs(cos);
|
||||
|
||||
else if (sin <= 0 && cos <= 0)
|
||||
return 2 + Math.Abs(sin);
|
||||
|
||||
else return 3 + cos;
|
||||
}
|
||||
#region Operations
|
||||
|
||||
/// <summary>
|
||||
/// Returns the union between given polygons
|
||||
@ -173,7 +162,6 @@ namespace TransportGame.Model
|
||||
union.Add(vertices[v]);
|
||||
|
||||
int newV = -1;
|
||||
float smallestAngle = -1;
|
||||
Vector2 smallestDir = Vector2.Zero;
|
||||
|
||||
foreach (var edge in edges)
|
||||
@ -184,11 +172,9 @@ namespace TransportGame.Model
|
||||
Vector2 dir = (vertices[otherv] - vertices[v]).Normalized;
|
||||
|
||||
// Find smallest angle
|
||||
float cmpAngle = CompareAngle(-prev, dir);
|
||||
if (cmpAngle < smallestAngle || smallestAngle < 0)
|
||||
if (newV == -1 || Vector2.TrigonometricComparer.Compare(dir, smallestDir) < 0)
|
||||
{
|
||||
newV = otherv;
|
||||
smallestAngle = cmpAngle;
|
||||
smallestDir = dir;
|
||||
}
|
||||
}
|
||||
@ -204,7 +190,13 @@ namespace TransportGame.Model
|
||||
return new Polygon(union);
|
||||
}
|
||||
|
||||
public static bool DoPolygonsIntersect(Polygon a, Polygon b)
|
||||
/// <summary>
|
||||
/// Tests if two polygons intersect
|
||||
/// </summary>
|
||||
/// <param name="a"></param>
|
||||
/// <param name="b"></param>
|
||||
/// <returns></returns>
|
||||
public static bool Intersect(Polygon a, Polygon b)
|
||||
{
|
||||
foreach (var poly in new[] { a, b })
|
||||
{
|
||||
@ -242,6 +234,42 @@ namespace TransportGame.Model
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests if polygon contains point
|
||||
/// </summary>
|
||||
/// <param name="point"></param>
|
||||
/// <returns></returns>
|
||||
public bool Contains(Vector2 point)
|
||||
{
|
||||
// Quick bounding box check
|
||||
Vector2 min = Points[0], max = Points[0];
|
||||
|
||||
for (int i = 0; i < Points.Length; i++)
|
||||
{
|
||||
min.X = Math.Min(min.X, Points[i].X);
|
||||
min.Y = Math.Min(min.Y, Points[i].Y);
|
||||
max.X = Math.Max(max.X, Points[i].X);
|
||||
max.Y = Math.Max(max.Y, Points[i].Y);
|
||||
}
|
||||
|
||||
if (point.X < min.X || point.Y < min.Y || point.X > max.X || point.Y > max.Y)
|
||||
return false;
|
||||
|
||||
// Ray casting method
|
||||
int j = Points.Length - 1;
|
||||
bool result = false;
|
||||
for (int i = 0; i < Points.Length; i++)
|
||||
{
|
||||
if ((Points[i].Y > point.Y) != (Points[j].Y > point.Y) &&
|
||||
(point.X < (Points[j].X - Points[i].X) * (point.Y - Points[i].Y) / (Points[j].Y - Points[i].Y) + Points[i].X))
|
||||
result = !result;
|
||||
|
||||
j = i;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ee96020375a8f3d4bb7a04e9f7a6b05e
|
||||
timeCreated: 1433489979
|
||||
guid: 3e0bb5f9b3dd2d84a9996806e2048f9d
|
||||
timeCreated: 1434538689
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
156
Game/Assets/Scripts/Primitives/Rectangle.cs
Normal file
156
Game/Assets/Scripts/Primitives/Rectangle.cs
Normal file
@ -0,0 +1,156 @@
|
||||
using System;
|
||||
|
||||
namespace TransportGame.Primitives
|
||||
{
|
||||
/// <summary>
|
||||
/// Rectangle
|
||||
/// </summary>
|
||||
public struct Rectangle
|
||||
{
|
||||
#region Properties
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the left boundary
|
||||
/// </summary>
|
||||
public float Left { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the top boundary
|
||||
/// </summary>
|
||||
public float Bottom { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the right boundary
|
||||
/// </summary>
|
||||
public float Right { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the bottom boundary
|
||||
/// </summary>
|
||||
public float Top { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
/// <summary>
|
||||
/// Initializes this rectangle
|
||||
/// </summary>
|
||||
/// <param name="left">Left boundary</param>
|
||||
/// <param name="bottom">Bottom boundary</param>
|
||||
/// <param name="right">Right boundary</param>
|
||||
/// <param name="top">Top boundary</param>
|
||||
public Rectangle(float left, float bottom, float right, float top)
|
||||
: this()
|
||||
{
|
||||
Left = left;
|
||||
Bottom = bottom;
|
||||
Right = right;
|
||||
Top = top;
|
||||
|
||||
if (left > right)
|
||||
throw new ArgumentException("Left must be smaller than right.");
|
||||
|
||||
if (bottom > top)
|
||||
throw new ArgumentException("Bottom must be smaller than top.");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Other properties
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the width of the rectangle
|
||||
/// </summary>
|
||||
public float Width
|
||||
{
|
||||
get
|
||||
{
|
||||
return Right - Left;
|
||||
}
|
||||
set
|
||||
{
|
||||
Right = Left + value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the height of the rectangle
|
||||
/// </summary>
|
||||
public float Height
|
||||
{
|
||||
get
|
||||
{
|
||||
return Top - Bottom;
|
||||
}
|
||||
set
|
||||
{
|
||||
Top = Bottom + value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the area of the rectangle
|
||||
/// </summary>
|
||||
public float Area
|
||||
{
|
||||
get
|
||||
{
|
||||
return Width * Height;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Operations
|
||||
|
||||
/// <summary>
|
||||
/// Tests if rectangle contains given point
|
||||
/// </summary>
|
||||
/// <param name="x">x coordinate</param>
|
||||
/// <param name="y">y coordinate</param>
|
||||
/// <returns>True if point is inside</returns>
|
||||
public bool Contains(float x, float y)
|
||||
{
|
||||
return x >= Left && x <= Right && y >= Bottom && y <= Top;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests if rectangle contains given point
|
||||
/// </summary>
|
||||
/// <param name="p">Vector</param>
|
||||
/// <returns>True if point is inside</returns>
|
||||
public bool Contains(Vector2 p)
|
||||
{
|
||||
return Contains(p.X, p.Y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests if two rectangles intersect
|
||||
/// </summary>
|
||||
/// <param name="a"></param>
|
||||
/// <param name="b"></param>
|
||||
/// <returns>True if rectangles intersect</returns>
|
||||
public static bool Intersect (Rectangle a, Rectangle b)
|
||||
{
|
||||
return !(b.Left > a.Right ||
|
||||
b.Right < a.Left ||
|
||||
b.Bottom > a.Top ||
|
||||
b.Top < a.Bottom);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Object overrides
|
||||
|
||||
/// <summary>
|
||||
/// Gets string representation
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override string ToString()
|
||||
{
|
||||
return string.Format("({0}, {1}, {2}, {3})", Left, Bottom, Right, Top);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 01c02a9fd2c3d0142905c23fb3142b6f
|
||||
timeCreated: 1432824087
|
||||
guid: 089c6381ade2028499552d764dcdaa25
|
||||
timeCreated: 1434538689
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
@ -1,13 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace TransportGame.Model
|
||||
namespace TransportGame.Primitives
|
||||
{
|
||||
/// <summary>
|
||||
/// 2D vector
|
||||
/// </summary>
|
||||
public struct Vector2
|
||||
{
|
||||
#region Constant vectors
|
||||
|
||||
/// <summary>
|
||||
/// Zero vector
|
||||
/// </summary>
|
||||
@ -18,6 +21,10 @@ namespace TransportGame.Model
|
||||
/// </summary>
|
||||
public static readonly Vector2 Unit = new Vector2(1, 0);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
/// <summary>
|
||||
/// Gets the X component
|
||||
/// </summary>
|
||||
@ -30,6 +37,10 @@ namespace TransportGame.Model
|
||||
[XmlAttribute("y")]
|
||||
public float Y { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a vector2
|
||||
/// </summary>
|
||||
@ -42,6 +53,31 @@ namespace TransportGame.Model
|
||||
Y = y;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the vector corresponding with specified angle (in radians)
|
||||
/// </summary>
|
||||
/// <param name="rads">Radians</param>
|
||||
/// <returns>Vector</returns>
|
||||
public static Vector2 FromRadians(float rads)
|
||||
{
|
||||
return new Vector2((float)Math.Cos(rads), (float)Math.Sin(rads));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the vector corresponding with specified angle (in degrees)
|
||||
/// </summary>
|
||||
/// <param name="degs">Degrees</param>
|
||||
/// <returns>Vector</returns>
|
||||
public static Vector2 FromDegrees(float degs)
|
||||
{
|
||||
float rads = (degs * (float)Math.PI / 180f);
|
||||
return FromRadians(rads);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties (length, normalized)
|
||||
|
||||
/// <summary>
|
||||
/// Gets the length of the vector
|
||||
/// </summary>
|
||||
@ -93,6 +129,10 @@ namespace TransportGame.Model
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Operations
|
||||
|
||||
/// <summary>
|
||||
/// Rotates vector by given number of radians
|
||||
/// </summary>
|
||||
@ -235,32 +275,24 @@ namespace TransportGame.Model
|
||||
return Math.Abs(Cross(a, b)) < 1e-12;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the vector corresponding with specified angle (in radians)
|
||||
/// </summary>
|
||||
/// <param name="rads">Radians</param>
|
||||
/// <returns>Vector</returns>
|
||||
public static Vector2 FromRadians(float rads)
|
||||
{
|
||||
return new Vector2((float)Math.Cos(rads), (float)Math.Sin(rads));
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Object overrides
|
||||
|
||||
/// <summary>
|
||||
/// Gets the vector corresponding with specified angle (in degrees)
|
||||
/// Gets string representation of vector2
|
||||
/// </summary>
|
||||
/// <param name="degs">Degrees</param>
|
||||
/// <returns>Vector</returns>
|
||||
public static Vector2 FromDegrees(float degs)
|
||||
{
|
||||
float rads = (degs * (float)Math.PI / 180f);
|
||||
return FromRadians(rads);
|
||||
}
|
||||
|
||||
/// <returns></returns>
|
||||
public override string ToString()
|
||||
{
|
||||
return String.Format("({0}, {1})", X, Y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests if two vectors are equal.
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj is Vector2)
|
||||
@ -272,11 +304,19 @@ namespace TransportGame.Model
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets hash code of vector2
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return X.GetHashCode() * 7 + Y.GetHashCode();
|
||||
return X.GetHashCode() * 29 + Y.GetHashCode();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparers
|
||||
|
||||
private class LengthComparerImpl : IComparer<Vector2>
|
||||
{
|
||||
public int Compare(Vector2 a, Vector2 b)
|
||||
@ -338,7 +378,16 @@ namespace TransportGame.Model
|
||||
}
|
||||
}
|
||||
|
||||
public static IComparer<Vector2> LengthComparer = new LengthComparerImpl();
|
||||
public static IComparer<Vector2> TrigonomicComparer = new TrigonometricComparerImpl();
|
||||
/// <summary>
|
||||
/// Length comparer - compares vectors by length
|
||||
/// </summary>
|
||||
public static readonly IComparer<Vector2> LengthComparer = new LengthComparerImpl();
|
||||
|
||||
/// <summary>
|
||||
/// Trigonometric comparer - compares vectors by angle
|
||||
/// </summary>
|
||||
public static readonly IComparer<Vector2> TrigonometricComparer = new TrigonometricComparerImpl();
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 94117dad82214b44382141a14825994e
|
||||
timeCreated: 1432281052
|
||||
guid: e100b9a2be8bd3243b55552195f6ce14
|
||||
timeCreated: 1434538689
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
@ -8,7 +8,7 @@ using TransportGame.Model;
|
||||
using TransportGame.Model.Road;
|
||||
using TransportGame.Utils;
|
||||
using UnityEngine;
|
||||
using Vector2 = TransportGame.Model.Vector2;
|
||||
using Vector2 = TransportGame.Primitives.Vector2;
|
||||
|
||||
//#define DEBUG_ROAD_MESH_GENERATOR
|
||||
|
||||
@ -144,7 +144,7 @@ namespace TransportGame.Unity
|
||||
vindex += 1;
|
||||
|
||||
// Sort adjacent segments in trigonometric order
|
||||
var segs = node.ArticulationSegments.OrderBy(segment => DirectionFrom(segment, node), Vector2.TrigonomicComparer).ToArray();
|
||||
var segs = node.ArticulationSegments.OrderBy(segment => DirectionFrom(segment, node), Vector2.TrigonometricComparer).ToArray();
|
||||
|
||||
Vector2[] sideCrns = new Vector2[segs.Length];
|
||||
Vector2[] strCrns = new Vector2[segs.Length];
|
||||
|
@ -1,49 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using TransportGame.Model;
|
||||
|
||||
namespace TransportGame.Utils
|
||||
{
|
||||
public static class Algorithmss
|
||||
{
|
||||
public static bool DoPolygonsIntersect(Vector2[] a, Vector2[] b)
|
||||
{
|
||||
foreach (var poly in new[] {a, b})
|
||||
{
|
||||
for (int i = 0; i < poly.Length; i++)
|
||||
{
|
||||
int j = (i + 1) % poly.Length;
|
||||
|
||||
var normal = new Vector2(poly[j].Y - poly[i].Y, poly[i].X - poly[j].X);
|
||||
|
||||
double? minA = null, maxA = null;
|
||||
foreach (var p in a)
|
||||
{
|
||||
var projected = Vector2.Dot(normal, p);
|
||||
if (minA == null || projected < minA)
|
||||
minA = projected;
|
||||
if (maxA == null || projected > maxA)
|
||||
maxA = projected;
|
||||
}
|
||||
|
||||
double? minB = null, maxB = null;
|
||||
foreach (var p in b)
|
||||
{
|
||||
var projected = Vector2.Dot(normal, p);
|
||||
if (minB == null || projected < minB)
|
||||
minB = projected;
|
||||
if (maxB == null || projected > maxB)
|
||||
maxB = projected;
|
||||
}
|
||||
|
||||
if (maxA <= minB || maxB <= minA)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
38
Game/Assets/Scripts/Utils/MathHelper.cs
Normal file
38
Game/Assets/Scripts/Utils/MathHelper.cs
Normal file
@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace TransportGame.Utils
|
||||
{
|
||||
public static class MathHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Clamps given value in the [0,1] interval
|
||||
/// </summary>
|
||||
/// <param name="value">Value</param>
|
||||
/// <returns>Clamped value</returns>
|
||||
public static float Clamp01(float value)
|
||||
{
|
||||
return Clamp(value, 0, 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clamps given value in the [min,max] interval
|
||||
/// </summary>
|
||||
/// <param name="value">Value</param>
|
||||
/// <param name="min">Minimum value</param>
|
||||
/// <param name="max">Maximum value</param>
|
||||
/// <returns>Clamped value</returns>
|
||||
public static float Clamp(float value, float min, float max)
|
||||
{
|
||||
if (value < min)
|
||||
return min;
|
||||
|
||||
if (value > max)
|
||||
return max;
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,8 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b436281f9c7d49f4d935ae009d4559a8
|
||||
guid: 29212ca52a4f0a64bbc8cffe74a3f4b7
|
||||
timeCreated: 1434538689
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -4,6 +4,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using TransportGame.Model;
|
||||
using TransportGame.Primitives;
|
||||
|
||||
namespace TransportGame.Utils
|
||||
{
|
||||
@ -62,12 +63,12 @@ namespace TransportGame.Utils
|
||||
private void Subdivide()
|
||||
{
|
||||
float midx = Boundary.Left + Boundary.Width / 2f;
|
||||
float midy = Boundary.Top + Boundary.Height / 2f;
|
||||
float midy = Boundary.Bottom + Boundary.Height / 2f;
|
||||
|
||||
NorthWest = new QuadTree<T>(Boundary.Left, Boundary.Top, midx, midy);
|
||||
NorthEast = new QuadTree<T>(midx, Boundary.Top, Boundary.Right, midy);
|
||||
SouthEast = new QuadTree<T>(midx, midy, Boundary.Right, Boundary.Bottom);
|
||||
SouthWest = new QuadTree<T>(Boundary.Left, midy, midx, Boundary.Bottom);
|
||||
NorthWest = new QuadTree<T>(Boundary.Left, Boundary.Bottom, midx, midy);
|
||||
NorthEast = new QuadTree<T>(midx, Boundary.Bottom, Boundary.Right, midy);
|
||||
SouthEast = new QuadTree<T>(midx, midy, Boundary.Right, Boundary.Top);
|
||||
SouthWest = new QuadTree<T>(Boundary.Left, midy, midx, Boundary.Top);
|
||||
|
||||
foreach (var point in points)
|
||||
Add(point);
|
||||
@ -258,7 +259,7 @@ namespace TransportGame.Utils
|
||||
public IEnumerable<T> Query(Rectangle rect)
|
||||
{
|
||||
// No intersection
|
||||
if (!Rectangle.Intersects(rect, Boundary))
|
||||
if (!Rectangle.Intersect(rect, Boundary))
|
||||
return Enumerable.Empty<T>();
|
||||
|
||||
if (NorthWest == null)
|
||||
|
@ -23,7 +23,7 @@ Global
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(MonoDevelopProperties) = preSolution
|
||||
GlobalSection(MonoDevelopProperties) = preSolution
|
||||
StartupItem = Assembly-CSharp.csproj
|
||||
Policies = $0
|
||||
$0.TextStylePolicy = $1
|
||||
|
@ -29,7 +29,7 @@ Global
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(MonoDevelopProperties) = preSolution
|
||||
GlobalSection(MonoDevelopProperties) = preSolution
|
||||
StartupItem = Assembly-CSharp.csproj
|
||||
Policies = $0
|
||||
$0.TextStylePolicy = $1
|
||||
|
@ -85,26 +85,26 @@
|
||||
<Compile Include="Assets\Scripts\Model\Config\BuildingGeneratorConfig.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Config\RoadGeneratorConfig.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Config\TerrainGeneratorConfig.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\IPositionable.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\LineSegment.cs" />
|
||||
<Compile Include="Assets\Scripts\Primitives\IPositionable.cs" />
|
||||
<Compile Include="Assets\Scripts\Primitives\LineSegment.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\CityMap.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Polygon.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Range.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Rectangle.cs" />
|
||||
<Compile Include="Assets\Scripts\Primitives\Polygon.cs" />
|
||||
<Compile Include="Assets\Scripts\Primitives\Interval.cs" />
|
||||
<Compile Include="Assets\Scripts\Primitives\Rectangle.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Road\RoadNetwork.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Road\RoadNode.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Road\RoadSegment.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Texture.cs" />
|
||||
<Compile Include="Assets\Scripts\Model\Vector2.cs" />
|
||||
<Compile Include="Assets\Scripts\Primitives\Vector2.cs" />
|
||||
<Compile Include="Assets\Scripts\Unity\BuildingMeshGenerator.cs" />
|
||||
<Compile Include="Assets\Scripts\Unity\InitializeScript.cs" />
|
||||
<Compile Include="Assets\Scripts\Unity\RoadMeshGenerator.cs" />
|
||||
<Compile Include="Assets\Scripts\Unity\TerrainGeneratorScript.cs" />
|
||||
<Compile Include="Assets\Scripts\Utils\Algorithms.cs" />
|
||||
<Compile Include="Assets\Scripts\Utils\Algorithms\GridTraverseAlgorithm.cs" />
|
||||
<Compile Include="Assets\Scripts\Utils\ColorHelper.cs" />
|
||||
<Compile Include="Assets\Scripts\Utils\Expression.cs" />
|
||||
<Compile Include="Assets\Scripts\Utils\Logger.cs" />
|
||||
<Compile Include="Assets\Scripts\Utils\MathHelper.cs" />
|
||||
<Compile Include="Assets\Scripts\Utils\QuadTree.cs" />
|
||||
<Compile Include="Assets\Scripts\Utils\RandomExtensions.cs" />
|
||||
<Compile Include="Assets\Scripts\Utils\SerializationHelper.cs" />
|
||||
@ -118,5 +118,6 @@
|
||||
<None Include="Assets\Data\Config\tergen.xml" />
|
||||
<None Include="Assets\Standard Assets\Environment\Water (Basic)\Shaders\FXWaterBasic.shader" />
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<Import Project="$(MSBuildExtensionsPath)\SyntaxTree\UnityVS\2013\UnityVS.CSharp.targets" />
|
||||
</Project>
|
Loading…
Reference in New Issue
Block a user