Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No top layer could you help me out? #3

Open
CarrotCraft opened this issue May 5, 2019 · 0 comments
Open

No top layer could you help me out? #3

CarrotCraft opened this issue May 5, 2019 · 0 comments

Comments

@CarrotCraft
Copy link

Uploading glitch.PNG…
glitch
THIS IS WHATS HAPPENING HERES THE CODE FOR WORLD-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class World : MonoBehaviour {

public Transform player;
public Vector3 spawn;

public Material material;
public BlockType[] blocktypes;

Chunk[,] chunks = new Chunk[VoxelData.WorldSizeInChunks, VoxelData.WorldSizeInChunks];
List<ChunkCoord> activeChunks = new List<ChunkCoord>();
ChunkCoord playerLastChunkCoord;

private void Start() {

	GenerateWorld();
	playerLastChunkCoord = GetChunkCoordFromVector3(player.transform.position);

}

private void Update() {

	if (!GetChunkCoordFromVector3(player.transform.position).Equals(playerLastChunkCoord))
		CheckViewDistance();

}

ChunkCoord GetChunkCoordFromVector3 (Vector3 pos) {

	int x = Mathf.FloorToInt(pos.x / VoxelData.ChunkWidth);
	int z = Mathf.FloorToInt(pos.z / VoxelData.ChunkWidth);
	return new ChunkCoord(x, z);

}

private void GenerateWorld () {

	for (int x = VoxelData.WorldSizeInChunks / 2 - VoxelData.ViewDistanceInChunks / 2; x < VoxelData.WorldSizeInChunks / 2 + VoxelData.ViewDistanceInChunks / 2; x++) {
		for (int z = VoxelData.WorldSizeInChunks / 2 - VoxelData.ViewDistanceInChunks / 2; z < VoxelData.WorldSizeInChunks / 2 + VoxelData.ViewDistanceInChunks / 2; z++) {

			CreateChunk(new ChunkCoord(x, z));

		}
	}

	spawn = new Vector3(VoxelData.WorldSizeInBlocks / 2, VoxelData.ChunkHeight + 2, VoxelData.WorldSizeInBlocks / 2);
	player.position = spawn;

}

private void CheckViewDistance () {

	int chunkX = Mathf.FloorToInt(player.position.x / VoxelData.ChunkWidth);
	int chunkZ = Mathf.FloorToInt(player.position.z / VoxelData.ChunkWidth);

	List<ChunkCoord> previouslyActiveChunks = new List<ChunkCoord>(activeChunks);

	for (int x = chunkX - VoxelData.ViewDistanceInChunks / 2; x < chunkX + VoxelData.ViewDistanceInChunks / 2; x++) {
		for (int z = chunkZ - VoxelData.ViewDistanceInChunks / 2; z < chunkZ + VoxelData.ViewDistanceInChunks / 2; z++) {

			// If the chunk is within the world bounds and it has not been created.
			if (IsChunkInWorld(x, z)) {

				ChunkCoord thisChunk = new ChunkCoord(x, z);

				if (chunks[x, z] == null)
					CreateChunk(thisChunk);
				else if (!chunks[x, z].isActive) {
					chunks[x, z].isActive = true;
					activeChunks.Add(thisChunk);
				}
				// Check if this chunk was already in the active chunks list.
				for (int i = 0; i < previouslyActiveChunks.Count; i++) {

					//if (previouslyActiveChunks[i].Equals(new ChunkCoord(x, z)))
					if (previouslyActiveChunks[i].x == x && previouslyActiveChunks[i].z == z)
						previouslyActiveChunks.RemoveAt(i);

				}

			}
		}
	}

	foreach (ChunkCoord coord in previouslyActiveChunks)
		chunks[coord.x, coord.z].isActive = false;

}

bool IsChunkInWorld(int x, int z) {

	if (x > 0 && x < VoxelData.WorldSizeInChunks - 1 && z > 0 && z < VoxelData.WorldSizeInChunks - 1)
		return true;
	else
		return false;

}

private void CreateChunk (ChunkCoord coord) {

	chunks[coord.x, coord.z] = new Chunk(new ChunkCoord(coord.x, coord.z), this);
	activeChunks.Add(new ChunkCoord(coord.x, coord.z));


}

public byte GetVoxel (Vector3 pos) {

	if (pos.x < 0 || pos.x > VoxelData.WorldSizeInBlocks - 1 || pos.y < 0 || pos.y > VoxelData.ChunkHeight - 1 || pos.z < 0 || pos.z > VoxelData.WorldSizeInBlocks - 1)
		return 0;
	if (pos.y < 1)
		return 1;
	else if (pos.y == VoxelData.ChunkHeight - 1)
		return 3;
	else
		return 2;

}

}

public class ChunkCoord {

public int x;
public int z;

public ChunkCoord (int _x, int _z) {

	x = _x;
	z = _z;

}

public bool Equals(ChunkCoord other) {

	if (other == null)
		return false;
	else if (other.x == x && other.z == z)
		return true;
	else
		return false;

}

}

[System.Serializable]
public class BlockType {

public string blockName;
public bool isSolid;

[Header("Texture Values")]
public int backFaceTexture;
public int frontFaceTexture;
public int topFaceTexture;
public int bottomFaceTexture;
public int leftFaceTexture;
public int rightFaceTexture;

// Back, Front, Top, Bottom, Left, Right

public int GetTextureID (int faceIndex) {

	switch (faceIndex) {

	case 0:
		return backFaceTexture;
	case 1:
		return frontFaceTexture;
	case 2:
		return topFaceTexture;
	case 3:
		return bottomFaceTexture;
	case 4:
		return leftFaceTexture;
	case 5:
		return rightFaceTexture;
	default:
		Debug.Log("Error in GetTextureID; invalid face index");
		return 0;


	}

}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant