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

public class FoodControl : MonoBehaviour
{

    private MeshCollider _collider;
    private Rigidbody _rigidbody;

    public float angle = 60;
    public float Force = 50;
    public float faddfroce = 7f;
    public bool IsFroce=false;
    private Vector3 dir;

    public float offestspeed = 1f;
    public float OffestX = 0f;
    public float OffestZ = 0f;


    public bool vegetables = false;
    void Start()
    {
        _collider = this.GetComponent<MeshCollider>() ? this.GetComponent<MeshCollider>() : this.gameObject.AddComponent<MeshCollider>();
        _collider.convex = true;
        _rigidbody = this.GetComponent<Rigidbody>() ? this.GetComponent<Rigidbody>() : this.gameObject.AddComponent<Rigidbody>();
        dir = new Vector3(0, Mathf.Tan(angle* (Mathf.PI * 2 / 360)),1);
      
    }

    void Update()
    {
     
    }
    private void FixedUpdate()
    {
        if (IsFroce)
        {
            AddForece();
        }
        if (isRotation==true ) 
        {
            this.transform.Rotate(Vector3.left*SpeedRotation*Time.deltaTime);
        }
    }

    public float SpeedRotation = 10;
     void AddForece() 
    {
        this.gameObject.GetComponent<Rigidbody>().AddForce(dir * Force * Time.deltaTime * 100f);
        IsFroce = false;
    }

    public void friction(Vector3 dirs,float speed) 
    {

        if (ISF)
        {
            this.transform.position = Vector3.MoveTowards(this.transform.position, new Vector3(dirs.x+OffestX, this.transform.position.y, dirs.z+OffestZ), speed*0.1F * Time.deltaTime*offestspeed);       
        }
    }

    public bool ISF = true;


    private bool isRotation = false;
  

    private void OnCollisionEnter(Collision collision)
    {
   
        if (collision.transform.tag=="guo") 
        {
            ISF = false;
        }

        if (collision.transform.tag=="Player") 
        {
            isRotation = false;
        }
        if (vegetables == true)
        {
            if (collision.transform.tag == "food")
            {
                ISF = false;
            }
        }
    }
    private void OnCollisionExit(Collision collision)
    {
        if (collision.transform.tag=="guo") 
        {
            ISF = true;
        }
        if (collision.transform.tag == "Player")
        {
            isRotation = true;
        }

       
    }
}
