DressUpObj.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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. public 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. if (changeToStand)
  353. {
  354. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(tempActionId);
  355. List<int> items = new List<int>(suitCfg.partsArr);
  356. if (suitCfg.partsOptionalArr != null && suitCfg.partsOptionalArr.Length > 0)
  357. {
  358. items.AddRange(suitCfg.partsOptionalArr);
  359. }
  360. foreach (int itemId in items)
  361. {
  362. if (DressUpMenuItemDataManager.CheckHasItem(itemId))
  363. {
  364. bool notInAction = SuitCfgArray.Instance.CheckItemNotInAction(itemId);
  365. if (!notInAction)
  366. {
  367. AddOrRemove(itemId, false, DressUpOption.Add);
  368. }
  369. }
  370. }
  371. }
  372. //更新非场景类型部件形态
  373. foreach (int itemId in itemList)
  374. {
  375. if (!DressUpMenuItemCfg1Array.Instance.CheckIsSceneType(itemId))
  376. {
  377. //场景类型不受动作影响并且本来就有动画,对非场景类型处理
  378. bool notInAction = SuitCfgArray.Instance.CheckItemNotInAction(itemId);
  379. if (notInAction)
  380. {
  381. //更新成图片模式
  382. AddOrRemove(itemId, false, DressUpOption.Add);
  383. }
  384. }
  385. }
  386. checkDefaultItem();
  387. }
  388. //穿戴一组换装数据
  389. public void PutOnDressUpData(DressUpData targetDressUpData, int[] excludeType = null)
  390. {
  391. bool oldIsAction = IsAction;
  392. _dressUpData.suitId = targetDressUpData.suitId;
  393. _dressUpData.actionId = targetDressUpData.actionId;
  394. var targetItemList = new List<int>();
  395. foreach(var itemId in targetDressUpData.itemList)
  396. {
  397. int subType = ItemUtilCS.GetItemSubType(itemId);
  398. if (excludeType == null || Array.IndexOf(excludeType, subType) < 0)
  399. {
  400. targetItemList.Add(itemId);
  401. }
  402. }
  403. if (targetDressUpData.bgId > 0 && (excludeType == null || Array.IndexOf(excludeType, ConstDressUpItemType.BEI_JING) < 0))
  404. {
  405. targetItemList.Add(targetDressUpData.bgId);
  406. }
  407. CompareAndAddItemList(oldIsAction, IsAction, targetItemList);
  408. checkDefaultItem();
  409. }
  410. //穿戴默认换装数据
  411. public void PutOnDefaultDressUpData()
  412. {
  413. PutOnDressUpData(DressUpData.CreateDefault());
  414. }
  415. //穿戴一组散件数据(会脱掉不包含的部分)
  416. public void PutOnItemList(List<int> targetItemList)
  417. {
  418. bool oldIsAction = IsAction;
  419. _dressUpData.suitId = 0;
  420. _dressUpData.actionId = 0;
  421. CompareAndAddItemList(oldIsAction, IsAction, targetItemList);
  422. checkDefaultItem();
  423. }
  424. public int GetItemIdBuyType(int subType)
  425. {
  426. for (int i = 0; i < itemList.Count; i++)
  427. {
  428. 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)
  429. {
  430. if (subType == ItemUtilCS.GetItemSubType(itemList[i]))
  431. {
  432. return itemList[i];
  433. }
  434. }
  435. }
  436. if (suitId > 0)
  437. {
  438. return suitId;
  439. }
  440. return 0;
  441. }
  442. private void Add(int itemId)
  443. {
  444. if (!_showSceneType)
  445. {
  446. if (DressUpMenuItemCfg1Array.Instance.CheckIsSceneType(itemId))
  447. {
  448. return;
  449. }
  450. }
  451. if (!_dressUpData.itemList.Contains(itemId))
  452. {
  453. _dressUpData.itemList.Add(itemId);
  454. bool showAni = IsAction || DressUpMenuItemCfg1Array.Instance.CheckIsSceneType(itemId);
  455. DressUpUtil.AddItem(itemId, _sceneObj, _needSetMask, showAni, _roleObj);
  456. // FightDataManager.Instance.score += ItemDataManager.GetItemAdditionScore(itemId, InstanceZonesDataManager.currentScoreType, InstanceZonesDataManager.currentFightTags);
  457. }
  458. }
  459. private void Remove(int itemId)
  460. {
  461. if (_dressUpData.itemList == null)
  462. {
  463. return;
  464. }
  465. if (_dressUpData.itemList.Contains(itemId))
  466. {
  467. _dressUpData.itemList.Remove(itemId);
  468. DressUpUtil.RemoveItem(itemId, _sceneObj, _roleObj);
  469. // FightDataManager.Instance.score -= ItemDataManager.GetItemAdditionScore(itemId, InstanceZonesDataManager.currentScoreType, InstanceZonesDataManager.currentFightTags);
  470. }
  471. }
  472. //与身上的散件对比差异然后添加
  473. private void CompareAndAddItemList(bool oldIsAction, bool newIsAction, List<int> targetItemList)
  474. {
  475. bool actionStatusChanged = (oldIsAction && !newIsAction) || (!oldIsAction && newIsAction);
  476. foreach (int itemID in itemList)
  477. {
  478. if (!targetItemList.Contains(itemID))
  479. {
  480. //移除不穿的部件
  481. AddOrRemove(itemID, false, DressUpOption.Remove);
  482. }
  483. else if (actionStatusChanged)
  484. {
  485. //当动画形态切换时
  486. if (!DressUpMenuItemCfg1Array.Instance.CheckIsSceneType(itemID))
  487. {
  488. //非场景类型重穿
  489. AddOrRemove(itemID, false, DressUpOption.Remove);
  490. }
  491. }
  492. }
  493. foreach (int itemID in targetItemList)
  494. {
  495. AddOrRemove(itemID, false, DressUpOption.Add);
  496. }
  497. }
  498. //检测当前穿戴是否是一件完整套装,且只穿了一件套装,返回套装id
  499. private int CheckCurDressIsSuit()
  500. {
  501. var suitId = 0;
  502. var suitIdList = new List<int>();
  503. for (int i = 0; i < _dressUpData.itemList.Count; i++)
  504. {
  505. int itemSuitId = SuitCfgArray.Instance.GetSuitIdOfItem(_dressUpData.itemList[i]);
  506. if (!suitIdList.Contains(itemSuitId))
  507. {
  508. suitIdList.Add(itemSuitId);
  509. }
  510. }
  511. foreach (var itemSuitId in suitIdList)
  512. {
  513. if (CheckSuitIsOn(itemSuitId))
  514. {
  515. suitId = itemSuitId;
  516. break;
  517. }
  518. }
  519. _dressUpData.suitId = suitId;
  520. return _dressUpData.suitId;
  521. }
  522. private void checkDefaultItem()
  523. {
  524. //是否有头发
  525. bool hasFaXing = false;
  526. //检查默认资源
  527. //是否有连衣裙
  528. bool hasLianYiQun = false;
  529. //是否有内搭
  530. bool hasNeiDa = false;
  531. //是否有上衣
  532. bool hasShangYi = false;
  533. //是否有下装
  534. bool hasXiaZhuang = false;
  535. //是否有默认内搭
  536. bool hasNeiDaDefault = false;
  537. //是否有默认下装
  538. bool hasXiaZhuangDefault = false;
  539. //是否有妆容
  540. bool hasZhuangRong = false;
  541. for (int i = 0; i < _dressUpData.itemList.Count; i++)
  542. {
  543. int itemID = (int)_dressUpData.itemList[i];
  544. int subType = ItemUtilCS.GetItemSubType(itemID);
  545. if (subType == (int)ConstDressUpItemType.FA_XING)
  546. {
  547. hasFaXing = true;
  548. }
  549. else if (subType == ConstDressUpItemType.LIAN_YI_QUN)
  550. {
  551. hasLianYiQun = true;
  552. }
  553. else if (subType == ConstDressUpItemType.NEI_DA)
  554. {
  555. hasNeiDa = true;
  556. }
  557. else if (subType == ConstDressUpItemType.XIA_ZHUANG)
  558. {
  559. hasXiaZhuang = true;
  560. }
  561. else if (subType == ConstDressUpItemType.SHANG_YI)
  562. {
  563. hasShangYi = true;
  564. }
  565. else if (subType == ConstDressUpItemType.ZHUANG_RONG)
  566. {
  567. hasZhuangRong = true;
  568. }
  569. if (itemID == ConstItemID.DEFULT_NEI_DA)
  570. {
  571. hasNeiDaDefault = true;
  572. }
  573. else if (itemID == ConstItemID.DEFULT_XIA_ZHUANG)
  574. {
  575. hasXiaZhuangDefault = true;
  576. }
  577. }
  578. if (!hasFaXing)
  579. {
  580. Add(ConstItemID.DEFULT_FA_XING);
  581. }
  582. if (!IsAction)
  583. {
  584. if (!hasLianYiQun)
  585. {
  586. if (!hasShangYi && (!hasNeiDa || hasNeiDaDefault) && (!hasXiaZhuang || hasXiaZhuangDefault))
  587. {
  588. Remove(ConstItemID.DEFULT_XIA_ZHUANG);
  589. Remove(ConstItemID.DEFULT_NEI_DA);
  590. Add(ConstItemID.DEFULT_LIAN_YI_QUN);
  591. }
  592. else
  593. {
  594. if (!hasXiaZhuang)
  595. {
  596. Add(ConstItemID.DEFULT_XIA_ZHUANG);
  597. }
  598. if (!hasNeiDa)
  599. {
  600. Add(ConstItemID.DEFULT_NEI_DA);
  601. }
  602. }
  603. }
  604. }
  605. CheckCurDressIsSuit();
  606. DressUpUtil.UpdateHead(!hasZhuangRong, _sceneObj, _needSetMask, _roleObj);
  607. UpdateBodyView();
  608. }
  609. //更新整个身体
  610. private void UpdateBodyView()
  611. {
  612. if (IsAction)
  613. {
  614. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(_dressUpData.actionId);
  615. var hasAniRes = ResPathUtil.CheckDressUpAnimationResExist(suitCfg.aniRes);
  616. var actionRes = hasAniRes ? suitCfg.aniRes : null;
  617. DressUpUtil.UpdateBody(actionRes, _sceneObj, false, _roleObj);
  618. }
  619. else
  620. {
  621. DressUpUtil.UpdateBody(null, _sceneObj, _needSetMask, _roleObj);
  622. }
  623. }
  624. }
  625. }