UnityGameProjectsCode / NightwatchGame / Computer.cs
Computer.cs
Raw
using System.Collections;
using TMPro;
using UnityEngine;

public class Computer : MonoBehaviour
{
    public GameObject screen;
    public AudioSource computerStartingSound, computerRunningSound;
    public Canvas screenTextCanvas;
    public InteractionTextControl textControl;
    public GameObject interactionUI;
    public GameObject screenScanLines;
    public GameObject screenCrack;
    private Animator animControl;
    public Material screenOn, screenOff, screenDark, screenOnGreen;
    public Light screenLight;
    private bool machineStarted;
    private bool playerNearby;
    private bool flashingActive;
    private bool currentlyFlashing;

    public bool MachineStarted { get => machineStarted; set => machineStarted = value; }

    private void Awake()
    {
        animControl = GetComponent<Animator>();
        animControl.enabled = false;
    }

    private void OnTriggerEnter(Collider other)
    {
        playerNearby = true;
        interactionUI.SetActive(true);
    }

    private void OnTriggerExit(Collider other)
    {
        playerNearby = false;
        interactionUI.SetActive(false);
    }

    private void Update()
    {
        if (flashingActive && !currentlyFlashing)
        {
            StartCoroutine(FlashScreen());
        }

        if (playerNearby && Input.GetKeyDown(KeyCode.E))
        {
            textControl.DisplayText("An old work computer.");
        }
    }

    public void ChangeCanvasText(string newText, float textSize)
    {
        screenTextCanvas.GetComponentInChildren<TextMeshProUGUI>().text = newText;
        screenTextCanvas.GetComponentInChildren<TextMeshProUGUI>().fontSize = textSize;
    }

    public void TurnOnComputer()
    {
        //turn on screen, play a sound, and display the text
        computerStartingSound.Play();
        screen.GetComponent<MeshRenderer>().material = screenOn;
        machineStarted = true;
        screenLight.intensity = 1;

        StartCoroutine(WaitBeforeRunning());
    }

    public void TurnOffComputer()
    {
        StartCoroutine(WaitBeforeShutdown());
    }

    public void StartComputerFlashing(bool state)
    {
        flashingActive = state;
        screenLight.color = Color.green;
        screenScanLines.SetActive(true);
    }

    public bool GetPlayerNearby()
    {
        return playerNearby;
    }

    private void SetDarkScreenMaterial()
    {
        screen.GetComponent<MeshRenderer>().material = screenDark;
    }

    private void SetLightScreenMaterial()
    {
        screen.GetComponent<MeshRenderer>().material = screenOnGreen;
    }

    public IEnumerator FlashScreen()
    {
        currentlyFlashing = true;

        computerStartingSound.Play();

        yield return new WaitForSecondsRealtime(1);

        screen.GetComponent<MeshRenderer>().material = screenDark;
        screenLight.intensity = 0.5f;

        yield return new WaitForSecondsRealtime(1);

        screen.GetComponent<MeshRenderer>().material = screenOnGreen;
        screenLight.intensity = 1;

        currentlyFlashing = false;
    }

    public IEnumerator WaitBeforeRunning()
    {
        yield return new WaitForSecondsRealtime(1.5f);

        computerRunningSound.Play();
        screenTextCanvas.enabled = true;

    }

    public IEnumerator WaitBeforeShutdown()
    {
        screen.GetComponent<MeshRenderer>().material = screenOff;
        machineStarted = false;
        screenTextCanvas.enabled = false;
        screenLight.intensity = 0;

        yield return new WaitForSecondsRealtime(3);

        computerRunningSound.Stop();
    }

    public void CrackScreen()
    {
        screenCrack.SetActive(true);
        screen.GetComponent<MeshRenderer>().material = screenDark;
        screenLight.intensity = 0.5f;
        animControl.enabled = true;
    }
}