> 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/cs2335_f20/project-3-code-mods/project-3-code/minigamewin-logic.md).

# MiniGameWin Logic

There are 2 different versions of MiniGState below. Version 1 always loads the EndScene, Version 2 loads the next scene depending on consequences of winning or losing the MiniGame.

## VERSION 1 - Always Executes: LoadEndScene( )

When the onMiniGame Event occurs, it triggers loading of the EndScene.

```
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using UnityEngine.Events;

public class MiniGState : IStateBase {

    Button btnOption1;
    LevelManager levelManager;

    /// <summary>
    /// The scene.
    /// </summary>
    private GameScene scene;

    /// <summary>
    /// Gets the scene number - enum
    /// </summary>
    /// <value>The scene.</value>
    public GameScene Scene
    {
        get { return scene; }
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="T:MiniGState"/> class.
    /// </summary>
    public MiniGState()
    {
        scene = GameScene.MiniGame;
    }


    public void InitializeObjectRefs()
    {
        btnOption1 = GameObject.Find("ButtonOption1").GetComponent<Button>();
        btnOption1.onClick.AddListener(LoadEndScene);

        levelManager = Object.FindObjectOfType<LevelManager>();
        levelManager.onMiniGameOver.AddListener(LoadEndScene);

    }


    public void LoadEndScene()
    {
        SceneManager.LoadScene("EndScene");
        StateManager.instanceRef.SwitchState(new EndState());
    }
}
```

## VERSION 2 - Conditional Logic Determines next Scene to Load

The code below shows how you can use the LevelManager custom UnityEvent: onMiniGameEnd. In the example code below, when the event occurs, then the MiniGameOver method is executed, it checks GameData, to determine if the player won or lost the MiniGame, and it determines the next scene based on that logic.

```java
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using UnityEngine.Events;

public class MiniGState : IStateBase {

    Button btnOption1;
    LevelManager levelManager;

    /// <summary>
    /// The scene.
    /// </summary>
    private GameScene scene;

    /// <summary>
    /// Gets the scene number - enum
    /// </summary>
    /// <value>The scene.</value>
    public GameScene Scene
    {
        get { return scene; }
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="T:MiniGState"/> class.
    /// </summary>
    public MiniGState()
    {
        scene = GameScene.MiniGame;
    }


    public void InitializeObjectRefs()
    {
        btnOption1 = GameObject.Find("ButtonOption1").GetComponent<Button>();
        btnOption1.onClick.AddListener(LoadEndScene);

        //Connect with LevelManager Event: onMiniGameOver
        levelManager = Object.FindObjectOfType<LevelManager>();
        levelManager.onMiniGameEnd.AddListener(MiniGameOver);

    }

    public void MiniGameOver()
    {
        if (GameData.instanceRef.miniGameWinner) //check GameData
        {
            LoadWinScene(); //true: player won
        }
        else
        {
            LoadLoseScene(); //false: player lost
        }

    }

    public void LoadWinScene()
    {
        SceneManager.LoadScene("WinScene");
        StateManager.instanceRef.SwitchState(new WinState());
    }

    public void LoadLoseScene()
    {
        SceneManager.LoadScene("LoseScene");
        StateManager.instanceRef.SwitchState(new LoseState());
    }

}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://kdoore.gitbook.io/cs2335/cs2335_f20/project-3-code-mods/project-3-code/minigamewin-logic.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
