CTIN 289 - Interactive Snippets Library

Powered by 🌱Roam Garden

LightMorph.cs


This script is a LightMeter that manages the light intensity in the game. The primary functions of this script are:

Awake(): Initializes references to other components such as GameManager, PlayerController, and Beacon.

Start(): Initializes the Vignette and Grain post-processing settings and sets the initial light level.

Update(): Calls several functions to manage light states (draining, charging, and creature draining) and checks for player death.

UpdateLightState(): Determines the current light state based on trigger counts (light triggers and creature triggers).

DrainLight(): Drains the light at a normal rate and updates the Vignette and Grain post-processing settings accordingly.

ChargeLight(): Charges the light and updates the Vignette and Grain post-processing settings accordingly.

UpdateVignette(): Updates the Vignette intensity based on the current light level.

UpdateGrain(): Updates the Grain intensity based on the current light level.

CreatureDrain(): Drains the light at a rate specified for creatures and updates the Vignette post-processing setting accordingly.

CheckDeath(): Checks if the player has died (i.e., when the light level reaches zero) and calls the Respawn() function.

Respawn(): Initiates the player's death and respawning sequence using a coroutine.

Death(): A coroutine that manages the player's death sequence, disabling player movement, stopping sounds, playing the death sound, waiting for 3 seconds, and then resetting the active scene.

OnTriggerEnter(Collider other): Increases the trigger count for light triggers and creature triggers when the player enters the corresponding trigger areas.

OnTriggerExit(Collider other): Decreases the trigger count for light triggers and creature triggers when the player exits the corresponding trigger areas.


using System.Collections.Generic;
using UnityEngine;

public class LightMorph : MonoBehaviour
{
    private List<Flower> flowers;

    public AudioSource wateringAudioSource;
    public AudioSource antAudioSource;
    public AudioClip wateringSound;
    public AudioClip antDeathAudioClip;

    // SMOKE
    public GameObject smokeParticlePrefab;
    public Camera mainCamera;
    private bool inLight = true;
    private bool inLightAndPickedUp = false;

    // ANTS
    public GameObject ant1;
    public GameObject ant2;
    public GameObject ant3;

    // VFX
    [SerializeField] GameObject deathVFX;

    private void Awake()
    {
        flowers = new List<Flower>(FindObjectsOfType<Flower>());
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Transform"))
        {
            inLightAndPickedUp = false;

            Pickup pickup = other.gameObject.GetComponent<Pickup>();

            if (pickup != null)
            {
                pickup.InLight();
                PlaySmokeEffect(pickup);
            }
        }

        if (other.gameObject.CompareTag("Flower"))
        {
            Flower flower = other.GetComponent<Flower>();
            if (flower != null)
            {
                flower.sunSoaking = true;
                PlayWateringSound();
            }
        }

        if (other.gameObject.CompareTag("AntCore1"))
        {
            GameObject deathVFXObject = Instantiate(deathVFX, ant1.transform.position, ant1.transform.rotation);
            Destroy(deathVFXObject, 2f);
            ant1.gameObject.SetActive(false);
            antAudioSource.PlayOneShot(antDeathAudioClip);
        }
        if (other.gameObject.CompareTag("AntCore2"))
        {
            GameObject deathVFXObject = Instantiate(deathVFX, ant2.transform.position, ant2.transform.rotation);
            Destroy(deathVFXObject, 2f);
            ant2.gameObject.SetActive(false);
            antAudioSource.PlayOneShot(antDeathAudioClip);
        }
        if (other.gameObject.CompareTag("AntCore3"))
        {
            GameObject deathVFXObject = Instantiate(deathVFX, ant3.transform.position, ant3.transform.rotation);
            Destroy(deathVFXObject, 2f);
            ant3.gameObject.SetActive(false);
            antAudioSource.PlayOneShot(antDeathAudioClip);
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject.CompareTag("Transform"))
        {
            inLightAndPickedUp = false;

            Pickup pickup = other.gameObject.GetComponent<Pickup>();

            if (pickup != null)
            {
                pickup.OutOfLight();
                PlaySmokeEffect(pickup);
            }
        }

        if (other.gameObject.CompareTag("Flower"))
        {
            Flower flower = other.GetComponent<Flower>();
            if (flower != null)
            {
                flower.sunSoaking = false;
                StopWateringSound();
            }
        }
    }

    private void OnTriggerStay(Collider other)
    {
        if (other.gameObject.CompareTag("Transform"))
        {
            inLightAndPickedUp = true;
        }
    }

    private void PlayWateringSound()
    {
        if (wateringSound != null && !wateringAudioSource.isPlaying)
        {
            wateringAudioSource.PlayOneShot(wateringSound);
        }
    }

    private void StopWateringSound()
    {
        if (wateringSound != null & wateringAudioSource.isPlaying)
        {
            wateringAudioSource.Stop();
        }
    }

    private void PlaySmokeEffect(Pickup pickup)
    {
        if (smokeParticlePrefab != null && !inLightAndPickedUp)
        {
            Vector3 objectPosition = pickup.transform.position;
            Vector3 smokePosition = objectPosition + new Vector3(0, 0, -1f);

            // Calculate the direction from the object to the player
            Vector3 directionToObject = (objectPosition - mainCamera.gameObject.transform.position).normalized;

            GameObject smoke = Instantiate(smokeParticlePrefab, smokePosition, Quaternion.LookRotation((mainCamera.transform.position - smokePosition).normalized));
            smoke.transform.localScale = new Vector3(1f, 1f, 1f); // Scale the smoke effect, adjust the values as needed
            Destroy(smoke, 2f); // Destroy the smoke particle effect after 5 seconds
        }
    }
}