File size: 4,795 Bytes
c80142e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

public class LogManager : MonoBehaviour
{
    [System.Serializable]
    public class LogEntry
    {
        public string wordOfInterest;
        public string userDefinition;
    }

    public List<LogEntry> logEntries = new List<LogEntry>();
    public GameObject logPrefab;
    public Transform contentPanel;
    public GameObject logMenu;
    private bool isLogOpen = false;
    private InteractManager interactMan;

    private StarterAssetsInputs input;
    public ScrollRect scrollRect;
    private CrabInterface crabInterface;
    public Animator scrollArea;
    
    [Header("Audio")]
    public AudioClip openLogSound;
    public AudioClip closeLogSound;
    public AudioClip confirmSound;
    public AudioClip clearSound;
    private AudioSource audioSource;

    [Copyable] public bool canOpenLog = false;

    void Start()
    {
        UpdateLog();
        interactMan = GetComponent<InteractManager>();
        input = GetComponent<StarterAssetsInputs>();
        logMenu.SetActive(false);
        
        audioSource = GetComponent<AudioSource>();
        if (audioSource == null)
        {
            audioSource = gameObject.AddComponent<AudioSource>();
            audioSource.playOnAwake = false;
        }
    }

    void Update()
    {
        if (!canOpenLog)
        {
            return;
        }

        if (input.log)
        {
            ToggleLogMenu(!isLogOpen);
            input.log = false;
        }
    }

    public void SetCrabInterface(CrabInterface newCrabInterface)
    {
        crabInterface = newCrabInterface;
    }

    public void ToggleLogMenu(bool open)
    {
        isLogOpen = open;
        logMenu.SetActive(isLogOpen);
        AudioManager.Instance.ToggleRadioEQ(isLogOpen);
        if (audioSource != null)
        {
            if (isLogOpen && openLogSound != null)
            {
                audioSource.PlayOneShot(openLogSound);
            }
            else if (!isLogOpen && closeLogSound != null)
            {
                audioSource.PlayOneShot(closeLogSound);
            }
        }
            
        if (isLogOpen)
        {
            AudioManager.Instance.ToggleRadioEQ(isLogOpen);
            Time.timeScale = 0f;
            input.SetCursorState(false);
            interactMan.UnlockInteract(false);
            scrollArea.Play("EnterScrollArea");
            if (scrollRect != null)
            {
                scrollRect.verticalNormalizedPosition = 1f;
            }
        }
        else
        {
            if (crabInterface != null)
            Destroy(crabInterface.gameObject);

            Time.timeScale = 1f;
            interactMan.UnlockInteract(true);
            input.SetCursorState(true);
        }
    }

    public void AddWord(string newWord)
    {
        interactMan = GetComponent<InteractManager>();
        input = GetComponent<StarterAssetsInputs>();
        newWord = newWord.TrimEnd('!', '.', ',', '?', ';', ':').ToLower();
        newWord = Char.ToUpper(newWord[0]) + newWord.Substring(1);

        if (logEntries.Exists(entry => entry.wordOfInterest == newWord))
            return;

        logEntries.Add(new LogEntry { wordOfInterest = newWord, userDefinition = "" });
        UpdateLog();
    }


    public void UpdateLog()
    {
        foreach (Transform child in contentPanel)
        {
            Destroy(child.gameObject);
        }

        foreach (var entry in logEntries.OrderBy(e => e.wordOfInterest))
        {
            GameObject logInstance = Instantiate(logPrefab, contentPanel);
            Button button = logInstance.GetComponent<Button>();
            LogUI logUI = logInstance.GetComponent<LogUI>();
            logUI.Setup(entry.wordOfInterest, entry.userDefinition, this);
            button.onClick.AddListener(() => OnWordClicked(entry));
        }
    }

    private void OnWordClicked(LogEntry entry)
    {
        if (crabInterface != null)
        {
            crabInterface.AddWordToBoard(entry.wordOfInterest);
        }

        if (GameManager.Instance != null)
        {
            GameManager.Instance.PlaySoundForWord(entry.wordOfInterest);
        }
    }

    public void UpdateUserDefinition(string word, string newDefinition)
    {
        LogEntry entry = logEntries.Find(e => e.wordOfInterest == word);
        if (entry != null)
        {
            entry.userDefinition = newDefinition;
        }
    }

    public void StartLogManager()
    {

    }

    public void UnlockLog(bool unlock)
    {
        canOpenLog = unlock;
    }
}