Updated project to unity 5, finalized terrain generation
This commit is contained in:
		@@ -1,12 +1,12 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Text;
 | 
			
		||||
 | 
			
		||||
namespace TransportGame.Utils
 | 
			
		||||
{
 | 
			
		||||
    public static class Algorithmss
 | 
			
		||||
    {
 | 
			
		||||
        
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Text;
 | 
			
		||||
 | 
			
		||||
namespace TransportGame.Utils
 | 
			
		||||
{
 | 
			
		||||
    public static class Algorithmss
 | 
			
		||||
    {
 | 
			
		||||
        
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,21 +1,21 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Text;
 | 
			
		||||
using UnityEngine;
 | 
			
		||||
 | 
			
		||||
namespace TransportGame.Utils
 | 
			
		||||
{
 | 
			
		||||
    public static class ColorHelper
 | 
			
		||||
    {
 | 
			
		||||
        public static Color FromArgb(int argb)
 | 
			
		||||
        {
 | 
			
		||||
            float a = ((argb >> 24) & 0xff) / 255f;
 | 
			
		||||
            float r = ((argb >> 16) & 0xff) / 255f;
 | 
			
		||||
            float g = ((argb >> 8) & 0xff) / 255f;
 | 
			
		||||
            float b = ((argb) & 0xff) / 255f;
 | 
			
		||||
 | 
			
		||||
            return new Color(r, g, b, a);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Text;
 | 
			
		||||
using UnityEngine;
 | 
			
		||||
 | 
			
		||||
namespace TransportGame.Utils
 | 
			
		||||
{
 | 
			
		||||
    public static class ColorHelper
 | 
			
		||||
    {
 | 
			
		||||
        public static Color FromArgb(int argb)
 | 
			
		||||
        {
 | 
			
		||||
            float a = ((argb >> 24) & 0xff) / 255f;
 | 
			
		||||
            float r = ((argb >> 16) & 0xff) / 255f;
 | 
			
		||||
            float g = ((argb >> 8) & 0xff) / 255f;
 | 
			
		||||
            float b = ((argb) & 0xff) / 255f;
 | 
			
		||||
 | 
			
		||||
            return new Color(r, g, b, a);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,20 +1,20 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Text;
 | 
			
		||||
 | 
			
		||||
namespace TransportGame.Utils
 | 
			
		||||
{
 | 
			
		||||
    public static class RandomExtensions
 | 
			
		||||
    {
 | 
			
		||||
        public static float NextSingle(this Random @this)
 | 
			
		||||
        {
 | 
			
		||||
            return Convert.ToSingle(@this.NextDouble());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public static float NextSingle(this Random @this, float minValue, float maxValue)
 | 
			
		||||
        {
 | 
			
		||||
            return @this.NextSingle() * (maxValue - minValue) + minValue;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Text;
 | 
			
		||||
 | 
			
		||||
namespace TransportGame.Utils
 | 
			
		||||
{
 | 
			
		||||
    public static class RandomExtensions
 | 
			
		||||
    {
 | 
			
		||||
        public static float NextSingle(this Random @this)
 | 
			
		||||
        {
 | 
			
		||||
            return Convert.ToSingle(@this.NextDouble());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public static float NextSingle(this Random @this, float minValue, float maxValue)
 | 
			
		||||
        {
 | 
			
		||||
            return @this.NextSingle() * (maxValue - minValue) + minValue;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,56 +1,56 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Text;
 | 
			
		||||
using System.Threading;
 | 
			
		||||
 | 
			
		||||
namespace TransportGame.Utils
 | 
			
		||||
{
 | 
			
		||||
    public static class Task
 | 
			
		||||
    {
 | 
			
		||||
        private class TaskInfo
 | 
			
		||||
        {
 | 
			
		||||
            public Action Action { get; set; }
 | 
			
		||||
            public bool? Success { get; set; }
 | 
			
		||||
            public Exception ThrownException { get; set; }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private static void RunAsync_ActionThread(object info)
 | 
			
		||||
        {
 | 
			
		||||
            TaskInfo taskInfo = (TaskInfo)info;
 | 
			
		||||
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                taskInfo.Action();
 | 
			
		||||
                taskInfo.Success = true;
 | 
			
		||||
            }
 | 
			
		||||
            catch (Exception ex)
 | 
			
		||||
            {
 | 
			
		||||
                taskInfo.ThrownException = ex;
 | 
			
		||||
                taskInfo.Success = false;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public static IEnumerable RunAsync(Action action)
 | 
			
		||||
        {
 | 
			
		||||
            // Set up task info object
 | 
			
		||||
            TaskInfo taskInfo = new TaskInfo();
 | 
			
		||||
            taskInfo.Action = action;
 | 
			
		||||
 | 
			
		||||
            // Start thread and wait
 | 
			
		||||
            var thread = new Thread(RunAsync_ActionThread);
 | 
			
		||||
            thread.Start(taskInfo);
 | 
			
		||||
 | 
			
		||||
            // Wait for thread to finish
 | 
			
		||||
            while (thread.ThreadState == ThreadState.Running)
 | 
			
		||||
                yield return null;
 | 
			
		||||
 | 
			
		||||
            thread.Join();
 | 
			
		||||
 | 
			
		||||
            // Rethrow exception
 | 
			
		||||
            if (taskInfo.Success.HasValue && !taskInfo.Success.Value)
 | 
			
		||||
                throw new Exception("Task failed", taskInfo.ThrownException);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Text;
 | 
			
		||||
using System.Threading;
 | 
			
		||||
 | 
			
		||||
namespace TransportGame.Utils
 | 
			
		||||
{
 | 
			
		||||
    public static class Task
 | 
			
		||||
    {
 | 
			
		||||
        private class TaskInfo
 | 
			
		||||
        {
 | 
			
		||||
            public Action Action { get; set; }
 | 
			
		||||
            public bool? Success { get; set; }
 | 
			
		||||
            public Exception ThrownException { get; set; }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private static void RunAsync_ActionThread(object info)
 | 
			
		||||
        {
 | 
			
		||||
            TaskInfo taskInfo = (TaskInfo)info;
 | 
			
		||||
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                taskInfo.Action();
 | 
			
		||||
                taskInfo.Success = true;
 | 
			
		||||
            }
 | 
			
		||||
            catch (Exception ex)
 | 
			
		||||
            {
 | 
			
		||||
                taskInfo.ThrownException = ex;
 | 
			
		||||
                taskInfo.Success = false;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public static IEnumerable RunAsync(Action action)
 | 
			
		||||
        {
 | 
			
		||||
            // Set up task info object
 | 
			
		||||
            TaskInfo taskInfo = new TaskInfo();
 | 
			
		||||
            taskInfo.Action = action;
 | 
			
		||||
 | 
			
		||||
            // Start thread and wait
 | 
			
		||||
            var thread = new Thread(RunAsync_ActionThread);
 | 
			
		||||
            thread.Start(taskInfo);
 | 
			
		||||
 | 
			
		||||
            // Wait for thread to finish
 | 
			
		||||
            while (thread.ThreadState == ThreadState.Running)
 | 
			
		||||
                yield return null;
 | 
			
		||||
 | 
			
		||||
            thread.Join();
 | 
			
		||||
 | 
			
		||||
            // Rethrow exception
 | 
			
		||||
            if (taskInfo.Success.HasValue && !taskInfo.Success.Value)
 | 
			
		||||
                throw new Exception("Task failed", taskInfo.ThrownException);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,197 +1,197 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Text;
 | 
			
		||||
using UnityEngine;
 | 
			
		||||
 | 
			
		||||
namespace TransportGame.Utils
 | 
			
		||||
{
 | 
			
		||||
    public static class Texture2DExtensions
 | 
			
		||||
    {
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Draws a line between two vectors
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="this">Texture</param>
 | 
			
		||||
        /// <param name="color">Color</param>
 | 
			
		||||
        /// <param name="p0">Position 1</param>
 | 
			
		||||
        /// <param name="p1">Position 2</param>
 | 
			
		||||
        public static void DrawLine(this Texture2D @this, Color color, Vector2 p0, Vector2 p1)
 | 
			
		||||
        {
 | 
			
		||||
            DrawLine(@this, color, Convert.ToInt32(p0.x), Convert.ToInt32(p0.y), Convert.ToInt32(p1.x), Convert.ToInt32(p1.y));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Draws a line between 2 points
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="this">The texture</param>
 | 
			
		||||
        /// <param name="x0">X0</param>
 | 
			
		||||
        /// <param name="y0">Y0</param>
 | 
			
		||||
        /// <param name="x1">X1</param>
 | 
			
		||||
        /// <param name="y1">Y1</param>
 | 
			
		||||
        /// <param name="color">Color</param>
 | 
			
		||||
        public static void DrawLine(this Texture2D @this, Color color, int x0, int y0, int x1, int y1)
 | 
			
		||||
        {
 | 
			
		||||
            int dy = (int)(y1 - y0);
 | 
			
		||||
            int dx = (int)(x1 - x0);
 | 
			
		||||
            int stepx, stepy;
 | 
			
		||||
 | 
			
		||||
            if (dy < 0) { dy = -dy; stepy = -1; }
 | 
			
		||||
            else { stepy = 1; }
 | 
			
		||||
            if (dx < 0) { dx = -dx; stepx = -1; }
 | 
			
		||||
            else { stepx = 1; }
 | 
			
		||||
            dy <<= 1;
 | 
			
		||||
            dx <<= 1;
 | 
			
		||||
 | 
			
		||||
            float fraction = 0;
 | 
			
		||||
 | 
			
		||||
            @this.SetPixelSafe(x0, y0, color);
 | 
			
		||||
            if (dx > dy)
 | 
			
		||||
            {
 | 
			
		||||
                fraction = dy - (dx >> 1);
 | 
			
		||||
                while (Mathf.Abs(x0 - x1) > 1)
 | 
			
		||||
                {
 | 
			
		||||
                    if (fraction >= 0)
 | 
			
		||||
                    {
 | 
			
		||||
                        y0 += stepy;
 | 
			
		||||
                        fraction -= dx;
 | 
			
		||||
                    }
 | 
			
		||||
                    x0 += stepx;
 | 
			
		||||
                    fraction += dy;
 | 
			
		||||
                    @this.SetPixelSafe(x0, y0, color);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                fraction = dx - (dy >> 1);
 | 
			
		||||
                while (Mathf.Abs(y0 - y1) > 1)
 | 
			
		||||
                {
 | 
			
		||||
                    if (fraction >= 0)
 | 
			
		||||
                    {
 | 
			
		||||
                        x0 += stepx;
 | 
			
		||||
                        fraction -= dy;
 | 
			
		||||
                    }
 | 
			
		||||
                    y0 += stepy;
 | 
			
		||||
                    fraction += dx;
 | 
			
		||||
                    @this.SetPixelSafe(x0, y0, color);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Draws a rhombus-like point
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="this">Texture</param>
 | 
			
		||||
        /// <param name="p0">Position</param>
 | 
			
		||||
        /// <param name="radius">Radius</param>
 | 
			
		||||
        /// <param name="color">Color</param>
 | 
			
		||||
        public static void DrawPoint(this Texture2D @this, Color color, Vector2 p0, int radius)
 | 
			
		||||
        {
 | 
			
		||||
            DrawPoint(@this, color, Convert.ToInt32(p0.x), Convert.ToInt32(p0.y), radius);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Draws a rhombus-like point
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="this">Texture</param>
 | 
			
		||||
        /// <param name="x0">Position</param>
 | 
			
		||||
        /// <param name="y0">Position</param>
 | 
			
		||||
        /// <param name="radius">Radius</param>
 | 
			
		||||
        /// <param name="color">Color</param>
 | 
			
		||||
        public static void DrawPoint(this Texture2D @this, Color color, int x0, int y0, int radius)
 | 
			
		||||
        {
 | 
			
		||||
            for (int y = y0 - radius; y <= y0 + radius; ++y)
 | 
			
		||||
            {
 | 
			
		||||
                int pts = radius - Math.Abs(y - y0);
 | 
			
		||||
 | 
			
		||||
                for (int x = x0 - pts; x <= x0 + pts; ++x)
 | 
			
		||||
                    @this.SetPixelSafe(x, y, color);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Fills a polygon
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="this">Texture</param>
 | 
			
		||||
        /// <param name="color">color</param>
 | 
			
		||||
        /// <param name="points">Points</param>
 | 
			
		||||
        public static void FillPolygon(this Texture2D @this, Color color, params Vector2[] points)
 | 
			
		||||
        {
 | 
			
		||||
            int minY = Int32.MaxValue, maxY = Int32.MinValue;
 | 
			
		||||
            // Unused... int[] ptX = points.Select(p => Convert.ToInt32(p.x)).ToArray();
 | 
			
		||||
            int[] ptY = points.Select(p => Convert.ToInt32(p.y)).ToArray();
 | 
			
		||||
            
 | 
			
		||||
            // Find min and max row
 | 
			
		||||
            for (int i = 0; i < points.Length; i++)
 | 
			
		||||
            {
 | 
			
		||||
                minY = Math.Min(minY, ptY[i]);
 | 
			
		||||
                maxY = Math.Max(maxY, ptY[i]);
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            List<int> intersPoints = new List<int>();
 | 
			
		||||
 | 
			
		||||
            // Go through each row
 | 
			
		||||
            for (int y = minY; y <= maxY; ++y)
 | 
			
		||||
            {
 | 
			
		||||
                intersPoints.Clear();
 | 
			
		||||
 | 
			
		||||
                // Find intersection points
 | 
			
		||||
                int j = points.Length - 1;
 | 
			
		||||
                for (int i = 0; i < points.Length; ++i)
 | 
			
		||||
                {
 | 
			
		||||
                    // Treat special case where we have 2 points on the y axis
 | 
			
		||||
                    if (ptY[i] == y && ptY[j] == y)
 | 
			
		||||
                    {
 | 
			
		||||
                        intersPoints.Add(Convert.ToInt32(points[i].x));
 | 
			
		||||
                        intersPoints.Add(Convert.ToInt32(points[j].x));
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    // Intersection
 | 
			
		||||
                    else if ((ptY[i] >= y && ptY[j] <= y) ||
 | 
			
		||||
                             (ptY[i] <= y && ptY[j] >= y))
 | 
			
		||||
                    {
 | 
			
		||||
                        int x = Convert.ToInt32(points[i].x + (y - points[i].y) * (points[j].x - points[i].x) / (points[j].y - points[i].y));
 | 
			
		||||
                        intersPoints.Add(x);
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    j = i;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                // Order pairs
 | 
			
		||||
                var intersPointsSorted = intersPoints.OrderBy(x => x).Distinct().ToArray();
 | 
			
		||||
 | 
			
		||||
                // Draw
 | 
			
		||||
                for (int i = 0; i < intersPointsSorted.Length / 2; i++)
 | 
			
		||||
                {
 | 
			
		||||
                    for (int x = intersPointsSorted[2 * i]; x <= intersPointsSorted[2 * i + 1]; x++)
 | 
			
		||||
                        @this.SetPixelSafe(x, y, color);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Fills texture with specified color
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="this">Texture</param>
 | 
			
		||||
        /// <param name="color">Color</param>
 | 
			
		||||
        public static void Fill(this Texture2D @this, Color color)
 | 
			
		||||
        {
 | 
			
		||||
            for (int x = 0; x < @this.width; ++x)
 | 
			
		||||
                for (int y = 0; y < @this.height; ++y)
 | 
			
		||||
                    @this.SetPixelSafe(x, y, color);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Sets a pixel after checking if coordinates are inside image
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="this">Texture</param>
 | 
			
		||||
        /// <param name="x">X</param>
 | 
			
		||||
        /// <param name="y">Y</param>
 | 
			
		||||
        /// <param name="color">Color</param>
 | 
			
		||||
        public static void SetPixelSafe(this Texture2D @this, int x, int y, Color color)
 | 
			
		||||
        {
 | 
			
		||||
            if (x >= 0 && y >= 0 && x < @this.width && y < @this.height)
 | 
			
		||||
                @this.SetPixel(x, y, color);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Text;
 | 
			
		||||
using UnityEngine;
 | 
			
		||||
 | 
			
		||||
namespace TransportGame.Utils
 | 
			
		||||
{
 | 
			
		||||
    public static class Texture2DExtensions
 | 
			
		||||
    {
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Draws a line between two vectors
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="this">Texture</param>
 | 
			
		||||
        /// <param name="color">Color</param>
 | 
			
		||||
        /// <param name="p0">Position 1</param>
 | 
			
		||||
        /// <param name="p1">Position 2</param>
 | 
			
		||||
        public static void DrawLine(this Texture2D @this, Color color, Vector2 p0, Vector2 p1)
 | 
			
		||||
        {
 | 
			
		||||
            DrawLine(@this, color, Convert.ToInt32(p0.x), Convert.ToInt32(p0.y), Convert.ToInt32(p1.x), Convert.ToInt32(p1.y));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Draws a line between 2 points
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="this">The texture</param>
 | 
			
		||||
        /// <param name="x0">X0</param>
 | 
			
		||||
        /// <param name="y0">Y0</param>
 | 
			
		||||
        /// <param name="x1">X1</param>
 | 
			
		||||
        /// <param name="y1">Y1</param>
 | 
			
		||||
        /// <param name="color">Color</param>
 | 
			
		||||
        public static void DrawLine(this Texture2D @this, Color color, int x0, int y0, int x1, int y1)
 | 
			
		||||
        {
 | 
			
		||||
            int dy = (int)(y1 - y0);
 | 
			
		||||
            int dx = (int)(x1 - x0);
 | 
			
		||||
            int stepx, stepy;
 | 
			
		||||
 | 
			
		||||
            if (dy < 0) { dy = -dy; stepy = -1; }
 | 
			
		||||
            else { stepy = 1; }
 | 
			
		||||
            if (dx < 0) { dx = -dx; stepx = -1; }
 | 
			
		||||
            else { stepx = 1; }
 | 
			
		||||
            dy <<= 1;
 | 
			
		||||
            dx <<= 1;
 | 
			
		||||
 | 
			
		||||
            float fraction = 0;
 | 
			
		||||
 | 
			
		||||
            @this.SetPixelSafe(x0, y0, color);
 | 
			
		||||
            if (dx > dy)
 | 
			
		||||
            {
 | 
			
		||||
                fraction = dy - (dx >> 1);
 | 
			
		||||
                while (Mathf.Abs(x0 - x1) > 1)
 | 
			
		||||
                {
 | 
			
		||||
                    if (fraction >= 0)
 | 
			
		||||
                    {
 | 
			
		||||
                        y0 += stepy;
 | 
			
		||||
                        fraction -= dx;
 | 
			
		||||
                    }
 | 
			
		||||
                    x0 += stepx;
 | 
			
		||||
                    fraction += dy;
 | 
			
		||||
                    @this.SetPixelSafe(x0, y0, color);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                fraction = dx - (dy >> 1);
 | 
			
		||||
                while (Mathf.Abs(y0 - y1) > 1)
 | 
			
		||||
                {
 | 
			
		||||
                    if (fraction >= 0)
 | 
			
		||||
                    {
 | 
			
		||||
                        x0 += stepx;
 | 
			
		||||
                        fraction -= dy;
 | 
			
		||||
                    }
 | 
			
		||||
                    y0 += stepy;
 | 
			
		||||
                    fraction += dx;
 | 
			
		||||
                    @this.SetPixelSafe(x0, y0, color);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Draws a rhombus-like point
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="this">Texture</param>
 | 
			
		||||
        /// <param name="p0">Position</param>
 | 
			
		||||
        /// <param name="radius">Radius</param>
 | 
			
		||||
        /// <param name="color">Color</param>
 | 
			
		||||
        public static void DrawPoint(this Texture2D @this, Color color, Vector2 p0, int radius)
 | 
			
		||||
        {
 | 
			
		||||
            DrawPoint(@this, color, Convert.ToInt32(p0.x), Convert.ToInt32(p0.y), radius);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Draws a rhombus-like point
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="this">Texture</param>
 | 
			
		||||
        /// <param name="x0">Position</param>
 | 
			
		||||
        /// <param name="y0">Position</param>
 | 
			
		||||
        /// <param name="radius">Radius</param>
 | 
			
		||||
        /// <param name="color">Color</param>
 | 
			
		||||
        public static void DrawPoint(this Texture2D @this, Color color, int x0, int y0, int radius)
 | 
			
		||||
        {
 | 
			
		||||
            for (int y = y0 - radius; y <= y0 + radius; ++y)
 | 
			
		||||
            {
 | 
			
		||||
                int pts = radius - Math.Abs(y - y0);
 | 
			
		||||
 | 
			
		||||
                for (int x = x0 - pts; x <= x0 + pts; ++x)
 | 
			
		||||
                    @this.SetPixelSafe(x, y, color);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Fills a polygon
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="this">Texture</param>
 | 
			
		||||
        /// <param name="color">color</param>
 | 
			
		||||
        /// <param name="points">Points</param>
 | 
			
		||||
        public static void FillPolygon(this Texture2D @this, Color color, params Vector2[] points)
 | 
			
		||||
        {
 | 
			
		||||
            int minY = Int32.MaxValue, maxY = Int32.MinValue;
 | 
			
		||||
            // Unused... int[] ptX = points.Select(p => Convert.ToInt32(p.x)).ToArray();
 | 
			
		||||
            int[] ptY = points.Select(p => Convert.ToInt32(p.y)).ToArray();
 | 
			
		||||
            
 | 
			
		||||
            // Find min and max row
 | 
			
		||||
            for (int i = 0; i < points.Length; i++)
 | 
			
		||||
            {
 | 
			
		||||
                minY = Math.Min(minY, ptY[i]);
 | 
			
		||||
                maxY = Math.Max(maxY, ptY[i]);
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            List<int> intersPoints = new List<int>();
 | 
			
		||||
 | 
			
		||||
            // Go through each row
 | 
			
		||||
            for (int y = minY; y <= maxY; ++y)
 | 
			
		||||
            {
 | 
			
		||||
                intersPoints.Clear();
 | 
			
		||||
 | 
			
		||||
                // Find intersection points
 | 
			
		||||
                int j = points.Length - 1;
 | 
			
		||||
                for (int i = 0; i < points.Length; ++i)
 | 
			
		||||
                {
 | 
			
		||||
                    // Treat special case where we have 2 points on the y axis
 | 
			
		||||
                    if (ptY[i] == y && ptY[j] == y)
 | 
			
		||||
                    {
 | 
			
		||||
                        intersPoints.Add(Convert.ToInt32(points[i].x));
 | 
			
		||||
                        intersPoints.Add(Convert.ToInt32(points[j].x));
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    // Intersection
 | 
			
		||||
                    else if ((ptY[i] >= y && ptY[j] <= y) ||
 | 
			
		||||
                             (ptY[i] <= y && ptY[j] >= y))
 | 
			
		||||
                    {
 | 
			
		||||
                        int x = Convert.ToInt32(points[i].x + (y - points[i].y) * (points[j].x - points[i].x) / (points[j].y - points[i].y));
 | 
			
		||||
                        intersPoints.Add(x);
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    j = i;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                // Order pairs
 | 
			
		||||
                var intersPointsSorted = intersPoints.OrderBy(x => x).Distinct().ToArray();
 | 
			
		||||
 | 
			
		||||
                // Draw
 | 
			
		||||
                for (int i = 0; i < intersPointsSorted.Length / 2; i++)
 | 
			
		||||
                {
 | 
			
		||||
                    for (int x = intersPointsSorted[2 * i]; x <= intersPointsSorted[2 * i + 1]; x++)
 | 
			
		||||
                        @this.SetPixelSafe(x, y, color);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Fills texture with specified color
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="this">Texture</param>
 | 
			
		||||
        /// <param name="color">Color</param>
 | 
			
		||||
        public static void Fill(this Texture2D @this, Color color)
 | 
			
		||||
        {
 | 
			
		||||
            for (int x = 0; x < @this.width; ++x)
 | 
			
		||||
                for (int y = 0; y < @this.height; ++y)
 | 
			
		||||
                    @this.SetPixelSafe(x, y, color);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Sets a pixel after checking if coordinates are inside image
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="this">Texture</param>
 | 
			
		||||
        /// <param name="x">X</param>
 | 
			
		||||
        /// <param name="y">Y</param>
 | 
			
		||||
        /// <param name="color">Color</param>
 | 
			
		||||
        public static void SetPixelSafe(this Texture2D @this, int x, int y, Color color)
 | 
			
		||||
        {
 | 
			
		||||
            if (x >= 0 && y >= 0 && x < @this.width && y < @this.height)
 | 
			
		||||
                @this.SetPixel(x, y, color);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,25 +1,25 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.IO;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Text;
 | 
			
		||||
using System.Xml.Serialization;
 | 
			
		||||
 | 
			
		||||
namespace TransportGame.Utils
 | 
			
		||||
{
 | 
			
		||||
    public static class XmlHelper
 | 
			
		||||
    {
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Deserializes a file
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <typeparam name="T">Type to deserialize</typeparam>
 | 
			
		||||
        /// <param name="filename">File name</param>
 | 
			
		||||
        /// <returns>Deserialized object</returns>
 | 
			
		||||
        public static T Deserialize<T>(string filename)
 | 
			
		||||
        {
 | 
			
		||||
            XmlSerializer serializer = new XmlSerializer(typeof(T));
 | 
			
		||||
            var stream = new StreamReader(filename);
 | 
			
		||||
            return (T)serializer.Deserialize(stream);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.IO;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Text;
 | 
			
		||||
using System.Xml.Serialization;
 | 
			
		||||
 | 
			
		||||
namespace TransportGame.Utils
 | 
			
		||||
{
 | 
			
		||||
    public static class XmlHelper
 | 
			
		||||
    {
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Deserializes a file
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <typeparam name="T">Type to deserialize</typeparam>
 | 
			
		||||
        /// <param name="filename">File name</param>
 | 
			
		||||
        /// <returns>Deserialized object</returns>
 | 
			
		||||
        public static T Deserialize<T>(string filename)
 | 
			
		||||
        {
 | 
			
		||||
            XmlSerializer serializer = new XmlSerializer(typeof(T));
 | 
			
		||||
            var stream = new StreamReader(filename);
 | 
			
		||||
            return (T)serializer.Deserialize(stream);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user