public List <voxel> getNeighbours(voxel toCheck) { List <voxel> neighbours = new List <voxel> (); for (int x = -1; x <= 1; x++) { for (int y = -1; y <= 1; y++) { for (int z = -1; z <= 1; z++) { if (x == 0 && y == 0 && z == 0) { continue; } int checkX = Mathf.RoundToInt(toCheck.idenLoc.x) + x; int checkY = Mathf.RoundToInt(toCheck.idenLoc.y) + y; int checkZ = Mathf.RoundToInt(toCheck.idenLoc.z) + z; if (checkX >= 0 && checkX < playSize && checkY >= 0 && checkY < playSize && checkZ >= 0 && checkZ < playSize) { neighbours.Add(spaces [checkX, checkY, checkZ]); } } } } return(neighbours); }
void plant() { Vector3 loc = seed.location + facing; idenspawn = seed.idenLoc - facing; while (limit > 0) { limit -= 1; voxel treeVox = spaces [Mathf.RoundToInt(idenspawn.x), Mathf.RoundToInt(idenspawn.y), Mathf.RoundToInt(idenspawn.z)]; Transform Treetrunk = Instantiate(planetgen.Voxelprefab, treeVox.location, Quaternion.identity, transform) as Transform; treeVox.assignedTo = true; treeVox.face = seed.face; Treetrunk.name = "Tree " + tring + " (" + limit + ")"; treeVox.ident = "tree"; treeVox.treeNum = limit; Treetrunk.gameObject.GetComponent <voxelHolder> ().BlockID = "tree"; Treetrunk.gameObject.GetComponent <voxelHolder> ().value = 6; Treetrunk.gameObject.GetComponent <MeshRenderer> ().material = loggy; treeVox.assignedToCube = Treetrunk.gameObject; planetgen.trees.Add(treeVox); idenspawn -= facing; loc += facing; trunk.Add(treeVox); } foliage(); }
void addTrees() { Vector3 facing = Vector3.zero; Vector3 spawn = Vector3.zero; Vector3 absSpawn = Vector3.zero; for (int i = 0; i < treeNum; i++) { voxel coord = Surface [Random.Range(0, Mathf.RoundToInt(Surface.Count - 1))]; string face = coord.face; float refCord = 0; if (coord.ident == "front") { facing = Vector3.back; refCord = coord.absLoc.z; } if (coord.ident == "back") { facing = Vector3.forward; refCord = coord.absLoc.z; } if (coord.ident == "left") { facing = Vector3.right; refCord = coord.absLoc.x; } if (coord.ident == "right") { facing = Vector3.left; refCord = coord.absLoc.x; } bool goAhead = true; Vector3 idenspawn = coord.idenLoc + facing; voxel treeVox = spaces [Mathf.RoundToInt(idenspawn.x), Mathf.RoundToInt(idenspawn.y), Mathf.RoundToInt(idenspawn.z)]; treeVox.face = face; foreach (var vox in getNeighbours(treeVox)) { if (vox.ident == "tree") { goAhead = false; treeNum += 1; Debug.Log("hit"); break; } } coord.assignedToCube.AddComponent <treeScript> (); treeScript tree = coord.assignedToCube.GetComponent <treeScript> (); tree.leafy = leaf; tree.itemlib = itemlib; tree.loggy = log; tree.seed = coord; tree.tring = i; } }
void dropPlayer(int rad) { voxel startPos = Front [Random.Range(0, Mathf.RoundToInt(Front.Count - 1))]; Vector3 pos = (startPos.location + new Vector3(0, 0, -2)); Transform player = Instantiate(Player, pos, Quaternion.Euler(Vector3.forward), transform); player.GetComponent <GravityBody> ().attractor = gameObject.GetComponent <GravityAttractor>(); playertrans = player; player.gameObject.tag = "Player"; }
void Map(int rad, int coreRad, int playArea) { spaces = new voxel[playArea, playArea, playArea]; for (int i = 0; i <= playSize - 1; i++) { for (int j = 0; j <= playSize - 1; j++) { for (int k = 0; k <= playSize - 1; k++) { Vector3 locator = (CoordToPos(i, j, k)); float absX = Mathf.Abs(locator.x); float absy = Mathf.Abs(locator.y); float absz = Mathf.Abs(locator.z); Vector3 absLoc = new Vector3(absX, absy, absz); //the voxel with its location in space spaces [i, j, k] = new voxel(transform.position - locator); spaces [i, j, k].ident = "null"; //the voxels pposition in the array; spaces [i, j, k].idenLoc = new Vector3(i, j, k); //the voxels location in space with no negative values. spaces [i, j, k].absLoc = absLoc; if ((locator.x >= -coreRad && locator.x <= coreRad) && (locator.y >= -coreRad && locator.y <= coreRad) && (locator.z >= -coreRad && locator.z <= coreRad)) { if ((absX == 0) && (absy == 0) && (absz == 0)) { spaces [i, j, k].ident = "edge"; } continue; } if (((absX == absy) && (absz <= absX)) || ((absX == absz) && (absy <= absX)) || ((absz == absy) && (absX <= absy))) { if ((absX <= rad) && (absy <= rad) && (absz <= rad)) { spaces [i, j, k].ident = "edge"; } } } } } }
private void CreateVoxelGrid() { _voxels = new voxel[GridDimension.x, GridDimension.y, GridDimension.z]; for (int x = 0; x < GridDimension.x; x++) { for (int y = 0; y < GridDimension.y; y++) { for (int z = 0; z < GridDimension.z; z++) { _voxels[x, y, z] = new voxel(new Vector3Int(x, y, z), _GoVoxelPrefab); } } } }
void foliage() { foreach (var trun in trunk) { Vector3 leafIdentitySpawn = Vector3.zero; Vector3 location = Vector3.zero; //top leaf if (trun.treeNum == 0) { leafIdentitySpawn = trun.idenLoc + facing; location = trun.location + facing; Transform leaf = Instantiate(planetgen.Voxelprefab, location, Quaternion.identity, transform) as Transform; leaf.name = "leaf " + trun.location + trun.idenLoc; leaf.gameObject.GetComponent <voxelHolder> ().BlockID = "leaf"; leaf.gameObject.GetComponent <voxelHolder> ().value = 2; leaf.gameObject.GetComponent <voxelHolder> ().itemheld = itemlib.find("Dry Sticks"); leaf.gameObject.GetComponent <MeshRenderer> ().material = leafy; voxel leafVox = spaces [Mathf.RoundToInt(leafIdentitySpawn.x), Mathf.RoundToInt(leafIdentitySpawn.y), Mathf.RoundToInt(leafIdentitySpawn.z)]; leafVox.assignedTo = true; leafVox.ident = "leaf"; leafVox.assignedToCube = leaf.gameObject; leaves.Add(leafVox); if ((trun.face == "front") || (trun.face == "back")) { for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if ((i == 0 && j == 0) || (i == -1 && j == -1) || (i == 1 && j == 1) || (i == -1 && j == 1) || (i == 1 && j == -1)) { continue; } Vector3Int addit = new Vector3Int(i, j, 0); leafIdentitySpawn = trun.idenLoc + addit; location = trun.location + addit; if (spaces [Mathf.RoundToInt(leafIdentitySpawn.x), Mathf.RoundToInt(leafIdentitySpawn.y), Mathf.RoundToInt(leafIdentitySpawn.z)].assignedTo == true) { continue; } leaf = Instantiate(planetgen.Voxelprefab, location, Quaternion.identity, transform) as Transform; leaf.name = "leaf " + trun.location + addit; leaf.gameObject.GetComponent <voxelHolder> ().BlockID = "leaf"; leaf.gameObject.GetComponent <voxelHolder> ().value = 2; leaf.gameObject.GetComponent <voxelHolder> ().itemheld = itemlib.find("Dry Sticks"); leaf.gameObject.GetComponent <MeshRenderer> ().material = leafy; leafVox = spaces [Mathf.RoundToInt(leafIdentitySpawn.x), Mathf.RoundToInt(leafIdentitySpawn.y), Mathf.RoundToInt(leafIdentitySpawn.z)]; leafVox.assignedTo = true; leafVox.ident = "leaf"; leafVox.assignedToCube = leaf.gameObject; leaves.Add(leafVox); } } } if ((trun.face == "left") || (trun.face == "right")) { for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if ((i == 0 && j == 0) || (i == -1 && j == -1) || (i == 1 && j == 1) || (i == -1 && j == 1) || (i == 1 && j == -1)) { continue; } Vector3Int addit = new Vector3Int(0, j, i); leafIdentitySpawn = trun.idenLoc + addit; location = trun.location + addit; if (spaces [Mathf.RoundToInt(leafIdentitySpawn.x), Mathf.RoundToInt(leafIdentitySpawn.y), Mathf.RoundToInt(leafIdentitySpawn.z)].assignedTo == true) { continue; } leaf = Instantiate(planetgen.Voxelprefab, location, Quaternion.identity, transform) as Transform; leaf.name = "leaf " + trun.location + addit; leaf.gameObject.GetComponent <voxelHolder> ().BlockID = "leaf"; leaf.gameObject.GetComponent <voxelHolder> ().value = 2; leaf.gameObject.GetComponent <voxelHolder> ().itemheld = itemlib.find("Dry Sticks"); leaf.gameObject.GetComponent <MeshRenderer> ().material = leafy; leafVox = spaces [Mathf.RoundToInt(leafIdentitySpawn.x), Mathf.RoundToInt(leafIdentitySpawn.y), Mathf.RoundToInt(leafIdentitySpawn.z)]; leafVox.assignedTo = true; leafVox.ident = "leaf"; leafVox.assignedToCube = leaf.gameObject; leaves.Add(leafVox); } } } } //mid leafs if (trun.treeNum == 1 && height >= 3) { if ((trun.face == "front") || (trun.face == "back")) { for (int i = -2; i <= 2; i++) { for (int j = -2; j <= 2; j++) { if ((i == 0 && j == 0) || (i == -2 && j == -2) || (i == 2 && j == 2) || (i == -2 && j == 2) || (i == 2 && j == -2)) { continue; } if ((i == -2 && j == -1) || (i == 2 && j == 1) || (i == -2 && j == 1) || (i == 2 && j == -1)) { continue; } if ((i == -1 && j == -2) || (i == 1 && j == 2) || (i == -1 && j == 2) || (i == 1 && j == -2)) { continue; } Vector3Int addit = new Vector3Int(i, j, 0); leafIdentitySpawn = trun.idenLoc + addit; location = trun.location + addit; if (spaces [Mathf.RoundToInt(leafIdentitySpawn.x), Mathf.RoundToInt(leafIdentitySpawn.y), Mathf.RoundToInt(leafIdentitySpawn.z)].assignedTo == true) { continue; } Transform leaf = Instantiate(planetgen.Voxelprefab, location, Quaternion.identity, transform) as Transform; leaf.name = "leaf " + trun.location + addit; leaf.gameObject.GetComponent <voxelHolder> ().BlockID = "leaf"; leaf.gameObject.GetComponent <voxelHolder> ().value = 2; leaf.gameObject.GetComponent <voxelHolder> ().itemheld = itemlib.find("Dry Sticks"); leaf.gameObject.GetComponent <MeshRenderer> ().material = leafy; voxel leafVox = spaces [Mathf.RoundToInt(leafIdentitySpawn.x), Mathf.RoundToInt(leafIdentitySpawn.y), Mathf.RoundToInt(leafIdentitySpawn.z)]; leafVox.assignedTo = true; leafVox.ident = "leaf"; leafVox.assignedToCube = leaf.gameObject; leaves.Add(leafVox); } } } if ((trun.face == "left") || (trun.face == "right")) { for (int i = -2; i <= 2; i++) { for (int j = -2; j <= 2; j++) { if ((i == 0 && j == 0) || (i == -2 && j == -2) || (i == 2 && j == 2) || (i == -2 && j == 2) || (i == 2 && j == -2)) { continue; } if ((i == -2 && j == -1) || (i == 2 && j == 1) || (i == -2 && j == 1) || (i == 2 && j == -1)) { continue; } if ((i == -1 && j == -2) || (i == 1 && j == 2) || (i == -1 && j == 2) || (i == 1 && j == -2)) { continue; } Vector3Int addit = new Vector3Int(0, j, i); leafIdentitySpawn = trun.idenLoc + addit; location = trun.location + addit; if (spaces [Mathf.RoundToInt(leafIdentitySpawn.x), Mathf.RoundToInt(leafIdentitySpawn.y), Mathf.RoundToInt(leafIdentitySpawn.z)].assignedTo == true) { continue; } Transform leaf = Instantiate(planetgen.Voxelprefab, location, Quaternion.identity, transform) as Transform; leaf.name = "leaf " + trun.location + addit; leaf.gameObject.GetComponent <voxelHolder> ().BlockID = "leaf"; leaf.gameObject.GetComponent <voxelHolder> ().value = 2; leaf.gameObject.GetComponent <voxelHolder> ().itemheld = itemlib.find("Dry Sticks"); leaf.gameObject.GetComponent <MeshRenderer> ().material = leafy; voxel leafVox = spaces [Mathf.RoundToInt(leafIdentitySpawn.x), Mathf.RoundToInt(leafIdentitySpawn.y), Mathf.RoundToInt(leafIdentitySpawn.z)]; leafVox.assignedTo = true; leafVox.ident = "leaf"; leafVox.assignedToCube = leaf.gameObject; leaves.Add(leafVox); } } } } } }
void AssignUnderWorld() { //north foreach (var vox in North) { int y = Mathf.RoundToInt(vox.location.y - transform.position.y) - 1; int locY = Mathf.RoundToInt(vox.idenLoc.y) + 1; while (y > coreRadius) { voxel holder = spaces [Mathf.RoundToInt(vox.idenLoc.x), locY, Mathf.RoundToInt(vox.idenLoc.z)]; if (holder.absLoc.y > holder.absLoc.x && holder.absLoc.y > holder.absLoc.z) { if (holder.absLoc.y > planetRadius - 3) { holder.ident = "ground"; holder.face = "north"; } else { holder.ident = "core"; } } y -= 1; locY += 1; } } foreach (var vox in South) { int y = Mathf.RoundToInt(vox.location.y - transform.position.y) + 1; int locY = Mathf.RoundToInt(vox.idenLoc.y) - 1; while (y < -coreRadius) { voxel holder = spaces [Mathf.RoundToInt(vox.idenLoc.x), locY, Mathf.RoundToInt(vox.idenLoc.z)]; if (holder.absLoc.y > holder.absLoc.x && holder.absLoc.y > holder.absLoc.z) { if (holder.absLoc.y > planetRadius - 3) { holder.ident = "ground"; holder.face = "south"; } else { holder.ident = "core"; } } y += 1; locY -= 1; } } foreach (var vox in Left) { int x = Mathf.RoundToInt(vox.location.x - transform.position.x) - 1; int locX = Mathf.RoundToInt(vox.idenLoc.x) + 1; while (x > coreRadius) { voxel holder = spaces [locX, Mathf.RoundToInt(vox.idenLoc.y), Mathf.RoundToInt(vox.idenLoc.z)]; if (holder.absLoc.x > holder.absLoc.y && holder.absLoc.x > holder.absLoc.z) { if (holder.absLoc.x > planetRadius - 3) { holder.ident = "ground"; holder.face = "left"; } else { holder.ident = "core"; } } x -= 1; locX += 1; } } foreach (var vox in Right) { int x = Mathf.RoundToInt(vox.location.x - transform.position.x) + 1; int locX = Mathf.RoundToInt(vox.idenLoc.x) - 1; while (x < -coreRadius) { voxel holder = spaces [locX, Mathf.RoundToInt(vox.idenLoc.y), Mathf.RoundToInt(vox.idenLoc.z)]; if (holder.absLoc.x > holder.absLoc.y && holder.absLoc.x > holder.absLoc.z) { if (holder.absLoc.x > planetRadius - 3) { holder.ident = "ground"; holder.face = "right"; } else { holder.ident = "core"; } } x += 1; locX -= 1; } } foreach (var vox in Back) { int z = Mathf.RoundToInt(vox.location.z - transform.position.z) - 1; int locZ = Mathf.RoundToInt(vox.idenLoc.z) + 1; while (z > coreRadius) { voxel holder = spaces [Mathf.RoundToInt(vox.idenLoc.x), Mathf.RoundToInt(vox.idenLoc.y), locZ]; if (holder.absLoc.z > holder.absLoc.x && holder.absLoc.z > holder.absLoc.y) { if (holder.absLoc.z > planetRadius - 3) { holder.ident = "ground"; holder.face = "back"; } else { holder.ident = "core"; } } z -= 1; locZ += 1; } } foreach (var vox in Front) { int z = Mathf.RoundToInt(vox.location.z - transform.position.z) + 1; int locZ = Mathf.RoundToInt(vox.idenLoc.z) - 1; while (z < -coreRadius) { voxel holder = spaces [Mathf.RoundToInt(vox.idenLoc.x), Mathf.RoundToInt(vox.idenLoc.y), locZ]; if (holder.absLoc.z > holder.absLoc.x && holder.absLoc.z > holder.absLoc.y) { if (holder.absLoc.z > planetRadius - 3) { holder.ident = "ground"; holder.face = "front"; } else { holder.ident = "core"; } } z += 1; locZ -= 1; } } }
void assignSurface() { foreach (var item in spaces) { Vector3 locator = item.location - transform.position; Vector3 idenLoc = item.idenLoc; if ((item.absLoc.y > item.absLoc.x) && (item.absLoc.y > item.absLoc.z)) { // north if (Mathf.RoundToInt(locator.y) == planetRadius) { int new_y = perlinifiy(idenLoc.y, idenLoc.x, idenLoc.z, 10, 5) - 2; voxel holder = spaces [Mathf.RoundToInt(idenLoc.x), new_y, Mathf.RoundToInt(idenLoc.z)]; if (holder.absLoc.y > holder.absLoc.x && holder.absLoc.y > holder.absLoc.z) { holder.ident = "north"; holder.face = "north"; North.Add(holder); } } //south if (Mathf.RoundToInt(locator.y) == -planetRadius) { int new_y = perlinifiy(idenLoc.y, idenLoc.x, idenLoc.z, 4, 4) - 2; voxel holder = spaces [Mathf.RoundToInt(idenLoc.x), new_y, Mathf.RoundToInt(idenLoc.z)]; if (holder.absLoc.y > holder.absLoc.x && holder.absLoc.y > holder.absLoc.z) { holder.ident = "south"; holder.face = "south"; South.Add(holder); } } } if ((item.absLoc.y < planetRadius) && (item.absLoc.z < planetRadius)) { //left if (Mathf.RoundToInt(locator.x) == planetRadius) { int new_X = perlinifiy(idenLoc.x, idenLoc.y, idenLoc.z, 15, 6) - 3; voxel holder = spaces [new_X, Mathf.RoundToInt(idenLoc.y), Mathf.RoundToInt(idenLoc.z)]; if (holder.absLoc.z < holder.absLoc.x && holder.absLoc.y < holder.absLoc.x) { holder.ident = "left"; holder.face = "left"; Left.Add(holder); } } //right if (Mathf.RoundToInt(locator.x) == -planetRadius) { int new_X = perlinifiy(idenLoc.x, idenLoc.y, idenLoc.z, 6, 10) - 3; voxel holder = spaces [new_X, Mathf.RoundToInt(idenLoc.y), Mathf.RoundToInt(idenLoc.z)]; if (holder.absLoc.z < holder.absLoc.x && holder.absLoc.y < holder.absLoc.x) { holder.ident = "right"; holder.face = "right"; Right.Add(holder); } } } if ((item.absLoc.y < planetRadius) && (item.absLoc.x < planetRadius)) { //back if (Mathf.RoundToInt(locator.z) == planetRadius) { int new_z = perlinifiy(idenLoc.z, idenLoc.y, idenLoc.x, 20, 5) - 3; voxel holder = spaces [Mathf.RoundToInt(idenLoc.x), Mathf.RoundToInt(idenLoc.y), new_z]; if (holder.absLoc.x < holder.absLoc.z && holder.absLoc.y < holder.absLoc.z) { holder.ident = "back"; holder.face = "back"; Back.Add(holder); } } //front if (Mathf.RoundToInt(locator.z) == -planetRadius) { int new_z = perlinifiy(idenLoc.z, idenLoc.y, idenLoc.x, 10, 3) - 1; voxel holder = spaces [Mathf.RoundToInt(idenLoc.x), Mathf.RoundToInt(idenLoc.y), new_z]; if (holder.absLoc.x < holder.absLoc.z && holder.absLoc.y < holder.absLoc.z) { holder.ident = "front"; holder.face = "front"; Front.Add(holder); } } } } }