Now, we will add our final features to make this game complete.
Right now, the game is playable, but there is no way to win! Let's change that by creating a new empty GameObject and naming it RoundManager
. Next, create a new C# script, name it WinConditions
, and add this code to the script:
public int Enemies; void Start () { GameObject[] e = GameObject.FindGameObjectsWithTag("Enemy"); Enemies = e.Length; } void Update () { if(Enemies <= 0) { if(Application.loadedLevel != 3) Application.LoadLevel(Application.loadedLevel + 1); else Application.LoadLevel(0); } }
What this script will do is get a count of how many enemies there are in the scene and use that information to decide whether the player wins. In the Start
function, we grab the amount of enemies and assign it to the int
variable we created. In the Update
function, we check that there are no more enemies left. In our game, when you kill all the enemies you move on to the next level; if there are no levels left, you return to the main menu.
In the AI_Agent
script, we need to add these two lines of code to allow stat tracking and, as we just created, a way to affect the win conditions. Add these two lines to the ChangeHealth
function just before you call the Destroy
function:
Camera.main.GetComponent<StatTracker>().SetStat("Kills", 1); GameObject.Find("RoundManager").GetComponent<WinConditions>().Enemies--;
The first line will add to the Kills
stat by 1
, improving that stat. In the second line, we decrease the number of enemies, bringing the player one step closer to victory.
Just as with the AI code, we will need to modify another script to get the potions to work correctly. In the GUI_2D
script, in the OnGUI
function, replace the code for first ItemButton
with this code:
if(GUI.Button(ItemButtons[0], Items[0].name)) { if(Items[0].name == "Potion") { Items[0].GetComponent<itemSelf>().selfType = SelfType.Potion; Items[0].GetComponent<itemSelf>().selfAction = SelfAction.ChangeHP; Items[0].GetComponent<itemSelf>().Amount = 25; } }
This code now makes that GUI button usable and will call the correct functions to use that potion.
13.59.32.1