Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- public class HexGrid : MonoBehaviour {
- public int width = 10;
- public int height = 10;
- public float hexSize = .5f;
- Hexagon[,] hexagons;
- List<Vector3> vertices;
- List<int> triangles;
- Dictionary<string,List<int>> triangleDictionary = new Dictionary<string, List<int>>();
- void Update() {
- if (Input.GetKeyDown (KeyCode.Mouse0)) {
- Generate();
- }
- }
- void Generate() {
- triangleDictionary.Clear ();
- int[,] map = GetComponent<MapGenerator> ().GenerateMap (width, height, 51);
- hexagons = new Hexagon[width,height];
- vertices = new List<Vector3> ();
- triangles = new List<int> ();
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x++) {
- float hexWidth = hexSize * 2 * Mathf.Sqrt(3)/2;
- float hexHeight = hexSize * 2;
- Vector3 position = new Vector3(-width/2 + hexWidth * x + ((y%2==0)?0:hexWidth/2),0, height/2 - hexHeight * 3f/4f * y);
- //print (position.z);
- // Vector3 position = new Vector3(-width/2 + hexRadius * x + ((y%2==0)?0:hexRadius),0 -height/2 + hexRadius * (y+1));
- hexagons[x,y] = new Hexagon(position);
- List<int> vertexIndices = new List<int>();
- for (int i = 0; i < 6; i ++) {
- int angle = 30 + 60*i;
- int vertexIndex = vertices.Count;
- Vector3 vertexPos;
- switch (i) {
- case 0:
- if (InRange(x+((y%2==0)?0:1),y-1)) {
- vertexIndex = hexagons[x+((y%2==0)?0:1),y-1].bottom;
- }
- break;
- case 1:
- if (InRange(x,y-1)) {
- if (y%2==0) {
- vertexIndex = hexagons[x, y-1].bottomLeft;
- }
- else {
- vertexIndex = hexagons[x, y-1].bottomRight;
- }
- }
- break;
- case 2:
- if (InRange(x-1,y)) {
- vertexIndex = hexagons[x-1, y].topRight;
- }
- else if (InRange(x,y-1)) {
- if (y%2==0) {
- if (InRange(x-1,y-1)) {
- vertexIndex = hexagons[x-1, y-1].bottom;
- }
- }
- else {
- vertexIndex = hexagons[x, y-1].bottom;
- }
- }
- break;
- case 3:
- if (InRange(x-1,y)) {
- vertexIndex = hexagons[x-1, y].bottomRight;
- }
- break;
- }
- if (vertexIndex < vertices.Count) {
- vertexPos = vertices[vertexIndex];
- }
- else {
- vertexPos = new Vector3(position.x + Mathf.Cos(angle * Mathf.Deg2Rad) * hexSize, 0, position.z + Mathf.Sin(angle * Mathf.Deg2Rad) * hexSize);
- vertices.Add(vertexPos);
- }
- vertexIndices.Add(vertexIndex);
- }
- hexagons[x,y].SetVertices(vertexIndices[0],vertexIndices[1],vertexIndices[2],vertexIndices[3],vertexIndices[4],vertexIndices[5]);
- //Debug.DrawLine(vertices[vertexIndices[0]],vertices[vertexIndices[1]],Color.green,10);
- //Debug.DrawLine(vertices[vertexIndices[1]],vertices[vertexIndices[2]],Color.green,10);
- //Debug.DrawLine(vertices[vertexIndices[2]],vertices[vertexIndices[3]],Color.green,10);
- //Debug.DrawLine(vertices[vertexIndices[3]],vertices[vertexIndices[4]],Color.green,10);
- //Debug.DrawLine(vertices[vertexIndices[4]],vertices[vertexIndices[5]],Color.green,10);
- triangles.Add(vertexIndices[0]);
- triangles.Add(vertexIndices[5]);
- triangles.Add(vertexIndices[4]);
- triangles.Add(vertexIndices[0]);
- triangles.Add(vertexIndices[4]);
- triangles.Add(vertexIndices[3]);
- triangles.Add(vertexIndices[0]);
- triangles.Add(vertexIndices[3]);
- triangles.Add(vertexIndices[2]);
- triangles.Add(vertexIndices[0]);
- triangles.Add(vertexIndices[2]);
- triangles.Add(vertexIndices[1]);
- List<int> t = new List<int>();
- t.Add(vertexIndices[0]);
- t.Add(vertexIndices[5]);
- t.Add(vertexIndices[4]);
- t.Add(vertexIndices[0]);
- t.Add(vertexIndices[4]);
- t.Add(vertexIndices[3]);
- t.Add(vertexIndices[0]);
- t.Add(vertexIndices[3]);
- t.Add(vertexIndices[2]);
- t.Add(vertexIndices[0]);
- t.Add(vertexIndices[2]);
- t.Add(vertexIndices[1]);
- triangleDictionary.Add((x+" " + y),t);
- }
- }
- for (int i =0; i <vertices.Count; i++) {
- Vector3 displacement = Random.insideUnitSphere * hexSize/4;
- displacement.y = 0;
- vertices[i] += displacement;
- }
- Mesh mesh = new Mesh ();
- mesh.vertices = vertices.ToArray();
- //mesh.triangles = triangles.ToArray ();
- //mesh.RecalculateNormals ();
- List<int> landTris = new List<int> ();
- List<int> seaTris = new List<int> ();
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x++) {
- if (map[x,y] == 0) {
- foreach (int n in triangleDictionary[x + " " + y]) {
- landTris.Add(n);
- }
- }
- else {
- foreach (int n in triangleDictionary[x + " " + y]) {
- seaTris.Add(n);
- }
- }
- }
- }
- mesh.subMeshCount = 2;
- mesh.SetTriangles (landTris.ToArray (), 0);
- mesh.SetTriangles (seaTris.ToArray (), 1);
- gameObject.GetComponent<MeshFilter> ().mesh = mesh;
- //gameObject.AddComponent<MeshRenderer> ();
- }
- bool InRange(int x, int y) {
- return x >= 0 && x < width && y >= 0 && y < height;
- }
- class Hexagon {
- public int top, bottom, bottomLeft, bottomRight, topLeft, topRight;
- public Vector3 centre;
- public Hexagon (Vector3 _centre) {
- centre = _centre;
- }
- public void SetVertices(int _topRight, int _top, int _topLeft, int _bottomLeft, int _bottom, int _bottomRight) {
- top = _top;
- bottom = _bottom;
- bottomLeft = _bottomLeft;
- bottomRight = _bottomRight;
- topLeft = _topLeft;
- topRight = _topRight;
- }
- }
- }
- using UnityEngine;
- using System.Collections;
- using System;
- public class MapGenerator : MonoBehaviour {
- public int width;
- public int height;
- public string seed;
- bool useRandomSeed = true;
- [Range(0,100)]
- public int randomFillPercent;
- int[,] map;
- public int[,] GenerateMap(int _width, int _height, int _fillPercent) {
- width = _width;
- height = _height;
- randomFillPercent = _fillPercent;
- map = new int[width,height];
- RandomFillMap();
- for (int i = 0; i < 5; i ++) {
- SmoothMap();
- }
- return map;
- }
- void RandomFillMap() {
- if (useRandomSeed) {
- seed = System.DateTime.Now.ToString() +Time.time.ToString();
- }
- System.Random pseudoRandom = new System.Random(seed.GetHashCode());
- for (int x = 0; x < width; x ++) {
- for (int y = 0; y < height; y ++) {
- if (x == 0 || x == width-1 || y == 0 || y == height -1) {
- map[x,y] = 1;
- }
- else {
- map[x,y] = (pseudoRandom.Next(0,100) < randomFillPercent)? 1: 0;
- }
- }
- }
- }
- void SmoothMap() {
- for (int x = 0; x < width; x ++) {
- for (int y = 0; y < height; y ++) {
- int neighbourWallTiles = GetSurroundingWallCount(x,y);
- if (neighbourWallTiles > 4)
- map[x,y] = 1;
- else if (neighbourWallTiles < 4)
- map[x,y] = 0;
- }
- }
- }
- int GetSurroundingWallCount(int gridX, int gridY) {
- int wallCount = 0;
- for (int neighbourX = gridX - 1; neighbourX <= gridX + 1; neighbourX ++) {
- for (int neighbourY = gridY - 1; neighbourY <= gridY + 1; neighbourY ++) {
- if (neighbourX >= 0 && neighbourX < width && neighbourY >= 0 && neighbourY < height) {
- if (neighbourX != gridX || neighbourY != gridY) {
- wallCount += map[neighbourX,neighbourY];
- }
- }
- else {
- wallCount ++;
- }
- }
- }
- return wallCount;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement