using System.Collections; 
 | 
using System.Collections.Generic; 
 | 
using UnityEngine; 
 | 
using System; 
 | 
using LitJson; 
 | 
using UnityEngine.UI; 
 | 
  
 | 
  
 | 
public class EquipModel : GameSystemManager<EquipModel> 
 | 
{ 
 | 
    public const int TotleEquip = 12;  //装备栏大小 
 | 
    public bool waitEquipOPPack = false;    // 穿戴装备的操作 不含分解 
 | 
    public event Action<bool, int> OnEquipOPResultAction;    //是否换上了新装备且分解了 装备索引 
 | 
    public event Action<List<int>, RectTransform> OnItemDropEvent; 
 | 
  
 | 
    //用于飘动逻辑 
 | 
    // public Dictionary<int, EquipOnFloorInfo> equipFloorInfo = new Dictionary<int, EquipOnFloorInfo>(); //真实背包的索引,对应地板装备的信息 
 | 
  
 | 
    public int[] equipUIEffectLights;  //装备掉落特效 
 | 
    public float[] dropEffectScales;    //掉装备的动画缩放 
 | 
    public Dictionary<int, int> newEquipIDToGuideID = new Dictionary<int, int>();  //装备掉落id,引导的id 
 | 
    public int[] equipUIEffects;  //装备TIP面板特效 
 | 
    public int[] equipUIGirdEffects;  //装备格子特效 
 | 
    public ItemModel selectFloorEquip; //选中的地板装备 
 | 
    public int lastShowEquipIndex = -1;  //上一次界面显示装备的索引,拾取后物品消失需记录下做表现 
 | 
    public Queue<int> waitEquipOP = new Queue<int>();    //等待操作的装备,需要和UI交互确认 不含分解 
 | 
  
 | 
    public List<int> lastDropIndexs = new List<int>(); //上一次掉落物品索引 
 | 
  
 | 
    public override void Init() 
 | 
    { 
 | 
        DTCA814_tagMCMakeItemAnswer.MakeItemAnswerEvent += OnEquipResult; 
 | 
        DTC0102_tagCDBPlayer.beforePlayerDataInitializeEventOnRelogin += BeforePlayerDataInitialize; 
 | 
        EventBroadcast.Instance.AddListener<string, BattleDrops, Action>(EventName.BATTLE_DROP_ITEMS, OnDropEvent); 
 | 
        ParseConfig(); 
 | 
    } 
 | 
  
 | 
    public override void Release() 
 | 
    { 
 | 
        DTCA814_tagMCMakeItemAnswer.MakeItemAnswerEvent -= OnEquipResult; 
 | 
        DTC0102_tagCDBPlayer.beforePlayerDataInitializeEventOnRelogin -= BeforePlayerDataInitialize; 
 | 
        EventBroadcast.Instance.RemoveListener<string, BattleDrops, Action>(EventName.BATTLE_DROP_ITEMS, OnDropEvent); 
 | 
    } 
 | 
  
 | 
  
 | 
    void ParseConfig() 
 | 
    { 
 | 
        var config = FuncConfigConfig.Get("DropItem"); 
 | 
        equipUIEffectLights = JsonMapper.ToObject<int[]>(config.Numerical1); 
 | 
        dropEffectScales = JsonMapper.ToObject<float[]>(config.Numerical2); 
 | 
        newEquipIDToGuideID = ConfigParse.ParseIntDict(config.Numerical3); 
 | 
  
 | 
        config = FuncConfigConfig.Get("EquipTip"); 
 | 
        equipUIEffects = JsonMapper.ToObject<int[]>(config.Numerical1); 
 | 
    } 
 | 
  
 | 
    void BeforePlayerDataInitialize() 
 | 
    { 
 | 
        selectFloorEquip = null; 
 | 
        waitEquipOP.Clear(); 
 | 
        lastShowEquipIndex = -1; 
 | 
        lastDropIndexs.Clear(); 
 | 
    } 
 | 
  
 | 
  
 | 
    public static int GetItemServerEquipPlace(int itemId) 
 | 
    { 
 | 
        var config = ItemConfig.Get(itemId); 
 | 
        if (config == null) 
 | 
        { 
 | 
            return -1; 
 | 
        } 
 | 
  
 | 
        if (config.EquipPlace > 0 && config.EquipPlace < 13) 
 | 
        { 
 | 
            return config.EquipPlace - 1; 
 | 
        } 
 | 
  
 | 
  
 | 
        return -1; 
 | 
    } 
 | 
  
 | 
    void OnDropEvent(string guid, BattleDrops drops, Action action) 
 | 
    { 
 | 
        if (!string.IsNullOrEmpty(guid)) 
 | 
            return; 
 | 
  
 | 
        lastDropIndexs = drops.dropItemPackIndex; 
 | 
        // Debug.Log("-掉落装备 " + lastDropIndexs.Count + " 个" + JsonMapper.ToJson(lastDropIndexs)); 
 | 
        NotifyItemDrop(drops.dropItemPackIndex, drops.rectTransform); 
 | 
        action?.Invoke(); 
 | 
    } 
 | 
  
 | 
    //掉落通知 
 | 
    public void NotifyItemDrop(List<int> indexs, RectTransform rect) 
 | 
    { 
 | 
        // 界面不显示则立即处理 
 | 
        if (!UIManager.Instance.IsOpened<HomeWin>()) 
 | 
        { 
 | 
            for (int i = 0; i < indexs.Count; i++) 
 | 
            { 
 | 
                CalcFloorEquip(indexs[i]); 
 | 
            } 
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            OnItemDropEvent?.Invoke(indexs, rect); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    public void CalcFloorEquip(int itemIndex) 
 | 
    { 
 | 
        if (itemIndex == -1) 
 | 
            return; 
 | 
  
 | 
        var item = PackManager.Instance.GetItemByIndex(PackType.DropItem, itemIndex); 
 | 
        if (item == null) 
 | 
            return; 
 | 
  
 | 
  
 | 
        //非自动模式下,筛选装备打开装备操作界面 
 | 
        if (IsEquip(item)) 
 | 
        { 
 | 
            OpenEquipExchangeWin(item); 
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            SendEquipOP(new ushort[] { (ushort)itemIndex }, 0); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    private void OnEquipResult(HA814_tagMCMakeItemAnswer info) 
 | 
    { 
 | 
        if (info.MakeType != (int)MakeType.EquipOP) 
 | 
            return; 
 | 
        if (info.MakeValue == 0) 
 | 
            return; 
 | 
  
 | 
        if (info.MakeValue == 2) 
 | 
            waitEquipOPPack = false; 
 | 
        bool isDone = false;    // 换上新装备且分解了旧装备 
 | 
        //选中的地板装备进行处理,替换还是找下一件,检查OnEquipOPResultAction 
 | 
        if (selectFloorEquip != null) 
 | 
        { 
 | 
            Debug.Log("OnEquipResult selectFloorEquip ID:" + selectFloorEquip.itemId); 
 | 
            if (PackManager.Instance.GetItemByIndex(PackType.DropItem, selectFloorEquip.gridIndex) == null) 
 | 
            { 
 | 
                selectFloorEquip = GetSelectFloorEquip();    //删除已选择的装备 
 | 
                if (info.MakeValue == 2) 
 | 
                    isDone = true; 
 | 
            } 
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            selectFloorEquip = GetSelectFloorEquip(); 
 | 
            if (info.MakeValue == 2) 
 | 
                isDone = true; 
 | 
        } 
 | 
  
 | 
        if (selectFloorEquip == null) 
 | 
        { 
 | 
            if (UIManager.Instance.IsOpened<EquipExchangeWin>()) 
 | 
            { 
 | 
                UIManager.Instance.CloseWindow<EquipExchangeWin>(); 
 | 
            } 
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            if (!UIManager.Instance.IsOpened<EquipExchangeWin>()) 
 | 
            { 
 | 
                UIManager.Instance.OpenWindow<EquipExchangeWin>(); 
 | 
            } 
 | 
        } 
 | 
  
 | 
        OnEquipOPResultAction?.Invoke(isDone, lastShowEquipIndex); 
 | 
    } 
 | 
  
 | 
    /// <summary> 
 | 
    /// 装备操作, // 0 - 拾取非装备物品;1 - 分解;2 - 穿戴/替换 
 | 
    /// </summary> 
 | 
    /// <param name="itemIndexList"></param> 
 | 
    /// <param name="opType"></param> 
 | 
    /// <param name="autoDecompose"></param> 
 | 
    public void SendEquipOP(ushort[] itemIndexList, byte opType, bool autoDecompose = false) 
 | 
    { 
 | 
        if (opType == 2 && waitEquipOPPack) return; 
 | 
  
 | 
        // NoteFloorEquip(itemIndexList, opType); 
 | 
        var pack = new CB415_tagCSMainDropItemOP(); 
 | 
        pack.Count = (byte)itemIndexList.Length; 
 | 
        pack.IndexList = itemIndexList; 
 | 
        pack.OPType = opType; 
 | 
        pack.OPValue = (byte)(autoDecompose ? 1 : 0); // 替换后是否自动分解原装备:0否1是 
 | 
  
 | 
        if (opType == 2) 
 | 
            waitEquipOPPack = true; 
 | 
        GameNetSystem.Instance.SendInfo(pack); 
 | 
    } 
 | 
  
 | 
    /// <summary> 
 | 
    /// equipFloorInfo记录地板道具的最新一次信息,用于替换和分解装备的表现 
 | 
    /// </summary> 
 | 
    /// <param name="itemIndexList">地板道具索引</param> 
 | 
    /// <param name="opType"></param> 
 | 
    // public void NoteFloorEquip(ushort[] itemIndexList, int opType) 
 | 
    // { 
 | 
    //     equipFloorInfo.Clear(); 
 | 
    //     foreach (var girdIndex in itemIndexList) 
 | 
    //     { 
 | 
    //         var equip = PackManager.Instance.GetItemByIndex(PackType.DropItem, girdIndex); 
 | 
    //         if (equip == null) continue; 
 | 
    //         equipFloorInfo[girdIndex] = new EquipOnFloorInfo() 
 | 
    //         { 
 | 
    //             equipID = equip.itemId, 
 | 
    //             posX = floorItemCellArr[girdIndex].transform.position.x, 
 | 
    //             posY = floorItemCellArr[girdIndex].transform.position.y, 
 | 
    //             opType = opType 
 | 
    //         }; 
 | 
    //     } 
 | 
  
 | 
    // } 
 | 
  
 | 
    public class EquipOnFloorInfo 
 | 
    { 
 | 
        public int equipID; //道具或者装备,道具分解对应不同货币,装备固定货币 
 | 
        public float posX;  //全局坐标 表现再转成local 
 | 
        public float posY; 
 | 
        public int opType;  // 0 - 拾取非装备物品;1 - 分解;2 - 穿戴/替换; 
 | 
    } 
 | 
  
 | 
    // public EquipOnFloorInfo FindExchangeEquipInfo() 
 | 
    // { 
 | 
    //     foreach (var info in equipFloorInfo) 
 | 
    //     { 
 | 
    //         if (info.Value.opType == 2) 
 | 
    //             return info.Value; 
 | 
    //     } 
 | 
    //     return new EquipOnFloorInfo(); 
 | 
    // } 
 | 
  
 | 
    public bool OpenEquipExchangeWin(ItemModel equip) 
 | 
    { 
 | 
        if (equip == null) 
 | 
            return false; 
 | 
  
 | 
        // if (!IsEquip(equip)) 
 | 
        //     return false; 
 | 
  
 | 
        if (AutoFightModel.Instance.TryAutoFightDoEquip(equip)) 
 | 
        { 
 | 
            //自动战力对比, 条件不满足的分解 
 | 
            return false; 
 | 
        } 
 | 
  
 | 
        // 好几件装备需要处理的情况存起来 
 | 
        waitEquipOP.Enqueue(equip.gridIndex); 
 | 
        //未回复装备操作结果,否则会显示旧装备 
 | 
        if (waitEquipOPPack) 
 | 
            return false; 
 | 
  
 | 
        // 后续补充引导或者其他情况下,不允许切换装备界面 
 | 
        // if (NewBieCenter.Instance.inGuiding) 
 | 
        //     return; 
 | 
  
 | 
        if (UIManager.Instance.IsOpened<EquipExchangeWin>()) 
 | 
        { 
 | 
            return false; 
 | 
        } 
 | 
  
 | 
         
 | 
        selectFloorEquip = GetSelectFloorEquip(); 
 | 
        if (selectFloorEquip == null) 
 | 
            return false; 
 | 
         
 | 
        UIManager.Instance.OpenWindow<EquipExchangeWin>(); 
 | 
        return true; 
 | 
  
 | 
    } 
 | 
  
 | 
  
 | 
    ItemModel GetSelectFloorEquip() 
 | 
    { 
 | 
        if (waitEquipOP.Count == 0) 
 | 
            return null; 
 | 
  
 | 
        return PackManager.Instance.GetItemByIndex(PackType.DropItem, waitEquipOP.Dequeue()); 
 | 
  
 | 
    } 
 | 
  
 | 
  
 | 
  
 | 
    //等级 
 | 
    public int GetEquipLV(ItemModel equip) 
 | 
    { 
 | 
        if (equip == null) 
 | 
        { 
 | 
            return 0; 
 | 
        } 
 | 
  
 | 
        return equip.GetUseDataFirstValue(22); 
 | 
    } 
 | 
  
 | 
    //基础属性ID列表 
 | 
    public List<int> GetEquipBaseAttrs(ItemModel equip) 
 | 
    { 
 | 
        if (equip == null) 
 | 
        { 
 | 
            return new List<int>(); 
 | 
        } 
 | 
        return equip.GetUseData(21); 
 | 
    } 
 | 
  
 | 
  
 | 
    //基础属性值列表 
 | 
    public List<int> GetEquipBaseValues(ItemModel equip) 
 | 
    { 
 | 
        if (equip == null) 
 | 
        { 
 | 
            return new List<int>(); 
 | 
        } 
 | 
        return equip.GetUseData(23); 
 | 
    } 
 | 
  
 | 
    //战斗属性ID列表 
 | 
    public List<int> GetEquipFightAttrs(ItemModel equip) 
 | 
    { 
 | 
        if (equip == null) 
 | 
        { 
 | 
            return new List<int>(); 
 | 
        } 
 | 
        return equip.GetUseData(17); 
 | 
    } 
 | 
  
 | 
    //战斗属性值列表 
 | 
    public List<int> GetEquipFightValues(ItemModel equip) 
 | 
    { 
 | 
        if (equip == null) 
 | 
        { 
 | 
            return new List<int>(); 
 | 
        } 
 | 
        return equip.GetUseData(19); 
 | 
    } 
 | 
  
 | 
  
 | 
    //得到装备位对应的部位名称 
 | 
    public string GetEquipPlaceName(int place) 
 | 
    { 
 | 
  
 | 
        return Language.Get("equipPlace" + place); 
 | 
    } 
 | 
  
 | 
    //是否装备 
 | 
    public bool IsEquip(ItemModel item) 
 | 
    { 
 | 
        return item.config.EquipPlace != 0; 
 | 
    } 
 | 
  
 | 
    public bool IsEquip(int itemID) 
 | 
    { 
 | 
        return ItemConfig.Get(itemID).EquipPlace != 0; 
 | 
    } 
 | 
  
 | 
    public ItemModel GetEquip(int index) 
 | 
    {  
 | 
        return PackManager.Instance.GetItemByIndex(PackType.Equip, index); 
 | 
    } 
 | 
} 
 |