DressUpObj.cs 20 KB

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