// *********************************************************************** // Assembly : Game // Author : Kimch // Created : 2020-11-03 // Description : // Last Modified By : // Last Modified On : // *********************************************************************** // // // *********************************************************************** using System.Collections; using System.Collections.Generic; using UnityEngine; namespace G { /// /// 招式商店 /// public class MovesShop : MonoBehaviour { /// /// /// private class MovesPackageItemInfo { public int id; public int parentId; public int weight; public int tmpWeight; } #region Field public const int MAX_CARD_COUNT = 3; /// /// /// public int movesPoint { get; private set; } /// /// /// public int totalMovesPoint { get; private set; } public int refreshCount { get; private set; } = 0; public int refreshCountMax { get; private set; } = 0; /// /// 显示的三张卡 /// public readonly List showMoves = new List(); /// /// 已购买 /// public readonly HashSet studiedMoves = new HashSet(); /// /// /// private readonly List _lastStudiedMoves = new List(); private int _movesPackageId; private int _movesPackageType = 0; /// /// /// private readonly Dictionary _movesPackage = new Dictionary(); private Callback2 _studyCallback; #endregion #region Method /// /// /// /// public void AddMovesPoint(int point) { movesPoint += point; totalMovesPoint += point; } /// /// /// public void ResetMovesPoint() { movesPoint = 0; } /// /// /// /// /// private bool CheckMoves(ItemMoves moves) { return moves.unlock == 0 || MissionProxy.Instance.IsCompletedMission(moves.unlock); } /// /// 技能卡包 /// /// public void SetMovesPackage(int id, bool auto, Callback2 callback) { _studyCallback = callback; _movesPackageId = id; _movesPackageType = 0; _movesPackage.Clear(); if (_movesPackageId > 0) { var package = ItemProxy.Instance.GetStaticItem(_movesPackageId); _movesPackageType = package.type; for (int i = 0; i < package.skillCards.Length; i += 2) { int pid = package.skillCards[i]; var pw = package.skillCards[i + 1]; var movesItem = ItemProxy.Instance.GetStaticItem(package.skillCards[i]); if (CheckMoves(movesItem)) _movesPackage.Add(pid, new MovesPackageItemInfo { id = pid, parentId = movesItem.parent, weight = pw != 0 ? pw : movesItem.weight, }); } movesPoint = 1; RefreshMoves(MAX_CARD_COUNT); GlobalNotifier.PostNotification(GlobalDefine.EVENT_GAME_WINDOW, auto ? "auto" : "", "skill_shop"); } } /// /// 多选技能 /// /// /// /// public void SetMultiMovesPackage(int id, int count, Callback2 callback) { _studyCallback = callback; _movesPackageId = id; _movesPackageType = 0; _movesPackage.Clear(); if (_movesPackageId > 0) { var package = ItemProxy.Instance.GetStaticItem(_movesPackageId); _movesPackageType = package.type; for (int i = 0; i < package.skillCards.Length; i += 2) { int pid = package.skillCards[i]; var pw = package.skillCards[i + 1]; var movesItem = ItemProxy.Instance.GetStaticItem(package.skillCards[i]); //if (true || CheckMoves(movesItem)) _movesPackage.Add(pid, new MovesPackageItemInfo { id = pid, parentId = movesItem.parent, weight = pw != 0 ? pw : movesItem.weight, }); } movesPoint = count; RefreshMultiMoves(count + 3); GlobalNotifier.PostNotification(GlobalDefine.EVENT_GAME_WINDOW, false, "skill_shop"); } } /// /// /// /// public bool RefreshMovesShop(bool free) { if (refreshCount > 0 || free) { #if UNITY_EDITOR || DEBUG_MY #else if (!free) this.refreshCount--; #endif RefreshMoves(MAX_CARD_COUNT); return true; } return false; } /// /// 设置可学 /// private void RefreshMoves(int count) { showMoves.Clear(); if (_movesPackageId > 0) { if (_movesPackageType == 0) { foreach (var item in _movesPackage.Values) { if (studiedMoves.Contains(item.id)) { item.weight = 0; item.tmpWeight = 0; } else { item.tmpWeight = item.parentId == 0 || studiedMoves.Contains(item.parentId) ? item.weight : 0; } } for (int i = 0; i < count; i++) { int totalW = 0; foreach (var item in _movesPackage.Values) { totalW += item.tmpWeight; } var currW = Random.Range(0, totalW); foreach (var item in _movesPackage.Values) { if (item.tmpWeight <= 0) continue; currW -= item.tmpWeight; if (currW < 0) { item.tmpWeight = 0; showMoves.Add(item.id); break; } } } } else if (_movesPackageType == 1) { foreach (var item in _movesPackage.Values) { if (showMoves.Count < count) showMoves.Add(item.id); } } } } /// /// 设置可学 /// private void RefreshMultiMoves(int count) { showMoves.Clear(); if (_movesPackageId <= 0) { return; } count = Mathf.Min(count, _movesPackage.Count); if (_movesPackageType == 0) { Comp: if (showMoves.Count == 0) { foreach (var item in _movesPackage.Values) { item.tmpWeight = item.parentId == 0 ? item.weight : 0; } } else { foreach (var item in _movesPackage.Values) { if (item.parentId > 0) item.tmpWeight = showMoves.Contains(item.parentId) ? item.weight : 0; } } for (int i = showMoves.Count; i < count; i++) { int totalW = 0; foreach (var item in _movesPackage.Values) { if (item.tmpWeight > 0) totalW += item.tmpWeight; } var currW = Random.Range(0, totalW); foreach (var item in _movesPackage.Values) { if (item.tmpWeight <= 0) continue; currW -= item.tmpWeight; if (currW < 0) { if (item.parentId > 0 && !showMoves.Contains(item.parentId)) { var parent = _movesPackage[item.parentId]; parent.tmpWeight = 0; showMoves.Add(parent.id); } else { item.tmpWeight = 0; showMoves.Add(item.id); } break; } } } if (showMoves.Count < count) { goto Comp; } } } /// /// /// /// public bool StudyMoves(int id, bool free = false) { if (id > 0) { var skillCardItem = ItemProxy.Instance.GetStaticItem(id); studiedMoves.Add(id); _lastStudiedMoves.Add(id); //if (movesPoint >= skillCardItem.price) //{ // movesPoint -= skillCardItem.price; // studiedMoves.Add(id); // _lastStudiedMoves.Add(id); //} KStatistics.Instance.ReportEvent("study_skill", $"{{\"ectype_name\":\"{skillCardItem.name}\"}}"); return true; } return false; } /// /// /// /// /// public void StudyMoves(IEnumerable movesIds, bool free = false) { foreach (var id in movesIds) if (id > 0) { studiedMoves.Add(id); _lastStudiedMoves.Add(id); //var skillCardItem = ItemProxy.Instance.GetStaticItem(id); //if (movesPoint >= skillCardItem.price) //{ // movesPoint -= skillCardItem.price; // studiedMoves.Add(id); // _lastStudiedMoves.Add(id); //} } KStatistics.Instance.ReportEvent("study_skill", $"{{\"ectype_name\":\"all\"}}"); } /// /// 学习指定 技能 /// /// public void StudyMoves(IEnumerable movesIds) { foreach (var id in movesIds) if (id > 0) { studiedMoves.Add(id); _lastStudiedMoves.Add(id); } ApplyLastStudiedMoves(); } /// /// /// /// public void StudyAllMoves(bool free = false) { StudyMoves(showMoves, free); } /// /// /// public void ApplyLastStudiedMoves(bool doubleValue = false) { //先学前置技能 for (int i = 0; i < _lastStudiedMoves.Count; i++) { var movesItem = ItemProxy.Instance.GetStaticItem(_lastStudiedMoves[i]); if (movesItem != null && movesItem.parent == 0) { EntityMainPlayer.Instance.AddMoves(movesItem.id, doubleValue); } } //再学子技能 for (int i = 0; i < _lastStudiedMoves.Count; i++) { var movesItem = ItemProxy.Instance.GetStaticItem(_lastStudiedMoves[i]); if (movesItem != null && movesItem.parent > 0) { EntityMainPlayer.Instance.AddMoves(movesItem.id, doubleValue); } } _lastStudiedMoves.Clear(); if (_studyCallback != null) { _studyCallback(0, ""); _studyCallback = null; } } #endregion #region Unity private static MovesShop _Instance; public static MovesShop Instance { get { return _Instance; } } // Use this for initialization private void Awake() { _Instance = this; } #endregion } }