156 lines
5.3 KiB
C#
156 lines
5.3 KiB
C#
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);
|
|
}
|
|
}
|
|
}
|