UnityGameProjectsCode / InTheDarkGame / Terminal.cs
Terminal.cs
Raw
using Cinemachine;
using UnityEngine;
using UnityEngine.Experimental.Rendering.Universal;

public class Terminal : MonoBehaviour
{
    /* In each level used, the terminal will move the camera to a given object for a set period of time
     * Text related to the tutorial will appear and fade out
     * The camera will be moved back to the player once the tutorial is finished
     * 
     * If the stagedTutorial bool is true, the tutorial will be done in parts
     * each part will have its own follow target and highlights
     * each part will also have its own run time
     * the staged tutorial will start by moving the camera to the first follow target and make use of the first highlight
     * it will also turn on any secondary highlights that are connected to the stage
     * once in the stage, it will count up to the stages run time and then move on to the next stage
     * it will continue this until all stages have been completed
     * Once complete, it will return control to the player and reset the camera
     */

    private CinemachineVirtualCamera vCam;
    private KeyCode interactionKey;
    private KeyCode escapeMenuKey;
    private PlayerMove pMove;
    private GUIManager guiMgr;
    private AudioSource terminalAudio;
    private Light2D screenLight;

    private float currentFlashTime;
    private bool playedTut;

    private float currentPlayTime;
    private bool playingTut;
    private bool playerInTrigger;

    [Header("General Variables")]
    public GameObject[] tutText;
    public float flashTime;
    public AudioClip useSound;
    public AudioClip notificationSound;
    public SpriteRenderer interactKeySprite;

    [Header("Solo Variables")]
    public GameObject highlighter;
    public GameObject[] secondaryHighlights;
    public GameObject tutFollowTarget;
    public float tutPlayTime;

    [Header("Staged Variables")]
    public bool stagedTutorial; //if the tutorial is setup in multiple parts
    public int numberOfStages; //how many transitions will happen in the tutorial
    public float[] stageTimes; // how much time should each stage take individually
    public GameObject[] stageFollowTargets; //each target for each stage of the tutorial
    public GameObject[] stageOneHighlights;
    public GameObject[] stageTwoHighlights;
    public GameObject[] stageThreeHighlights;
    private int currentStage = 0;

    private void Awake()
    {
        vCam = GameObject.Find("Main Camera").GetComponent<CinemachineVirtualCamera>();
        pMove = GameObject.Find("Player Object").GetComponent<PlayerMove>();
        interactionKey = InputManager.GetInputKey(5);
        escapeMenuKey = InputManager.GetInputKey(7);
        highlighter.SetActive(false);
        guiMgr = GameObject.Find("PersistentObject").GetComponent<GUIManager>();
        terminalAudio = GetComponent<AudioSource>();
        screenLight = gameObject.transform.parent.GetComponentInChildren<Light2D>();
        interactKeySprite.enabled = false;

        foreach (GameObject highlight in secondaryHighlights)
        {
            highlight.SetActive(false);
        }

        if (stagedTutorial)
        {
            for (int i = 0; i < 3; i++)
            {
                switch (i)
                {
                    case 0:
                        foreach (GameObject highlight in stageOneHighlights)
                        {
                            highlight.SetActive(false);
                        }
                        break;
                    case 1:
                        foreach (GameObject highlight in stageTwoHighlights)
                        {
                            highlight.SetActive(false);
                        }
                        break;
                    case 2:
                        foreach (GameObject highlight in stageThreeHighlights)
                        {
                            highlight.SetActive(false);
                        }
                        break;
                }
            }
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("Player"))
        {
            interactKeySprite.enabled = true;
            playerInTrigger = true;
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("Player"))
        {
            interactKeySprite.enabled = false;
            playerInTrigger = false;
        }
    }

    private void Update()
    {
        if(!playingTut && !playedTut)
        {
            currentFlashTime += Time.deltaTime;

            if(currentFlashTime >= flashTime)
            {
                terminalAudio.clip = notificationSound;
                terminalAudio.pitch = 1;
                terminalAudio.Play();

                screenLight.enabled = !screenLight.enabled;
                currentFlashTime = 0.0f;
            }
        }

        if(playedTut && !screenLight.enabled)
        {
            screenLight.enabled = true;
        }

        if (Input.GetKeyDown(escapeMenuKey) && !guiMgr.GetEscapeMenuState() && playingTut)
        {
            terminalAudio.clip = useSound;
            terminalAudio.pitch = 0.9f;
            terminalAudio.Play();

            if (!stagedTutorial)
                PlaySoloTutorial(false);
            else
                PlayStagedTutorial(false);
        }

        if (Input.GetKeyDown(interactionKey) && !guiMgr.GetEscapeMenuState() && !playingTut && playerInTrigger)
        {
            terminalAudio.clip = useSound;
            terminalAudio.pitch = 1.1f;
            terminalAudio.Play();
            playedTut = true;

            if (!stagedTutorial)
                PlaySoloTutorial(true);
            else
                PlayStagedTutorial(true);
        }


        if (playingTut && !stagedTutorial)
        {
            currentPlayTime += Time.deltaTime;
            highlighter.transform.position = tutFollowTarget.transform.position;

            if (currentPlayTime >= tutPlayTime)
            {
                PlaySoloTutorial(false);
            }
        }
        else if (playingTut && stagedTutorial)
        {
            currentPlayTime += Time.deltaTime;

            switch (currentStage)
            {
                case 0:
                    stageOneHighlights[0].transform.position = stageFollowTargets[currentStage].transform.position;
                    break;
                case 1:
                    stageTwoHighlights[0].transform.position = stageFollowTargets[currentStage].transform.position;
                    break;
                case 2:
                    stageThreeHighlights[0].transform.position = stageFollowTargets[currentStage].transform.position;
                    break;
            }

            if (currentPlayTime >= stageTimes[currentStage] && currentStage < numberOfStages - 1)
            {
                currentPlayTime = 0.0f;
                currentStage++;
                PlayStagedTutorial(true);
            }
            else if (currentPlayTime >= stageTimes[currentStage] && currentStage >= numberOfStages - 1)
            {
                PlayStagedTutorial(false);
            }
        }
    }

    public void PlaySoloTutorial(bool state)
    {
        if (state)
        {
            playingTut = true;
            guiMgr.SetEscapeMenuState(true);
            vCam.m_Follow = tutFollowTarget.transform;

            foreach (GameObject textItem in tutText)
            {
                textItem.SetActive(true);
                textItem.GetComponent<FadeInAndOut>().ResetTextFade();
            }

            foreach (GameObject highlight in secondaryHighlights)
            {
                highlight.SetActive(true);
            }

            pMove.SetMovementState(true);
            pMove.SetCursorLockState(true);
            highlighter.SetActive(true);
        }
        else
        {
            playingTut = false;
            guiMgr.SetEscapeMenuState(false);
            vCam.m_Follow = pMove.gameObject.transform;

            foreach (GameObject textItem in tutText)
            {
                textItem.SetActive(false);
            }

            foreach (GameObject highlight in secondaryHighlights)
            {
                highlight.SetActive(false);
            }

            pMove.SetMovementState(false);
            pMove.SetCursorLockState(false);
            highlighter.SetActive(false);
            currentPlayTime = 0.0f;
        }
    }

    public void PlayStagedTutorial(bool state)
    {
        if (state)
        {
            playingTut = true;
            guiMgr.SetEscapeMenuState(true);
            vCam.m_Follow = stageFollowTargets[currentStage].transform;

            tutText[currentStage].SetActive(true);
            tutText[currentStage].GetComponent<FadeInAndOut>().ResetTextFade();

            switch (currentStage)
            {
                case 0:
                    foreach (GameObject highlight in stageOneHighlights)
                    {
                        highlight.SetActive(true);
                    }
                    break;
                case 1:
                    foreach (GameObject highlight in stageOneHighlights)
                    {
                        highlight.SetActive(false);
                    }
                    foreach (GameObject highlight in stageTwoHighlights)
                    {
                        highlight.SetActive(true);
                    }
                    break;
                case 2:
                    foreach (GameObject highlight in stageOneHighlights)
                    {
                        highlight.SetActive(false);
                    }
                    foreach (GameObject highlight in stageTwoHighlights)
                    {
                        highlight.SetActive(false);
                    }
                    foreach (GameObject highlight in stageThreeHighlights)
                    {
                        highlight.SetActive(true);
                    }
                    break;
            }

            pMove.SetMovementState(true);
            pMove.SetCursorLockState(true);
        }
        else
        {
            playingTut = false;
            guiMgr.SetEscapeMenuState(false);
            vCam.m_Follow = pMove.gameObject.transform;

            foreach (GameObject textItem in tutText)
            {
                textItem.SetActive(false);
            }

            for (int i = 0; i < 3; i++)
            {
                switch (i)
                {
                    case 0:
                        foreach (GameObject highlight in stageOneHighlights)
                        {
                            highlight.SetActive(false);
                        }
                        break;
                    case 1:
                        foreach (GameObject highlight in stageTwoHighlights)
                        {
                            highlight.SetActive(false);
                        }
                        break;
                    case 2:
                        foreach (GameObject highlight in stageThreeHighlights)
                        {
                            highlight.SetActive(false);
                        }
                        break;
                }
            }

            pMove.SetMovementState(false);
            pMove.SetCursorLockState(false);
            currentPlayTime = 0.0f;
            currentStage = 0;
        }
    }

    public void SetNewKey()
    {
        interactionKey = InputManager.GetInputKey(5);
        escapeMenuKey = InputManager.GetInputKey(7);
    }
}