DressUpObj.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using System;
  4. using System.Linq;
  5. using ET;
  6. namespace GFGGame
  7. {
  8. public enum DressUpOption
  9. {
  10. Auto,
  11. Add,
  12. Remove
  13. }
  14. public class DressUpObj
  15. {
  16. private GameObject _sceneObj;
  17. private GameObject _roleObj;
  18. private bool _needSetMask;
  19. private bool _showSceneType = true;
  20. private bool _showBg = true;
  21. private DressUpData _dressUpData = new DressUpData();
  22. public int bgId
  23. {
  24. get
  25. {
  26. return _dressUpData.bgId;
  27. }
  28. }
  29. public int suitId
  30. {
  31. get
  32. {
  33. return _dressUpData.suitId;
  34. }
  35. }
  36. public int actionId
  37. {
  38. get
  39. {
  40. return _dressUpData.actionId;
  41. }
  42. }
  43. public bool IsAction
  44. {
  45. get
  46. {
  47. return _dressUpData.actionId > 0;
  48. }
  49. }
  50. public List<int> itemList
  51. {
  52. get
  53. {
  54. return _dressUpData.itemList.ToList();
  55. }
  56. }
  57. /// <summary>
  58. ///
  59. /// </summary>
  60. /// <param name="sceneObj"></param>
  61. /// <param name="needSetMask"></param>
  62. /// <param name="showSceneType"></param>
  63. /// <param name="roleObj"></param>
  64. /// <param name="showBg"></param>
  65. /// <param name="resetData">是否重置装备数据</param>
  66. public void setSceneObj(GameObject sceneObj, bool needSetMask = false, bool showSceneType = true, GameObject roleObj = null, bool showBg = true)
  67. {
  68. if (_sceneObj != null && _sceneObj != sceneObj)
  69. {
  70. GameObject.Destroy(_sceneObj);
  71. }
  72. _sceneObj = sceneObj;
  73. _needSetMask = needSetMask;
  74. _showSceneType = showSceneType;
  75. _showBg = showBg;
  76. _roleObj = roleObj;
  77. if (_dressUpData.IsNew)
  78. {
  79. PutOnDefaultDressUpData();
  80. }
  81. else
  82. {
  83. var tempData = DressUpDataClone();
  84. TakeOffAll();
  85. PutOnDressUpData(tempData);
  86. }
  87. }
  88. public DressUpData DressUpDataClone()
  89. {
  90. return _dressUpData.Clone();
  91. }
  92. public void Dispose()
  93. {
  94. _sceneObj = null;
  95. _dressUpData = null;
  96. }
  97. //传入一个服装部件Id,判断是否有与此部件同类型服装已穿着
  98. public bool CheckSameTypeIsOn(int itemId)
  99. {
  100. int subType = ItemUtilCS.GetItemSubType(itemId);
  101. for (int i = 0; i < _dressUpData.itemList.Count; i++)
  102. {
  103. int _subType = ItemUtilCS.GetItemSubType(_dressUpData.itemList[i]);
  104. if (subType == _subType) return true;
  105. }
  106. return false;
  107. }
  108. /// <summary>
  109. /// 仅判断换装部件是否已穿着
  110. /// </summary>
  111. /// <param name="itemId"></param>
  112. /// <returns></returns>
  113. public bool CheckDressUpItemIsOn(int itemId)
  114. {
  115. if (itemId == _dressUpData.bgId)
  116. {
  117. return true;
  118. }
  119. return _dressUpData.itemList.Contains(itemId);
  120. }
  121. /// <summary>
  122. /// 仅判断套装是否穿上
  123. /// </summary>
  124. /// <param name="id"></param>
  125. /// <returns></returns>
  126. public bool CheckSuitIsOn(int id)
  127. {
  128. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(id);
  129. if (suitCfg == null)
  130. {
  131. return false;
  132. }
  133. int[] items = suitCfg.partsArr;
  134. foreach (int itemId in items)
  135. {
  136. if (_dressUpData.actionId == id)
  137. {
  138. if (SuitCfgArray.Instance.CheckActionContainsItem(itemId, id))
  139. {
  140. continue;
  141. }
  142. }
  143. bool isOn = CheckDressUpItemIsOn(itemId);
  144. if (!isOn)
  145. {
  146. return false;
  147. }
  148. }
  149. return true;
  150. }
  151. public void AddOrRemove(int itemId, bool checkDefault, DressUpOption dressUpOption = DressUpOption.Auto)
  152. {
  153. int subType = ItemUtilCS.GetItemSubType(itemId);
  154. if (subType == ConstDressUpItemType.BEI_JING)
  155. {
  156. if (!_showBg)
  157. {
  158. return;
  159. }
  160. _dressUpData.bgId = itemId;
  161. DressUpUtil.AddItem(_dressUpData.bgId, _sceneObj, _needSetMask, true, _roleObj);
  162. }
  163. else
  164. {
  165. if (!CheckDressUpItemIsOn(itemId))
  166. {
  167. if (dressUpOption != DressUpOption.Remove)
  168. {
  169. if (checkDefault)
  170. {
  171. TryCancelActionWhenPutOn(itemId);
  172. }
  173. checkRemoveSameType(subType);
  174. Add(itemId);
  175. }
  176. }
  177. else
  178. {
  179. if (dressUpOption != DressUpOption.Add)
  180. {
  181. Remove(itemId);
  182. }
  183. }
  184. if (checkDefault)
  185. {
  186. checkDefaultItem();
  187. }
  188. }
  189. }
  190. public void checkRemoveSameType(int type)
  191. {
  192. int count = 0;
  193. int firstTeshuId = 0;
  194. for (int i = 0; i < _dressUpData.itemList.Count; i++)
  195. {
  196. int itemID = (int)_dressUpData.itemList[i];
  197. int subType = ItemUtilCS.GetItemSubType(itemID);
  198. if (subType == type
  199. || (type == ConstDressUpItemType.LIAN_YI_QUN && (subType == ConstDressUpItemType.SHANG_YI || subType == ConstDressUpItemType.XIA_ZHUANG || subType == ConstDressUpItemType.NEI_DA))
  200. || (type == ConstDressUpItemType.SHANG_YI && subType == ConstDressUpItemType.LIAN_YI_QUN)
  201. || (type == ConstDressUpItemType.XIA_ZHUANG && subType == ConstDressUpItemType.LIAN_YI_QUN)
  202. || (type == ConstDressUpItemType.NEI_DA && subType == ConstDressUpItemType.LIAN_YI_QUN))
  203. {
  204. Remove(itemID);
  205. i--;
  206. }
  207. if (subType > ConstDressUpItemType.TE_SHU)
  208. {
  209. if (count == 0)
  210. {
  211. firstTeshuId = itemID;
  212. }
  213. count++;
  214. }
  215. }
  216. if (type > ConstDressUpItemType.TE_SHU && count >= 3)
  217. {
  218. //特殊饰品最多穿三件,第四件会自动顶掉第一件
  219. Remove(firstTeshuId);
  220. }
  221. }
  222. //脱掉所有换装,换成默认装(不处理背景)
  223. public void TakeOffAll(bool checkDefault = true)
  224. {
  225. _dressUpData.suitId = 0;
  226. _dressUpData.actionId = 0;
  227. foreach (int itemID in itemList)
  228. {
  229. AddOrRemove(itemID, false, DressUpOption.Remove);
  230. }
  231. if (checkDefault)
  232. {
  233. checkDefaultItem();
  234. }
  235. RoleLevelCfg roleLevelCfg = RoleLevelCfgArray.Instance.GetCfg(GameGlobal.myNumericComponent.GetAsInt(NumericType.Lvl));
  236. // FightDataManager.Instance.score = roleLevelCfg.baseScore;
  237. // foreach (int itemId in _dressUpData.itemList)
  238. // {
  239. // FightDataManager.Instance.score += ItemDataManager.GetItemAdditionScore(itemId, InstanceZonesDataManager.currentScoreType, InstanceZonesDataManager.currentFightTags);
  240. // }
  241. }
  242. //穿上或脱掉一个动作
  243. public void PutOnOrTakeOffAction(int actionId)
  244. {
  245. if (_dressUpData.actionId == actionId)
  246. {
  247. CancelAction();
  248. }
  249. else
  250. {
  251. PutOnAction(actionId);
  252. }
  253. }
  254. //当穿一件部件时检查是否需要取消动作,如果需要则取消该动作
  255. public void TryCancelActionWhenPutOn(int itemId)
  256. {
  257. if (_dressUpData.actionId > 0)
  258. {
  259. bool replaceableByAction = SuitCfgArray.Instance.CheckItemReaplaceableOnAction(itemId, _dressUpData.actionId);
  260. if (!replaceableByAction)
  261. {
  262. CancelAction();
  263. _dressUpData.actionId = 0;
  264. }
  265. }
  266. }
  267. /// <summary>
  268. /// 尝试穿戴配置套装
  269. /// </summary>
  270. /// <param name="suitId">套装id</param>
  271. /// <param name="tryShowAction">尝试穿上动作</param>
  272. /// <param name="excludeType">排除类型列表</param>
  273. /// <param name="showOptional">是否显示可选部件</param>
  274. /// <param name="CheckOwn">是否只显示主角拥有的部件</param>
  275. public void PutOnSuitCfg(int suitId, bool tryShowAction, int[] excludeType = null, bool showOptional = true, bool CheckOwn = true)
  276. {
  277. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(suitId);
  278. if(suitCfg == null)
  279. {
  280. return;
  281. }
  282. bool oldIsAction = IsAction;
  283. _dressUpData.suitId = suitId;
  284. bool hasSuitActionRes = DressUpMenuSuitDataManager.CheckSuitHasActionRes(_dressUpData.suitId);
  285. _dressUpData.actionId = (hasSuitActionRes && tryShowAction) ? suitId : 0;
  286. List<int> items = new List<int>(suitCfg.partsArr);
  287. if (showOptional)
  288. {
  289. if (suitCfg.partsOptionalArr != null && suitCfg.partsOptionalArr.Length > 0)
  290. {
  291. items.AddRange(suitCfg.partsOptionalArr);
  292. }
  293. }
  294. int subType = 0;
  295. //找到要穿的散件
  296. List<int> targetItemList = new List<int>();
  297. foreach (int itemId in items)
  298. {
  299. if (!CheckOwn || DressUpMenuItemDataManager.CheckHasItem(itemId))
  300. {
  301. subType = ItemUtilCS.GetItemSubType(itemId);
  302. bool notInAction = SuitCfgArray.Instance.CheckItemNotInAction(itemId);
  303. if (!IsAction || notInAction)
  304. {
  305. if (excludeType == null || Array.IndexOf(excludeType, subType) < 0)
  306. {
  307. targetItemList.Add(itemId);
  308. }
  309. }
  310. }
  311. }
  312. CompareAndAddItemList(oldIsAction, IsAction, targetItemList);
  313. checkDefaultItem();
  314. }
  315. //穿上一个动作
  316. private void PutOnAction(int actionId)
  317. {
  318. bool hasSuitActionRes = DressUpMenuSuitDataManager.CheckSuitHasActionRes(actionId);
  319. _dressUpData.actionId = actionId;
  320. if(hasSuitActionRes)
  321. {
  322. foreach (int itemId in itemList)
  323. {
  324. if(!DressUpMenuItemCfg1Array.Instance.CheckIsSceneType(itemId))
  325. {
  326. //场景类型不受动作影响并且本来就有动画,对非场景类型处理
  327. AddOrRemove(itemId, false, DressUpOption.Remove);
  328. bool notInAction = SuitCfgArray.Instance.CheckItemNotInAction(itemId);
  329. if (notInAction)
  330. {
  331. //更新成动画模式
  332. AddOrRemove(itemId, false, DressUpOption.Add);
  333. }
  334. }
  335. }
  336. }
  337. else
  338. {
  339. CancelAction(true);
  340. }
  341. checkDefaultItem();
  342. }
  343. //取消动作
  344. public void CancelAction(bool changeToStand = false)
  345. {
  346. if (_dressUpData.actionId <= 0)
  347. {
  348. return;
  349. }
  350. var tempActionId = _dressUpData.actionId;
  351. _dressUpData.actionId = 0;
  352. //更新非场景类型部件形态
  353. foreach (int itemId in itemList)
  354. {
  355. if (!DressUpMenuItemCfg1Array.Instance.CheckIsSceneType(itemId))
  356. {
  357. //场景类型不受动作影响并且本来就有动画,对非场景类型处理
  358. bool notInAction = SuitCfgArray.Instance.CheckItemNotInAction(itemId);
  359. if (notInAction)
  360. {
  361. //更新成图片模式
  362. AddOrRemove(itemId, false, DressUpOption.Remove);
  363. AddOrRemove(itemId, false, DressUpOption.Add);
  364. }
  365. }
  366. }
  367. if (changeToStand)
  368. {
  369. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(tempActionId);
  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. foreach (int itemId in items)
  376. {
  377. if (DressUpMenuItemDataManager.CheckHasItem(itemId))
  378. {
  379. bool notInAction = SuitCfgArray.Instance.CheckItemNotInAction(itemId);
  380. if (!notInAction)
  381. {
  382. AddOrRemove(itemId, false, DressUpOption.Add);
  383. }
  384. }
  385. }
  386. }
  387. checkDefaultItem();
  388. }
  389. //穿戴一组换装数据
  390. public void PutOnDressUpData(DressUpData targetDressUpData, int[] excludeType = null)
  391. {
  392. bool oldIsAction = IsAction;
  393. _dressUpData.suitId = targetDressUpData.suitId;
  394. _dressUpData.actionId = targetDressUpData.actionId;
  395. var targetItemList = new List<int>();
  396. foreach(var itemId in targetDressUpData.itemList)
  397. {
  398. int subType = ItemUtilCS.GetItemSubType(itemId);
  399. if (excludeType == null || Array.IndexOf(excludeType, subType) < 0)
  400. {
  401. targetItemList.Add(itemId);
  402. }
  403. }
  404. if (targetDressUpData.bgId > 0 && (excludeType == null || Array.IndexOf(excludeType, ConstDressUpItemType.BEI_JING) < 0))
  405. {
  406. targetItemList.Add(targetDressUpData.bgId);
  407. }
  408. CompareAndAddItemList(oldIsAction, IsAction, targetItemList);
  409. checkDefaultItem();
  410. }
  411. //穿戴默认换装数据
  412. public void PutOnDefaultDressUpData(bool bgType = false)
  413. {
  414. var dressUpData = DressUpData.CreateDefault();
  415. if (bgType) {
  416. dressUpData.bgId = 180002;
  417. }
  418. PutOnDressUpData(dressUpData);
  419. }
  420. //穿戴一组散件数据(会脱掉不包含的部分)
  421. public void PutOnItemList(List<int> targetItemList)
  422. {
  423. bool oldIsAction = IsAction;
  424. _dressUpData.suitId = 0;
  425. _dressUpData.actionId = 0;
  426. CompareAndAddItemList(oldIsAction, IsAction, targetItemList);
  427. checkDefaultItem();
  428. }
  429. public int GetItemIdBuyType(int subType)
  430. {
  431. for (int i = 0; i < itemList.Count; i++)
  432. {
  433. if (itemList[i] != ConstItemID.DEFULT_LIAN_YI_QUN && itemList[i] != ConstItemID.DEFULT_NEI_DA && itemList[i] != ConstItemID.DEFULT_XIA_ZHUANG && itemList[i] != ConstItemID.DEFULT_FA_XING)
  434. {
  435. if (subType == ItemUtilCS.GetItemSubType(itemList[i]))
  436. {
  437. return itemList[i];
  438. }
  439. }
  440. }
  441. if (suitId > 0)
  442. {
  443. return suitId;
  444. }
  445. return 0;
  446. }
  447. private void Add(int itemId)
  448. {
  449. if (!_showSceneType)
  450. {
  451. if (DressUpMenuItemCfg1Array.Instance.CheckIsSceneType(itemId))
  452. {
  453. return;
  454. }
  455. }
  456. if (!_dressUpData.itemList.Contains(itemId))
  457. {
  458. _dressUpData.itemList.Add(itemId);
  459. bool showAni = IsAction || DressUpMenuItemCfg1Array.Instance.CheckIsSceneType(itemId);
  460. DressUpUtil.AddItem(itemId, _sceneObj, _needSetMask, showAni, _roleObj);
  461. // FightDataManager.Instance.score += ItemDataManager.GetItemAdditionScore(itemId, InstanceZonesDataManager.currentScoreType, InstanceZonesDataManager.currentFightTags);
  462. }
  463. }
  464. private void Remove(int itemId)
  465. {
  466. if (_dressUpData.itemList == null)
  467. {
  468. return;
  469. }
  470. if (_dressUpData.itemList.Contains(itemId))
  471. {
  472. _dressUpData.itemList.Remove(itemId);
  473. DressUpUtil.RemoveItem(itemId, _sceneObj, _roleObj);
  474. // FightDataManager.Instance.score -= ItemDataManager.GetItemAdditionScore(itemId, InstanceZonesDataManager.currentScoreType, InstanceZonesDataManager.currentFightTags);
  475. }
  476. }
  477. //与身上的散件对比差异然后添加
  478. private void CompareAndAddItemList(bool oldIsAction, bool newIsAction, List<int> targetItemList)
  479. {
  480. bool actionStatusChanged = (oldIsAction && !newIsAction) || (!oldIsAction && newIsAction);
  481. foreach (int itemID in itemList)
  482. {
  483. if (!targetItemList.Contains(itemID))
  484. {
  485. //移除不穿的部件
  486. AddOrRemove(itemID, false, DressUpOption.Remove);
  487. }
  488. else if (actionStatusChanged)
  489. {
  490. //当动画形态切换时
  491. if (!DressUpMenuItemCfg1Array.Instance.CheckIsSceneType(itemID))
  492. {
  493. //非场景类型重穿
  494. AddOrRemove(itemID, false, DressUpOption.Remove);
  495. }
  496. }
  497. }
  498. foreach (int itemID in targetItemList)
  499. {
  500. AddOrRemove(itemID, false, DressUpOption.Add);
  501. }
  502. }
  503. //检测当前穿戴是否是一件完整套装,且只穿了一件套装,返回套装id
  504. private int CheckCurDressIsSuit()
  505. {
  506. var suitId = 0;
  507. var suitIdList = new List<int>();
  508. for (int i = 0; i < _dressUpData.itemList.Count; i++)
  509. {
  510. int itemSuitId = SuitCfgArray.Instance.GetSuitIdOfItem(_dressUpData.itemList[i]);
  511. if (!suitIdList.Contains(itemSuitId))
  512. {
  513. suitIdList.Add(itemSuitId);
  514. }
  515. }
  516. foreach (var itemSuitId in suitIdList)
  517. {
  518. if (CheckSuitIsOn(itemSuitId))
  519. {
  520. suitId = itemSuitId;
  521. break;
  522. }
  523. }
  524. _dressUpData.suitId = suitId;
  525. return _dressUpData.suitId;
  526. }
  527. private void checkDefaultItem()
  528. {
  529. //是否有头发
  530. bool hasFaXing = false;
  531. //检查默认资源
  532. //是否有连衣裙
  533. bool hasLianYiQun = false;
  534. //是否有内搭
  535. bool hasNeiDa = false;
  536. //是否有上衣
  537. bool hasShangYi = false;
  538. //是否有下装
  539. bool hasXiaZhuang = false;
  540. //是否有默认内搭
  541. bool hasNeiDaDefault = false;
  542. //是否有默认下装
  543. bool hasXiaZhuangDefault = false;
  544. //是否有妆容
  545. bool hasZhuangRong = false;
  546. for (int i = 0; i < _dressUpData.itemList.Count; i++)
  547. {
  548. int itemID = (int)_dressUpData.itemList[i];
  549. int subType = ItemUtilCS.GetItemSubType(itemID);
  550. if (subType == (int)ConstDressUpItemType.FA_XING)
  551. {
  552. hasFaXing = true;
  553. }
  554. else if (subType == ConstDressUpItemType.LIAN_YI_QUN)
  555. {
  556. hasLianYiQun = true;
  557. }
  558. else if (subType == ConstDressUpItemType.NEI_DA)
  559. {
  560. hasNeiDa = true;
  561. }
  562. else if (subType == ConstDressUpItemType.XIA_ZHUANG)
  563. {
  564. hasXiaZhuang = true;
  565. }
  566. else if (subType == ConstDressUpItemType.SHANG_YI)
  567. {
  568. hasShangYi = true;
  569. }
  570. else if (subType == ConstDressUpItemType.ZHUANG_RONG)
  571. {
  572. hasZhuangRong = true;
  573. }
  574. if (itemID == ConstItemID.DEFULT_NEI_DA)
  575. {
  576. hasNeiDaDefault = true;
  577. }
  578. else if (itemID == ConstItemID.DEFULT_XIA_ZHUANG)
  579. {
  580. hasXiaZhuangDefault = true;
  581. }
  582. }
  583. if (!hasFaXing)
  584. {
  585. Add(ConstItemID.DEFULT_FA_XING);
  586. }
  587. if (!IsAction)
  588. {
  589. if (!hasLianYiQun)
  590. {
  591. if (!hasShangYi && (!hasNeiDa || hasNeiDaDefault) && (!hasXiaZhuang || hasXiaZhuangDefault))
  592. {
  593. Remove(ConstItemID.DEFULT_XIA_ZHUANG);
  594. Remove(ConstItemID.DEFULT_NEI_DA);
  595. Add(ConstItemID.DEFULT_LIAN_YI_QUN);
  596. }
  597. else
  598. {
  599. if (!hasXiaZhuang)
  600. {
  601. Add(ConstItemID.DEFULT_XIA_ZHUANG);
  602. }
  603. if (!hasNeiDa)
  604. {
  605. Add(ConstItemID.DEFULT_NEI_DA);
  606. }
  607. }
  608. }
  609. }
  610. CheckCurDressIsSuit();
  611. DressUpUtil.UpdateHead(!hasZhuangRong, _sceneObj, _needSetMask, _roleObj);
  612. UpdateBodyView();
  613. }
  614. //更新整个身体
  615. private void UpdateBodyView()
  616. {
  617. if (IsAction)
  618. {
  619. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(_dressUpData.actionId);
  620. var hasAniRes = ResPathUtil.CheckDressUpAnimationResExist(suitCfg.aniRes);
  621. var actionRes = hasAniRes ? suitCfg.aniRes : null;
  622. DressUpUtil.UpdateBody(actionRes, _sceneObj, false, _roleObj);
  623. }
  624. else
  625. {
  626. DressUpUtil.UpdateBody(null, _sceneObj, _needSetMask, _roleObj);
  627. }
  628. }
  629. }
  630. }