public bool DrawGrid()//this method draws the grid using the Vector3 method to iterate across the x and z axis. GO are added to an array list for later reference { for (float x = 0; x < gridSize; x += gridSpacing) { for (float z = 0; z < gridSize; z += gridSpacing) { Vector3 point = GetNearestPointOnGrid(new Vector3(x, 0f, z)); GameObject go = Instantiate(gridSquare, new Vector3(point.x, 0, point.z), Quaternion.identity); go.transform.SetParent(gridParent.transform, true); GridCellManager gsm = new GridCellManager(); gsm.location = new Vector3(point.x - (gridSize / 2) + planeOffset, 0, point.z - (gridSize / 2) + planeOffset); gsm.tile = go; gsm.tileInUse = false; int gsmTemp; int.TryParse(x.ToString() + z.ToString(), out gsmTemp);//int.Parse(x.ToString() + z.ToString()); gsm.ID = gsmTemp; go.name = "Tile " + x + ", z" + z; gridcellArray.Add(gsm); } } gridParent.transform.position = new Vector3(-(gridSize / 2) + planeOffset, 0, -(gridSize / 2) + planeOffset); #region list locations of tiles //foreach (GridCellManager gsm in gridcellArray) //{ // Debug.Log(gsm.location + ": location " + gsm.tile.name + "tile"); //} #endregion gridGenerated = true; return(true); }
public void DragOff(Vector3 position) { for (int i = 0; i < availableCells.Count; i++) { GridCellManager gsm = availableCells[i]; if (gsm.location == currentLocation) { Debug.Log("Set Unavailable"); StartCoroutine(SetUnavailable(gsm)); } } }
public void BeginDrag(GameObject buildingSelected) { Vector3 selectedLocation = buildingSelected.transform.position; for (int i = 0; i < unavailableCells.Count; i++) { if (unavailableCells[i].location == selectedLocation) { GridCellManager gsm = unavailableCells[i]; Debug.Log("building found"); StartCoroutine(SetAvailable(gsm)); } } }
private void Start() { availableCells = new List <GridCellManager>(); unavailableCells = new List <GridCellManager>(); totalCells = new List <GridCellManager>(); if (m_GridGenerator.gridGenerated) { for (int i = 0; i < m_GridGenerator.gridcellArray.Count; i++) { GridCellManager gsm = m_GridGenerator.gridcellArray[i]; availableCells.Add(gsm); totalCells.Add(gsm); } } }
Vector3 ClosestTile(Vector3 currentpos) // returns the nearest available tile. { Vector3 closest = currentpos; float closestDistSqr = Mathf.Infinity; for (int i = 0; i < availableCells.Count; i++) { GridCellManager gsm = availableCells[i]; Vector3 directionToTarget = gsm.location - currentpos; float distSqrToTarget = directionToTarget.sqrMagnitude; if (distSqrToTarget < closestDistSqr) { closestDistSqr = distSqrToTarget; closest = gsm.location; currentCell = gsm; } } return(closest); }
public IEnumerator PutOnMap(GameObject buildingSelected)// this puts the building on the map. Setting the cell as unavailable so that multiple objects cannot be held in one place. { buildingSelected.transform.position = ClosestTile(new Vector3(0, 0, 0)); for (int i = 0; i < availableCells.Count; i++) { if (buildingSelected.transform.position == availableCells[i].location) { GridCellManager gsm = availableCells[i]; currentCell = gsm; currentCell.building = buildingSelected; buildingSelected.transform.position = currentCell.location; StartCoroutine(SetUnavailable(gsm)); availableCells.Remove(gsm); unavailableCells.Add(gsm); } } Instantiate(buildingSelected, currentCell.tile.transform.position, Quaternion.identity); currentCell.tileInUse = true; buildingSelected.GetComponent <BuildingDataclass>().onMap = true; yield return(null); }
public IEnumerator SetUnavailable(GridCellManager gsm) { availableCells.Remove(gsm); unavailableCells.Add(gsm); yield return(null); }