using System;
using Game.Plugins.App.Sync;
using Plugins.JNGame.Sync.Frame.AStar.RVO2;
using RVO;
using UnityEngine;
using Object = UnityEngine.Object;
using Vector2 = RVO.Vector2;

namespace Game.Plugins.App.Game.RVO2
{
    public class JNGRVO2Agent : JNGSyncFrameDefault
    {
        
        private JNRVO2Simulator Simulator => GetComponentInParent<JNGRVO2Manager>().Simulator;
        public float maxSpeed = 20;
        public float radius = 0.5f;
        [HideInInspector] public int sid = -1;
        private Vector2 target;

        public override void OnSyncLoad()
        {
            base.OnSyncLoad();
            
            var position = this.transform.position;
            //设置Agent的默认属性
            float neighborDist = 15.0f;//检测与邻居避障的最大距离（要关心的范围），越大纳入思考的数据量越大，越小越不安全
            int maxNeighbors = 10;//检测与邻居避障的最大个数（要关心的其他单位），越大纳入思考的数据量越大，越小越不安全
            float timeHorizon = 5.0f; //预测提前规避时间, 提前得越多，速度变化越频繁
            float timeHorizonObst = 5.0f; //预测提前规避时间(针对固定障碍), 提前得越多，速度变化越频繁
            float radius = this.radius;//阻挡半径
            float maxSpeed = this.maxSpeed;//所能达到得最大速度
            Vector2 velocity = new Vector2(0.0f, 0.0f);//初始的 2元线性速度，影响出生单位时排挤其他的速度
            sid = Simulator.addAgent(new Vector2(position.x, position.z), neighborDist, maxNeighbors, timeHorizon,
                timeHorizonObst, radius, maxSpeed,velocity);

        }

        //设置目标点
        public void SetTarget(Vector2 targetPoint)
        {
            this.target = targetPoint;
        }
        public void SetTarget(Vector3 targetPoint)
        {
            this.target = new Vector2(targetPoint.x,targetPoint.z);
        }

        public override void OnSyncUpdate(int dt, JNFrameInfo frame, Object input)
        {
            base.OnSyncUpdate(dt, frame, input);
            
            if (sid >= 0)
            {
                Vector2 pos = Simulator.getAgentPosition(sid);
                Vector2 vel = Simulator.getAgentPrefVelocity(sid);
                transform.position = new Vector3(pos.x(), transform.position.y , pos.y());
                if (Math.Abs(vel.x()) > 0.01f && Math.Abs(vel.y()) > 0.01f)
                    //以Y轴为移动朝向
                    transform.forward = new Vector3(vel.x(), 0, vel.y()).normalized;
            }

            // if (!Input.GetMouseButton(1))//如果没有按下右键的话, 就不给予速度向量
            // {
            // Simulator.Instance.setAgentPrefVelocity(sid, new Vector2(0, 0));
            // return;
            // }

            Vector2 goalVector = new Vector2(target.x(),target.y()) - Simulator.getAgentPosition(sid);
            if (RVOMath.absSq(goalVector) > 1.0f)
            {
                goalVector = RVOMath.normalize(goalVector);
            }
            Simulator.setAgentPrefVelocity(sid, goalVector);

            /* Perturb a little to avoid deadlocks due to perfect symmetry. */
            /* 因为完美对称，所以需要加入些许抖动用来避免死锁
             * 这里的完美对称，测试出是指两个完全一样的单位，不抖动=中心点一样=无法把其他排斥出去
             */
            float angle = (float)GetSync().nRandomFloat(0,1) * 2.0f * (float)Math.PI;
            float dist = (float)GetSync().nRandomFloat(0,1) * 0.0001f;
            
            Simulator.setAgentPrefVelocity(sid, Simulator.getAgentPrefVelocity(sid) +
                                                         dist *
                                                         new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle)));
        }
    }
}