Initial commit
This commit is contained in:
155
Game/Assets/Scripts/Generator/MapImageRenderer.cs
Normal file
155
Game/Assets/Scripts/Generator/MapImageRenderer.cs
Normal file
@ -0,0 +1,155 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using TransportGame.Model;
|
||||
using TransportGame.Utils;
|
||||
using UnityEngine;
|
||||
|
||||
namespace TransportGame.Generators
|
||||
{
|
||||
public static class MapImageRenderer
|
||||
{
|
||||
[Flags]
|
||||
public enum Layers
|
||||
{
|
||||
None = 0,
|
||||
Elevation = 1,
|
||||
Grid = 2,
|
||||
Centers = 4,
|
||||
All = Elevation | Grid | Centers
|
||||
};
|
||||
|
||||
private const int Scale = 20;
|
||||
|
||||
/// <summary>
|
||||
/// Renders a map to a file
|
||||
/// </summary>
|
||||
/// <param name="file"></param>
|
||||
/// <param name="map"></param>
|
||||
/// <param name="layers"></param>
|
||||
//public static void Render(this Map map, string file, Layers layers = Layers.All)
|
||||
//{
|
||||
// // Create texture on which to draw
|
||||
// Texture2D texture = new Texture2D(
|
||||
// map.Width * Scale,
|
||||
// map.Height * Scale,
|
||||
// TextureFormat.RGB24,
|
||||
// false);
|
||||
|
||||
// // First layer - cells
|
||||
// DrawCells(texture, map, (layers & Layers.Elevation) > 0, (layers & Layers.Centers) > 0);
|
||||
|
||||
// // Second layer - grid
|
||||
// if ((layers & Layers.Grid) > 0)
|
||||
// DrawGrid(texture, map);
|
||||
|
||||
// // Write to file
|
||||
// File.WriteAllBytes(file + ".png", texture.EncodeToPNG());
|
||||
//}
|
||||
|
||||
//private static void DrawCells(Texture2D texture, Map map, bool elevation, bool centers)
|
||||
//{
|
||||
// for (int x = 0; x < map.Width; x++)
|
||||
// for (int y = 0; y < map.Height; y++)
|
||||
// {
|
||||
// // Calculate scaled corners
|
||||
// Vector2 c00 = new Vector2(
|
||||
// map[x, y].Corner00.Pos.x * Scale,
|
||||
// map[x, y].Corner00.Pos.y * Scale);
|
||||
// Vector2 c01 = new Vector2(
|
||||
// map[x, y].Corner01.Pos.x * Scale,
|
||||
// map[x, y].Corner01.Pos.y * Scale);
|
||||
// Vector2 c10 = new Vector2(
|
||||
// map[x, y].Corner10.Pos.x * Scale,
|
||||
// map[x, y].Corner10.Pos.y * Scale);
|
||||
// Vector2 c11 = new Vector2(
|
||||
// map[x, y].Corner11.Pos.x * Scale,
|
||||
// map[x, y].Corner11.Pos.y * Scale);
|
||||
|
||||
// // Calculate color
|
||||
// Color c = ColorHelper.FromArgb(0x555500);
|
||||
|
||||
// // Water - always blue
|
||||
// if (map.IsWater(x, y))
|
||||
// c = Color.blue;
|
||||
|
||||
// // Map type is elevation map - get elevation color
|
||||
// else if (elevation)
|
||||
// c = GetCollorOfTerrain(map[x, y].Elevation);
|
||||
|
||||
// // Draw polygon
|
||||
// texture.FillPolygon(c, c00, c01, c11, c10);
|
||||
|
||||
// // Draw center
|
||||
// if (centers)
|
||||
// texture.DrawPoint(Color.red,
|
||||
// Convert.ToInt32(map[x, y].Center.x * Scale),
|
||||
// Convert.ToInt32(map[x, y].Center.y * Scale),
|
||||
// 2);
|
||||
// }
|
||||
//}
|
||||
|
||||
//private static void DrawGrid(Texture2D texture, Map map)
|
||||
//{
|
||||
// for (int x = 0; x < map.Width + 1; x++)
|
||||
// for (int y = 0; y < map.Height + 1; y++)
|
||||
// {
|
||||
// Corner c0 = map.Corner(x, y);
|
||||
// Vector2 c0pos = new Vector2(c0.Pos.x * Scale, c0.Pos.y * Scale);
|
||||
|
||||
// // Draw edges
|
||||
// if (x > 0)
|
||||
// {
|
||||
// Corner c1 = map.Corner(x - 1, y);
|
||||
// Vector2 c1pos = new Vector2(c1.Pos.x * Scale, c1.Pos.y * Scale);
|
||||
// texture.DrawLine(Color.white, c0pos, c1pos);
|
||||
// }
|
||||
// if (y > 0)
|
||||
// {
|
||||
// Corner c1 = map.Corner(x, y - 1);
|
||||
// Vector2 c1pos = new Vector2(c1.Pos.x * Scale, c1.Pos.y * Scale);
|
||||
// texture.DrawLine(Color.white, c0pos, c1pos);
|
||||
// }
|
||||
|
||||
// // Draw corner
|
||||
// texture.DrawPoint(Color.white, c0pos, 2);
|
||||
// }
|
||||
//}
|
||||
|
||||
private static Color GetCollorOfTerrain(float height)
|
||||
{
|
||||
Color color1, color2;
|
||||
float alpha;
|
||||
|
||||
if (height < 10f)
|
||||
{
|
||||
color1 = ColorHelper.FromArgb(0x00ffa2);
|
||||
color2 = ColorHelper.FromArgb(0xFCD628);
|
||||
alpha = height / 10f;
|
||||
}
|
||||
|
||||
else if (height < 25f)
|
||||
{
|
||||
color1 = ColorHelper.FromArgb(0xFCD628);
|
||||
color2 = ColorHelper.FromArgb(0x9C6713);
|
||||
alpha = (height - 10f) / 15f;
|
||||
}
|
||||
|
||||
else if (height < 50f)
|
||||
{
|
||||
color1 = ColorHelper.FromArgb(0xaaaaaa);
|
||||
color2 = ColorHelper.FromArgb(0xffffff);
|
||||
alpha = (height - 25f) / 25f;
|
||||
}
|
||||
else
|
||||
{
|
||||
color1 = color2 = Color.white;
|
||||
alpha = 1;
|
||||
}
|
||||
|
||||
return Color.Lerp(color1, color2, alpha);
|
||||
}
|
||||
}
|
||||
}
|
8
Game/Assets/Scripts/Generator/MapImageRenderer.cs.meta
Normal file
8
Game/Assets/Scripts/Generator/MapImageRenderer.cs.meta
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d89d584776261fe4b81720b3615755bb
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
61
Game/Assets/Scripts/Generator/TerrainGenerator.cs
Normal file
61
Game/Assets/Scripts/Generator/TerrainGenerator.cs
Normal file
@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using TransportGame.Business;
|
||||
using TransportGame.Model;
|
||||
using TransportGame.Noise;
|
||||
using TransportGame.Utils;
|
||||
using TransportGame.Utils.Algorithms;
|
||||
using UnityEngine;
|
||||
|
||||
namespace TransportGame.Generator
|
||||
{
|
||||
public class TerrainGenerator
|
||||
{
|
||||
public NoiseGenerator Noise { get; set; }
|
||||
|
||||
private System.Random random = new System.Random();
|
||||
|
||||
public TerrainGenerator()
|
||||
{
|
||||
Noise = new PerlinNoiseGenerator();
|
||||
Noise.Octaves = ConfigurationManager.TerrGenConfig.NoiseOctaves;
|
||||
Noise.NonLinearPower = ConfigurationManager.TerrGenConfig.NoiseNonLinearPower;
|
||||
Noise.Scale = ConfigurationManager.TerrGenConfig.ElevationScale;
|
||||
}
|
||||
|
||||
public Map Generate(int width, int height)
|
||||
{
|
||||
// Create map
|
||||
Map map = new Map(width, height);
|
||||
|
||||
// Pick a random biome
|
||||
map.Biome = PickBiome();
|
||||
|
||||
// Generate elevation
|
||||
GenerateElevation(map);
|
||||
|
||||
// Generate water level
|
||||
float waterAmount = random.NextSingle(map.Biome.Moisture.Minimum, map.Biome.Moisture.Maximum);
|
||||
map.WaterLevel = Mathf.Pow(waterAmount, 3) * (map.Biome.HeightRange.Maximum - map.Biome.HeightRange.Minimum) + map.Biome.HeightRange.Minimum;
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
private Biome PickBiome()
|
||||
{
|
||||
int biomeCount = BiomeManager.Biomes.Count();
|
||||
int biome = random.Next(biomeCount);
|
||||
|
||||
return BiomeManager.Biomes.ElementAt(biome);
|
||||
}
|
||||
|
||||
private void GenerateElevation(Map map)
|
||||
{
|
||||
for (int x = 0; x < map.Width; ++x)
|
||||
for (int y = 0; y < map.Height; ++y)
|
||||
map[x, y] = Noise.Generate(x, y, map.Biome.HeightRange.Minimum, map.Biome.HeightRange.Maximum);
|
||||
}
|
||||
}
|
||||
}
|
8
Game/Assets/Scripts/Generator/TerrainGenerator.cs.meta
Normal file
8
Game/Assets/Scripts/Generator/TerrainGenerator.cs.meta
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f4042531a480f4149bdda36c67975b0c
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
Reference in New Issue
Block a user