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

public class MyEnemyController : MonoBehaviour
{

    //֮ģûд



    //ĸ
    public Vector3[] movePoints;
    //лʲôٷ֮٣λ
    public float movePercent = 0f;
    //ÿƶٰٷֱȵľ
    public float percentSpeed = 0f;
    //ǷʹƽٶȣʹƽٶȣôpercentSpeedҪٶvǱtٶȣעǵǲһ
    public bool isAverageSpeed;

    //ʹmoveTimerͣmoveControlڱxʱͣN룬ɵ
    private float moveTimer = 0f;
    private float stayTime = 0f;
    //Ƿƶ
    public bool canMove = true;

    //Ƿ߳תԼķ
    public bool curveRotate = true;


    //
    public int lineStep;
    //ƶ׶Ĭϴ0
    private int currentStep = 0;

    //ƶɺǷͻ
    public bool curveLoop = false;

    //moveControl飬ڿĳٷֱǷıٶȣǷͣͣõ
    [SerializeField]
    public MoveControl[] moveControls;

    //¼´ġٷֱ-Ƿͣ-ͣʱ䡿ϵƶ״̬ѭƶôͻ֮ҪmoveControlĸͣ״̬
    public bool[] moveControlStayOriginal;

    //˶
    private Vector3 nextPosition;

    void Start()
    {
        //һ֡л͵
        transform.position = movePoints[0];
    }

    void Update()
    {
        Move();
    }

    //Ů÷к
    void Move()
    {
        //һʹýڵķʽ
        if (canMove == true)
        {
            //moveControlǿգôҪıƶݣֱӲ
            if (moveControls != null)
            {
                for (int i = 0; i < moveControls.Length; i++)
                {
                    //жϵǰtǷ¼ıڣıƶ
                    if (currentStep >= moveControls[i].moveStep)
                    {
                        if (moveControls[i].isAverageSpeed == true)
                        {
                            isAverageSpeed = true;
                            percentSpeed = moveControls[i].percentSpeedChange;
                        }
                        else
                        {
                            isAverageSpeed = false;
                            percentSpeed = moveControls[i].percentSpeedChange;
                        }
                        //Կóһϵжǵģǡж͡߼Ҹоǲۻطеģֻѭ
                        //
                        if (moveControls[i].isStay == true)
                        {
                            canMove = false;
                            stayTime = moveControls[i].stayTime;
                            moveTimer = 0f;
                            moveControls[i].isStay = false;
                        }
                    }
                }
            }


            ///====ѷ1====
            ///ʹٶȲһλӦϵİٷ֮ٵλ
            ///ΪTime.deltaTimeʹʱͱ߼ʹõıtϣĬΪ1ȫ
            ///ʹðٷֱٶҪòọ̈̄罫deltaTime * 0.1,ôҪ10ܴﵽ1ֵ
            ///movePercent += Time.deltaTime * percentSpeed;
            ///transform.position = Bezier.GetPoint(movePoints, movePercent);
            ///ʵٶtٶȣƶٶȡtٵģv
            ///ȱֻܱ֤ʱ˶ܱ֤꣩˶ÿ߶پ룩
            ///ҪʹԽ˶ôҪһʱ亯һt¾жӦtٶȣdelta t/delta TimeӰ
            ///ҲᣬԷ
            ///οҳhttps://zhuanlan.zhihu.com/p/488558717
            ///====Ůöֺͣ˭˭====
            ///
            ///====ѷ2====
            ///ʹAnimationCurveһʱ-tٶȡߣ½⣬Ҳƶúƽ
            ///Ҹ˵Ļһ㣬Ҫдһȥ
            ///õǷֶٶȣΪֱߣֳNݣNҪȽϴ󣩣Ȼÿ߶εƽٶȣٷӳAnimationCurve
            ///ôƶĺƽ֧ȫԶ壬Ǻ鷳
            ///
            ///====õ====˼·By ӣ
            ///ҲǻΪֱ˼·ΪNֱ
            ///ʹÿֱϸƶtٶȣÿֱ߾һdelta tҶ˶ٶʼձֲ䣬ô˶
            ///ÿtһٶȣԱ֤ÿֱ۳̣һͬʱƶ꣬Ǳ˶߱tӵ˶
            ///˶ôʹñȡöֱߵյ㣬ֱʹVector3.MoveTowardsƶƶɺt׶+1
            ///Ǳ˶ÿٱߣҲtӵٶȣôÿֱҪƶʱΪʱ/ֱ߶Ȼʹñȡöֱߵյ㣬һٶȣִͬVector3.MoveTowardsƶɺt׶+1
            ///˺ѭƶֱյ


            //ʹƽٶȻǱٶ
            if (isAverageSpeed == true)
            {
                if(currentStep < lineStep)
                {
                    nextPosition = Bezier.GetPoint(movePoints,(currentStep+1) / (float)lineStep);
                    // ÿ֡һƶ ٶ x deltaTime 룬ֱȫһ
                    if (Vector3.Distance(transform.position, nextPosition) > 0.01f)
                    {
                        transform.position = Vector3.MoveTowards(transform.position, nextPosition, percentSpeed * Time.deltaTime);
                    }
                    else
                    {
                        currentStep += 1;
                        movePercent += 1f / lineStep;
                    }
                }
                else
                {
                    currentStep = lineStep + 1;
                }
            }
            else
            {
                if (currentStep < lineStep)
                {
                    float tempSpeed;
                    nextPosition = Bezier.GetPoint(movePoints, (currentStep + 1) / (float)lineStep);
                    tempSpeed = Vector3.Distance(Bezier.GetPoint(movePoints, (float)currentStep / (float)lineStep), Bezier.GetPoint(movePoints, (currentStep + 1) / (float)lineStep))/(1f / lineStep) * percentSpeed;
                    // ÿ֡һƶ ٶ x deltaTime 룬ֱȫһ
                    if (Vector3.Distance(transform.position, nextPosition) > 0.01f)
                    {
                        transform.position = Vector3.MoveTowards(transform.position, nextPosition, tempSpeed * Time.deltaTime);
                    }
                    else
                    {
                        currentStep += 1;
                        movePercent += 1f / lineStep;
                    }
                }
                else
                {
                    //˵˾ǿƴع
                    movePercent = 1.01f;
                }
            }


            //ȡһstepλãҺ͵ǰλ
            if (curveRotate == true)
            {
                //ΪС0011Բbug
                transform.right = Bezier.GetPoint(movePoints, (currentStep+1)/(float)lineStep) - transform.position;
                transform.Rotate(Vector3.forward * 90);
            }

            if (currentStep < 0 || currentStep > lineStep)
            {
                //ѭѭ
                if (curveLoop == true)
                {
                    //ͻ
                    currentStep = 0;
                    transform.position = movePoints[0];

                    //moveControlľֹ
                    for (int i = 0;i<moveControls.Length;i++)
                    {
                        moveControls[i].isStay = moveControlStayOriginal[i];
                    }
                }
                else
                {
                    //ûƶͷŶ󣬶ԼĹܣֱٵ
                    //Ҳûƶ·ƶһ֮ɵڶĹ
                    //ҲûʱͷųĹܣΪڹ̶ʱҵĵлҾӵȻңҷֱǾ͵ĵ˶ǹ̶
                    Destroy(gameObject);
                }
            }
        }
        else
        {
            //ֹƶȴһʱ
            moveTimer += Time.deltaTime;
            if (moveTimer >= stayTime)
            {
                canMove = true;
                moveTimer = 0f;
                stayTime = 0f;
            }
        }
    }
}
