Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class World : MonoBehaviour {
- public Material mat;
- public static Material mats;
- public static Transform me;
- public const int viewRange = 4; //how many chunks i have to move for new once to spawn / view distance like in mincraft;
- public const int viewRangeHalf = viewRange / 2;
- void Awake ()
- {
- Block.blocks.Add(new Block("dirt", "Dirt", 2, 15));
- Block.blocks.Add(new Block("stone", "Stone", 1, 15));
- Block.blocks.Add(new Block("blockgrass", "Grass", 0, 15, 3, 15, 2, 15));
- Block.blocks.Add(new Block("bedrock", "Bedrock", 1, 14));
- me = transform;
- mats = mat;
- }
- void Update ()
- {
- Vector3 cameraPos = Camera.main.transform.position;
- for(int x = (int)cameraPos.x - (Chunk.width * viewRange); x < (Chunk.width * viewRange) + (int)cameraPos.x; x += viewRange)
- {
- for(int z = (int)cameraPos.z - (Chunk.width * viewRange); z < (Chunk.width * viewRange)+ (int)cameraPos.z; z += viewRange)
- {
- int xx = Mathf.FloorToInt(x / Chunk.width) * Chunk.width;
- int zz = Mathf.FloorToInt(z / Chunk.width) * Chunk.width;
- Vector3 cPos = new Vector3(xx, 0, zz);
- if(Chunk.ChunkExists(cPos))
- {
- continue;
- }
- Chunk.AddChunk(cPos);
- }
- }
- TickUpdate();
- }
- void TickUpdate ()
- {
- if(Chunk.Working) return;
- Vector3 cameraPos = Camera.main.transform.position;
- Dictionary<Vector3, Chunk> chunkList = Chunk.chunks;
- foreach(var c in chunkList)
- {
- Chunk chunk = c.Value;
- Vector3 pos = c.Key;
- if(Vector3.Distance(cameraPos, pos) > (Chunk.width * (viewRange / 2)) + (Chunk.width * 3))
- {
- chunk.gameObject.SetActive(false);
- }
- else
- {
- if(chunk.gameObject.activeInHierarchy == false)
- {
- chunk.gameObject.SetActive(true);
- }
- }
- chunk.chunkPos = pos;
- if(chunk.dirty)
- {
- Chunk.Working = true;
- if(!chunk.calculateMap)
- {
- chunk.CMap();
- }
- chunk.CMesh();
- chunk.dirty = false;
- return;
- }
- if(chunk.lightDirty)
- {
- Chunk.Working = true;
- chunk.CLight();
- chunk.lightDirty = false;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment