//-------------------------------------------------------- // [Author]: 第二世界 // [ Date ]: Wednesday, October 11, 2017 //-------------------------------------------------------- using UnityEngine; using System.Collections; using UnityEngine.UI; using TableConfig; namespace Snxxz.UI { public class Achievement { public readonly int id; public readonly AchievementRewardItem[] rewardItem = new AchievementRewardItem[] { }; public readonly AchievementRewardCurrency[] rewardCurrency = new AchievementRewardCurrency[] { }; public readonly int rewardExp = 0; bool m_Completed = false;//如果为true表示已经领了奖 public bool completed { get { return m_Completed; } set { m_Completed = value; } } int m_Progress = 0; public int progress { get { return m_Progress; } set { m_Progress = value; } } public Achievement(int _id, string _itemReward, string _currencyReward, int _exp) { this.id = _id; if (!string.IsNullOrEmpty(_itemReward) && _itemReward.Length > 1) { var itemRewardArray = _itemReward.Split('|'); rewardItem = new AchievementRewardItem[itemRewardArray.Length]; for (var i = 0; i < itemRewardArray.Length; i++) { rewardItem[i] = new AchievementRewardItem(itemRewardArray[i]); } } if (!string.IsNullOrEmpty(_currencyReward) && _currencyReward.Length > 1) { var currencyRewardArray = _currencyReward.Split('|'); rewardCurrency = new AchievementRewardCurrency[currencyRewardArray.Length]; for (var i = 0; i < currencyRewardArray.Length; i++) { rewardCurrency[i] = new AchievementRewardCurrency(currencyRewardArray[i]); } } rewardExp = _exp; } public void ResetAchievementState() { completed = false; progress = 0; } public Item[] GetAllBehaviourItems() { var items = new Item[rewardItem.Length + rewardCurrency.Length + (rewardExp > 0 ? 1 : 0)]; var index = 0; for (int i = 0; i < rewardCurrency.Length; i++) { int displayId = GeneralConfig.Instance.moneyDisplayIds.ContainsKey(rewardCurrency[i].id) ? GeneralConfig.Instance.moneyDisplayIds[rewardCurrency[i].id] : rewardCurrency[i].id; items[index] = new Item(displayId, rewardCurrency[i].count); index++; } for (int i = 0; i < rewardItem.Length; i++) { items[index] = new Item(rewardItem[i].id, rewardItem[i].count); index++; } if (rewardExp > 0) { items[index] = new Item(GeneralConfig.Instance.expDisplayId, rewardExp); } return items; } public static bool IsReach(int _achievementId, int _progress) { var config = Config.Instance.Get(_achievementId); if (config == null) { return false; } return _progress >= config.NeedCnt; } public static int Compare(Achievement _lhs, Achievement _rhs, int _recommended) { if (_lhs == null) { return -1; } else if (_rhs == null) { return 1; } else if (!_lhs.completed && _rhs.completed) { return -1; } else if (_lhs.completed && !_rhs.completed) { return 1; } else if (IsReach(_lhs.id, _lhs.progress) && !IsReach(_rhs.id, _rhs.progress)) { return -1; } else if (IsReach(_rhs.id, _rhs.progress) && !IsReach(_lhs.id, _lhs.progress)) { return 1; } else if (_recommended == _lhs.id && _recommended != _rhs.id) { return -1; } else if (_recommended != _lhs.id && _recommended == _rhs.id) { return 1; } else { var configA = Config.Instance.Get(_lhs.id); var configB = Config.Instance.Get(_rhs.id); return configA.ID < configB.ID ? -1 : 1; } } } public struct AchievementRewardItem { public int id; public int count; public int bind; public AchievementRewardItem(string _config) { var configArray = _config.Split('_'); if (configArray.Length > 0) { int.TryParse(configArray[0], out id); } else { id = 0; } if (configArray.Length > 1) { int.TryParse(configArray[1], out count); } else { count = 0; } if (configArray.Length > 2) { int.TryParse(configArray[0], out bind); } else { bind = 0; } } } public struct AchievementRewardCurrency { public int id; public int count; public AchievementRewardCurrency(string _config) { var configArray = _config.Split('_'); if (configArray.Length > 0) { int.TryParse(configArray[0], out id); } else { id = 0; } if (configArray.Length > 1) { int.TryParse(configArray[1], out count); } else { count = 0; } } } }