using CommonAI.RTS; using CommonLang.Vector;
using CommonAI.Zone.Helper;
using CommonLang;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CommonAI.Zone.Instance
{

    //--------------------------------------------------------------------------------------------------------
    /// <summary>
    /// 场景内机关
    /// </summary>
    public class InstanceTriggerUnit : InstanceUnit
    {
        private Vector2 target = new Vector2();

        public InstanceTriggerUnit(InstanceZone zone, UnitInfo info, string name, int force, int level)
            : base(zone, info, name, force, level)
        {

        }

        public override bool IsActive { get { return false; } }
        public override bool IsAttackable { get { return false; } }
        public override bool IntersectMap { get { return false; } }
        public override bool IntersectObj { get { return false; } }
        public override bool Moveable { get { return false; } }

        override protected void onUpdate(bool slowRefresh)
        {
            base.onUpdate(slowRefresh);
            this.target.SetX(X);
            this.target.SetY(Y);
            if (!IsPaused && !IsNoneSkill)
            {
                if (CurrentState is StateIdle)
                {
                    if (!doSomething2())
                    {
                        launchRandomSkillForAll(new InstanceUnit.LaunchSkillParam(0, target));
                    }
                }
            }
        }

        private bool doSomething2()
        {
            if (Info.GuardRange > 0)
            {
                using (var list = ListObjectPool<InstanceUnit>.AllocAutoRelease())
                {
                    //随机找个目标施法//
                    Parent.getObjectsRoundRange<InstanceUnit>(Collider.Object_Pos_IncludeInRound, X, Y, Info.GuardRange, list , this.AoiStatus);
                    DoAndRemoveCollection.UpdateAndRemove<InstanceUnit>(list, (InstanceUnit u) =>
                    {
                        return !u.IsActive;
                    });
                    CUtils.RandomList(Parent.RandomN, list);
                    foreach (SkillState skill in SkillStatus)
                    {
                        if (skill.TryLaunch())
                        {
                            foreach (InstanceUnit u in list)
                            {
                                if (Parent.IsAttackable(this, u, skill.Data.ExpectTarget, AttackReason.Look, Info))
                                {
                                    //检测是否有可释放技能//
                                    if (tryAutoLaunch(skill, u))
                                    {
                                        return true;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return false;
        }

        private bool tryAutoLaunch(SkillState st, InstanceUnit target)
        {
            if (st != null)
            {
                if (launchSkill(st.ID, new LaunchSkillParam(target.ID, new Vector2(target.X, target.Y), false)) != null)
                {
                    return true;
                }
            }
            return false;
        }
    }
}