Menu.cs 6.78 KB
Newer Older
radow's avatar
radow committed
1
2
3
4
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
radow's avatar
radow committed
5
using UnityEngine.Rendering.PostProcessing;
radow's avatar
radow committed
6
using UnityEngine.SceneManagement;
radow's avatar
radow committed
7
using UnityEngine.UI;
radow's avatar
radow committed
8
using TMPro;
radow's avatar
radow committed
9
10
11

public class Menu : MonoBehaviour
{
radow's avatar
radow committed
12
    public static Menu instance;
radow's avatar
radow committed
13
14
    private void Awake()
    {
radow's avatar
radow committed
15
        instance = this;
radow's avatar
radow committed
16
        UISensivitySettings.LoadSoundSettingsLocally();
radow's avatar
radow committed
17
18
19
20
21
22
        UISoundSettings.LoadSoundSettingsLocally();
        UIToggleButton.LoadDataLocally();
    }
    
    public enum Sub {CREDITS = 1, MAIN = 3, SETTINGS = 2, CONTROLS = 0,  QUIT = 4}
    public Transform mainCamAnchor, settingsCamAnchor, creditsCamAnchor, controlsCamAnchor, quitCamAnchor;
radow's avatar
radow committed
23
    public GameObject settingsMain, settingsGraphics, settingsAudio, settingsControls;
radow's avatar
radow committed
24
    public GameObject menuWrapper, mainMenuGO, settingsGO, creditsGO, controlsGO, quitGO;
radow's avatar
radow committed
25
    public GameObject playButton, creditButton, continueButton, helpButton;
radow's avatar
radow committed
26
    public Button gameplayButton, noButton;
radow's avatar
radow committed
27
28
29
    public Ease ease;
    public float duration = 3f;

radow's avatar
radow committed
30
    Sequence mover;
radow's avatar
radow committed
31
    Sub currentMenu = Sub.MAIN;
radow's avatar
radow committed
32

radow's avatar
radow committed
33
34
    public void GoToSubMenu(SubMenu nextMenu)
    {
radow's avatar
radow committed
35
36
37
38
39
40
        GoToSubMenu(nextMenu.menu);
    }

    public void GoToSubMenu(Sub nextMenu)
    {
        if (currentMenu == nextMenu) return;
radow's avatar
radow committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

        mover?.Kill();

        switch (currentMenu) //Leaving
        {
            case Sub.MAIN:
                mainMenuGO.SetActive(false);
                break;
            case Sub.SETTINGS:
                settingsGO.SetActive(false);
                break;
            case Sub.CONTROLS:
                controlsGO.SetActive(false);
                break;
            case Sub.CREDITS:
                creditsGO.SetActive(false);
                break;
radow's avatar
radow committed
58
59
60
            case Sub.QUIT:
                quitGO.SetActive(false);
                break;
radow's avatar
radow committed
61
        }
radow's avatar
radow committed
62
63
        int dist = (int)(Mathf.Abs((int)currentMenu - (int)nextMenu));
        currentMenu = nextMenu;
radow's avatar
radow committed
64

radow's avatar
radow committed
65
        if(SceneManager.GetActiveScene().buildIndex == 0)
radow's avatar
radow committed
66
        {
radow's avatar
radow committed
67
            switch (currentMenu) //Entering
radow's avatar
radow committed
68
69
70
71
72
            {
                case Sub.MAIN:
                    mover = DOTween.Sequence().Append(Camera.main.transform.DOMove(mainCamAnchor.position, duration * dist).SetEase(ease)).AppendCallback(() => { mainMenuGO.SetActive(true); });
                    break;
                case Sub.SETTINGS:
radow's avatar
radow committed
73
74
75
76
77
                    settingsMain.SetActive(true);
                    settingsGraphics.SetActive(false);
                    settingsAudio.SetActive(false);
                    settingsControls.SetActive(false);

radow's avatar
radow committed
78
                    mover = DOTween.Sequence().Append(Camera.main.transform.DOMove(settingsCamAnchor.position, duration * dist).SetEase(ease)).AppendCallback(() => { settingsGO.SetActive(true); gameplayButton.Select(); });
radow's avatar
radow committed
79
80
81
82
83
84
85
86
                    break;
                case Sub.CONTROLS:
                    mover = DOTween.Sequence().Append(Camera.main.transform.DOMove(controlsCamAnchor.position, duration * dist).SetEase(ease)).AppendCallback(() => { controlsGO.SetActive(true); });
                    break;
                case Sub.CREDITS:
                    mover = DOTween.Sequence().Append(Camera.main.transform.DOMove(creditsCamAnchor.position, duration * dist).SetEase(ease)).AppendCallback(() => { creditsGO.SetActive(true); });
                    break;
                case Sub.QUIT:
radow's avatar
radow committed
87
                    mover = DOTween.Sequence().Append(Camera.main.transform.DOMove(quitCamAnchor.position, duration * dist).SetEase(ease)).AppendCallback(() => { quitGO.SetActive(true); noButton.Select(); });
radow's avatar
radow committed
88
89
90
91
92
93
94
95
96
97
98
99
                    break;
            }
        }
        else
        {
            switch (currentMenu) //Leaving
            {
                case Sub.MAIN:
                    mainMenuGO.SetActive(true);
                    break;
                case Sub.SETTINGS:
                    settingsGO.SetActive(true);
radow's avatar
radow committed
100

radow's avatar
radow committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
                    break;
                case Sub.CONTROLS:
                    controlsGO.SetActive(true);
                    break;
                case Sub.CREDITS:
                    creditsGO.SetActive(true);
                    break;
                case Sub.QUIT:
                    quitGO.SetActive(true);
                    break;
            }
        }
        
    }

radow's avatar
radow committed
116
117
118
119
120
121
122
123
124
    public static Button currentBackButton; 
    public static void RegisterBackButton(Button b)
    {
        currentBackButton = b;
    }
    public static void DeRegisterBackButton(Button b)
    {
        if (currentBackButton == b) currentBackButton = null;
    }
radow's avatar
radow committed
125
126
127

    PostProcessVolume m_Volume;
    DepthOfField m_DepthOfField;
radow's avatar
radow committed
128
129
130
    bool menuMode;
    private void Update()
    {
radow's avatar
radow committed
131
        if(Input.GetButtonDown("Cancel") && (SceneManager.GetActiveScene().buildIndex != 0) && InputMode.mode == InputMode.Mode.PLAYMODE)
radow's avatar
radow committed
132
        {
radow's avatar
radow committed
133
134
            SetPausemenu(!menuMode);
        }
radow's avatar
radow committed
135
        else if ((SceneManager.GetActiveScene().buildIndex == 0 || menuMode) && (Input.GetButtonDown("Cancel")|| Input.GetButtonDown("Fire2")))
radow's avatar
radow committed
136
137
138
139
140
141
142
143
144
        {
            if (currentMenu == Sub.MAIN) {
                if(SceneManager.GetActiveScene().buildIndex != 0) SetPausemenu(false);
            }
            else
            {
                if (currentBackButton) currentBackButton.onClick.Invoke();
            }
        }
radow's avatar
radow committed
145
146
147
148
149
150
151
152
153
    }

    public void SetPausemenu(bool on)
    {
        menuMode = on;
        menuWrapper.SetActive(on);
        InputMode.SetInputMode(on ? InputMode.Mode.MENU : InputMode.Mode.PLAYMODE);
        if (on)
        {
radow's avatar
radow committed
154
            helpTextDisplay.gameObject.SetActive(false);
radow's avatar
radow committed
155
156
            GoToSubMenu(Sub.MAIN);
            playButton.SetActive(false);
radow's avatar
radow committed
157
            creditButton.SetActive(false);
radow's avatar
radow committed
158
159
            continueButton.SetActive(true);
            helpButton.SetActive(true);
radow's avatar
radow committed
160
            continueButton.GetComponent<Button>().Select();
radow's avatar
radow committed
161

radow's avatar
radow committed
162
163
164
165
166
167
168
169
170
171
            m_DepthOfField = ScriptableObject.CreateInstance<DepthOfField>();
            m_DepthOfField.enabled.Override(true);
            m_DepthOfField.focalLength.value = 0;

            m_Volume = PostProcessManager.instance.QuickVolume(gameObject.layer, 100f, m_DepthOfField);
            m_Volume.gameObject.layer = 6;
        }
        else
        {
            if (m_Volume) RuntimeUtilities.DestroyVolume(m_Volume, true, true);
radow's avatar
radow committed
172
173
        }
    }
radow's avatar
radow committed
174

radow's avatar
radow committed
175
176
177
178
179
    void OnDestroy()
    {
        if (m_Volume) RuntimeUtilities.DestroyVolume(m_Volume, true, true);
    }

radow's avatar
radow committed
180
181
182
183
184
185
186
187
188
    public void StartGame()
    {
        LoadScene(1);
    }

    public static void LoadScene(int i)
    {
        SceneManager.LoadScene(i);
    }
radow's avatar
radow committed
189
190
191

    public void QuitGame()
    {
radow's avatar
radow committed
192
        Application.Quit();
radow's avatar
radow committed
193
    }
radow's avatar
radow committed
194
195
196

    public void GetHelp()
    {
radow's avatar
radow committed
197
198
199
200
201
202
203
204
205
        DisplayHelpInfo(Helproom.GetHelp());
        //SetPausemenu(false);
    }

    public TextMeshProUGUI helpTextDisplay;
    public void DisplayHelpInfo(string helpinfo)
    {
        helpTextDisplay.gameObject.SetActive(true);
        helpTextDisplay.text = helpinfo;
radow's avatar
radow committed
206
    }
radow's avatar
radow committed
207
208
}