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

public class WaterArea : MonoBehaviour
{
    public float Speed = 1;
    public float Frequency = 1;
    [Range(0, 1)]
    public float Amplitude = 0.3f;

    PolygonCollider2D _collider;
    Mesh _mesh;

    private void Awake()
    {
        _collider = GetComponent<PolygonCollider2D>();
        _mesh = GetComponentInChildren<MeshFilter>().mesh;
    }


    void Start()
    {

    }


    void Update()
    {
        List<Vector3> vertices = new List<Vector3>(_mesh.vertices);

        float speedFactor = 0;
        if(Speed != 0)
        {
            speedFactor = 1 / Speed;
        }


        for(int i = 51; i < _mesh.vertexCount; i++)
        {
            Vector3 v = vertices[i];
            vertices[i] = new Vector3(v.x, -0.5f + 0.5f * Amplitude * Mathf.Sin((Time.time + (v.x * speedFactor)) * Frequency), 0);
        }

        _mesh.SetVertices(vertices);


        Vector2[] colliderPoints = _collider.points;

        for(int i = 0; i < colliderPoints.Length - 2; i++)
        {
            colliderPoints[i] = vertices[i + 51];
        }

        _collider.points = colliderPoints;

    }
}
