using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WavesController : MonoBehaviour
{
    public Collider2D[] waves;
    public float waveSpeed;
    public Collider2D firstWave;
    public Collider2D lastWave;
    public int direction ;//= 1;

    private Vector2 wavePosition = new Vector2();
    private Vector2 startPosition = new Vector2();
    private Vector2 endPosition = new Vector2();



    void Start()
    {
        firstWave = waves[0];
        lastWave = waves[waves.Length - 1];
    }

    void FixedUpdate()
    {
        foreach (var wave in waves)
        {
            if (direction == 1)
            {
                if (wave.transform.position.x < -wave.bounds.size.x)
                {
                    endPosition.Set(lastWave.transform.position.x + lastWave.bounds.size.x, lastWave.transform.position.y);
                    wave.transform.position = endPosition;
                    lastWave = wave;
                }
            }
            else if (direction == -1)
            {
                if (wave.transform.position.x > wave.bounds.size.x)
                {
                    startPosition.Set(firstWave.transform.position.x - firstWave.bounds.size.x, firstWave.transform.position.y);
                    wave.transform.position = startPosition;
                    firstWave = wave;
                }
            }

            wavePosition.Set(wave.transform.position.x - waveSpeed * Time.deltaTime, wave.transform.position.y);
            wave.transform.position = wavePosition;
        }
    }
}
