Updated project to unity 5, finalized terrain generation

This commit is contained in:
2015-05-08 11:09:28 +03:00
parent 76527c2619
commit b1a8da324d
748 changed files with 20345 additions and 1571 deletions

Binary file not shown.

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TransportGame.Model;
namespace Assets.Scripts.Generator
{
public class RoadGenerator
{
public RoadGenerator()
{
}
public void Generate(Map map)
{
}
}
}

View File

@ -1,234 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TransportGame.Business;
using TransportGame.Model;
namespace TransportGame.Generator
{
/// <summary>
/// Simulates water erosion
/// </summary>
public class TerrainEroder
{
#region Constants
/// <summary> Direction vector (X)</summary>
/// <remarks>
/// In order: Center, North, North-East, East, South-East, South, South-West, West, North-West
/// </remarks>
readonly static private int[] DirectionsX = { 0, 0, 1, 1, 1, 0, -1, -1, -1 };
/// <summary> Direction vector (Y)</summary>
/// <remarks>
/// In order: Center, North, North-East, East, South-East, South, South-West, West, North-West
/// </remarks>
readonly static private int[] DirectionsY = { 0, -1, -1, 0, 1, 1, 1, 0, -1 };
#endregion
#region Public properties
/// <summary>
/// Gets the terrain to erode
/// </summary>
public Map Terrain { get; private set; }
#endregion
protected int SpringCount { get { return ConfigurationManager.TerrGenConfig.ErodePoints; } }
protected int IterationCount { get { return ConfigurationManager.TerrGenConfig.ErodeIterations; } }
protected float ErosionAmount { get { return ConfigurationManager.TerrGenConfig.ErodeAmountPercent; } }
#region Private variables
private Random random = new Random();
private float[,] water;
private int[] springsX;
private int[] springsY;
#endregion
public TerrainEroder(Map terrain)
{
this.Terrain = terrain;
}
public void Erode()
{
Initialize();
GenerateSprings();
for (int i = 0; i < IterationCount; i++)
NextIteration();
}
private void Initialize()
{
// Initialize matrices
water = new float[Terrain.Width, Terrain.Height];
}
private void GenerateSprings()
{
// Step 1: Generate random points, which will be the water springs
springsX = new int[SpringCount];
springsY = new int[SpringCount];
for (int i = 0; i < SpringCount; i++)
{
springsX[i] = random.Next(0, Terrain.Width);
springsY[i] = random.Next(0, Terrain.Height);
}
// Step 2: Find local maximums, where the springs are placed
bool changed;
int iterations = 100;
do
{
changed = false;
for (int i = 0; i < SpringCount; i++)
{
// Find best neighbour
float maxHeight = Terrain[springsX[i], springsY[i]];
int maxDir = 0;
ForEachDirection(springsX[i], springsY[i], (dx, dy, dir) =>
{
if (Terrain[dx, dy] >= maxHeight)
{
maxHeight = Terrain[dx, dy];
maxDir = dir;
}
});
if (maxDir > 0)
{
springsX[i] += DirectionsX[maxDir];
springsY[i] += DirectionsY[maxDir];
changed = true;
}
}
--iterations;
} while (changed && iterations > 0);
}
private void NextIteration()
{
Step1_RainAndErode();
Step2_MoveWater();
Step3_Evaporate();
}
private void Step1_RainAndErode()
{
// Springs generate a lot of water
for (int i = 0; i < SpringCount; i++)
water[springsX[i], springsY[i]] += 100f;
// Rain in the rest of the terrain
for (int x = 0; x < Terrain.Width; x++)
for (int y = 0; y < Terrain.Height; y++)
{
water[x, y] += 1f;
// Erode some terrain
Terrain[x, y] -= ErosionAmount * water[x, y];
}
}
private void Step2_MoveWater()
{
for (int x = 0; x < Terrain.Width; x++)
for (int y = 0; y < Terrain.Height; y++)
{
List<int> directions = new List<int>();
// Find relevant directions
ForEachDirection(x, y, (dx, dy, dir) =>
{
if (Level(x, y) > Level(dx, dy))
directions.Add(dir);
});
// Order directions by level
directions.Sort((dir1, dir2) =>
{
float level1 = Level(x + DirectionsX[dir1], y + DirectionsY[dir1]);
float level2 = Level(x + DirectionsX[dir2], y + DirectionsY[dir2]);
return Convert.ToInt32(100 * (level1 - level2));
});
// Distribute water
for (int dirIndex = 0; dirIndex < directions.Count; dirIndex++)
{
int dir = directions[dirIndex];
// We distribute all the water equally to cells from
// directions 0..dir.
float distributeAmount = water[x, y] / (dirIndex + 1);
// If the resulting level is higher than the level of the next direction,
// we only add the level difference
if (dirIndex < directions.Count - 1)
{
int dx = x + DirectionsX[dir], dy = y + DirectionsY[dir];
int dx1 = x + DirectionsX[directions[dirIndex + 1]], dy1 = y + DirectionsY[directions[dirIndex + 1]];
distributeAmount = Math.Min(distributeAmount, Level(dx1, dy1) - Level(dx, dy));
}
// Distribute water & sediment
for (int dirDistribIndex = 0; dirDistribIndex < dirIndex; dirDistribIndex++)
{
int dirDistrib = directions[dirDistribIndex];
water[x + DirectionsX[dirDistrib], y + DirectionsY[dirDistrib]] += distributeAmount;
}
// Subtract from current cell
water[x, y] -= distributeAmount * (dirIndex + 1);
}
}
}
private void Step3_Evaporate()
{
for (int x = 0; x < Terrain.Width; x++)
for (int y = 0; y < Terrain.Height; y++)
{
// Some sediment deposits
Terrain[x, y] += water[x, y] * ErosionAmount;
// Water evaporates
water[x, y] *= .5f;
}
}
private void ForEachDirection(int x, int y, Action<int, int, int> action)
{
for (int dir = 1; dir < DirectionsX.Length; dir++)
{
int dx = x + DirectionsX[dir];
int dy = y + DirectionsY[dir];
if (Terrain.IsInside(dx, dy))
action(dx, dy, dir);
}
}
/// <summary>
/// Gets the soil + water level
/// </summary>
/// <param name="x">x coordinate</param>
/// <param name="y">y coordinate</param>
/// <returns>level</returns>
private float Level(int x, int y)
{
return Terrain[x, y] + water[x, y];
}
}
}

View File

@ -27,7 +27,7 @@ namespace TransportGame.Generator
Noise = new PerlinNoiseGenerator();
if (ConfigurationManager.TerrGenConfig == null)
throw new Exception("WTF?");
throw new Exception("Not initialized!");
Noise.Octaves = ConfigurationManager.TerrGenConfig.NoiseOctaves;
Noise.NonLinearPower = ConfigurationManager.TerrGenConfig.NoiseNonLinearPower;
@ -50,12 +50,6 @@ namespace TransportGame.Generator
float waterAmount = random.NextSingle(map.Biome.Moisture.Minimum, map.Biome.Moisture.Maximum);
map.WaterLevel = Mathf.Pow(waterAmount, ConfigurationManager.TerrGenConfig.WaterNonLinearPower) * map.Biome.Height;
DumpData(map, "1generated.map");
// Simulate water erosion
new TerrainEroder(map).Erode();
DumpData(map, "2eroded.map");
return map;
}

View File

@ -1,12 +0,0 @@
fileFormatVersion: 2
guid: c1e1bcc13284685449456c96db1d1754
timeCreated: 1425647800
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: -100
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Assets.Scripts.Model
{
class Texture
{
}
}

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 204c90c5b648fab4eaf221d2d96e56c9
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

View File

@ -1,28 +1,28 @@
using System.Collections;
using System.Threading;
using TransportGame.Business;
using TransportGame.Model;
using TransportGame.Utils;
using UnityEngine;
public class InitializeScript : MonoBehaviour
{
// Use this for initialization
public void Start()
{
// Load configuration
Logger.Info("Loading configuration...");
ConfigurationManager.LoadConfiguration();
Logger.Info("Finished loading configuration.");
// Load biomes
Logger.Info("Loading biomes...");
BiomeManager.LoadBiomes();
Logger.Info("Finished loading biomes.");
}
// Update is called once per frame
void Update()
{
}
}
using System.Collections;
using System.Threading;
using TransportGame.Business;
using TransportGame.Model;
using TransportGame.Utils;
using UnityEngine;
public class InitializeScript : MonoBehaviour
{
// Use this for initialization
public void Start()
{
// Load configuration
Logger.Info("Loading configuration...");
ConfigurationManager.LoadConfiguration();
Logger.Info("Finished loading configuration.");
// Load biomes
Logger.Info("Loading biomes...");
BiomeManager.LoadBiomes();
Logger.Info("Finished loading biomes.");
}
// Update is called once per frame
void Update()
{
}
}

View File

@ -10,8 +10,8 @@ public class TerrainGeneratorScript : MonoBehaviour
{
private Map map = null;
public int TerrainWidth = 256;
public int TerrainHeight = 256;
public int TerrainWidth = 1024;
public int TerrainHeight = 1024;
public GameObject WaterObject;
// Use this for initialization
@ -20,7 +20,7 @@ public class TerrainGeneratorScript : MonoBehaviour
StartCoroutine(GenerateMap());
}
private void GenerateMapThread()
private void GenerateTerrainThread()
{
TerrainGenerator generator = new TerrainGenerator();
map = generator.Generate(TerrainWidth, TerrainHeight);
@ -51,8 +51,8 @@ public class TerrainGeneratorScript : MonoBehaviour
private IEnumerator GenerateMap()
{
// Wait for the map generation thread
foreach (var i in Task.RunAsync(GenerateMapThread))
yield return i;
foreach (var i in Task.RunAsync(GenerateTerrainThread))
yield return i;
// Generate terrain data
TerrainData terrainData = new TerrainData();
@ -78,6 +78,9 @@ public class TerrainGeneratorScript : MonoBehaviour
MeshFilter waterMesh = WaterObject.GetComponent<MeshFilter>();
waterMesh.mesh = GenerateWater();
}
// Set up textures
}
// Update is called once per frame

View File

@ -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
{
}
}

View File

@ -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);
}
}
}

View File

@ -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;
}
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -0,0 +1,67 @@
 /*
Writen by Windexglow 11-13-10. Use it, edit it, steal it I don't care.
Simple flycam I made, since I couldn't find any others made public.
Made simple to use (drag and drop, done) for regular keyboard layout
wasd : basic movement
shift : Makes camera accelerate
space : Moves camera on X and Z axis only. So camera doesn't gain any height*/
var mainSpeed : float = 100.0; //regular speed
var shiftAdd : float = 250.0; //multiplied by how long shift is held. Basically running
var maxShift : float = 1000.0; //Maximum speed when holdin gshift
var camSens : float = 0.25; //How sensitive it with mouse
private var lastMouse = Vector3(255, 255, 255); //kind of in the middle of the screen, rather than at the top (play)
private var totalRun : float = 1.0;
function Update () {
lastMouse = Input.mousePosition - lastMouse ;
lastMouse = Vector3(-lastMouse.y * camSens, lastMouse.x * camSens, 0 );
lastMouse = Vector3(transform.eulerAngles.x + lastMouse.x , transform.eulerAngles.y + lastMouse.y, 0);
transform.eulerAngles = lastMouse;
lastMouse = Input.mousePosition;
//Mouse camera angle done.
//Keyboard commands
var f : float = 0.0;
var p = GetBaseInput();
if (Input.GetKey (KeyCode.LeftShift)){
totalRun += Time.deltaTime;
p = p * totalRun * shiftAdd;
p.x = Mathf.Clamp(p.x, -maxShift, maxShift);
p.y = Mathf.Clamp(p.y, -maxShift, maxShift);
p.z = Mathf.Clamp(p.z, -maxShift, maxShift);
}
else{
totalRun = Mathf.Clamp(totalRun * 0.5, 1, 1000);
p = p * mainSpeed;
}
p = p * Time.deltaTime;
if (Input.GetKey(KeyCode.Space)){ //If player wants to move on X and Z axis only
f = transform.position.y;
transform.Translate(p);
transform.position.y = f;
}
else{
transform.Translate( p);
}
}
private function GetBaseInput() : Vector3 { //returns the basic values, if it's 0 than it's not active.
var p_Velocity : Vector3;
if (Input.GetKey (KeyCode.W)){
p_Velocity += Vector3(0, 0 , 1);
}
if (Input.GetKey (KeyCode.S)){
p_Velocity += Vector3(0, 0 , -1);
}
if (Input.GetKey (KeyCode.A)){
p_Velocity += Vector3(-1, 0 , 0);
}
if (Input.GetKey (KeyCode.D)){
p_Velocity += Vector3(1, 0 , 0);
}
return p_Velocity;
}

View File

@ -1,32 +1,32 @@
using System;
using UnityEngine;
namespace UnityStandardAssets.Water
{
[ExecuteInEditMode]
public class WaterBasic : MonoBehaviour
{
void Update()
{
Renderer r = GetComponent<Renderer>();
if (!r)
{
return;
}
Material mat = r.sharedMaterial;
if (!mat)
{
return;
}
Vector4 waveSpeed = mat.GetVector("WaveSpeed");
float waveScale = mat.GetFloat("_WaveScale");
float t = Time.time / 20.0f;
Vector4 offset4 = waveSpeed * (t * waveScale);
Vector4 offsetClamped = new Vector4(Mathf.Repeat(offset4.x, 1.0f), Mathf.Repeat(offset4.y, 1.0f),
Mathf.Repeat(offset4.z, 1.0f), Mathf.Repeat(offset4.w, 1.0f));
mat.SetVector("_WaveOffset", offsetClamped);
}
}
using System;
using UnityEngine;
namespace UnityStandardAssets.Water
{
[ExecuteInEditMode]
public class WaterBasic : MonoBehaviour
{
void Update()
{
Renderer r = GetComponent<Renderer>();
if (!r)
{
return;
}
Material mat = r.sharedMaterial;
if (!mat)
{
return;
}
Vector4 waveSpeed = mat.GetVector("WaveSpeed");
float waveScale = mat.GetFloat("_WaveScale");
float t = Time.time / 20.0f;
Vector4 offset4 = waveSpeed * (t * waveScale);
Vector4 offsetClamped = new Vector4(Mathf.Repeat(offset4.x, 1.0f), Mathf.Repeat(offset4.y, 1.0f),
Mathf.Repeat(offset4.z, 1.0f), Mathf.Repeat(offset4.w, 1.0f));
mat.SetVector("_WaveOffset", offsetClamped);
}
}
}

View File

@ -1,7 +1,18 @@
fileFormatVersion: 2
guid: 38d405c119fcc7c4e83d4a478a40ff2f
MonoAssemblyImporter:
PluginImporter:
serializedVersion: 1
iconMap: {}
executionOrder: {}
isPreloaded: 0
platformData:
Any:
enabled: 0
settings: {}
Editor:
enabled: 1
settings:
DefaultValueInitialized: true
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,7 +1,18 @@
fileFormatVersion: 2
guid: 4ad02dc83da735c4e8d945332b9202f6
MonoAssemblyImporter:
PluginImporter:
serializedVersion: 1
iconMap: {}
executionOrder: {}
isPreloaded: 0
platformData:
Any:
enabled: 0
settings: {}
Editor:
enabled: 1
settings:
DefaultValueInitialized: true
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,6 +1,6 @@
fileFormatVersion: 2
guid: 4e25b9a3d527f7a4eb6c14cd8d61012b
timeCreated: 1425647041
timeCreated: 1431071541
licenseType: Free
PluginImporter:
serializedVersion: 1