Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.40 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class World : MonoBehaviour {
  6.  
  7.     public static World instance;
  8.  
  9.     public GameObject playerObject;
  10.     public GameObject colliderObject;
  11.     public Material material;
  12.  
  13.     public int worldWidth;
  14.     public int worldHeight;
  15.  
  16.     public string seed;
  17.     public bool randomSeed;
  18.  
  19.     public float frequency;
  20.     public float amplitude;
  21.  
  22.     public float lacunarity;
  23.     public float persistance;
  24.  
  25.     public int octaves;
  26.  
  27.     public Tile[,] tiles;
  28.  
  29.     // Height Values
  30.     public float seaLevel;
  31.  
  32.     public float beachStartHeight;
  33.     public float beachEndHeight;
  34.  
  35.     public float earthStartHeight;
  36.     public float earthEndHeight;
  37.  
  38.     public float grassStartHeight;
  39.     public float grassEndHeight;
  40.  
  41.     public float stoneStartHeight;
  42.     public float stoneEndHeight;
  43.  
  44.     Noise noise;
  45.  
  46.  
  47.     // Use this for initialization
  48.     void Start () {
  49.         createTiles();
  50.         SegmentTileArray();
  51.  
  52.         for (int i = 0; i < worldWidth; i++)
  53.         {
  54.             for (int k = 0; k < worldHeight; k++)
  55.             {
  56.                 if (GetTileAt(i, k).tileType == Tile.TileType.Water)
  57.                 {
  58.                     //Debug.Log("Water at " + i + ", " + k);
  59.                     GameObject.Instantiate(colliderObject, new Vector3(i + 0.5f, k + 0.5f, -3), new Quaternion(0, 0, 0, 0), this.transform);
  60.                 }
  61.             }
  62.         }
  63.  
  64.         // Player spawn to: x + 0.5, y + 0.5
  65.  
  66.         for (int i = 0; i < worldWidth; i++)
  67.         {
  68.             for (int j = 0; j < worldHeight; j++)
  69.             {
  70.                 if(GetTileAt(i, j).tileType == Tile.TileType.Sand && i > 10 && j > 10)
  71.                 {
  72.                     playerObject.transform.position = new Vector3(i + 0.5f, j + 0.5f, -3);
  73.                     break;
  74.                 }
  75.             }
  76.         }
  77.     }
  78.  
  79.     void Awake()
  80.     {
  81.         instance = this;
  82.  
  83.         if(randomSeed)
  84.         {
  85.             int value = Random.Range(0, 10000);
  86.             seed = value.ToString();
  87.  
  88.             while (seed.GetHashCode() > 13000 || seed.GetHashCode() < 0)
  89.             {
  90.                 int value2 = Random.Range(0, 10000);
  91.                 seed = value2.ToString();
  92.             }
  93.         }
  94.  
  95.         noise = new Noise(seed.GetHashCode(), frequency, amplitude, lacunarity, persistance, octaves);
  96.     }
  97.    
  98.     // Update is called once per frame
  99.     void Update () {
  100.        
  101.     }
  102.  
  103.     void createTiles()
  104.     {
  105.         tiles = new Tile[worldWidth, worldHeight];
  106.  
  107.         float[,] noiseValues = noise.GetNoiseValues(worldWidth, worldHeight);
  108.  
  109.         for (int i = 0; i < worldWidth; i++)
  110.         {
  111.             for (int j = 0; j < worldHeight; j++)
  112.             {
  113.                 tiles[i, j] = MakeTileAtNoiseHeight(noiseValues[i,j]);
  114.             }
  115.         }
  116.     }
  117.  
  118.     Tile MakeTileAtNoiseHeight(float currentHeight)
  119.     {
  120.         if(currentHeight <= seaLevel)
  121.         {
  122.             return new Tile(Tile.TileType.Water);
  123.         }
  124.         if(currentHeight >= beachStartHeight && currentHeight <= beachEndHeight)
  125.         {
  126.             return new Tile(Tile.TileType.Sand);
  127.         }
  128.         if(currentHeight >= earthStartHeight && currentHeight <= earthEndHeight)
  129.         {
  130.             return new Tile(Tile.TileType.Earth);
  131.         }
  132.         if (currentHeight >= grassStartHeight && currentHeight <= grassEndHeight)
  133.         {
  134.             return new Tile(Tile.TileType.Grass);
  135.         }
  136.         if (currentHeight >= stoneStartHeight && currentHeight <= stoneEndHeight)
  137.         {
  138.             return new Tile(Tile.TileType.Stone);
  139.         }
  140.         return new Tile(Tile.TileType.MISSING_TEXTURE);
  141.     }
  142.  
  143.     void SegmentTileArray(int index1 = 0, int index2 = 0)
  144.     {
  145.  
  146.         // Get the size the segment needs to be
  147.  
  148.         int sizeX;
  149.         int sizeY;
  150.  
  151.         if(tiles.GetLength(0) - index1 > 100)
  152.         {
  153.             sizeX = 100;
  154.         }
  155.         else
  156.         {
  157.             sizeX = tiles.GetLength(0) - index1;
  158.         }
  159.  
  160.         if (tiles.GetLength(1) - index2 > 100)
  161.         {
  162.             sizeY = 100;
  163.         }
  164.         else
  165.         {
  166.             sizeY = tiles.GetLength(1) - index2;
  167.         }
  168.  
  169.         GenerateMesh(index1, index2, sizeX, sizeY);
  170.  
  171.         if (tiles.GetLength(0) >= index1 + 100)
  172.         {
  173.             SegmentTileArray(index1 + 100, index2);
  174.             return;
  175.         }
  176.  
  177.         if (tiles.GetLength(1) >= index2 + 100)
  178.         {
  179.             SegmentTileArray(0, index2 + 100);
  180.             return;
  181.         }
  182.     }
  183.  
  184.     void GenerateMesh(int x, int y, int width, int height)
  185.     {
  186.         MeshData meshData = new MeshData(x, y, width, height);
  187.  
  188.         GameObject meshObject = new GameObject("CHUNK (" + x + "," + y + ")");
  189.         meshObject.transform.SetParent(this.transform);
  190.         MeshFilter meshFilter = meshObject.AddComponent<MeshFilter>();
  191.         MeshRenderer meshRenderer = meshObject.AddComponent<MeshRenderer>();
  192.  
  193.         meshRenderer.material = material;
  194.  
  195.         Mesh mesh = meshFilter.mesh;
  196.  
  197.         mesh.vertices = meshData.vertices.ToArray();
  198.         mesh.triangles = meshData.triangles.ToArray();
  199.         mesh.uv = meshData.UVs.ToArray();
  200.     }
  201.  
  202.     public Tile GetTileAt(int x, int y)
  203.     {
  204.         if (x < 0 || x >= worldWidth || y < 0 || y >= worldHeight)
  205.         {
  206.             return null;
  207.         }
  208.         return tiles[x, y];
  209.     }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement