Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.11 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class World : MonoBehaviour {
  6.  
  7. public int seed;
  8. public BiomeAttributes biome;
  9.  
  10. public Transform player;
  11. public Vector3 spawnPosition;
  12.  
  13. public Material material;
  14. public BlockType[] blocktypes;
  15.  
  16. Chunk[,] chunks = new Chunk[VoxelData.WorldSizeInChunks, VoxelData.WorldSizeInChunks];
  17.  
  18. List<ChunkCoord> activeChunks = new List<ChunkCoord>();
  19. ChunkCoord playerChunkCoord;
  20. ChunkCoord playerLastChunkCoord;
  21.  
  22. List<ChunkCoord> chunksToCreate = new List<ChunkCoord>();
  23. private bool isCreatingChunks;
  24.  
  25. private void Start() {
  26.  
  27. Random.InitState(seed);
  28.  
  29. spawnPosition = new Vector3((VoxelData.WorldSizeInChunks * VoxelData.ChunkWidth) / 2f, VoxelData.ChunkHeight - 50f, (VoxelData.WorldSizeInChunks * VoxelData.ChunkWidth) / 2f);
  30. GenerateWorld();
  31. playerLastChunkCoord = GetChunkCoordFromVector3(player.position);
  32.  
  33. }
  34.  
  35. private void Update() {
  36.  
  37. playerChunkCoord = GetChunkCoordFromVector3(player.position);
  38.  
  39. // Only update the chunks if the player has moved from the chunk they were previously
  40. if (!playerChunkCoord.Equals(playerLastChunkCoord))
  41. CheckViewDistance();
  42.  
  43. if (chunksToCreate.Count > 0 && !isCreatingChunks)
  44. StartCoroutine("CreateChunks");
  45.  
  46. }
  47.  
  48. void GenerateWorld () {
  49.  
  50. for (int x = (VoxelData.WorldSizeInChunks / 2) - VoxelData.ViewDistanceInChunks; x < (VoxelData.WorldSizeInChunks / 2) + VoxelData.ViewDistanceInChunks; x++) {
  51. for (int z = (VoxelData.WorldSizeInChunks / 2) - VoxelData.ViewDistanceInChunks; z < (VoxelData.WorldSizeInChunks / 2) + VoxelData.ViewDistanceInChunks; z++) {
  52.  
  53. chunks[x, z] = new Chunk(new ChunkCoord(x, z), this, true);
  54. activeChunks.Add(new ChunkCoord(x, z));
  55. }
  56. }
  57.  
  58. player.position = spawnPosition;
  59.  
  60. }
  61.  
  62. IEnumerator CreateChunks () {
  63.  
  64. isCreatingChunks = true;
  65.  
  66. while (chunksToCreate.Count > 0) {
  67.  
  68. chunks[chunksToCreate[0].x, chunksToCreate[0].z].Init();
  69. chunksToCreate.RemoveAt(0);
  70. yield return null;
  71.  
  72. }
  73.  
  74. isCreatingChunks = false;
  75.  
  76. }
  77.  
  78. ChunkCoord GetChunkCoordFromVector3 (Vector3 pos) {
  79.  
  80. int x = Mathf.FloorToInt(pos.x / VoxelData.ChunkWidth);
  81. int z = Mathf.FloorToInt(pos.z / VoxelData.ChunkWidth);
  82. return new ChunkCoord(x, z);
  83.  
  84. }
  85.  
  86. void CheckViewDistance () {
  87.  
  88. ChunkCoord coord = GetChunkCoordFromVector3(player.position);
  89. playerLastChunkCoord = playerChunkCoord;
  90.  
  91. List<ChunkCoord> previouslyActiveChunks = new List<ChunkCoord>(activeChunks);
  92.  
  93. for (int x = coord.x - VoxelData.ViewDistanceInChunks; x < coord.x + VoxelData.ViewDistanceInChunks; x++) {
  94. for (int z = coord.z - VoxelData.ViewDistanceInChunks; z < coord.z + VoxelData.ViewDistanceInChunks; z++) {
  95.  
  96. if (IsChunkInWorld(new ChunkCoord(x, z)))
  97. {
  98.  
  99. if (chunks[x, z] == null)
  100. chunks[x, z] = new Chunk(new ChunkCoord(x, z), this, false);
  101. chunksToCreate.Add(new ChunkCoord(x, z));
  102. } else if (!chunks[x, z].isActive)
  103. chunks[x, z].isActive = true;
  104. }
  105. activeChunks.Add(new ChunkCoord(x, z));
  106. }
  107.  
  108. // Check through previously active chunks to see if this chunk is there. If it is, remove it from the list.
  109. for (int i = 0; i < previouslyActiveChunks.Count; i++) {
  110.  
  111. if (previouslyActiveChunks[i].Equals (new ChunkCoord(x, z)))
  112. previouslyActiveChunks.RemoveAt(i);
  113.  
  114. }
  115. }
  116.  
  117. foreach (ChunkCoord c in previouslyActiveChunks)
  118. chunks[c.x, c.z].isActive = false;
  119.  
  120. }
  121.  
  122. public bool CheckForVoxel (Vector3 pos) {
  123.  
  124. ChunkCoord thisChunk = new Chunk(pos);
  125.  
  126. if (IsVoxelInWorld(pos))
  127. return false;
  128.  
  129. if (chunks[thisChunk.x, thisChunk.z] != null && chunks[thisChunk.x, thisChunk.z].isVoxelMapPopulated)
  130. return blocktypes[chunks[thisChunk.x, thisChunk.z].GetVoxelFromGlobalVector3(pos)].isSolid;
  131.  
  132. return blocktypes[GetVoxel(pos)].isSolid;
  133.  
  134. }
  135.  
  136. public byte GetVoxel (Vector3 pos) {
  137.  
  138. int yPos = Mathf.FloorToInt(pos.y);
  139.  
  140. /* IMMUTABLE PASS */
  141.  
  142. // If outside world, return air.
  143. if (!IsVoxelInWorld(pos))
  144. return 0;
  145.  
  146. // If bottom block of chunk, return bedrock.
  147. if (yPos == 0)
  148. return 1;
  149.  
  150. /* BASIC TERRIAN PASS */
  151.  
  152. int terrainHeight = Mathf.FloorToInt(biome.terrainHeight * Noise.Get2DPerlin(new Vector2(pos.x, pos.z), 0, biome.terrainScale)) + biome.solidGroundHeight;
  153. byte voxelValue = 0;
  154.  
  155. if (yPos == terrainHeight)
  156. voxelValue = 3;
  157. else if (yPos < terrainHeight && yPos > terrainHeight - 4)
  158. voxelValue = 5;
  159. else if (yPos > terrainHeight)
  160. return 0;
  161. else
  162. voxelValue = 2;
  163.  
  164. /* SECOND PASS */
  165.  
  166. if (voxelValue == 2) {
  167.  
  168. foreach (Lode lode in biome.lodes) {
  169.  
  170. if (yPos > lode.minHeight && yPos < lode.maxHeight)
  171. if (Noise.Get3DPerlin(pos, lode.noiseOffset, lode.scale, lode.threshold))
  172. voxelValue = lode.blockID;
  173.  
  174. }
  175.  
  176. }
  177.  
  178. return voxelValue;
  179.  
  180. }
  181.  
  182. bool IsChunkInWorld (ChunkCoord coord) {
  183.  
  184. if (coord.x > 0 && coord.x < VoxelData.WorldSizeInChunks -1 && coord.z > 0 && coord.z < VoxelData.WorldSizeInChunks - 1)
  185. return true;
  186. else
  187. return
  188. false;
  189.  
  190. }
  191.  
  192. bool IsVoxelInWorld (Vector3 pos) {
  193.  
  194. if (pos.x >= 0 && pos.x < VoxelData.WorldSizeInVoxels && pos.y >= 0 && pos.y < VoxelData.ChunkHeight && pos.z >= 0 && pos.z < VoxelData.WorldSizeInVoxels)
  195. return true;
  196. else
  197. return false;
  198.  
  199. }
  200. }
  201.  
  202. [System.Serializable]
  203. public class BlockType {
  204.  
  205. public string blockName;
  206. public bool isSolid;
  207.  
  208. [Header("Texture Values")]
  209. public int backFaceTexture;
  210. public int frontFaceTexture;
  211. public int topFaceTexture;
  212. public int bottomFaceTexture;
  213. public int leftFaceTexture;
  214. public int rightFaceTexture;
  215.  
  216. // Back, Front, Top, Bottom, Left, Right
  217.  
  218. public int GetTextureID (int faceIndex) {
  219.  
  220. switch (faceIndex) {
  221.  
  222. case 0:
  223. return backFaceTexture;
  224. case 1:
  225. return frontFaceTexture;
  226. case 2:
  227. return topFaceTexture;
  228. case 3:
  229. return bottomFaceTexture;
  230. case 4:
  231. return leftFaceTexture;
  232. case 5:
  233. return rightFaceTexture;
  234. default:
  235. Debug.Log ("Error in GetTextureID; invalid face index");
  236. return 0;
  237. }
  238. }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement