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

public class WingAnimation : MonoBehaviour {
    public GameObject frontWing;
    public GameObject backWing;

    public float animationFrame = 0;
    public float animationSpeed = 40;

    public Vector3 lastPosition = new Vector3();

    public float positionalSpeed = 0;

    void Start() {
        lastPosition = transform.position;
    }

    void Update() {
        positionalSpeed = Vector2.Distance(lastPosition, transform.position);
	
		lastPosition = transform.position;

        animationFrame += positionalSpeed * animationSpeed;

      
        float scaleX = Mathf.Cos(animationFrame);
        float scaleY = 1; //Mathf.Sin(animationFrame);

        Vector2 oldScale = transform.localScale;

        if (oldScale.x > 0.1f && scaleX <= 0.1f) {       
            AudioSystem.Play((AudioSound)Random.Range(0, 5));
        }
        transform.localScale = new Vector3(scaleX, scaleY, 1);

        
        
        PlayerController player = PlayerController.Get();
        if (player.isGrounded) {
            frontWing.SetActive(false);
            backWing.SetActive(false);
        } else {
            frontWing.SetActive(true);
            backWing.SetActive(true);
        }
    }
}
