Guest User

TERRAIN

a guest
Sep 27th, 2013
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class Terrain : MonoBehaviour {
  6.    
  7.     public BlockList _blocks;
  8.    
  9.     public int seed = 0;
  10.     public int chunkSize = 20;
  11.     public int viewSize = 100;
  12.     public List <Chunk> chunks = new List<Chunk>();
  13.     public Chunk chunkFab;
  14.    
  15.     public static Terrain activeTerrain;
  16.     public static BlockList blocks;
  17.    
  18.     // Use this for initialization
  19.     void Start () {
  20.        
  21.         activeTerrain = this;
  22.         blocks = _blocks;
  23.         if (seed == 0)
  24.             seed = Mathf.FloorToInt(Random.value * int.MaxValue);
  25.        
  26.         chunks.Add((Chunk)Instantiate(chunkFab));
  27.        
  28.        
  29.         for (float x = transform.position.x - viewSize; x <= transform.position.x + viewSize; x+= chunkSize){
  30.             for (float z = transform.position.z - viewSize; z <= transform.position.z + viewSize; z+= chunkSize){
  31.                     if (ChunkExists(x, z)) continue;
  32.                     int chunkX = Mathf.FloorToInt(x / chunkSize) * chunkSize;
  33.                     int chunkZ = Mathf.FloorToInt(z / chunkSize) * chunkSize;
  34.                     chunks.Add ( (Chunk)Instantiate(chunkFab, new Vector3(chunkX, 0, chunkZ), Quaternion.identity) );
  35.                 }
  36.             }
  37.     }
  38.    
  39.     // Update is called once per frame
  40.     void Update () {
  41.         CreateChunksIfWeNeedTo();
  42.     }
  43.    
  44.     public bool ChunkExists(float x, float z)
  45.     {
  46.         for (int a = 0; a < chunks.Count; a++)
  47.         {
  48.             if ( (x < chunks[a].transform.position.x) ||
  49.                 (z < chunks[a].transform.position.z) ||
  50.                 (x >= chunks[a].transform.position.x + chunkSize) ||
  51.                 (z >= chunks[a].transform.position.z + chunkSize))
  52.             continue;
  53.        
  54.             return true;
  55.         }
  56.         return false;
  57.     }
  58.    
  59.    
  60.    
  61.    
  62.     public void CreateChunksIfWeNeedTo() {
  63.        
  64.         /*bool make = true;
  65.        
  66.         if(make == true){
  67.         for (float x = transform.position.x - viewSize; x <= transform.position.x + viewSize; x+= chunkSize){
  68.             for (float z = transform.position.z - viewSize; z <= transform.position.z + viewSize; z+= chunkSize){
  69.                     if (ChunkExists(x, z)) continue;
  70.                     int chunkX = Mathf.FloorToInt(x / chunkSize) * chunkSize;
  71.                     int chunkZ = Mathf.FloorToInt(z / chunkSize) * chunkSize;
  72.                     chunks.Add ( (Chunk)Instantiate(chunkFab, new Vector3(chunkX, 0, chunkZ), Quaternion.identity) );
  73.                 }
  74.             }
  75.         }
  76.         /*/
  77.     }
  78.    
  79.     public Chunk GetChunk( int x, int y, int z) {
  80.         for (int a = 0; a < chunks.Count; a++)
  81.         {
  82.             if ( (x < chunks[a].transform.position.x) ||
  83.                 (z < chunks[a].transform.position.z) ||
  84.                 (x >= chunks[a].transform.position.x + chunkSize) ||
  85.                 (z >= chunks[a].transform.position.z + chunkSize))
  86.             continue;
  87.             return chunks[a];
  88.    
  89.         }
  90.         return null;
  91.     }
  92.    
  93.    
  94.    
  95. }
Advertisement
Add Comment
Please, Sign In to add comment