UnityGameProjectsCode / Rise2Point0Game / Obstacles / DeathWall.cs
DeathWall.cs
Raw
using System.Collections.Generic;
using UnityEngine;

public class DeathWall : MonoBehaviour
{
    public float movementSpeed;
    public float movementIncrease;
    public float timeBeforeMovementStart;
    public float movementThreshold;
    public float timeBetweenBubbles;
    public GameObject bubblesPrefab;
    public GameObject killBarrier;
    private float timeSinceStart;
    private Vector2 startPosition;
    private GameObject deathPanel;
    private float timeSinceBubble;
    private List<GameObject> lastBubbles = new List<GameObject>();
    private AudioSource aSource;
    public ParticleSystem sparkParticles;
    public ParticleSystem cloudParticles;
    public GameObject chillSprite;

    private bool disableDeathWall;
    public bool enableBubbles;
    private bool cameraFollowingWall;

    private ScoreManager scoreManager;

    private void Awake()
    {
        startPosition = transform.position;
        scoreManager = GameObject.Find("Persistent Object").GetComponent<ScoreManager>();
        aSource = GetComponent<AudioSource>();
    }

    private void Update()
    {
        if (Time.timeScale == 1)
        {
            float playerHeight = scoreManager.GetHighestHeight();

            //does not work
            if (playerHeight < transform.position.y && cameraFollowingWall == false && GetComponent<SpriteRenderer>().enabled == true)
            {
                GUIManager.FollowDeathWall();
                cameraFollowingWall = true;
            }

            if (playerHeight > movementThreshold && timeSinceStart < timeBeforeMovementStart)
                timeSinceStart += Time.deltaTime;

            if (timeSinceStart > 0.0f && !aSource.isPlaying && !disableDeathWall)
            {
                aSource.Play();
                sparkParticles.Play();
                cloudParticles.Play();
            }


            if (timeSinceStart >= timeBeforeMovementStart)
            {
                float speedIncrease = 0;

                if (!disableDeathWall)
                {
                    for (int i = 10; i < playerHeight; i += 10)
                    {
                        speedIncrease += movementIncrease;
                    }

                    speedIncrease += movementSpeed;
                }

                if (playerHeight - transform.position.y >= 160)
                    speedIncrease += 80;

                transform.position = new Vector2(0, transform.position.y + speedIncrease);
            }

            if (!disableDeathWall && enableBubbles)
            {
                timeSinceBubble += Time.deltaTime;

                if (timeSinceBubble >= timeBetweenBubbles)
                {
                    timeSinceBubble = 0.0f;

                    int listCount = lastBubbles.Count - 1;

                    for (int i = listCount; i >= 0; i--)
                    {
                        Destroy(lastBubbles[i]);
                    }

                    lastBubbles.Clear();

                    SpawnBubble();
                }
            }

        }
    }

    public void SetDisabledState(bool newState)
    {
        disableDeathWall = newState;

        if (disableDeathWall)
        {
            GetComponent<SpriteRenderer>().enabled = false;
            killBarrier.GetComponent<BoxCollider2D>().isTrigger = false;
            chillSprite.SetActive(true);
        }
        else
        {
            GetComponent<SpriteRenderer>().enabled = true;
            killBarrier.GetComponent<BoxCollider2D>().isTrigger = true;
            chillSprite.SetActive(false);
        }

    }

    public void ResetPosition()
    {
        transform.position = startPosition;
        timeSinceStart = 0;
        aSource.Stop();
        sparkParticles.Stop();
        cloudParticles.Stop();
        cloudParticles.Clear();
        sparkParticles.Clear();
        cameraFollowingWall = false;
    }

    public void SpawnBubble()
    {
        List<GameObject> newBubbles = new List<GameObject>();

        int randomBubbleCount = Random.Range(0, 4);

        for (int i = 0; i <= randomBubbleCount; i++)
        {
            Vector2 newBubblePosition = new Vector2(Random.Range(-1.4f, 1.4f), 0.03f);

            GameObject newBubble = Instantiate(bubblesPrefab as GameObject, transform);
            newBubble.transform.localPosition = newBubblePosition;
            newBubble.GetComponent<Animator>().SetFloat("speedMultiplier", Random.Range(0.8f, 1.2f));

            newBubbles.Add(newBubble);
        }

        lastBubbles = newBubbles;
    }
}