public void GenerateMap() { const string mapHolderName = "Generated Map"; //check if map holder already exists & delete if it does if (transform.Find(mapHolderName)) { DestroyImmediate(transform.Find(mapHolderName).gameObject); } //create new mapholder Transform mapHolder = new GameObject(mapHolderName).transform; mapHolder.parent = transform; const string unitHolderName = "Units"; //check if unit holder already exists & delete if it does if (transform.Find(unitHolderName)) { DestroyImmediate(transform.Find(unitHolderName).gameObject); } //create new unitholder Transform unitHolder = new GameObject(unitHolderName).transform; unitHolder.parent = transform; grid.PopulateGrid(); //loop over each grid position for (int x = 0; x < Grid.GridWidth; x++) { for (int y = 0; y < Grid.GridHeight; y++) { //instantiate new tile at position Vector3 tilePosition = new Vector3(x + Grid.CellOffset, Grid.MapHeight, y + Grid.CellOffset); Transform newTile = Instantiate(tilePrefab, tilePosition, Quaternion.identity) as Transform; newTile.parent = mapHolder; //add tile to tile array tiles[x, y] = newTile; //check if there is a unit to spawn at this location if (grid.GetGridArray(new Coords(x, y)) != null) { //instantiate new unit Transform newObject = Instantiate(grid.GetGridArray(new Coords(x, y)).transform, tilePosition, Quaternion.identity); newObject.parent = unitHolder; //add new object to grid (replacing the prefab that is currently there) grid.SetGridArray(new Coords(x, y), newObject.gameObject); //check new unit's tag and add it's unitcontroller to correct army list if (newObject.CompareTag("Friendly Unit")) { unitManager.playerArmy.Add(newObject.GetComponent <UnitController>()); } else if (newObject.CompareTag("Enemy Unit")) { unitManager.enemyArmy.Add(newObject.GetComponent <UnitController>()); } } } } }
/// <summary> /// Update function when state is select and move /// </summary> private void SelectAndMoveUpdate() { //SELECT //check if left click was pressed if (Input.GetButtonDown("Select")) { //get collider clicked on Collider clickedOnCollider = ClickOnCollider(); //check if player clicked on collider if (clickedOnCollider != null) { //check if clicked on friendly unit AND unit has action if (clickedOnCollider.gameObject.CompareTag("Friendly Unit")) { //get unitcontroller clicked on UnitController unitController = clickedOnCollider.gameObject.GetComponent <UnitController>(); //check if unit has action or not. If it has no action, don't select anything if (!unitController.GetHasAction()) { return; } //select unit clicked on SelectUnit(unitController); } //clicked on ground else { //unset selected unit DeselectUnit(); } } } //MOVE //check if right click was pressed and there is a selected unit else if (Input.GetButtonDown("Alt Select") && _selectedUnit != null) { //get collider clicked on Collider clickedOnCollider = ClickOnCollider(); //check if player clicked on collider if (clickedOnCollider != null) { Coords oldCoords = _selectedUnit.GetUnitCoords(); //the current coords of selected unit Coords toCoords = Grid.WorldSpaceToCoords(clickedOnCollider.transform.position); //the coordinates player clicked on (to move to) //check if grid space clicked is selectedunit's own space. Skip movement in that case if (grid.GetGridArray(toCoords) == _selectedUnit.gameObject) { _selectedUnit.UnhighlightTiles(); SetStateAttacking(); } //check and move unit else if (_selectedUnit.Move(toCoords)) { //move was successful, update grid grid.SetGridArray(toCoords, _selectedUnit.gameObject); grid.SetGridArray(oldCoords, null); //state becomes attacking(with selectedunit) SetStateAttacking(); } else { //move failed DeselectUnit(); } } } }