少年修仙传客户端代码仓库
lcy
2024-12-16 a39c35fc6449430cd02bccb681c4a0a880e46cd9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//--------------------------------------------------------
//    [Author]:           第二世界
//    [  Date ]:           Sunday, January 21, 2018
//--------------------------------------------------------
using System;
using System.Collections.Generic;
 
 
namespace vnxbqy.UI
{
 
    
    public class ItemUseModel : Model, IBeforePlayerDataInitialize, ISwitchAccount
    {
        Dictionary<string, UseItem> itemStack = new Dictionary<string, UseItem>();
        List<string> itemGuids = new List<string>();
 
        PackModel playerPack { get { return ModelCenter.Instance.GetModel<PackModel>(); } }
        DungeonModel dungeonModel { get { return ModelCenter.Instance.GetModel<DungeonModel>(); } }
 
        public UseItem currentShowItem { get; private set; }
        public event Action showItemRefreshEvent;
 
        public override void Init()
        {
            playerPack.refrechPackEvent += OnPackageRefresh;
            playerPack.refreshItemCountEvent += OnPackageItemRefresh;
            playerPack.itemUseAct += OnGetUseItem;
        }
 
        public override void UnInit()
        {
            playerPack.refrechPackEvent -= OnPackageRefresh;
            playerPack.refreshItemCountEvent -= OnPackageItemRefresh;
            playerPack.itemUseAct -= OnGetUseItem;
        }
 
        public void OnBeforePlayerDataInitialize()
        {
        }
 
        public void OnSwitchAccount()
        {
            currentShowItem = default(UseItem);
        }
 
        public bool TryGetItem(string _guid, out UseItem _itemModel)
        {
            return itemStack.TryGetValue(_guid, out _itemModel);
        }
 
        public void ReportConfirmUseItem(UseItem _UseItem)
        {
            if (itemGuids.Contains(_UseItem.guid))
            {
                itemGuids.Remove(_UseItem.guid);
            }
 
            if (itemStack.ContainsKey(_UseItem.guid))
            {
                itemStack.Remove(_UseItem.guid);
            }
 
            RefreshCurrrentShowUseItem();
        }
 
        public int GetShowItemId()
        {
            if (currentShowItem == default(UseItem))
            {
                return 0;
            }
            else
            {
                var item = playerPack.GetItemByGuid(currentShowItem.guid);
                return item == null ? 0 : item.config.ID;
            }
        }
 
        private void OnGetUseItem(PackType type, string guid)
        {
            var UseItem = new UseItem(type, guid);
            if (!itemGuids.Contains(guid))
            {
                itemGuids.Add(guid);
            }
 
            itemStack[guid] = UseItem;
 
            RefreshCurrrentShowUseItem();
        }
 
        private void OnPackageRefresh(PackType _type)
        {
            if (_type != PackType.Item)
            {
                return;
            }
 
            RefreshCurrrentShowUseItem();
        }
 
        private void OnPackageItemRefresh(PackType _type, int _index, int _itemId)
        {
            OnPackageRefresh(_type);
        }
 
        private bool FindLatestItem(out UseItem _item)
        {
            if (itemGuids.Count == 0)
            {
                _item = default(UseItem);
                return false;
            }
 
            var guid = itemGuids[itemGuids.Count - 1];
            _item = itemStack[guid];
            return true;
        }
 
        private void Trim()
        {
            for (int i = itemGuids.Count - 1; i >= 0; i--)
            {
                var item = itemStack[itemGuids[i]];
                if (playerPack.GetItemByGuid(item.guid) == null
                    || playerPack.GetItemByGuid(item.guid).packType != PackType.Item)
                {
                    itemGuids.RemoveAt(i);
                }
            }
        }
 
        private void RefreshCurrrentShowUseItem()
        {
            Trim();
 
            UseItem tempItem;
            if (FindLatestItem(out tempItem))
            {
                if (tempItem != currentShowItem)
                {
                    currentShowItem = tempItem;
                }
            }
            else
            {
                currentShowItem = default(UseItem);
            }
 
            if (showItemRefreshEvent != null)
            {
                showItemRefreshEvent();
            }
        }
 
        public bool ShowUseItemAble()
        {
            var mapId = PlayerDatas.Instance.baseData.MapID;
            var lineId = PlayerDatas.Instance.baseData.dungeonLineId;
            var dungeonId = dungeonModel.GetDungeonId(dungeonModel.GetDataMapIdByMapId(mapId), lineId);
            if (dungeonId == 0)
            {
                return true;
            }
            else
            {
                var config = DungeonConfig.Get(dungeonId);
                return config.ShowNewItemTip == 1;
            }
        }
 
 
        public struct UseItem
        {
            public PackType packType;
            public string guid;
 
            public UseItem(PackType _packType, string _guid)
            {
                this.packType = _packType;
                this.guid = _guid;
            }
 
            public static bool operator ==(UseItem _lhs, UseItem _rhs)
            {
                return _lhs.packType == _rhs.packType && _lhs.guid == _rhs.guid;
            }
 
            public static bool operator !=(UseItem _lhs, UseItem _rhs)
            {
                return _lhs.packType != _rhs.packType || _lhs.guid != _rhs.guid;
            }
 
 
        }
 
    }
 
}