Menu.cs 6.38 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
9
10

public class Menu : MonoBehaviour
{
radow's avatar
radow committed
11

radow's avatar
radow committed
12
13
    private void Awake()
    {
radow's avatar
radow committed
14
        UISensivitySettings.LoadSoundSettingsLocally();
radow's avatar
radow committed
15
16
17
18
19
20
        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
21
    public GameObject settingsMain, settingsGraphics, settingsAudio, settingsControls;
radow's avatar
radow committed
22
    public GameObject menuWrapper, mainMenuGO, settingsGO, creditsGO, controlsGO, quitGO;
radow's avatar
radow committed
23
    public GameObject playButton, creditButton, continueButton, helpButton;
radow's avatar
radow committed
24
    public Button gameplayButton, noButton;
radow's avatar
radow committed
25
26
27
    public Ease ease;
    public float duration = 3f;

radow's avatar
radow committed
28
    Sequence mover;
radow's avatar
radow committed
29
    Sub currentMenu = Sub.MAIN;
radow's avatar
radow committed
30

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

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

        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
56
57
58
            case Sub.QUIT:
                quitGO.SetActive(false);
                break;
radow's avatar
radow committed
59
        }
radow's avatar
radow committed
60
61
        int dist = (int)(Mathf.Abs((int)currentMenu - (int)nextMenu));
        currentMenu = nextMenu;
radow's avatar
radow committed
62

radow's avatar
radow committed
63
        if(SceneManager.GetActiveScene().buildIndex == 0)
radow's avatar
radow committed
64
        {
radow's avatar
radow committed
65
            switch (currentMenu) //Entering
radow's avatar
radow committed
66
67
68
69
70
            {
                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
71
72
73
74
75
                    settingsMain.SetActive(true);
                    settingsGraphics.SetActive(false);
                    settingsAudio.SetActive(false);
                    settingsControls.SetActive(false);

radow's avatar
radow committed
76
                    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
77
78
79
80
81
82
83
84
                    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
85
                    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
86
87
88
89
90
91
92
93
94
95
96
97
                    break;
            }
        }
        else
        {
            switch (currentMenu) //Leaving
            {
                case Sub.MAIN:
                    mainMenuGO.SetActive(true);
                    break;
                case Sub.SETTINGS:
                    settingsGO.SetActive(true);
radow's avatar
radow committed
98

radow's avatar
radow committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
                    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
114
115
116
117
118
119
120
121
122
    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
123
124
125

    PostProcessVolume m_Volume;
    DepthOfField m_DepthOfField;
radow's avatar
radow committed
126
127
128
    bool menuMode;
    private void Update()
    {
radow's avatar
radow committed
129
        if(Input.GetButtonDown("Cancel") && (SceneManager.GetActiveScene().buildIndex != 0) && InputMode.mode == InputMode.Mode.PLAYMODE)
radow's avatar
radow committed
130
        {
radow's avatar
radow committed
131
132
            SetPausemenu(!menuMode);
        }
radow's avatar
radow committed
133
134
135
136
137
138
139
140
141
142
        if ((SceneManager.GetActiveScene().buildIndex == 0 || menuMode) && (Input.GetButtonDown("Cancel")|| Input.GetButtonDown("Fire2")))
        {
            if (currentMenu == Sub.MAIN) {
                if(SceneManager.GetActiveScene().buildIndex != 0) SetPausemenu(false);
            }
            else
            {
                if (currentBackButton) currentBackButton.onClick.Invoke();
            }
        }
radow's avatar
radow committed
143
144
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)
        {
            GoToSubMenu(Sub.MAIN);
            playButton.SetActive(false);
radow's avatar
radow committed
154
            creditButton.SetActive(false);
radow's avatar
radow committed
155
156
            continueButton.SetActive(true);
            helpButton.SetActive(true);
radow's avatar
radow committed
157

radow's avatar
radow committed
158
159
160
161
162
163
164
165
166
167
            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
168
169
        }
    }
radow's avatar
radow committed
170

radow's avatar
radow committed
171
172
173
174
175
    void OnDestroy()
    {
        if (m_Volume) RuntimeUtilities.DestroyVolume(m_Volume, true, true);
    }

radow's avatar
radow committed
176
177
178
179
180
181
182
183
184
    public void StartGame()
    {
        LoadScene(1);
    }

    public static void LoadScene(int i)
    {
        SceneManager.LoadScene(i);
    }
radow's avatar
radow committed
185
186
187

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

    public void GetHelp()
    {
        Helproom.GetHelp();
        SetPausemenu(false);
    }
radow's avatar
radow committed
196
197
}