Guest User

Untitled

a guest
Oct 27th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.17 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 Material mat;
  8.     public static Material mats;
  9.     public static Transform me;
  10.  
  11.     public const int viewRange = 4; //how many chunks i have to move for new once to spawn / view distance like in mincraft;
  12.     public const int viewRangeHalf = viewRange / 2;
  13.  
  14.     void Awake ()
  15.     {
  16.         Block.blocks.Add(new Block("dirt", "Dirt", 2, 15));
  17.         Block.blocks.Add(new Block("stone", "Stone", 1, 15));
  18.         Block.blocks.Add(new Block("blockgrass", "Grass", 0, 15, 3, 15, 2, 15));
  19.         Block.blocks.Add(new Block("bedrock", "Bedrock", 1, 14));
  20.  
  21.         me = transform;
  22.         mats = mat;
  23.     }
  24.  
  25.     void Update ()
  26.     {
  27.         Vector3 cameraPos = Camera.main.transform.position;
  28.  
  29.         for(int x = (int)cameraPos.x - (Chunk.width * viewRange); x < (Chunk.width * viewRange) + (int)cameraPos.x; x += viewRange)
  30.         {
  31.             for(int z = (int)cameraPos.z - (Chunk.width * viewRange); z < (Chunk.width * viewRange)+ (int)cameraPos.z; z += viewRange)
  32.             {
  33.                 int xx = Mathf.FloorToInt(x / Chunk.width) * Chunk.width;
  34.                 int zz = Mathf.FloorToInt(z / Chunk.width) * Chunk.width;
  35.  
  36.                 Vector3 cPos = new Vector3(xx, 0, zz);
  37.                 if(Chunk.ChunkExists(cPos))
  38.                 {
  39.                     continue;
  40.                 }
  41.  
  42.                 Chunk.AddChunk(cPos);
  43.             }
  44.         }
  45.  
  46.         TickUpdate();
  47.     }
  48.  
  49.     void TickUpdate ()
  50.     {
  51.         if(Chunk.Working) return;
  52.  
  53.         Vector3 cameraPos = Camera.main.transform.position;
  54.  
  55.         Dictionary<Vector3, Chunk> chunkList = Chunk.chunks;
  56.  
  57.         foreach(var c in chunkList)
  58.         {
  59.             Chunk chunk = c.Value;
  60.             Vector3 pos = c.Key;
  61.  
  62.             if(Vector3.Distance(cameraPos, pos) > (Chunk.width * (viewRange / 2)) + (Chunk.width * 3))
  63.             {
  64.                 chunk.gameObject.SetActive(false);
  65.             }
  66.             else
  67.             {
  68.                 if(chunk.gameObject.activeInHierarchy == false)
  69.                 {
  70.                     chunk.gameObject.SetActive(true);
  71.                 }
  72.             }
  73.  
  74.             chunk.chunkPos = pos;
  75.  
  76.             if(chunk.dirty)
  77.             {
  78.                 Chunk.Working = true;
  79.  
  80.                 if(!chunk.calculateMap)
  81.                 {
  82.                     chunk.CMap();
  83.                 }
  84.  
  85.                 chunk.CMesh();
  86.                 chunk.dirty = false;
  87.  
  88.                 return;
  89.             }
  90.  
  91.             if(chunk.lightDirty)
  92.             {
  93.                 Chunk.Working = true;
  94.                 chunk.CLight();
  95.                 chunk.lightDirty = false;
  96.             }
  97.         }
  98.     }
  99. }
Add Comment
Please, Sign In to add comment