Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class Terrain : MonoBehaviour {
- public BlockList _blocks;
- public int seed = 0;
- public int chunkSize = 20;
- public int viewSize = 100;
- public List <Chunk> chunks = new List<Chunk>();
- public Chunk chunkFab;
- public static Terrain activeTerrain;
- public static BlockList blocks;
- // Use this for initialization
- void Start () {
- activeTerrain = this;
- blocks = _blocks;
- if (seed == 0)
- seed = Mathf.FloorToInt(Random.value * int.MaxValue);
- chunks.Add((Chunk)Instantiate(chunkFab));
- for (float x = transform.position.x - viewSize; x <= transform.position.x + viewSize; x+= chunkSize){
- for (float z = transform.position.z - viewSize; z <= transform.position.z + viewSize; z+= chunkSize){
- if (ChunkExists(x, z)) continue;
- int chunkX = Mathf.FloorToInt(x / chunkSize) * chunkSize;
- int chunkZ = Mathf.FloorToInt(z / chunkSize) * chunkSize;
- chunks.Add ( (Chunk)Instantiate(chunkFab, new Vector3(chunkX, 0, chunkZ), Quaternion.identity) );
- }
- }
- }
- // Update is called once per frame
- void Update () {
- CreateChunksIfWeNeedTo();
- }
- public bool ChunkExists(float x, float z)
- {
- for (int a = 0; a < chunks.Count; a++)
- {
- if ( (x < chunks[a].transform.position.x) ||
- (z < chunks[a].transform.position.z) ||
- (x >= chunks[a].transform.position.x + chunkSize) ||
- (z >= chunks[a].transform.position.z + chunkSize))
- continue;
- return true;
- }
- return false;
- }
- public void CreateChunksIfWeNeedTo() {
- /*bool make = true;
- if(make == true){
- for (float x = transform.position.x - viewSize; x <= transform.position.x + viewSize; x+= chunkSize){
- for (float z = transform.position.z - viewSize; z <= transform.position.z + viewSize; z+= chunkSize){
- if (ChunkExists(x, z)) continue;
- int chunkX = Mathf.FloorToInt(x / chunkSize) * chunkSize;
- int chunkZ = Mathf.FloorToInt(z / chunkSize) * chunkSize;
- chunks.Add ( (Chunk)Instantiate(chunkFab, new Vector3(chunkX, 0, chunkZ), Quaternion.identity) );
- }
- }
- }
- /*/
- }
- public Chunk GetChunk( int x, int y, int z) {
- for (int a = 0; a < chunks.Count; a++)
- {
- if ( (x < chunks[a].transform.position.x) ||
- (z < chunks[a].transform.position.z) ||
- (x >= chunks[a].transform.position.x + chunkSize) ||
- (z >= chunks[a].transform.position.z + chunkSize))
- continue;
- return chunks[a];
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment