UnityGameProjectsCode / NightwatchGame / ElectricBox.cs
ElectricBox.cs
Raw
using UnityEngine;

public class ElectricBox : MonoBehaviour
{
    private GameManager gameMgr;
    private AudioSource aSource;
    public ParticleSystem sparks;
    public GameObject interactionUI;
    public InteractionTextControl textControl;
    private Animator boxAnim;
    public PlayerController pControl;
    private bool boxEnabled;
    private bool playerNearby;

    public bool BoxEnabled { get => boxEnabled; set => boxEnabled = value; }

    private void Awake()
    {
        gameMgr = GameObject.FindGameObjectWithTag("GameController").GetComponent<GameManager>();
        aSource = GetComponent<AudioSource>();
        boxAnim = GetComponent<Animator>();
    }

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

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

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.E) && boxEnabled && playerNearby)
        {
            gameMgr.TurnLightsOn();
            StopBoxesGlow();
            boxEnabled = false;
        }

        if (Input.GetKeyDown(KeyCode.E) && !boxEnabled && playerNearby)
        {
            textControl.DisplayText("Thermostat and fuse boxes.");
        }

        if (Input.GetKeyDown(KeyCode.E) && !boxEnabled && !sparks.isPlaying && playerNearby && !pControl.GetFlashlightState())
        {
            textControl.DisplayText("Need a flashlight.");
        }

        if (Input.GetKeyDown(KeyCode.E) && !boxEnabled && sparks.isPlaying && playerNearby && pControl.GetFlashlightState())
        {
            textControl.DisplayText("Not fixing that.");
        }
    }

    public void BreakBox()
    {
        aSource.Play();
        sparks.Play();
    }

    public void StartBoxesGlow()
    {
        boxAnim.Play("Fuse Boxes Glow", 0, 0);
    }

    public void StopBoxesGlow()
    {
        boxAnim.Play("Idle", 0, 0);
    }
}