Advertisement
SebastianLague

Hex Gen Test

Jul 1st, 2015
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.29 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3.  
  4. public class HexGrid : MonoBehaviour {
  5.  
  6.     public int width = 10;
  7.     public int height = 10;
  8.     public float hexSize = .5f;
  9.  
  10.     Hexagon[,] hexagons;
  11.  
  12.     List<Vector3> vertices;
  13.     List<int> triangles;
  14.  
  15.     Dictionary<string,List<int>> triangleDictionary = new Dictionary<string, List<int>>();
  16.  
  17.     void Update() {
  18.         if (Input.GetKeyDown (KeyCode.Mouse0)) {
  19.             Generate();
  20.         }
  21.     }
  22.  
  23.     void Generate() {
  24.         triangleDictionary.Clear ();
  25.         int[,] map = GetComponent<MapGenerator> ().GenerateMap (width, height, 51);
  26.  
  27.         hexagons = new Hexagon[width,height];
  28.         vertices = new List<Vector3> ();
  29.         triangles = new List<int> ();
  30.  
  31.         for (int y = 0; y < height; y++) {
  32.             for (int x = 0; x < width; x++) {
  33.                 float hexWidth = hexSize * 2 * Mathf.Sqrt(3)/2;
  34.                 float hexHeight = hexSize * 2;
  35.                 Vector3 position = new Vector3(-width/2 + hexWidth * x + ((y%2==0)?0:hexWidth/2),0, height/2 - hexHeight * 3f/4f * y);
  36.                 //print (position.z);
  37.                 //  Vector3 position = new Vector3(-width/2 + hexRadius * x + ((y%2==0)?0:hexRadius),0 -height/2 + hexRadius * (y+1));
  38.                 hexagons[x,y] = new Hexagon(position);
  39.                 List<int> vertexIndices = new List<int>();
  40.  
  41.                 for (int i = 0; i < 6; i ++) {
  42.                     int angle = 30 + 60*i;
  43.                     int vertexIndex = vertices.Count;
  44.                     Vector3 vertexPos;
  45.  
  46.                     switch (i) {
  47.                     case 0:
  48.                         if (InRange(x+((y%2==0)?0:1),y-1)) {
  49.                             vertexIndex = hexagons[x+((y%2==0)?0:1),y-1].bottom;
  50.                         }
  51.                         break;
  52.                     case 1:
  53.                         if (InRange(x,y-1)) {
  54.                             if (y%2==0) {
  55.                                 vertexIndex = hexagons[x, y-1].bottomLeft;
  56.                             }
  57.                             else {
  58.                                 vertexIndex = hexagons[x, y-1].bottomRight;
  59.                             }
  60.                         }
  61.                         break;
  62.                     case 2:
  63.                         if (InRange(x-1,y)) {
  64.                             vertexIndex = hexagons[x-1, y].topRight;
  65.                         }
  66.  
  67.                         else if (InRange(x,y-1)) {
  68.                             if (y%2==0) {
  69.                                 if (InRange(x-1,y-1)) {
  70.                                     vertexIndex = hexagons[x-1, y-1].bottom;
  71.                                 }
  72.                             }
  73.                             else {
  74.                                 vertexIndex = hexagons[x, y-1].bottom;
  75.                             }
  76.                         }
  77.                         break;
  78.                     case 3:
  79.                         if (InRange(x-1,y)) {
  80.                             vertexIndex = hexagons[x-1, y].bottomRight;
  81.                         }
  82.                         break;
  83.                     }
  84.  
  85.                     if (vertexIndex < vertices.Count) {
  86.                         vertexPos = vertices[vertexIndex];
  87.                     }
  88.                     else {
  89.                         vertexPos = new Vector3(position.x + Mathf.Cos(angle * Mathf.Deg2Rad) * hexSize, 0, position.z +  Mathf.Sin(angle * Mathf.Deg2Rad) * hexSize);
  90.                         vertices.Add(vertexPos);
  91.                     }
  92.                     vertexIndices.Add(vertexIndex);
  93.                 }
  94.                 hexagons[x,y].SetVertices(vertexIndices[0],vertexIndices[1],vertexIndices[2],vertexIndices[3],vertexIndices[4],vertexIndices[5]);
  95.                 //Debug.DrawLine(vertices[vertexIndices[0]],vertices[vertexIndices[1]],Color.green,10);
  96.                 //Debug.DrawLine(vertices[vertexIndices[1]],vertices[vertexIndices[2]],Color.green,10);
  97.                 //Debug.DrawLine(vertices[vertexIndices[2]],vertices[vertexIndices[3]],Color.green,10);
  98.                 //Debug.DrawLine(vertices[vertexIndices[3]],vertices[vertexIndices[4]],Color.green,10);
  99.                 //Debug.DrawLine(vertices[vertexIndices[4]],vertices[vertexIndices[5]],Color.green,10);
  100.  
  101.                 triangles.Add(vertexIndices[0]);
  102.                 triangles.Add(vertexIndices[5]);
  103.                 triangles.Add(vertexIndices[4]);
  104.  
  105.                 triangles.Add(vertexIndices[0]);
  106.                 triangles.Add(vertexIndices[4]);
  107.                 triangles.Add(vertexIndices[3]);
  108.  
  109.                 triangles.Add(vertexIndices[0]);
  110.                 triangles.Add(vertexIndices[3]);
  111.                 triangles.Add(vertexIndices[2]);
  112.  
  113.                 triangles.Add(vertexIndices[0]);
  114.                 triangles.Add(vertexIndices[2]);
  115.                 triangles.Add(vertexIndices[1]);
  116.  
  117.                 List<int> t = new List<int>();
  118.                 t.Add(vertexIndices[0]);
  119.                 t.Add(vertexIndices[5]);
  120.                 t.Add(vertexIndices[4]);
  121.                 t.Add(vertexIndices[0]);
  122.                 t.Add(vertexIndices[4]);
  123.                 t.Add(vertexIndices[3]);
  124.                 t.Add(vertexIndices[0]);
  125.                 t.Add(vertexIndices[3]);
  126.                 t.Add(vertexIndices[2]);
  127.                 t.Add(vertexIndices[0]);
  128.                 t.Add(vertexIndices[2]);
  129.                 t.Add(vertexIndices[1]);
  130.  
  131.                 triangleDictionary.Add((x+" " + y),t);
  132.  
  133.             }
  134.         }
  135.  
  136.         for (int i =0; i <vertices.Count; i++) {
  137.             Vector3 displacement = Random.insideUnitSphere * hexSize/4;
  138.             displacement.y = 0;
  139.             vertices[i] += displacement;
  140.         }
  141.        
  142.  
  143.         Mesh mesh = new Mesh ();
  144.         mesh.vertices = vertices.ToArray();
  145.         //mesh.triangles = triangles.ToArray ();
  146.         //mesh.RecalculateNormals ();
  147.  
  148.    
  149.  
  150.         List<int> landTris = new List<int> ();
  151.         List<int> seaTris = new List<int> ();
  152.  
  153.         for (int y = 0; y < height; y++) {
  154.             for (int x = 0; x < width; x++) {
  155.                 if (map[x,y] == 0) {
  156.                     foreach (int n in triangleDictionary[x + " " + y]) {
  157.                         landTris.Add(n);
  158.                     }
  159.                 }
  160.                 else {
  161.                     foreach (int n in triangleDictionary[x + " " + y]) {
  162.                         seaTris.Add(n);
  163.                     }
  164.                 }
  165.             }
  166.         }
  167.  
  168.         mesh.subMeshCount = 2;
  169.         mesh.SetTriangles (landTris.ToArray (), 0);
  170.         mesh.SetTriangles (seaTris.ToArray (), 1);
  171.         gameObject.GetComponent<MeshFilter> ().mesh = mesh;
  172.         //gameObject.AddComponent<MeshRenderer> ();
  173.     }
  174.  
  175.     bool InRange(int x, int y) {
  176.         return x >= 0 && x < width && y >= 0 && y < height;
  177.     }
  178.  
  179.     class Hexagon {
  180.         public int top, bottom, bottomLeft, bottomRight, topLeft, topRight;
  181.         public Vector3 centre;
  182.  
  183.         public Hexagon (Vector3 _centre) {
  184.             centre = _centre;
  185.         }
  186.  
  187.         public void SetVertices(int _topRight, int _top, int _topLeft, int _bottomLeft, int _bottom, int _bottomRight) {
  188.             top = _top;
  189.             bottom = _bottom;
  190.             bottomLeft = _bottomLeft;
  191.             bottomRight = _bottomRight;
  192.             topLeft = _topLeft;
  193.             topRight = _topRight;
  194.         }
  195.  
  196.  
  197.     }
  198. }
  199.  
  200. using UnityEngine;
  201. using System.Collections;
  202. using System;
  203.  
  204. public class MapGenerator : MonoBehaviour {
  205.  
  206.     public int width;
  207.     public int height;
  208.  
  209.     public string seed;
  210.     bool useRandomSeed = true;
  211.  
  212.     [Range(0,100)]
  213.     public int randomFillPercent;
  214.  
  215.     int[,] map;
  216.  
  217.  
  218.     public int[,] GenerateMap(int _width, int _height, int _fillPercent) {
  219.         width = _width;
  220.         height = _height;
  221.         randomFillPercent = _fillPercent;
  222.  
  223.         map = new int[width,height];
  224.         RandomFillMap();
  225.  
  226.         for (int i = 0; i < 5; i ++) {
  227.             SmoothMap();
  228.         }
  229.  
  230.         return map;
  231.     }
  232.  
  233.  
  234.     void RandomFillMap() {
  235.         if (useRandomSeed) {
  236.             seed = System.DateTime.Now.ToString() +Time.time.ToString();
  237.         }
  238.  
  239.         System.Random pseudoRandom = new System.Random(seed.GetHashCode());
  240.  
  241.         for (int x = 0; x < width; x ++) {
  242.             for (int y = 0; y < height; y ++) {
  243.                 if (x == 0 || x == width-1 || y == 0 || y == height -1) {
  244.                     map[x,y] = 1;
  245.                 }
  246.                 else {
  247.                     map[x,y] = (pseudoRandom.Next(0,100) < randomFillPercent)? 1: 0;
  248.                 }
  249.             }
  250.         }
  251.     }
  252.  
  253.     void SmoothMap() {
  254.         for (int x = 0; x < width; x ++) {
  255.             for (int y = 0; y < height; y ++) {
  256.                 int neighbourWallTiles = GetSurroundingWallCount(x,y);
  257.  
  258.                 if (neighbourWallTiles > 4)
  259.                     map[x,y] = 1;
  260.                 else if (neighbourWallTiles < 4)
  261.                     map[x,y] = 0;
  262.  
  263.             }
  264.         }
  265.     }
  266.  
  267.     int GetSurroundingWallCount(int gridX, int gridY) {
  268.         int wallCount = 0;
  269.         for (int neighbourX = gridX - 1; neighbourX <= gridX + 1; neighbourX ++) {
  270.             for (int neighbourY = gridY - 1; neighbourY <= gridY + 1; neighbourY ++) {
  271.                 if (neighbourX >= 0 && neighbourX < width && neighbourY >= 0 && neighbourY < height) {
  272.                     if (neighbourX != gridX || neighbourY != gridY) {
  273.                         wallCount += map[neighbourX,neighbourY];
  274.                     }
  275.                 }
  276.                 else {
  277.                     wallCount ++;
  278.                 }
  279.             }
  280.         }
  281.  
  282.         return wallCount;
  283.     }
  284.  
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement