//-------------------------------------------------------- // [Author]: 第二世界 // [ Date ]: Sunday, January 21, 2018 //-------------------------------------------------------- using System; using System.Collections.Generic; namespace vnxbqy.UI { public class ItemUseModel : Model, IBeforePlayerDataInitialize, ISwitchAccount { Dictionary itemStack = new Dictionary(); List itemGuids = new List(); PackModel playerPack { get { return ModelCenter.Instance.GetModel(); } } DungeonModel dungeonModel { get { return ModelCenter.Instance.GetModel(); } } 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; } } } }