DressUpObj.cs 20 KB

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