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