DressUpObj.cs 22 KB

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