> For the complete documentation index, see [llms.txt](https://kdoore.gitbook.io/cs2335/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kdoore.gitbook.io/cs2335/f20_bkup_v2/project1-code/minigamemanager.md).

# MiniGameManager

### To complete Project 1:  **Create The following GameObjects:**

**MiniGameManager:** Empty GameObject

[ **ResultsPanel Page:**](/cs2335/f20_bkup_v2/project1-code/minigamemanager/resultspanel.md)

* **ResultsPanel: UI-Panel** with **CanvasGroup** Component&#x20;
* **ResultsText: UI-Text** - **child** of ResultsPanel&#x20;

&#x20;[**StartButton Page:**](/cs2335/f20_bkup_v2/project1-code/minigamemanager/startbutton.md)  &#x20;

* **StartButton: UI-Button**&#x20;

## MiniGameManager.cs

This script will be on emptyGameObject: **MiniGameManager** You'll need to populate public fields in the inspector once you have created the other required gameObjects. See Images below:

Put Script on an empty gameObject in the Scene.

![](/files/-M0-KjEgIXwUbl42veoH)

## Overview - MiniGameManager.cs:

### The **MiniGameManager** Class manages **logic** for:

* **Start Button** to Start Gameplay
  * Function **`ReStartGame( )`** is executed when the StartButton is clicked
    * **Resets GameData**
    * **Hides** the **Results Panel:**
    * Sets the **curGameState** to **MiniGameState.active**
    * **Hide** the **StartButton** so it's inactive (hidden)
    * Set the **Spawner** **activeSpawning = true**
    * Execute **Spawner StartSpawning( )** method.
* **Polling** - **Code Executed in `Update( )`: - Checked Every Frame**
  * Checks to determine what the current value of **`curGameState`**&#x20;
  * if **c`urGameState`** ==**`MiniGameState.active`**
    * Check that **Health** is still **greater than 0**
    * If **Health is Greater than 0**
      * Check Score, if **Score > WinScore**
        * Set **MiniGameState.Win**
        * Set **win** text for **`ResultsText.text`**
        * Call **`GameOver( )`**
    * Else if **Health is Less than or equal to 0**
      * Set **MiniGameState.Lose**
        * Set **lose** text for **`ResultsText.text`**
        * Call **GameOver( )**
* **GameOver( ) Method**
  * **Show** **ResultsPanel** ( Use Utility to set CanvasGroup values)&#x20;
  * **Show StartButton** - setActive is true, makes visible&#x20;
  * **Stop Spawner** by activeSpawning = false
  * &#x20;**Spawner: `StopAllSpawning( )`** Also destroys all spawned objects

## Code MiniGameState, MiniGameManager &#x20;

#### **See Version2 below for Included Spawner Code**

```java
//Updated Sept 17 2020

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;   //Required for modifying any UI element

public class MiniGameManager : MonoBehaviour
{

    enum MiniGameState { idle, active, win, lose }

    [SerializeField]
     MiniGameState curGameState;

    //TODO Add Spawner

    [SerializeField]
    Button startButton; //Button component on the StartButton gameObject

    [SerializeField]
    Text resultsText;   //Text component on the ResultsText gameObject

    [SerializeField]
    CanvasGroup resultsPanelCG; //canvas group component on the ResultsPanel

    [SerializeField]
    int winScore = 30;

    // Start is called before the first frame update
    void Start()
    {
        curGameState = MiniGameState.idle;
        resultsText.text = "Score " + winScore + " To Win";
        Utility.ShowCG(resultsPanelCG); //make sure panel is visible

        startButton.onClick.AddListener(ReStartGame);  //IMPORTANT - Method to be exectued when onClick event happens
    }

    // Update is called once per frame
    void Update()
    {
        if (curGameState == MiniGameState.active)
        {
            if (GameData.instanceRef.Health > 0)
            {
                if (GameData.instanceRef.Score >= winScore)
                {
                    //won the game
                    curGameState = MiniGameState.win;
                    resultsText.text = "You are a winner";
                    GameOver();
                }
            }
            else  //lost due to health
            {
                curGameState = MiniGameState.lose;
                resultsText.text = "You lost, so sorry, try again";
                GameOver();
            }

        } //end if
    } //end Update

    /// <summary>
    /// Restart the game
    /// </summary>
    /// Syntax for a method to be executed by a Unity Event
    /// MUST BE PUBLIC to be executed by Unity EVENT
    public void ReStartGame()  //important syntax
    {
        GameData.instanceRef.ResetGameData();  //use singleton variable to call public method
        Utility.HideCG(resultsPanelCG); //toggle to hide panel
        curGameState = MiniGameState.active;
        startButton.gameObject.SetActive(false);

        //TODO 
        //start spawner
    }

    /// <summary>
    /// Games the over.
    /// Private since only executed from within this class
    /// </summary>
    private void GameOver()
    {
        Utility.ShowCG(resultsPanelCG); //toggle to make visible
        ///TODO stop spawner, destroy all spawned objects
        /// Get Text that is the child of the StartButton
        startButton.gameObject.SetActive(true); //must set gameObject to active before finding child text
        Text btnText = startButton.GetComponentInChildren<Text>();
        btnText.text = "Play Again";
    }

}//end class

```

## Version 2 - With Spawner Code Added

The code below is a simplified version, either version should work, select the one you prefer

```java
//Sept 17, 2020
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;   //Required for modifying any UI element

public class MiniGameManager : MonoBehaviour
{

    enum MiniGameState { idle, active, win, lose }

    [SerializeField]
     MiniGameState curGameState;

    [SerializeField]
    Spawner spawner;

    [SerializeField]
    Button startButton; //Button component on the StartButton gameObject

    [SerializeField]
    Text resultsText;   //Text component on the ResultsText gameObject

    [SerializeField]
    CanvasGroup resultsPanelCG; //canvas group component on the ResultsPanel

    [SerializeField]
    int winScore = 30;


    // Start is called before the first frame update
    void Start()
    {
        curGameState = MiniGameState.idle;
        Utility.ShowCG(resultsPanelCG); //make sure panel is visible
        resultsText.text = "Score " + winScore + " To Win";
        startButton.onClick.AddListener(ReStartGame);
    }

    // Update is called once per frame
    void Update()
    {
        if (curGameState == MiniGameState.active)
        {
            if (GameData.instanceRef.Health > 0)
            {
                if (GameData.instanceRef.Score >= winScore)
                {
                    //won the game
                    curGameState = MiniGameState.win;
                    resultsText.text = "You are a winner";
                    GameOver();
                }
            }
            else  //lost due to health
            {
                curGameState = MiniGameState.lose;
                resultsText.text = "You lost, so sorry, try again";
                GameOver();
            }

        } //end if
    } //end Update

    /// <summary>
    /// Restart the game
    /// </summary>
    /// Syntax for a method to be executed by a Unity Event
    /// MUST BE PUBLIC to be executed by Unity EVENT
    public void ReStartGame()  //important syntax
    {
        GameData.instanceRef.ResetGameData();  //use singleton variable to call public method
        Utility.HideCG(resultsPanelCG); //toggle to hide panel
        curGameState = MiniGameState.active;
        startButton.gameObject.SetActive( false );
        //start spawner
        spawner.activeSpawning = true;
        spawner.StartSpawning();
    }

    /// <summary>
    /// Games the over.
    /// Private since only executed from within this class
    /// </summary>
    private void GameOver()
    {
        Utility.ShowCG(resultsPanelCG); //toggle to make visible
        
        ///TODO stop spawner, destroy all spawned objects
        spawner.StopAllSpawning();
        
        startButton.gameObject.SetActive( true );
        /// Get Text that is the child of the StartButton 
        Text btnText = startButton.GetComponentInChildren<Text>();
        btnText.text = "Play Again";
    }

}//end class

```
