Advertisement
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 static World instance;
- public GameObject playerObject;
- public GameObject colliderObject;
- public Material material;
- public int worldWidth;
- public int worldHeight;
- public string seed;
- public bool randomSeed;
- public float frequency;
- public float amplitude;
- public float lacunarity;
- public float persistance;
- public int octaves;
- public Tile[,] tiles;
- // Height Values
- public float seaLevel;
- public float beachStartHeight;
- public float beachEndHeight;
- public float earthStartHeight;
- public float earthEndHeight;
- public float grassStartHeight;
- public float grassEndHeight;
- public float stoneStartHeight;
- public float stoneEndHeight;
- Noise noise;
- // Use this for initialization
- void Start () {
- createTiles();
- SegmentTileArray();
- for (int i = 0; i < worldWidth; i++)
- {
- for (int k = 0; k < worldHeight; k++)
- {
- if (GetTileAt(i, k).tileType == Tile.TileType.Water)
- {
- //Debug.Log("Water at " + i + ", " + k);
- GameObject.Instantiate(colliderObject, new Vector3(i + 0.5f, k + 0.5f, -3), new Quaternion(0, 0, 0, 0), this.transform);
- }
- }
- }
- // Player spawn to: x + 0.5, y + 0.5
- for (int i = 0; i < worldWidth; i++)
- {
- for (int j = 0; j < worldHeight; j++)
- {
- if(GetTileAt(i, j).tileType == Tile.TileType.Sand && i > 10 && j > 10)
- {
- playerObject.transform.position = new Vector3(i + 0.5f, j + 0.5f, -3);
- break;
- }
- }
- }
- }
- void Awake()
- {
- instance = this;
- if(randomSeed)
- {
- int value = Random.Range(0, 10000);
- seed = value.ToString();
- while (seed.GetHashCode() > 13000 || seed.GetHashCode() < 0)
- {
- int value2 = Random.Range(0, 10000);
- seed = value2.ToString();
- }
- }
- noise = new Noise(seed.GetHashCode(), frequency, amplitude, lacunarity, persistance, octaves);
- }
- // Update is called once per frame
- void Update () {
- }
- void createTiles()
- {
- tiles = new Tile[worldWidth, worldHeight];
- float[,] noiseValues = noise.GetNoiseValues(worldWidth, worldHeight);
- for (int i = 0; i < worldWidth; i++)
- {
- for (int j = 0; j < worldHeight; j++)
- {
- tiles[i, j] = MakeTileAtNoiseHeight(noiseValues[i,j]);
- }
- }
- }
- Tile MakeTileAtNoiseHeight(float currentHeight)
- {
- if(currentHeight <= seaLevel)
- {
- return new Tile(Tile.TileType.Water);
- }
- if(currentHeight >= beachStartHeight && currentHeight <= beachEndHeight)
- {
- return new Tile(Tile.TileType.Sand);
- }
- if(currentHeight >= earthStartHeight && currentHeight <= earthEndHeight)
- {
- return new Tile(Tile.TileType.Earth);
- }
- if (currentHeight >= grassStartHeight && currentHeight <= grassEndHeight)
- {
- return new Tile(Tile.TileType.Grass);
- }
- if (currentHeight >= stoneStartHeight && currentHeight <= stoneEndHeight)
- {
- return new Tile(Tile.TileType.Stone);
- }
- return new Tile(Tile.TileType.MISSING_TEXTURE);
- }
- void SegmentTileArray(int index1 = 0, int index2 = 0)
- {
- // Get the size the segment needs to be
- int sizeX;
- int sizeY;
- if(tiles.GetLength(0) - index1 > 100)
- {
- sizeX = 100;
- }
- else
- {
- sizeX = tiles.GetLength(0) - index1;
- }
- if (tiles.GetLength(1) - index2 > 100)
- {
- sizeY = 100;
- }
- else
- {
- sizeY = tiles.GetLength(1) - index2;
- }
- GenerateMesh(index1, index2, sizeX, sizeY);
- if (tiles.GetLength(0) >= index1 + 100)
- {
- SegmentTileArray(index1 + 100, index2);
- return;
- }
- if (tiles.GetLength(1) >= index2 + 100)
- {
- SegmentTileArray(0, index2 + 100);
- return;
- }
- }
- void GenerateMesh(int x, int y, int width, int height)
- {
- MeshData meshData = new MeshData(x, y, width, height);
- GameObject meshObject = new GameObject("CHUNK (" + x + "," + y + ")");
- meshObject.transform.SetParent(this.transform);
- MeshFilter meshFilter = meshObject.AddComponent<MeshFilter>();
- MeshRenderer meshRenderer = meshObject.AddComponent<MeshRenderer>();
- meshRenderer.material = material;
- Mesh mesh = meshFilter.mesh;
- mesh.vertices = meshData.vertices.ToArray();
- mesh.triangles = meshData.triangles.ToArray();
- mesh.uv = meshData.UVs.ToArray();
- }
- public Tile GetTileAt(int x, int y)
- {
- if (x < 0 || x >= worldWidth || y < 0 || y >= worldHeight)
- {
- return null;
- }
- return tiles[x, y];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement