DressUpObjDataCache.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using System;
  4. using System.Linq;
  5. namespace GFGGame
  6. {
  7. public class DressUpObjDataCache
  8. {
  9. private GameObject _sceneObj;
  10. private bool _needSetMask;
  11. public void setSceneObj(GameObject sceneObj, bool needSetMask = false)
  12. {
  13. _sceneObj = sceneObj;
  14. _needSetMask = needSetMask;
  15. }
  16. private int _bgId;
  17. public int bgId
  18. {
  19. get
  20. {
  21. return _bgId;
  22. }
  23. }
  24. private int _suitId;
  25. public int suitId
  26. {
  27. get
  28. {
  29. return _suitId;
  30. }
  31. }
  32. private bool _isPic;
  33. public int picStatus
  34. {
  35. get
  36. {
  37. return _isPic ? 1 : 0;
  38. }
  39. }
  40. private List<int> _equipDatas = new List<int>();
  41. public int[] equipDatas
  42. {
  43. get
  44. {
  45. return _equipDatas.ToArray();
  46. }
  47. }
  48. //角色基础分+部件基础分
  49. private int _score;
  50. public int score
  51. {
  52. get
  53. {
  54. return _score;
  55. }
  56. private set
  57. {
  58. _score = value;
  59. EventAgent.DispatchEvent(ConstMessage.DRESS_UP_SCORE_CHANGED, _score);
  60. }
  61. }
  62. //最终得分
  63. private int _totalScore;
  64. public int totalScore
  65. {
  66. get
  67. {
  68. return _totalScore;
  69. }
  70. set
  71. {
  72. _totalScore = value;
  73. }
  74. }
  75. public void Dispose()
  76. {
  77. _sceneObj = null;
  78. }
  79. private void Add(int value)
  80. {
  81. if (!_equipDatas.Contains(value))
  82. {
  83. _equipDatas.Add(value);
  84. DressUpUtil.AddItem(value, _sceneObj, _needSetMask);
  85. score += DressUpMenuItemDataManager.GetItemScore(value);
  86. }
  87. }
  88. private void Remove(int value)
  89. {
  90. if (_equipDatas == null)
  91. {
  92. return;
  93. }
  94. if (_equipDatas.Contains(value))
  95. {
  96. _equipDatas.Remove(value);
  97. DressUpUtil.RemoveItem(value, _sceneObj);
  98. score -= DressUpMenuItemDataManager.GetItemScore(value);
  99. }
  100. }
  101. /// <summary>
  102. /// 仅判断换装部件是否已穿着
  103. /// </summary>
  104. /// <param name="id"></param>
  105. /// <returns></returns>
  106. public bool CheckDressUpItemIsOn(int id)
  107. {
  108. if (id == _bgId)
  109. {
  110. return true;
  111. }
  112. return _equipDatas.Contains(id);
  113. }
  114. /// <summary>
  115. /// 仅判断套装是否穿上
  116. /// </summary>
  117. /// <param name="id"></param>
  118. /// <returns></returns>
  119. public bool CheckSuitIsOn(int id)
  120. {
  121. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(id);
  122. if (suitCfg == null)
  123. {
  124. return false;
  125. }
  126. int[] items = suitCfg.partsArr;
  127. foreach (int itemId in items)
  128. {
  129. bool isOn = CheckDressUpItemIsOn(itemId);
  130. if (!isOn)
  131. {
  132. return false;
  133. }
  134. }
  135. return true;
  136. }
  137. public void AddOrRemove(int value, bool checkDefault, bool isAdd = false, bool isRemove = false)
  138. {
  139. int subType = ItemUtil.GetItemSubType(value);
  140. if (subType == ConstDressUpItemType.BEI_JING)
  141. {
  142. _bgId = value;
  143. DressUpUtil.AddItem(_bgId, _sceneObj, _needSetMask);
  144. }
  145. else
  146. {
  147. if (!CheckDressUpItemIsOn(value))
  148. {
  149. if (!isRemove)
  150. {
  151. checkRemoveSameType(subType);
  152. Add(value);
  153. }
  154. }
  155. else
  156. {
  157. if (!isAdd)
  158. {
  159. Remove(value);
  160. }
  161. }
  162. if (checkDefault)
  163. {
  164. checkDefaultItem();
  165. }
  166. }
  167. }
  168. public void checkRemoveSameType(int type)
  169. {
  170. int count = 0;
  171. int firstTeshuId = 0;
  172. for (int i = 0; i < _equipDatas.Count; i++)
  173. {
  174. int itemID = (int)_equipDatas[i];
  175. int subType = ItemUtil.GetItemSubType(itemID);
  176. if (subType == type
  177. || (type == ConstDressUpItemType.LIAN_YI_QUN && (subType == ConstDressUpItemType.SHANG_YI || subType == ConstDressUpItemType.XIA_ZHUANG || subType == ConstDressUpItemType.NEI_DA))
  178. || (type == ConstDressUpItemType.SHANG_YI && subType == ConstDressUpItemType.LIAN_YI_QUN)
  179. || (type == ConstDressUpItemType.XIA_ZHUANG && subType == ConstDressUpItemType.LIAN_YI_QUN)
  180. || (type == ConstDressUpItemType.NEI_DA && subType == ConstDressUpItemType.LIAN_YI_QUN))
  181. {
  182. Remove(itemID);
  183. i--;
  184. }
  185. if (subType > ConstDressUpItemType.TE_SHU)
  186. {
  187. if (count == 0)
  188. {
  189. firstTeshuId = itemID;
  190. }
  191. count++;
  192. }
  193. }
  194. if (type > ConstDressUpItemType.TE_SHU && count >= 3)
  195. {
  196. //特殊饰品最多穿三件,第四件会自动顶掉第一件
  197. Remove(firstTeshuId);
  198. }
  199. }
  200. private void checkDefaultItem()
  201. {
  202. if (!IsSuitPic)
  203. {
  204. //检查默认资源
  205. //是否有头发
  206. bool has1 = false;
  207. //是否有连衣裙
  208. bool has2 = false;
  209. //是否有内搭
  210. bool has3 = false;
  211. //是否有上衣
  212. // bool has4 = false;
  213. //是否有下装
  214. bool has5 = false;
  215. //是否有默认内搭
  216. // bool has30000 = false;
  217. //是否有默认下装
  218. // bool has50000 = false;
  219. for (int i = 0; i < _equipDatas.Count; i++)
  220. {
  221. int itemID = (int)_equipDatas[i];
  222. int subType = ItemUtil.GetItemSubType(itemID);
  223. if (subType == (int)ConstDressUpItemType.FA_XING)
  224. {
  225. has1 = true;
  226. }
  227. else if (subType == ConstDressUpItemType.LIAN_YI_QUN)
  228. {
  229. has2 = true;
  230. }
  231. else if (subType == ConstDressUpItemType.NEI_DA)
  232. {
  233. has3 = true;
  234. }
  235. else if (subType == ConstDressUpItemType.XIA_ZHUANG)
  236. {
  237. has5 = true;
  238. }
  239. }
  240. if (!has1)
  241. {
  242. Add(10000);
  243. }
  244. if (!has2)
  245. {
  246. if (!has5)
  247. {
  248. Add(50000);
  249. }
  250. if (!has3)
  251. {
  252. Add(30000);
  253. }
  254. }
  255. }
  256. }
  257. private void UpdatePicAction()
  258. {
  259. if (IsSuitPic)
  260. {
  261. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(_suitId);
  262. DressUpUtil.UpdateBody(suitCfg.picRes, _sceneObj, !string.IsNullOrEmpty(suitCfg.aniRes), suitCfg.effRes);
  263. }
  264. else
  265. {
  266. DressUpUtil.UpdateBody(null, _sceneObj, false, null, _needSetMask);
  267. }
  268. }
  269. public void TakeOffAll(bool checkDefault = true)
  270. {
  271. _suitId = 0;
  272. _isPic = false;
  273. // AddOrRemove(propID, false, true);
  274. int[] tempList = equipDatas;
  275. foreach (int itemID in tempList)
  276. {
  277. AddOrRemove(itemID, false, false, true);
  278. }
  279. if (checkDefault)
  280. {
  281. checkDefaultItem();
  282. UpdatePicAction();
  283. }
  284. RoleLevelCfg roleLevelCfg = RoleLevelCfgArray.Instance.GetCfg(RoleDataManager.lvl);
  285. score = roleLevelCfg.baseScore;
  286. foreach (int itemId in _equipDatas)
  287. {
  288. score += DressUpMenuItemDataManager.GetItemScore(itemId);
  289. }
  290. }
  291. public void ChangeAction()
  292. {
  293. if (!HasSuitPicRes)
  294. {
  295. return;
  296. }
  297. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(_suitId);
  298. _isPic = !_isPic;
  299. if (_isPic)
  300. {
  301. int[] tempList = equipDatas;
  302. foreach (int itemID in tempList)
  303. {
  304. if (!DressUpMenuItemDataManager.CheckIsSceneType(itemID))
  305. {
  306. AddOrRemove(itemID, false, false, true);
  307. }
  308. }
  309. }
  310. else
  311. {
  312. int[] items = suitCfg.partsArr;
  313. foreach (int itemId in items)
  314. {
  315. if (!DressUpMenuItemDataManager.CheckIsSceneType(itemId))
  316. {
  317. AddOrRemove(itemId, false, true);
  318. }
  319. }
  320. }
  321. checkDefaultItem();
  322. UpdatePicAction();
  323. }
  324. public void TryCancelSuit(int itemID)
  325. {
  326. if (_suitId > 0)
  327. {
  328. if (!DressUpMenuItemDataManager.CheckIsSceneType(itemID))
  329. {
  330. if (_isPic)
  331. {
  332. ChangeAction();
  333. }
  334. _suitId = 0;
  335. }
  336. }
  337. }
  338. public bool IsSuitPic
  339. {
  340. get
  341. {
  342. return _suitId > 0 && _isPic;
  343. }
  344. }
  345. public bool HasSuitPicRes
  346. {
  347. get
  348. {
  349. if (_suitId > 0)
  350. {
  351. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(_suitId);
  352. if (suitCfg.picRes != null && suitCfg.picRes.Length > 0)
  353. {
  354. return true;
  355. }
  356. }
  357. return false;
  358. }
  359. }
  360. public void PutOnSuitCfg(int id, bool checkPic, bool noSceneType, int[] excludeType = null)
  361. {
  362. if (_suitId == id)
  363. {
  364. return;
  365. }
  366. TakeOffAll(false);
  367. _suitId = id;
  368. _isPic = HasSuitPicRes && checkPic;
  369. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(_suitId);
  370. List<int> items = new List<int>(suitCfg.partsArr);
  371. if (suitCfg.partsOptionalArr != null && suitCfg.partsOptionalArr.Length > 0)
  372. {
  373. items.AddRange(suitCfg.partsOptionalArr);
  374. }
  375. int subType = 0;
  376. foreach (int itemID in items)
  377. {
  378. if (DressUpMenuItemDataManager.CheckHasItem(itemID))
  379. {
  380. bool isSceneType = DressUpMenuItemDataManager.CheckIsSceneType(itemID);
  381. subType = ItemUtil.GetItemSubType(itemID);
  382. if (!_isPic || isSceneType)
  383. {
  384. if (!noSceneType || !isSceneType)
  385. {
  386. if (excludeType == null || Array.IndexOf(excludeType, subType) < 0)
  387. {
  388. AddOrRemove(itemID, false, true);
  389. }
  390. }
  391. }
  392. }
  393. }
  394. checkDefaultItem();
  395. UpdatePicAction();
  396. }
  397. public void PutOnSuitSaved(int index)
  398. {
  399. TakeOffAll(false);
  400. CustomSuitData suitSavedData = CustomSuitDataManager.GetSuitList(index);
  401. if (suitSavedData.bg > 0)
  402. {
  403. AddOrRemove(suitSavedData.bg, false);
  404. }
  405. foreach (int itemID in suitSavedData.equipDatas)
  406. {
  407. AddOrRemove(itemID, false, true);
  408. }
  409. _suitId = suitSavedData.suitId;
  410. _isPic = suitSavedData.pic > 0;
  411. checkDefaultItem();
  412. UpdatePicAction();
  413. }
  414. public void PutOnSuitSavedInFight(int index)
  415. {
  416. TakeOffAll(false);
  417. CustomSuitData suitSavedData = CustomSuitDataManager.GetSuitList(index);
  418. if (suitSavedData.suitId > 0)
  419. {
  420. PutOnSuitCfg(suitSavedData.suitId, false, true);
  421. }
  422. else
  423. {
  424. foreach (int itemID in suitSavedData.equipDatas)
  425. {
  426. if (!DressUpMenuItemDataManager.CheckIsSceneType(itemID))
  427. {
  428. AddOrRemove(itemID, false, true);
  429. }
  430. }
  431. checkDefaultItem();
  432. }
  433. }
  434. public void PutOnCurrentSuitSaved()
  435. {
  436. PutOnSuitSaved(CustomSuitDataManager.currentIndex);
  437. }
  438. public void PutOnDefaultSuitSaved(bool withBg = true)
  439. {
  440. TakeOffAll(false);
  441. CustomSuitData suitSavedData = CustomSuitDataManager.CreateDefaultSuitData(0);
  442. foreach (int itemID in suitSavedData.equipDatas)
  443. {
  444. AddOrRemove(itemID, false, true);
  445. }
  446. if (withBg)
  447. {
  448. if (suitSavedData.bg > 0)
  449. {
  450. AddOrRemove(suitSavedData.bg, false);
  451. }
  452. }
  453. checkDefaultItem();
  454. UpdatePicAction();
  455. }
  456. public void PutOnRecommendItems()
  457. {
  458. TakeOffAll(false);
  459. List<int> recommendList = DressUpMenuItemDataManager.GetRecommendItemList();
  460. StoryLevelCfg levelCfg = StoryLevelCfgArray.Instance.GetCfg(StoryDataManager.currentLevelID);
  461. StoryFightCfg fightCfg = StoryFightCfgArray.Instance.GetCfg(levelCfg.fightID);
  462. //推荐搭配自动穿必穿品
  463. if (fightCfg.needItemId > 0 && DressUpMenuItemDataManager.CheckHasItem(fightCfg.needItemId) && recommendList.IndexOf(fightCfg.needItemId) < 0)
  464. {
  465. recommendList.Add(fightCfg.needItemId);
  466. }
  467. else if (fightCfg.needSuitId > 0 && DressUpMenuSuitDataManager.CheckHaveSuit(fightCfg.needSuitId))
  468. {
  469. recommendList.Clear();
  470. SuitCfg cfg = SuitCfgArray.Instance.GetCfg(fightCfg.needSuitId);
  471. recommendList.AddRange(cfg.partsArr);
  472. }
  473. foreach (int itemID in recommendList)
  474. {
  475. AddOrRemove(itemID, false, true);
  476. }
  477. checkDefaultItem();
  478. UpdatePicAction();
  479. }
  480. public bool CheckCurrentScoreEnough()
  481. {
  482. return StoryDataManager.CheckCurrentScoreEnough(score);
  483. }
  484. public bool CheckEquipedFightNeeded()
  485. {
  486. StoryLevelCfg levelCfg = StoryLevelCfgArray.Instance.GetCfg(StoryDataManager.currentLevelID);
  487. StoryFightCfg fightCfg = StoryFightCfgArray.Instance.GetCfg(levelCfg.fightID);
  488. if (fightCfg.needItemId > 0)
  489. {
  490. return CheckDressUpItemIsOn(fightCfg.needItemId);
  491. }
  492. else if (fightCfg.needSuitId > 0)
  493. {
  494. return CheckSuitIsOn(fightCfg.needSuitId);
  495. }
  496. return true;
  497. }
  498. }
  499. }