public IEnumerator spawnUnit(unitType unitType) //this function spawns a unit, of type unitType, in sector spawningSector if there is available space { bool isSpace = false; //this bool is used to keep track of when an empty space in the sector is found for (int positionIndex = 0; positionIndex < 3; positionIndex++) //search all possible standing positions on spawning sector too see if the sector is full { if (spawnSector.GetComponent <sectorScript>().unitsContained[positionIndex] == null && !isSpace) //if an empty space is found { gameMainScript GameMainScript = gameMain.GetComponent <gameMainScript>(); //set reference to gameMain's gameMainScript gameMapScript GameMapScript = gameMain.transform.GetChild(0).GetComponent <gameMapScript>(); //set reference to gameMap's gameMapScript GameMapScript.createUnit(unitType, GameMainScript.currentPlayer, spawnSector); //use the "createUnit" funtion in the gameMapScript to spawn a new unit isSpace = true; //stop searching for spaces } } if (!isSpace) //if no space was found for the unit to stand on the sector show a warning message to the user { warningMessage.gameObject.SetActive(true); //show warning message yield return(new WaitForSeconds(2)); //show message for 2 seconds warningMessage.gameObject.SetActive(false); //stop showing warning message } }
void OnMouseDown() //when the user clicks on this sector { gameMapScript theGameMapScript = this.gameMap.GetComponent <gameMapScript>(); //set a reference to the gameMapScript script of the gameMap GameObject theSelectedUnit = theGameMapScript.selectedUnit; //set a reference to the gameMap's selected unit to see if a unit was selected before this sector was clicked on theGameMapScript.selectedUnit = null; //a sector has now been selected, so set the selectedUnit to null if (theSelectedUnit != null && theSelectedUnit.GetComponent <unitScript>().canMoveTo().Contains(this.gameObject)) //if a unit has been selected, and this sector has been clicked, move { //that unit to this sector, if it is within range unitScript theSelectedUnitScript = theSelectedUnit.GetComponent <unitScript>(); //set a reference to the selected unit's unitScript script theSelectedUnitScript.moveUnit(this.gameObject); //move unit to this sector } }
void OnMouseDown() //when a building is clicked, it should open the unit buy menu and hide the game map { gameMapScript theGameMapScript = this.gameMap.GetComponent <gameMapScript>(); //set a reference to the gameMapScript script of the gameMap GameObject theSelectedUnit = theGameMapScript.selectedUnit; //set a reference to the gameMap's selected unit to see if a unit was selected before this sector was clicked on theGameMapScript.selectedUnit = null; //a sector has now been selected, so set the selectedUnit to null if (theSelectedUnit != null && theSelectedUnit.GetComponent <unitScript>().canMoveTo().Contains(this.sector)) //if a unit has been selected, and this building has been clicked, move { //that unit to this sector, if it is within range unitScript theSelectedUnitScript = theSelectedUnit.GetComponent <unitScript>(); //set a reference to the selected unit's unitScript script theSelectedUnitScript.moveUnit(this.sector); //move unit to this sector } else if (theSelectedUnit == null) { this.buyUnitCanvas.gameObject.SetActive(true); //open the unit buying canvas this.gameMap.gameObject.SetActive(false); //close the game map buyUnitCanvas.GetComponent <unitCanvasScript>().warningMessage.gameObject.SetActive(false); //do not display the warningMessage //set the "spawnPoint" value in the buyUnitCanvas' "unitCanvasScript" script so that newly purchased units will spawn on the same sector //as the building that they clicked to open the buy unit menu this.buyUnitCanvas.GetComponent <unitCanvasScript>().setSpawnPoint(sector.gameObject); } }