public GameObject TrySpawnUnit(int i, int j, Unit unitToSpawn, bool isEnemySpawn, int tierOverride, bool isCreep) // try to spawn a unit and return it { string prefabName = Enum.GetName(typeof(Unit), unitToSpawn); //get the units name as defined in the enum GameObject tile = chessBoard[i, j]; // get the tile we want to spawn on if (tile.GetComponent <TileBehaviour>().occupyingUnit != null) // tile is occupied { return(null); // tile occupied is occupied so we break the function. } Quaternion spawnRotation = new Quaternion(); if (isEnemySpawn) { spawnRotation = Quaternion.Euler(0, 90, 0); // fix the default spawn rotation for enemy } else { spawnRotation = Quaternion.Euler(0, -90, 0); //fix the default spawn rotation for friendly } GameObject spawnedNPC = (GameObject)GameObject.Instantiate(Resources.Load(prefabName), tile.transform.position, spawnRotation); // finally spawn the object spawnedNPC.GetComponentInChildren <NPC>().isEnemy = isEnemySpawn; //initialize the object tile.GetComponent <TileBehaviour>().occupyingUnit = spawnedNPC; //init spawnedNPC.GetComponentInChildren <NPC>().occupyingTile = tile; //init spawnedNPC.GetComponentInChildren <NPC>().PrepareNPC3DHud(); //init if (tierOverride == 2) //force spawn override tier to tier2 { spawnedNPC.GetComponentInChildren <NPC>().ApplyTier2Upgrades(); //apply t2 upgrades } else if (tierOverride == 3) //force spawn override tier to tier3 { spawnedNPC.GetComponentInChildren <NPC>().ApplyTier2Upgrades(); //apply t2 upgrades spawnedNPC.GetComponentInChildren <NPC>().ApplyTier3Upgrades(); //also apply the t3 upgrades } npcController.UpdateNpcList(); // update the NPC lists with the new NPC return(spawnedNPC); // return the created object }
public bool SpawnFriendlyNPC(Unit unitToSpawn) // spawn a friendly npc in the reserve board if there is place { string prefabName = Enum.GetName(typeof(Unit), unitToSpawn); bool doneSearching = false; bool reserveHasEmptySlot = false; int i = 0; int j = 0; for (j = 0; j < 8; j++) { if (boardController.chessBoard[8, j].GetComponent <TileBehaviour>().occupyingUnit == null) { reserveHasEmptySlot = true; } } if (reserveHasEmptySlot) { while (!doneSearching) { for (i = 0; i < 8; i++) { if (boardController.chessBoard[8, i].GetComponent <TileBehaviour>().occupyingUnit == null) { doneSearching = true; break; } } } GameObject tile = boardController.chessBoard[8, i]; GameObject go = (GameObject)GameObject.Instantiate(Resources.Load(prefabName), tile.transform.position, Quaternion.Euler(0, -90, 0)); int unitCost = 0; NPC_COST_DATA.TryGetValue(unitToSpawn, out unitCost); tile.GetComponent <TileBehaviour>().occupyingUnit = go; go.GetComponentInChildren <NPC>().occupyingTile = tile; go.GetComponentInChildren <NPC>().PrepareNPC3DHud(); npcController.UpdateNpcList(); go.GetComponentInChildren <NPC>().TryLevelUpFriendly(); SetPlayerGoldCount(playerGoldCount - unitCost); return(true); } return(false); }