UnityGameProjectsCode / NightwatchGame / GameManager.cs
GameManager.cs
Raw
using System.Collections;
using UnityEngine;

public class GameManager : MonoBehaviour
{
    //setup the game and control major events that occur for the story
    private MachineRandomizer mRandomizer;
    private AmbianceControl ambControl;
    public VendingMachine vMachine;
    public Computer computer;
    public TrashBin bin;
    public ElectricBox elecBox;
    public OfficeRadio officeRadio;
    public FlashLightPickup flashLight;
    public PlayerEndMovement pEndMovement;
    public PlayerLookingAt pLooking;
    public PlayerController pControl;

    public DoorUse frontDoor, backDoor;
    public GameObject[] allLights = new GameObject[13];
    public Animator flashLightAnim;
    public BoxCollider dumpsterTrigger;
    public AudioClip doorBreakAudio;

    public Material lightOnMat, lightOffMat, wallNoteDark, wallNoteLight, wallNote1Dark, wallNote1Light, wallNote2Dark, wallNote2Light;
    public GameObject humanProfileInside, humanProfileOutside, interactionUI, startMenuCanvas, girlRunningAudio, flockOfBirds, birdTakeOffAudio, glassBreakPlane, wallNote, wallNote1, wallNote2,
    objectsToMove, movedObjects;
    public PlayerEventTrigger computerOnTrigger, returnFromDumpsterTrigger, headingBackTrigger, seePersonTrigger, returnFromOutsideTrigger;
    public AudioSource frontDoorUseSource, powerDownSource, frontDoorKnockSource, personRunningSource, runningInsideSource, runningInsideBreathingSource, doorBreakSource,
        musicSource, acAudioSource, forestWalkAudio, suspenseMusic, rearDoorBreathing;
    private bool firstSetStarted, vendingMachineOnFirstTime, computerOnFirstTime, frontDoorAudioPlayed, lightsTurnedOff, thirdSetStarted,
    elecBoxUsed, elecBoxBroke, frontDoorKnockPlayed, personRanAway, doorBroken, insideRunningStarted, backDoorLockedFinal, forestWalkPlayed,
    doorBreakRoutineRunning, gamePaused, frontDoorKnockRoutineRunning;
    public float computerOnWaitTime, vendingMachineWaitTime, forestWalkDelay;

    private void Awake()
    {
        mRandomizer = GetComponent<MachineRandomizer>();
        ambControl = GetComponent<AmbianceControl>();
        flockOfBirds.SetActive(false);
    }

    private void Start()
    {
        interactionUI.SetActive(false);
        backDoor.LockDoor(true);
        elecBox.BoxEnabled = false;
    }

    private void Update()
    {
        if (startMenuCanvas == null && !gamePaused)
        {
            // Run the first set of washers and dryers
            if (!firstSetStarted)
            {
                StartCoroutine(mRandomizer.RunSet());
                firstSetStarted = true;
            }

            //Once all machines are stopped, turn on the vending machine

            if (!vendingMachineOnFirstTime && mRandomizer.CheckAllMachinesStopped() && mRandomizer.GetActiveRunSetComplete())
            {
                StartCoroutine(turnOnVendingMachineDelay());

                computerOnTrigger.gameObject.SetActive(true);

                vendingMachineOnFirstTime = true;

                suspenseMusic.volume = 0.3f;
            }

            //Turn on the computer message and enable the trash bin

            if (!computerOnFirstTime && computerOnTrigger.Triggered)
            {
                StartCoroutine(startComputerWait());

                computerOnFirstTime = true;
            }

            //Once the bin is dropped at the dumpster and the player is in the area, setup for the front door scare

            if (bin.PickedUp && !bin.InDumpsterArea && ambControl.GetPlayingInsideAmb() == false && !forestWalkPlayed)
            {
                forestWalkPlayed = true;
                StartCoroutine(delayForestWalk());
                suspenseMusic.volume = 0.35f;
            }

            if (!bin.PickedUp && bin.dumpsterBin.activeSelf && bin.InDumpsterArea)
            {
                returnFromDumpsterTrigger.gameObject.SetActive(true);
                bin.TrashBinEnabled = false;
            }

            //lock the front door once the player gets close by and then start the second set of machines

            if (returnFromDumpsterTrigger.Triggered && !frontDoorAudioPlayed)
            {
                StartCoroutine(LockFrontDoor());

                headingBackTrigger.gameObject.SetActive(true);
                humanProfileInside.SetActive(true);
                suspenseMusic.volume = 0.45f;

                StartCoroutine(mRandomizer.RunSet());
            }

            //As the player heads back, turn off all lights, start music, and trigger the forest birds and screaming scare

            if (headingBackTrigger.Triggered && !lightsTurnedOff && !elecBoxUsed)
            {
                foreach (GameObject light in allLights)
                {
                    light.GetComponent<AudioSource>().Stop();

                    foreach (Transform childObject in light.GetComponentsInChildren<Transform>())
                    {
                        if (childObject.name == "Point Light")
                        {
                            if (childObject.GetComponent<Animator>())
                                childObject.GetComponent<Animator>().Play("Cieling Light Flicker");
                            else
                                childObject.GetComponent<Light>().enabled = false;
                        }


                        if (childObject.name.Contains("Tube"))
                            childObject.GetComponent<MeshRenderer>().material = lightOffMat;
                    }
                }

                wallNote.GetComponent<MeshRenderer>().material = wallNoteDark;
                wallNote1.GetComponent<MeshRenderer>().material = wallNote1Dark;
                wallNote2.GetComponent<MeshRenderer>().material = wallNote2Dark;

                humanProfileInside.SetActive(false);
                girlRunningAudio.GetComponent<AudioSource>().Play();
                girlRunningAudio.GetComponent<Animator>().Play("Forest Running");
                flockOfBirds.SetActive(true);
                flockOfBirds.GetComponent<Animator>().Play("Birds Flying Away");
                birdTakeOffAudio.SetActive(true);

                foreach (Transform child in objectsToMove.GetComponentsInChildren<Transform>())
                {
                    child.gameObject.SetActive(false);
                }

                movedObjects.SetActive(true);

                lightsTurnedOff = true;

                foreach (AudioSource audioSource in powerDownSource.GetComponentsInChildren<AudioSource>())
                {
                    audioSource.Play();
                }

                musicSource.Play();
                musicSource.time = 15f;

                mRandomizer.PauseRandomizer();

                elecBox.BoxEnabled = true;
                elecBox.StartBoxesGlow();
                computer.TurnOffComputer();
                vMachine.TurnOffMachine();
                backDoor.LockDoor(false);
                returnFromOutsideTrigger.gameObject.SetActive(true);
                officeRadio.SetPitch(0.86f);
                suspenseMusic.volume = 0.55f;

                acAudioSource.Stop();
            }

            //Lock the back door once the player is inside

            if (returnFromOutsideTrigger.Triggered && !backDoorLockedFinal)
            {
                flockOfBirds.SetActive(false);
                backDoor.CloseDoor();
                backDoor.LockDoor(true);
                backDoorLockedFinal = true;
            }

            //Once the 2nd set is complete, start the third set

            if (!lightsTurnedOff && frontDoor.GetDoorLockedState() && backDoor.GetDoorLockedState() && !thirdSetStarted && returnFromOutsideTrigger.Triggered && mRandomizer.GetActiveRunSetComplete())
            {
                StartCoroutine(mRandomizer.RunSet());

                thirdSetStarted = true;
            }

            //Once every machine is running or at least 90%, turn off the power completely (all machine lights, and building lights.
            //play a sound for the electric box breaking

            if (mRandomizer.GetRunsCompleted() == 2 && mRandomizer.MostMachinesRunning() && !elecBoxBroke && returnFromOutsideTrigger.Triggered)
            {
                foreach (GameObject light in allLights)
                {
                    light.GetComponent<AudioSource>().Stop();

                    foreach (Transform childObject in light.GetComponentsInChildren<Transform>())
                    {
                        if (childObject.name == "Point Light")
                        {
                            childObject.GetComponent<Light>().enabled = false;

                            if (childObject.GetComponent<Animator>())
                                childObject.GetComponent<Animator>().enabled = false;
                        }

                        if (childObject.name.Contains("Tube"))
                            childObject.GetComponent<MeshRenderer>().material = lightOffMat;
                    }
                }

                lightsTurnedOff = true;

                foreach (AudioSource audioSource in powerDownSource.GetComponentsInChildren<AudioSource>())
                {
                    audioSource.Play();
                }

                wallNote.GetComponent<MeshRenderer>().material = wallNoteDark;
                wallNote1.GetComponent<MeshRenderer>().material = wallNote1Dark;
                wallNote2.GetComponent<MeshRenderer>().material = wallNote2Dark;

                flashLightAnim.Play("Light Flicker", 0);
                flashLight.SetFlashLightActive(true);
                elecBoxBroke = true;
                suspenseMusic.volume = 0.65f;

                acAudioSource.Stop();

                mRandomizer.PauseRandomizer();
                vMachine.TurnOffMachine();
            }

            if (elecBoxBroke && pLooking.GetObjectName().Contains("Electric Box") && pLooking.GetDistanceToObject() <= 5 && !frontDoorKnockPlayed &&
                pControl.GetFlashlightState() && !frontDoorKnockRoutineRunning && returnFromOutsideTrigger.Triggered)
            {
                StartCoroutine(FrontDoorKnockDelay());
            }

            //Once the player is close enough to see the person, play the scare
            //Turn on the computer flashing animation

            if (seePersonTrigger.Triggered && !personRanAway && !frontDoorKnockRoutineRunning && returnFromOutsideTrigger.Triggered)
            {
                //trigger audio for person running and disable the profile object
                glassBreakPlane.SetActive(true);
                humanProfileOutside.GetComponent<Animator>().Play("Profile Slide Away");
                humanProfileOutside.GetComponent<AudioSource>().Stop();
                personRunningSource.Play();
                personRunningSource.GetComponent<Animator>().Play("Running Audio Movement");
                officeRadio.SetPitch(0.84f);
                suspenseMusic.volume = 0.8f;
                personRanAway = true;
                rearDoorBreathing.Play();

                StartCoroutine(endingComputerWait());
            }

            //once the player is near the computer and looking at the screen, freeze their movement

            if (personRanAway && !doorBroken && !personRunningSource.isPlaying && computer.GetPlayerNearby() && (pLooking.GetObjectName() == "Screen" || pLooking.GetObjectName() == "Computer")
                && returnFromOutsideTrigger.Triggered) //check if the player is looking at the computer and is near it
            {
                humanProfileOutside.SetActive(false);
                StartCoroutine(RearDoorBreak());
                doorBroken = true;
                pControl.SetFreezePlayer(true);
                pControl.TurnOnAnimator();
                officeRadio.SetPitch(0.8f);
                officeRadio.SetVolume(0.8f);
                pControl.DisableCursorLockForEnd();
                rearDoorBreathing.Stop();
                pEndMovement.enabled = true;
            }

            //break down the back door play steps.
            //Running animation brings up credits screen.

            if (doorBroken && !doorBreakRoutineRunning && !insideRunningStarted && returnFromOutsideTrigger.Triggered)
            {
                runningInsideSource.Play();
                runningInsideSource.GetComponent<Animator>().Play("Running Inside Movement");
                runningInsideBreathingSource.Play();
                insideRunningStarted = true;
                computer.StartComputerFlashing(false);
            }
        }
    }

    public void TurnLightsOn()
    {
        foreach (GameObject light in allLights)
        {
            foreach (Transform childObject in light.GetComponentsInChildren<Transform>())
            {
                light.GetComponent<AudioSource>().Play();

                if (childObject.name == "Point Light")
                    childObject.GetComponentInChildren<Light>().enabled = true;

                if (childObject.name.Contains("Tube"))
                    childObject.GetComponent<MeshRenderer>().material = lightOnMat;
            }
        }

        lightsTurnedOff = false;

        foreach (AudioSource audioSource in powerDownSource.GetComponentsInChildren<AudioSource>())
        {
            audioSource.pitch = 1.5f;
            audioSource.Play();
        }

        wallNote.GetComponent<MeshRenderer>().material = wallNoteLight;
        wallNote1.GetComponent<MeshRenderer>().material = wallNote1Light;
        wallNote2.GetComponent<MeshRenderer>().material = wallNote2Light;

        mRandomizer.TurnOnAllMachines();
        mRandomizer.ResumeSet();
        vMachine.StartMachine();

        acAudioSource.Play();

        elecBoxUsed = true;
    }

    public IEnumerator RearDoorBreak()
    {
        //knocking on back door
        doorBreakRoutineRunning = true;
        doorBreakSource.Play();

        yield return new WaitForSecondsRealtime(2.5f);

        //breaking door open
        doorBreakSource.clip = doorBreakAudio;
        doorBreakSource.Play();
        officeRadio.SetVolume(0.9f);
        pEndMovement.SetStartAnimation();
        suspenseMusic.volume = 1;

        //waiting before running to build suspense
        yield return new WaitForSecondsRealtime(3f);

        doorBreakRoutineRunning = false;
    }

    public IEnumerator FrontDoorKnockDelay()
    {
        frontDoorKnockRoutineRunning = true;

        elecBox.BreakBox();

        yield return new WaitForSecondsRealtime(4f);

        frontDoorKnockSource.Play();
        frontDoorKnockPlayed = true;
        humanProfileOutside.SetActive(true);
        humanProfileOutside.GetComponent<Animator>().Play("Profile Head Banging");
        suspenseMusic.volume = 0.7f;

        seePersonTrigger.gameObject.SetActive(true);

        frontDoorKnockRoutineRunning = false;
    }

    public IEnumerator startComputerWait()
    {
        yield return new WaitForSecondsRealtime(computerOnWaitTime);

        computer.TurnOnComputer();
        bin.TrashBinEnabled = true;
        dumpsterTrigger.enabled = true;
    }

    public IEnumerator turnOnVendingMachineDelay()
    {
        yield return new WaitForSecondsRealtime(vendingMachineWaitTime);

        if (!vMachine.MachineRunning)
        {
            vMachine.StartMachine();
        }
    }

    public IEnumerator delayForestWalk()
    {
        yield return new WaitForSecondsRealtime(forestWalkDelay);

        forestWalkAudio.Play();
    }

    public IEnumerator endingComputerWait()
    {
        yield return new WaitForSecondsRealtime(5);

        computer.TurnOnComputer();
        computer.ChangeCanvasText("01000011 01100001 01110010 01101110 01100001 01101100 00100000 01000011 01010010 01010100 00100000 01000011 01101111 01101110 01100110 01101001 01100100 01100101 01101110 01110100 " +
            "01101001 01100001 01101100 00111010 00001101 00001010 01000011 01100001 01110000 01110100 01110101 01110010 01100101 00100000 01010011 01110101 01100010 01101010 01100101 01100011 01110100 00100000 00110000 " +
            "00110000 00110011 00110001 00101110 00100000 01001101 01101001 01110011 01110011 01101001 01101111 01101110 00100000 01000011 01110010 01101001 01110100 01101001 01100011 01100001 01101100 00101110", 0.039f);
        computer.StartComputerFlashing(true);
    }

    public IEnumerator LockFrontDoor()
    {
        frontDoor.CloseDoor();
        frontDoorAudioPlayed = true;

        yield return new WaitForSecondsRealtime(1);

        frontDoor.LockDoor(true);
        frontDoorUseSource.Play();
    }

    public void PauseGame(bool state)
    {
        gamePaused = state;
    }
}