Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class World : MonoBehaviour
- {
- public static World Instance { get; set; }
- public GameObject chunk;
- public Chunk[,,] chunks;
- public int chunkSize = 16;
- public byte[,,] data;
- public int worldX = 16;
- public int worldY = 16;
- public int worldZ = 16;
- // Use this for initialization
- void Start()
- {
- Instance = this;
- data = new byte[worldX, worldY, worldZ];
- for (int x = 0; x < worldX; x++)
- {
- for (int z = 0; z < worldZ; z++)
- {
- int stone = PerlinNoise(x, 0, z, 1, 1, 1) + 10;
- stone += PerlinNoise(x, 100, z, 60, 3, 3);
- int dirt = PerlinNoise(x, 101, z, 75, 3, 3);
- int grass = PerlinNoise(x, 110, z, 125, 3, 4);
- //int snow = PerlinNoise(x, 175, z, 67.5f, 3, 3);
- for (int y = 0; y < worldY; y++)
- {
- if (y == 0)
- {
- data[x, y, z] = 128;
- }
- else if (y <= stone)
- {
- data[x, y, z] = 1;
- }
- else if (y <= stone + dirt)
- {
- data[x, y, z] = 2;
- }
- else if (y <= stone + dirt + grass)
- {
- data[x, y, z] = 3;
- }
- //else if (y >= snow)
- //{
- // data[x, y, z] = 4;
- //}
- }
- }
- }
- chunks = new Chunk[Mathf.FloorToInt(worldX / chunkSize),
- Mathf.FloorToInt(worldY / chunkSize),
- Mathf.FloorToInt(worldZ / chunkSize)];
- }
- int PerlinNoise(int x, int y, int z, float scale, float height, float power)
- {
- float rValue;
- rValue = Noise.GetNoise(((double)x) / scale, ((double)y) / scale, ((double)z) / scale);
- rValue *= height;
- if (power != 0)
- {
- rValue = Mathf.Pow(rValue, power);
- }
- return (int)rValue;
- }
- /// <summary>
- /// This method is used in cases where the chunk the player is planning to navigate to non existant
- /// so populate the chunk data and add it to the pool for later deactivation when unloading is called
- /// </summary>
- /// <param name="x"></param>
- /// <param name="z"></param>
- public void GenColumn(int x, int z)
- {
- for (int y = 0; y < chunks.GetLength(1); y++)
- {
- //Create a temporary Gameobject for the new chunk instead of using chunks[x,y,z]
- GameObject newChunk = Instantiate(chunk, new Vector3(x * chunkSize - 0.5f,
- y * chunkSize + 0.5f, z * chunkSize - 0.5f), new Quaternion(0, 0, 0, 0)) as GameObject;
- newChunk.transform.parent = transform;
- chunks[x, y, z] = newChunk.GetComponent<Chunk>() as Chunk;
- chunks[x, y, z].worldGO = gameObject;
- chunks[x, y, z].chunkSize = chunkSize;
- chunks[x, y, z].chunkX = x * chunkSize;
- chunks[x, y, z].chunkY = y * chunkSize;
- chunks[x, y, z].chunkZ = z * chunkSize;
- }
- }
- /// <summary>
- /// This method is in charge of pulling the chunk back from the pool
- /// </summary>
- /// <param name="x"></param>
- /// <param name="z"></param>
- public void LoadColumn(int x, int z)
- {
- }
- /// <summary>
- /// This method is incharge of pushing the chunk back to the pool
- /// </summary>
- /// <param name="x"></param>
- /// <param name="z"></param>
- public void UnloadColumn(int x, int z)
- {
- for (int y = 0; y < chunks.GetLength(1); y++) {
- Destroy(chunks[x, y, z].gameObject);
- }
- }
- // Update is called once per frame
- void Update()
- {
- }
- public byte Block(int x, int y, int z)
- {
- if (x >= worldX || x < 0 || y >= worldY || y < 0 || z >= worldZ || z < 0)
- {
- return (byte)1;
- }
- return data[x, y, z];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement