Advertisement
Rakshalpha

World

Oct 29th, 2019
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.22 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class World : MonoBehaviour
  5. {
  6.     public static World Instance { get; set; }
  7.     public GameObject chunk;
  8.     public Chunk[,,] chunks;
  9.     public int chunkSize = 16;
  10.  
  11.     public byte[,,] data;
  12.     public int worldX = 16;
  13.     public int worldY = 16;
  14.     public int worldZ = 16;
  15.  
  16.     // Use this for initialization
  17.     void Start()
  18.     {
  19.         Instance = this;
  20.         data = new byte[worldX, worldY, worldZ];
  21.  
  22.         for (int x = 0; x < worldX; x++)
  23.         {
  24.             for (int z = 0; z < worldZ; z++)
  25.             {
  26.                 int stone = PerlinNoise(x, 0, z, 1, 1, 1) + 10;
  27.                 stone += PerlinNoise(x, 100, z, 60, 3, 3);
  28.                 int dirt = PerlinNoise(x, 101, z, 75, 3, 3);
  29.                 int grass = PerlinNoise(x, 110, z, 125, 3, 4);
  30.                 //int snow = PerlinNoise(x, 175, z, 67.5f, 3, 3);
  31.  
  32.                 for (int y = 0; y < worldY; y++)
  33.                 {
  34.                     if (y == 0)
  35.                     {
  36.                         data[x, y, z] = 128;
  37.                     }
  38.                     else if (y <= stone)
  39.                     {
  40.                         data[x, y, z] = 1;
  41.                     }
  42.                     else if (y <= stone + dirt)
  43.                     {
  44.                         data[x, y, z] = 2;
  45.                     }
  46.                     else if (y <= stone + dirt + grass)
  47.                     {
  48.                         data[x, y, z] = 3;
  49.                     }
  50.                     //else if (y >= snow)
  51.                     //{
  52.                     //    data[x, y, z] = 4;
  53.                     //}
  54.                 }
  55.             }
  56.         }
  57.  
  58.         chunks = new Chunk[Mathf.FloorToInt(worldX / chunkSize),
  59.                            Mathf.FloorToInt(worldY / chunkSize),
  60.                            Mathf.FloorToInt(worldZ / chunkSize)];
  61.     }
  62.  
  63.     int PerlinNoise(int x, int y, int z, float scale, float height, float power)
  64.     {
  65.         float rValue;
  66.         rValue = Noise.GetNoise(((double)x) / scale, ((double)y) / scale, ((double)z) / scale);
  67.         rValue *= height;
  68.  
  69.         if (power != 0)
  70.         {
  71.             rValue = Mathf.Pow(rValue, power);
  72.         }
  73.  
  74.         return (int)rValue;
  75.     }
  76.  
  77.     /// <summary>
  78.     /// This method is used in cases where the chunk the player is planning to navigate to non existant
  79.     /// so populate the chunk data and add it to the pool for later deactivation when unloading is called
  80.     /// </summary>
  81.     /// <param name="x"></param>
  82.     /// <param name="z"></param>
  83.     public void GenColumn(int x, int z)
  84.     {
  85.         for (int y = 0; y < chunks.GetLength(1); y++)
  86.         {
  87.             //Create a temporary Gameobject for the new chunk instead of using chunks[x,y,z]
  88.             GameObject newChunk = Instantiate(chunk, new Vector3(x * chunkSize - 0.5f,
  89.             y * chunkSize + 0.5f, z * chunkSize - 0.5f), new Quaternion(0, 0, 0, 0)) as GameObject;
  90.             newChunk.transform.parent = transform;
  91.  
  92.             chunks[x, y, z] = newChunk.GetComponent<Chunk>() as Chunk;
  93.             chunks[x, y, z].worldGO = gameObject;
  94.             chunks[x, y, z].chunkSize = chunkSize;
  95.             chunks[x, y, z].chunkX = x * chunkSize;
  96.             chunks[x, y, z].chunkY = y * chunkSize;
  97.             chunks[x, y, z].chunkZ = z * chunkSize;
  98.         }
  99.     }
  100.  
  101.     /// <summary>
  102.     /// This method is in charge of pulling the chunk back from the pool
  103.     /// </summary>
  104.     /// <param name="x"></param>
  105.     /// <param name="z"></param>
  106.     public void LoadColumn(int x, int z)
  107.     {
  108.  
  109.     }
  110.  
  111.     /// <summary>
  112.     /// This method is incharge of pushing the chunk back to the pool
  113.     /// </summary>
  114.     /// <param name="x"></param>
  115.     /// <param name="z"></param>
  116.     public void UnloadColumn(int x, int z)
  117.     {
  118.         for (int y = 0; y < chunks.GetLength(1); y++) {
  119.             Destroy(chunks[x, y, z].gameObject);
  120.         }
  121.     }
  122.  
  123.     // Update is called once per frame
  124.     void Update()
  125.     {
  126.  
  127.     }
  128.  
  129.     public byte Block(int x, int y, int z)
  130.     {
  131.  
  132.         if (x >= worldX || x < 0 || y >= worldY || y < 0 || z >= worldZ || z < 0)
  133.         {
  134.             return (byte)1;
  135.         }
  136.  
  137.         return data[x, y, z];
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement