public void Highlight(TerrainCube tc) { transform.position = tc.transform.position; model.transform.localScale = new Vector3(1, 500, 1); transform.eulerAngles = tc.transform.eulerAngles; model.GetComponent <Renderer>().material.color = whiteColor; }
TerrainCube CreateCubeObject(Vector3 coord, Vector3 pos, float Size, float Density, Chunk ParentChunk) { GameObject cube = new GameObject($"cube ({coord.x}, {coord.y}, {coord.z})"); cube.AddComponent <TerrainCube>(); cube.transform.parent = ParentChunk.transform; TerrainCube TC = cube.GetComponent <TerrainCube>(); TC.SetUp(Size, coord, pos, Density); return(TC); }
void UpdateCube(Vector3 coord, Vector3 pos, float Size, Chunk ParentChunk) { //return density value from the noise generator for the current position within the chunk float DensityVal = noiseGenerator.GenerateDensityValue(pos); //store the max / min noise for debugging //ISOValCheck(DensityVal); //get the upper/lower limits for inclusion float upperLimit = ISOValue + (VoxelDepth * 0.5f); float lowerLimit = ISOValue - (VoxelDepth * 0.5f); bool ShouldExist = Between(DensityVal, lowerLimit, upperLimit, true); //check if our cube already exists at that position if (ParentChunk.ExistingCubes.ContainsKey(coord)) { //loop though and find it for (int i = 0; i < ParentChunk.Cubes.Count; i++) { if (ParentChunk.Cubes[i].Coord == coord) { //check if its inside the isosuface val if (!ShouldExist) { //remove it from the dictionary ParentChunk.ExistingCubes.Remove(coord); //destroy it ParentChunk.Cubes[i].Destroy(); //remote it from the list ParentChunk.Cubes.RemoveAt(i); } else { //re init ParentChunk.Cubes[i].Init(Size, coord, pos, DensityVal); } //exit return; } } } else if (ShouldExist) { TerrainCube NewCube = CreateCubeObject(coord, pos, Size, DensityVal, ParentChunk); //add it to the list ParentChunk.Cubes.Add(NewCube); //record it in the dictionary ParentChunk.ExistingCubes.Add(coord, NewCube); } }
public void Highlight(TerrainCube tc, Building b) { transform.position = tc.transform.position; model.transform.localScale = new Vector3(1, 500, 1); transform.eulerAngles = tc.transform.eulerAngles; if (b.BuildingConstraintsSatisfied(tc.x, tc.z) && gm.CanPlaceBuilding(tc.x, tc.z) && b.CanPayForBuilding()) { model.GetComponent <Renderer>().material.color = greenColor; } else { model.GetComponent <Renderer>().material.color = redColor; } }
void Update() { List <GameObject> hitties = new List <GameObject>(); List <RaycastHit> hits; hits = new List <RaycastHit>(Physics.RaycastAll(start.transform.position, end.transform.position)); for (int i = 0; i < hits.Count; i++) { if (hits[i].transform.GetComponent <TerrainCube>()) { if (hits[i].transform.GetComponent <TerrainCube>().surfaceCube) { currentCube = hits[i].transform.GetComponent <TerrainCube>(); i = hits.Count; } } } }
public void LoadFromFile(string path) { /* Delete all cubes */ GameObject[] cubes = GameObject.FindGameObjectsWithTag("TerrainCube"); foreach (GameObject go in cubes) { GameObject.Destroy(go); } using (System.IO.StreamReader reader = new System.IO.StreamReader(path)) { string contents_of_file = reader.ReadToEnd(); string[] seperated_contents = contents_of_file.Split(','); width = System.Convert.ToInt32(seperated_contents[0]); height = System.Convert.ToInt32(seperated_contents[1]); depth = System.Convert.ToInt32(seperated_contents[2]); terrain_map = new TerrainCube[width, height, depth]; int current_index = 3; for(int x = 0; x < width; ++x) { for(int y = 0; y < height; ++y) { for(int z = 0; z < depth; ++z) { terrain_map[x, y, z] = new TerrainCube(x, y, z); terrain_map[x, y, z].CubeCode = System.Convert.ToInt32 (seperated_contents[current_index]); current_index++; } } } } }
public void tryToPlaceThis() { GameObject[] gos; gos = GameObject.FindGameObjectsWithTag("TerrainCube"); TerrainCube closest = null; float distance = Mathf.Infinity; Vector3 position = transform.position; foreach (GameObject go in gos) { Vector3 diff = go.transform.position - position; float curDistance = diff.sqrMagnitude; if (curDistance < distance) { closest = go.GetComponent <TerrainCube>(); distance = curDistance; } } Debug.Log("Found closest"); if (distance <= 1 && closest.x != 0 && closest.z != 0) { Debug.Log("X: " + closest.x); Debug.Log("Z: " + closest.z); //Debug.Log("GameManager: " + game_manager == null); TerrainCube newCube = game_manager.GetSurfaceCube(closest.x, closest.z); if (newCube != null) { Debug.Log("Placing"); //GameObject newBldg = Instantiate(buildingObj, this.transform.position, this.transform.rotation); if (!game_manager.PlaceBuilding(buildingObj, closest.x, closest.z)) { Debug.Log("False!"); } } } }
private void InitMap() { terrain_map = new TerrainCube[width, height, depth]; for(int x = 0; x < width; ++x) { for(int y = 0; y < height; ++y) { for(int z = 0; z < depth; ++z) { terrain_map[x, y, z] = new TerrainCube(x, y, z); } } } }
// Use this for initialization void Start() { TargetLocation = Camera.main.transform.localPosition; GrassCube = new KeyValuePair<int, GameObject>(0, prefabs[0]); DirtCube = new KeyValuePair<int, GameObject>(1, prefabs[1]); RockCube = new KeyValuePair<int, GameObject>(2, prefabs[2]); terrain_map = new TerrainCube[MAX_WIDTH, MAX_HEIGHT, MAX_DEPTH]; for(int x = 0; x < MAX_WIDTH; ++x) { for(int y = 0; y < MAX_HEIGHT; ++y) { for(int z = 0; z < MAX_DEPTH; ++z) { terrain_map[x, y, z] = new TerrainCube(x, y, z); } } } SelectedCube = GrassCube; CreateInitialFloor(); }
private void LoadFromFile(string path) { /* Delete all cubes */ GameObject[] cubes = GameObject.FindGameObjectsWithTag("TerrainCube"); foreach (GameObject go in cubes) { GameObject.Destroy(go); } using (System.IO.StreamReader reader = new System.IO.StreamReader(path)) { string contents_of_file = reader.ReadToEnd(); string[] seperated_contents = contents_of_file.Split(','); MAX_WIDTH = System.Convert.ToInt32(seperated_contents[0]); MAX_HEIGHT = System.Convert.ToInt32(seperated_contents[1]); MAX_DEPTH = System.Convert.ToInt32(seperated_contents[2]); terrain_map = new TerrainCube[MAX_WIDTH, MAX_HEIGHT, MAX_DEPTH]; int current_index = 3; for(int x = 0; x < MAX_WIDTH; ++x) { for(int y = 0; y < MAX_HEIGHT; ++y) { for(int z = 0; z < MAX_DEPTH; ++z) { terrain_map[x, y, z] = new TerrainCube(x, y, z); terrain_map[x, y, z].CubeCode = System.Convert.ToInt32 (seperated_contents[current_index]); if (terrain_map[x, y, z].CubeCode != -1) { Debug.Log (terrain_map[x, y, z].CubeCode); AddCubeAtLocation(prefabs[terrain_map[x, y, z].CubeCode], x, y, z); } current_index++; } } } } }