Example #1
0
    // This Unity function grabs all keyboard events, among other things.
    void OnGUI()
    {
        // Let's make sure this is a keyboard event, specifically a NEW keystroke.
        if (Event.current.isKey && Event.current.type == EventType.KeyDown)
        {
            if (WordComponent.keycodeDict.ContainsValue(Event.current.keyCode))
            {
                //This is a letter. Do we have a locked-in word?
                if (LockedInWord != null)
                {
                    //Is it the correct letter?
                    if (Event.current.keyCode == LockedInWord.GetNextLetter())
                    {
                        st.ChangeLettersTyped(1);
                        LockedInWord.NextLetterTyped();
                    }
                    else
                    {
                        // WRONG!
                        CountFail();
                    }
                }
                else
                {
                    // No active word, let's find one!
                    foreach (WordComponent word in ActiveWords)
                    {
                        if (word.enabled == false)
                        {
                            // Don't grab onto words that are disabled.
                            continue;
                        }

                        if (word.GetNextLetter() == Event.current.keyCode)
                        {
                            // This word starts with the typed letter, it's now active.
                            st.ChangeLettersTyped(1);
                            word.NextLetterTyped();
                            break;
                        }
                    }
                    if (LockedInWord == null)
                    {
                        CountFail();
                    }
                }
            }
        }
    }