Untitled

mail@pastecode.io avatar
unknown
plain_text
12 days ago
3.2 kB
1
Indexable
Never
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Spawner : MonoBehaviour
{
    [Header("Transforms")]
    [SerializeField] Transform pipeSpawnPoint;
    [SerializeField] Transform pipeEndPoint;

    [Header("Spawn Rules")]
    [SerializeField, Range(0f, 10f)] float movementSpeed;
    [SerializeField, Range(0f, 15f)] float verticalPipeSpacing;
    [SerializeField, Range(0f, 10f)] float maxOffset;
    [SerializeField, Range(0f, 10f)] float spawnDelay;

    [Header("Pipes")]
    [SerializeField] Transform topPipe;
    [SerializeField] Transform bottomPipe;
    List<Transform> currentPipes = new List<Transform>();    

    void Start()
    {
        SpawnPipes();
    }

    void Update()
    {
        MovePipes();
        ManagePipes();        
    }

    void SpawnPipes()
    {   
        //Update the values of the current pipes array with new bottom and top pipes,
        //spaced "verticalPipeSpacing" units away from eachother and centered on the screen.

        Transform currentBottomPipe = Instantiate(bottomPipe, new Vector3(pipeSpawnPoint.position.x, -(verticalPipeSpacing / 2f), 0f),
        Quaternion.identity, this.transform);
        Transform currentTopPipe = Instantiate(topPipe, new Vector3(pipeSpawnPoint.position.x, currentBottomPipe.position.y + verticalPipeSpacing, 0f),
        Quaternion.identity, this.transform);

        currentPipes.Insert(0, currentBottomPipe);
        currentPipes.Insert(1, currentTopPipe);

        OffsetPipes();

        StartCoroutine(SpawnPipesWithDelay(spawnDelay));
    }

    void OffsetPipes()
    {
        float randomOffset = UnityEngine.Random.Range(0f, maxOffset);

        //Pick randomly between 1 and -1.
        int multiplier;
        do
        {
            multiplier = UnityEngine.Random.Range(-1, 2);
        }
        while (multiplier == 0);
            
        float finalOffset = randomOffset * multiplier;

        //Offset the last two pipes only.
        for (int i = 0; i < 2; i++)
        {
            currentPipes[i].position = new Vector3(currentPipes[i].position.x, currentPipes[i].position.y + finalOffset, 0f);
        }
    }

    void MovePipes()
    {
        //Move all bottom and top pipes to the left at a steady pace.
        foreach(Transform pipe in currentPipes)
        {
            pipe.position = new Vector3(pipe.position.x - (movementSpeed * Time.deltaTime), pipe.position.y, 0f);
        }
    }    

    void ManagePipes()
    {
        if (currentPipes[^1].position.x <= pipeEndPoint.position.x)
        {
            //Destroy the last bottom and top pipes.
            for (int i = currentPipes.Count - 2; i < currentPipes.Count; i++)
            {
                Destroy(currentPipes[i].gameObject);               
            }

            currentPipes.RemoveAt(currentPipes.Count - 1);
            currentPipes.RemoveAt(currentPipes.Count - 2);
        }
    }

    IEnumerator SpawnPipesWithDelay(float delay)
    {
        yield return new WaitForSecondsRealtime(delay);
        SpawnPipes();
    }
}
Leave a Comment