DressUpObj.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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 bool HasSuitActionRes
  45. {
  46. get
  47. {
  48. if (_dressUpData.suitId > 0)
  49. {
  50. return SuitCfgArray.Instance.CheckSuitHasAction(_dressUpData.suitId);
  51. }
  52. return false;
  53. }
  54. }
  55. public List<int> itemList
  56. {
  57. get
  58. {
  59. return _dressUpData.itemList.ToList();
  60. }
  61. }
  62. /// <summary>
  63. ///
  64. /// </summary>
  65. /// <param name="sceneObj"></param>
  66. /// <param name="needSetMask"></param>
  67. /// <param name="showSceneType"></param>
  68. /// <param name="roleObj"></param>
  69. /// <param name="showBg"></param>
  70. /// <param name="resetData">是否重置装备数据</param>
  71. public void setSceneObj(GameObject sceneObj, bool needSetMask = false, bool showSceneType = true, GameObject roleObj = null, bool showBg = true)
  72. {
  73. if (_sceneObj != null && _sceneObj != sceneObj)
  74. {
  75. GameObject.Destroy(_sceneObj);
  76. }
  77. _sceneObj = sceneObj;
  78. _needSetMask = needSetMask;
  79. _showSceneType = showSceneType;
  80. _showBg = showBg;
  81. _roleObj = roleObj;
  82. if(_dressUpData.IsNew)
  83. {
  84. PutOnDefaultDressUpData();
  85. }
  86. else
  87. {
  88. UpdateRoleView();
  89. }
  90. }
  91. public DressUpData DressUpDataClone()
  92. {
  93. return _dressUpData.Clone();
  94. }
  95. public void Dispose()
  96. {
  97. _sceneObj = null;
  98. _dressUpData = null;
  99. }
  100. //传入一个服装部件Id,判断是否有与此部件同类型服装已穿着
  101. public bool CheckSameTypeIsOn(int itemId)
  102. {
  103. int subType = ItemUtilCS.GetItemSubType(itemId);
  104. for (int i = 0; i < _dressUpData.itemList.Count; i++)
  105. {
  106. int _subType = ItemUtilCS.GetItemSubType(_dressUpData.itemList[i]);
  107. if (subType == _subType) return true;
  108. }
  109. return false;
  110. }
  111. /// <summary>
  112. /// 仅判断换装部件是否已穿着
  113. /// </summary>
  114. /// <param name="itemId"></param>
  115. /// <returns></returns>
  116. public bool CheckDressUpItemIsOn(int itemId)
  117. {
  118. if (itemId == _dressUpData.bgId)
  119. {
  120. return true;
  121. }
  122. return _dressUpData.itemList.Contains(itemId);
  123. }
  124. /// <summary>
  125. /// 仅判断套装是否穿上
  126. /// </summary>
  127. /// <param name="id"></param>
  128. /// <returns></returns>
  129. public bool CheckSuitIsOn(int id)
  130. {
  131. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(id);
  132. if (suitCfg == null)
  133. {
  134. return false;
  135. }
  136. int[] items = suitCfg.partsArr;
  137. foreach (int itemId in items)
  138. {
  139. if (_dressUpData.actionId == id)
  140. {
  141. if (SuitCfgArray.Instance.CheckActionContainsItem(itemId, id))
  142. {
  143. continue;
  144. }
  145. }
  146. bool isOn = CheckDressUpItemIsOn(itemId);
  147. if (!isOn)
  148. {
  149. return false;
  150. }
  151. }
  152. return true;
  153. }
  154. public void AddOrRemove(int itemId, bool checkDefault, bool isAdd = false, bool isRemove = false)
  155. {
  156. int subType = ItemUtilCS.GetItemSubType(itemId);
  157. if (subType == ConstDressUpItemType.BEI_JING)
  158. {
  159. if (!_showBg)
  160. {
  161. return;
  162. }
  163. _dressUpData.bgId = itemId;
  164. bool showAni = IsAction || DressUpMenuItemCfg1Array.Instance.CheckIsSceneType(itemId);
  165. DressUpUtil.AddItem(_dressUpData.bgId, _sceneObj, _needSetMask, showAni, _roleObj);
  166. }
  167. else
  168. {
  169. if (!CheckDressUpItemIsOn(itemId))
  170. {
  171. if (!isRemove)
  172. {
  173. checkRemoveSameType(subType);
  174. Add(itemId);
  175. }
  176. }
  177. else
  178. {
  179. if (!isAdd)
  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, false, true);
  230. }
  231. if (checkDefault)
  232. {
  233. checkDefaultItem();
  234. UpdateWholeBodyView();
  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 CheckCancelActionWhenPutOn(int itemId)
  257. {
  258. if (_dressUpData.actionId > 0)
  259. {
  260. bool replaceableByAction = SuitCfgArray.Instance.CheckItemReaplaceableByAction(itemId, _dressUpData.actionId);
  261. if (!replaceableByAction)
  262. {
  263. CancelAction();
  264. _dressUpData.actionId = 0;
  265. }
  266. }
  267. }
  268. //刷新视图,用于新设置sceneobj后的初始显示
  269. private void UpdateRoleView()
  270. {
  271. PutOnDressUpData(DressUpDataClone());
  272. }
  273. /// <summary>
  274. /// 尝试穿戴配置套装
  275. /// </summary>
  276. /// <param name="id">套装id</param>
  277. /// <param name="tryShowAction">尝试穿上动作</param>
  278. /// <param name="excludeType">排除类型列表</param>
  279. /// <param name="showOptional">是否显示可选部件</param>
  280. /// <param name="CheckOwn">是否只显示主角拥有的部件</param>
  281. public void PutOnSuitCfg(int id, bool tryShowAction, int[] excludeType = null, bool showOptional = true, bool CheckOwn = true)
  282. {
  283. if (_dressUpData.suitId == id)
  284. {
  285. return;
  286. }
  287. TakeOffAll(false);
  288. _dressUpData.suitId = id;
  289. _dressUpData.actionId = (HasSuitActionRes && tryShowAction) ? id : 0;
  290. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(_dressUpData.suitId);
  291. List<int> items = new List<int>(suitCfg.partsArr);
  292. if (showOptional)
  293. {
  294. if (suitCfg.partsOptionalArr != null && suitCfg.partsOptionalArr.Length > 0)
  295. {
  296. items.AddRange(suitCfg.partsOptionalArr);
  297. }
  298. }
  299. int subType = 0;
  300. foreach (int itemId in items)
  301. {
  302. if (!CheckOwn || DressUpMenuItemDataManager.CheckHasItem(itemId))
  303. {
  304. subType = ItemUtilCS.GetItemSubType(itemId);
  305. bool replaceableByAction = SuitCfgArray.Instance.CheckItemReaplaceableByAction(itemId, suitId);
  306. if (!IsAction || replaceableByAction)
  307. {
  308. if (excludeType == null || Array.IndexOf(excludeType, subType) < 0)
  309. {
  310. AddOrRemove(itemId, false, true);
  311. }
  312. }
  313. }
  314. }
  315. checkDefaultItem();
  316. UpdateWholeBodyView();
  317. }
  318. //穿戴一组换装数据
  319. public void PutOnDressUpData(DressUpData dressUpData)
  320. {
  321. TakeOffAll(false);
  322. _dressUpData.suitId = dressUpData.suitId;
  323. _dressUpData.actionId = dressUpData.actionId;
  324. if (dressUpData.bgId > 0)
  325. {
  326. AddOrRemove(dressUpData.bgId, false);
  327. }
  328. foreach (int itemID in dressUpData.itemList)
  329. {
  330. AddOrRemove(itemID, false, true);
  331. }
  332. checkDefaultItem();
  333. UpdateWholeBodyView();
  334. }
  335. //穿戴默认换装数据
  336. public void PutOnDefaultDressUpData()
  337. {
  338. PutOnDressUpData(DressUpData.CreateDefault());
  339. }
  340. //穿戴一组散件数据(会先脱掉原来的再穿)
  341. public void PutOnItemList(List<int> itemList)
  342. {
  343. TakeOffAll(false);
  344. foreach (int itemID in itemList)
  345. {
  346. AddOrRemove(itemID, false, true);
  347. }
  348. checkDefaultItem();
  349. UpdateWholeBodyView();
  350. }
  351. public int GetItemIdBuyType(int subType)
  352. {
  353. for (int i = 0; i < itemList.Count; i++)
  354. {
  355. 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)
  356. {
  357. if (subType == ItemUtilCS.GetItemSubType(itemList[i]))
  358. {
  359. return itemList[i];
  360. }
  361. }
  362. }
  363. if (suitId > 0)
  364. {
  365. return suitId;
  366. }
  367. return 0;
  368. }
  369. private void Add(int itemId)
  370. {
  371. if (!_showSceneType)
  372. {
  373. if (DressUpMenuItemCfg1Array.Instance.CheckIsSceneType(itemId))
  374. {
  375. return;
  376. }
  377. }
  378. if (!_dressUpData.itemList.Contains(itemId))
  379. {
  380. _dressUpData.itemList.Add(itemId);
  381. bool showAni = IsAction || DressUpMenuItemCfg1Array.Instance.CheckIsSceneType(itemId);
  382. DressUpUtil.AddItem(itemId, _sceneObj, _needSetMask, showAni, _roleObj);
  383. // FightDataManager.Instance.score += ItemDataManager.GetItemAdditionScore(itemId, InstanceZonesDataManager.currentScoreType, InstanceZonesDataManager.currentFightTags);
  384. }
  385. }
  386. private void Remove(int itemId)
  387. {
  388. if (_dressUpData.itemList == null)
  389. {
  390. return;
  391. }
  392. if (_dressUpData.itemList.Contains(itemId))
  393. {
  394. _dressUpData.itemList.Remove(itemId);
  395. DressUpUtil.RemoveItem(itemId, _sceneObj, _roleObj);
  396. // FightDataManager.Instance.score -= ItemDataManager.GetItemAdditionScore(itemId, InstanceZonesDataManager.currentScoreType, InstanceZonesDataManager.currentFightTags);
  397. }
  398. }
  399. //检测当前穿戴是否是一件完整套装,且只穿了一件套装,返回套装id
  400. private int CheckCurDressIsSuit()
  401. {
  402. var suitId = 0;
  403. var suitIdList = new List<int>();
  404. for (int i = 0; i < _dressUpData.itemList.Count; i++)
  405. {
  406. int itemSuitId = SuitCfgArray.Instance.GetSuitIdOfItem(_dressUpData.itemList[i]);
  407. if (!suitIdList.Contains(itemSuitId))
  408. {
  409. suitIdList.Add(itemSuitId);
  410. }
  411. }
  412. foreach (var itemSuitId in suitIdList)
  413. {
  414. if (CheckSuitIsOn(itemSuitId))
  415. {
  416. suitId = itemSuitId;
  417. break;
  418. }
  419. }
  420. _dressUpData.suitId = suitId;
  421. return _dressUpData.suitId;
  422. }
  423. private void checkDefaultItem()
  424. {
  425. //是否有头发
  426. bool hasFaXing = false;
  427. //检查默认资源
  428. //是否有连衣裙
  429. bool hasLianYiQun = false;
  430. //是否有内搭
  431. bool hasNeiDa = false;
  432. //是否有上衣
  433. bool hasShangYi = false;
  434. //是否有下装
  435. bool hasXiaZhuang = false;
  436. //是否有默认内搭
  437. bool hasNeiDaDefault = false;
  438. //是否有默认下装
  439. bool hasXiaZhuangDefault = false;
  440. for (int i = 0; i < _dressUpData.itemList.Count; i++)
  441. {
  442. int itemID = (int)_dressUpData.itemList[i];
  443. int subType = ItemUtilCS.GetItemSubType(itemID);
  444. if (subType == (int)ConstDressUpItemType.FA_XING)
  445. {
  446. hasFaXing = true;
  447. }
  448. else if (subType == ConstDressUpItemType.LIAN_YI_QUN)
  449. {
  450. hasLianYiQun = true;
  451. }
  452. else if (subType == ConstDressUpItemType.NEI_DA)
  453. {
  454. hasNeiDa = true;
  455. }
  456. else if (subType == ConstDressUpItemType.XIA_ZHUANG)
  457. {
  458. hasXiaZhuang = true;
  459. }
  460. else if (subType == ConstDressUpItemType.SHANG_YI)
  461. {
  462. hasShangYi = true;
  463. }
  464. if (itemID == ConstItemID.DEFULT_NEI_DA)
  465. {
  466. hasNeiDaDefault = true;
  467. }
  468. else if (itemID == ConstItemID.DEFULT_XIA_ZHUANG)
  469. {
  470. hasXiaZhuangDefault = true;
  471. }
  472. }
  473. if (!hasFaXing)
  474. {
  475. Add(ConstItemID.DEFULT_FA_XING);
  476. }
  477. if (!IsAction)
  478. {
  479. if (!hasLianYiQun)
  480. {
  481. if (!hasShangYi && (!hasNeiDa || hasNeiDaDefault) && (!hasXiaZhuang || hasXiaZhuangDefault))
  482. {
  483. Remove(ConstItemID.DEFULT_XIA_ZHUANG);
  484. Remove(ConstItemID.DEFULT_NEI_DA);
  485. Add(ConstItemID.DEFULT_LIAN_YI_QUN);
  486. }
  487. else
  488. {
  489. if (!hasXiaZhuang)
  490. {
  491. Add(ConstItemID.DEFULT_XIA_ZHUANG);
  492. }
  493. if (!hasNeiDa)
  494. {
  495. Add(ConstItemID.DEFULT_NEI_DA);
  496. }
  497. }
  498. }
  499. }
  500. CheckCurDressIsSuit();
  501. }
  502. //更新整个身体层(包括头和躯干)
  503. private void UpdateWholeBodyView()
  504. {
  505. if (IsAction)
  506. {
  507. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(_dressUpData.actionId);
  508. var hasAniRes = !string.IsNullOrEmpty(suitCfg.aniRes);
  509. var res = hasAniRes ? suitCfg.aniRes : suitCfg.picRes;
  510. DressUpUtil.UpdateWholeBody(res, _sceneObj, hasAniRes, suitCfg.effRes, false, _roleObj);
  511. }
  512. else
  513. {
  514. DressUpUtil.UpdateWholeBody(null, _sceneObj, false, null, _needSetMask, _roleObj);
  515. }
  516. }
  517. //穿上一个动作
  518. public void PutOnAction(int actionId)
  519. {
  520. _dressUpData.actionId = actionId;
  521. foreach (int itemId in itemList)
  522. {
  523. bool replaceableByAction = SuitCfgArray.Instance.CheckItemReaplaceableByAction(itemId, _dressUpData.actionId);
  524. if (!replaceableByAction)
  525. {
  526. AddOrRemove(itemId, false, false, true);
  527. }
  528. }
  529. checkDefaultItem();
  530. UpdateWholeBodyView();
  531. }
  532. //取消动作
  533. public void CancelAction(bool changeToStand = false)
  534. {
  535. if (_dressUpData.actionId <= 0)
  536. {
  537. return;
  538. }
  539. var tempActionId = _dressUpData.actionId;
  540. _dressUpData.actionId = 0;
  541. if(changeToStand)
  542. {
  543. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(tempActionId);
  544. List<int> items = new List<int>(suitCfg.partsArr);
  545. if (suitCfg.partsOptionalArr != null && suitCfg.partsOptionalArr.Length > 0)
  546. {
  547. items.AddRange(suitCfg.partsOptionalArr);
  548. }
  549. int subType = 0;
  550. foreach (int itemId in items)
  551. {
  552. if (DressUpMenuItemDataManager.CheckHasItem(itemId))
  553. {
  554. subType = ItemUtilCS.GetItemSubType(itemId);
  555. bool replaceableByAction = SuitCfgArray.Instance.CheckItemReaplaceableByAction(itemId, tempActionId);
  556. if (!replaceableByAction)
  557. {
  558. AddOrRemove(itemId, false, true);
  559. }
  560. }
  561. }
  562. }
  563. checkDefaultItem();
  564. UpdateWholeBodyView();
  565. }
  566. }
  567. }