DressUpObj.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using System;
  4. using System.Linq;
  5. using ET;
  6. namespace GFGGame
  7. {
  8. public enum DressUpOption
  9. {
  10. Auto,
  11. Add,
  12. Remove
  13. }
  14. public class DressUpObj
  15. {
  16. private GameObject _sceneObj;
  17. private GameObject _roleObj;
  18. private bool _needSetMask;
  19. private bool _showSceneType = true;
  20. private bool _showBg = true;
  21. private DressUpData _dressUpData = new DressUpData();
  22. public int bgId
  23. {
  24. get
  25. {
  26. return _dressUpData.bgId;
  27. }
  28. }
  29. public int suitId
  30. {
  31. get
  32. {
  33. return _dressUpData.suitId;
  34. }
  35. }
  36. public int actionId
  37. {
  38. get
  39. {
  40. return _dressUpData.actionId;
  41. }
  42. }
  43. public bool IsAction
  44. {
  45. get
  46. {
  47. return _dressUpData.actionId > 0;
  48. }
  49. }
  50. public List<int> itemList
  51. {
  52. get
  53. {
  54. return _dressUpData.itemList.ToList();
  55. }
  56. }
  57. /// <summary>
  58. ///
  59. /// </summary>
  60. /// <param name="sceneObj"></param>
  61. /// <param name="needSetMask"></param>
  62. /// <param name="showSceneType"></param>
  63. /// <param name="roleObj"></param>
  64. /// <param name="showBg"></param>
  65. /// <param name="resetData">是否重置装备数据</param>
  66. public void setSceneObj(GameObject sceneObj, bool needSetMask = false, bool showSceneType = true, GameObject roleObj = null, bool showBg = true)
  67. {
  68. if (_sceneObj != null && _sceneObj != sceneObj)
  69. {
  70. GameObject.Destroy(_sceneObj);
  71. }
  72. _sceneObj = sceneObj;
  73. _needSetMask = needSetMask;
  74. _showSceneType = showSceneType;
  75. _showBg = showBg;
  76. _roleObj = roleObj;
  77. if(_dressUpData.IsNew)
  78. {
  79. PutOnDefaultDressUpData();
  80. }
  81. else
  82. {
  83. var tempData = DressUpDataClone();
  84. TakeOffAll();
  85. PutOnDressUpData(tempData);
  86. }
  87. }
  88. public DressUpData DressUpDataClone()
  89. {
  90. return _dressUpData.Clone();
  91. }
  92. public void Dispose()
  93. {
  94. _sceneObj = null;
  95. _dressUpData = null;
  96. }
  97. //传入一个服装部件Id,判断是否有与此部件同类型服装已穿着
  98. public bool CheckSameTypeIsOn(int itemId)
  99. {
  100. int subType = ItemUtilCS.GetItemSubType(itemId);
  101. for (int i = 0; i < _dressUpData.itemList.Count; i++)
  102. {
  103. int _subType = ItemUtilCS.GetItemSubType(_dressUpData.itemList[i]);
  104. if (subType == _subType) return true;
  105. }
  106. return false;
  107. }
  108. /// <summary>
  109. /// 仅判断换装部件是否已穿着
  110. /// </summary>
  111. /// <param name="itemId"></param>
  112. /// <returns></returns>
  113. public bool CheckDressUpItemIsOn(int itemId)
  114. {
  115. if (itemId == _dressUpData.bgId)
  116. {
  117. return true;
  118. }
  119. return _dressUpData.itemList.Contains(itemId);
  120. }
  121. /// <summary>
  122. /// 仅判断套装是否穿上
  123. /// </summary>
  124. /// <param name="id"></param>
  125. /// <returns></returns>
  126. public bool CheckSuitIsOn(int id)
  127. {
  128. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(id);
  129. if (suitCfg == null)
  130. {
  131. return false;
  132. }
  133. int[] items = suitCfg.partsArr;
  134. foreach (int itemId in items)
  135. {
  136. if (_dressUpData.actionId == id)
  137. {
  138. if (SuitCfgArray.Instance.CheckActionContainsItem(itemId, id))
  139. {
  140. continue;
  141. }
  142. }
  143. bool isOn = CheckDressUpItemIsOn(itemId);
  144. if (!isOn)
  145. {
  146. return false;
  147. }
  148. }
  149. return true;
  150. }
  151. public void AddOrRemove(int itemId, bool checkDefault, DressUpOption dressUpOption = DressUpOption.Auto)
  152. {
  153. int subType = ItemUtilCS.GetItemSubType(itemId);
  154. if (subType == ConstDressUpItemType.BEI_JING)
  155. {
  156. if (!_showBg)
  157. {
  158. return;
  159. }
  160. _dressUpData.bgId = itemId;
  161. DressUpUtil.AddItem(_dressUpData.bgId, _sceneObj, _needSetMask, true, _roleObj);
  162. }
  163. else
  164. {
  165. if (!CheckDressUpItemIsOn(itemId))
  166. {
  167. if (dressUpOption != DressUpOption.Remove)
  168. {
  169. if (checkDefault)
  170. {
  171. TryCancelActionWhenPutOn(itemId);
  172. }
  173. checkRemoveSameType(subType);
  174. Add(itemId);
  175. }
  176. }
  177. else
  178. {
  179. if (dressUpOption != DressUpOption.Add)
  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, DressUpOption.Remove);
  230. }
  231. if (checkDefault)
  232. {
  233. checkDefaultItem();
  234. }
  235. RoleLevelCfg roleLevelCfg = RoleLevelCfgArray.Instance.GetCfg(GameGlobal.myNumericComponent.GetAsInt(NumericType.Lvl));
  236. // FightDataManager.Instance.score = roleLevelCfg.baseScore;
  237. // foreach (int itemId in _dressUpData.itemList)
  238. // {
  239. // FightDataManager.Instance.score += ItemDataManager.GetItemAdditionScore(itemId, InstanceZonesDataManager.currentScoreType, InstanceZonesDataManager.currentFightTags);
  240. // }
  241. }
  242. //穿上或脱掉一个动作
  243. public void PutOnOrTakeOffAction(int actionId)
  244. {
  245. if (_dressUpData.actionId == actionId)
  246. {
  247. CancelAction();
  248. }
  249. else
  250. {
  251. PutOnAction(actionId);
  252. }
  253. }
  254. //当穿一件部件时检查是否需要取消动作,如果需要则取消该动作
  255. public void TryCancelActionWhenPutOn(int itemId)
  256. {
  257. if (_dressUpData.actionId > 0)
  258. {
  259. bool replaceableByAction = SuitCfgArray.Instance.CheckItemReaplaceableOnAction(itemId, _dressUpData.actionId);
  260. if (!replaceableByAction)
  261. {
  262. CancelAction();
  263. _dressUpData.actionId = 0;
  264. }
  265. }
  266. }
  267. /// <summary>
  268. /// 尝试穿戴配置套装
  269. /// </summary>
  270. /// <param name="id">套装id</param>
  271. /// <param name="tryShowAction">尝试穿上动作</param>
  272. /// <param name="excludeType">排除类型列表</param>
  273. /// <param name="showOptional">是否显示可选部件</param>
  274. /// <param name="CheckOwn">是否只显示主角拥有的部件</param>
  275. public void PutOnSuitCfg(int id, bool tryShowAction, int[] excludeType = null, bool showOptional = true, bool CheckOwn = true)
  276. {
  277. bool oldIsAction = IsAction;
  278. _dressUpData.suitId = id;
  279. bool hasSuitActionRes = DressUpMenuSuitDataManager.CheckSuitHasActionRes(_dressUpData.suitId);
  280. _dressUpData.actionId = (hasSuitActionRes && tryShowAction) ? id : 0;
  281. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(_dressUpData.suitId);
  282. List<int> items = new List<int>(suitCfg.partsArr);
  283. if (showOptional)
  284. {
  285. if (suitCfg.partsOptionalArr != null && suitCfg.partsOptionalArr.Length > 0)
  286. {
  287. items.AddRange(suitCfg.partsOptionalArr);
  288. }
  289. }
  290. int subType = 0;
  291. //找到要穿的散件
  292. List<int> targetItemList = new List<int>();
  293. foreach (int itemId in items)
  294. {
  295. if (!CheckOwn || DressUpMenuItemDataManager.CheckHasItem(itemId))
  296. {
  297. subType = ItemUtilCS.GetItemSubType(itemId);
  298. bool notInAction = SuitCfgArray.Instance.CheckItemNotInAction(itemId);
  299. if (!IsAction || notInAction)
  300. {
  301. if (excludeType == null || Array.IndexOf(excludeType, subType) < 0)
  302. {
  303. targetItemList.Add(itemId);
  304. }
  305. }
  306. }
  307. }
  308. CompareAndAddItemList(oldIsAction, IsAction, targetItemList);
  309. checkDefaultItem();
  310. }
  311. //穿上一个动作
  312. public void PutOnAction(int actionId)
  313. {
  314. bool hasSuitActionRes = DressUpMenuSuitDataManager.CheckSuitHasActionRes(actionId);
  315. _dressUpData.actionId = actionId;
  316. if(hasSuitActionRes)
  317. {
  318. foreach (int itemId in itemList)
  319. {
  320. if(!DressUpMenuItemCfg1Array.Instance.CheckIsSceneType(itemId))
  321. {
  322. //场景类型不受动作影响并且本来就有动画,对非场景类型处理
  323. AddOrRemove(itemId, false, DressUpOption.Remove);
  324. bool notInAction = SuitCfgArray.Instance.CheckItemNotInAction(itemId);
  325. if (notInAction)
  326. {
  327. //更新成动画模式
  328. AddOrRemove(itemId, false, DressUpOption.Add);
  329. }
  330. }
  331. }
  332. }
  333. else
  334. {
  335. CancelAction(true);
  336. }
  337. checkDefaultItem();
  338. }
  339. //取消动作
  340. public void CancelAction(bool changeToStand = false)
  341. {
  342. if (_dressUpData.actionId <= 0)
  343. {
  344. return;
  345. }
  346. var tempActionId = _dressUpData.actionId;
  347. _dressUpData.actionId = 0;
  348. if (changeToStand)
  349. {
  350. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(tempActionId);
  351. List<int> items = new List<int>(suitCfg.partsArr);
  352. if (suitCfg.partsOptionalArr != null && suitCfg.partsOptionalArr.Length > 0)
  353. {
  354. items.AddRange(suitCfg.partsOptionalArr);
  355. }
  356. foreach (int itemId in items)
  357. {
  358. if (DressUpMenuItemDataManager.CheckHasItem(itemId))
  359. {
  360. bool notInAction = SuitCfgArray.Instance.CheckItemNotInAction(itemId);
  361. if (!notInAction)
  362. {
  363. AddOrRemove(itemId, false, DressUpOption.Add);
  364. }
  365. }
  366. }
  367. }
  368. //更新非场景类型部件形态
  369. foreach (int itemId in itemList)
  370. {
  371. if (!DressUpMenuItemCfg1Array.Instance.CheckIsSceneType(itemId))
  372. {
  373. //场景类型不受动作影响并且本来就有动画,对非场景类型处理
  374. bool notInAction = SuitCfgArray.Instance.CheckItemNotInAction(itemId);
  375. if (notInAction)
  376. {
  377. //更新成图片模式
  378. AddOrRemove(itemId, false, DressUpOption.Add);
  379. }
  380. }
  381. }
  382. checkDefaultItem();
  383. }
  384. //穿戴一组换装数据
  385. public void PutOnDressUpData(DressUpData targetDressUpData)
  386. {
  387. bool oldIsAction = IsAction;
  388. _dressUpData.suitId = targetDressUpData.suitId;
  389. _dressUpData.actionId = targetDressUpData.actionId;
  390. var targetItemList = new List<int>();
  391. targetItemList.AddRange(targetDressUpData.itemList);
  392. if (targetDressUpData.bgId > 0)
  393. {
  394. targetItemList.Add(targetDressUpData.bgId);
  395. }
  396. CompareAndAddItemList(oldIsAction, IsAction, targetItemList);
  397. checkDefaultItem();
  398. }
  399. //穿戴默认换装数据
  400. public void PutOnDefaultDressUpData()
  401. {
  402. PutOnDressUpData(DressUpData.CreateDefault());
  403. }
  404. //穿戴一组散件数据(会脱掉不包含的部分)
  405. public void PutOnItemList(List<int> targetItemList)
  406. {
  407. bool oldIsAction = IsAction;
  408. _dressUpData.suitId = 0;
  409. _dressUpData.actionId = 0;
  410. CompareAndAddItemList(oldIsAction, IsAction, targetItemList);
  411. checkDefaultItem();
  412. }
  413. public int GetItemIdBuyType(int subType)
  414. {
  415. for (int i = 0; i < itemList.Count; i++)
  416. {
  417. 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)
  418. {
  419. if (subType == ItemUtilCS.GetItemSubType(itemList[i]))
  420. {
  421. return itemList[i];
  422. }
  423. }
  424. }
  425. if (suitId > 0)
  426. {
  427. return suitId;
  428. }
  429. return 0;
  430. }
  431. private void Add(int itemId)
  432. {
  433. if (!_showSceneType)
  434. {
  435. if (DressUpMenuItemCfg1Array.Instance.CheckIsSceneType(itemId))
  436. {
  437. return;
  438. }
  439. }
  440. if (!_dressUpData.itemList.Contains(itemId))
  441. {
  442. _dressUpData.itemList.Add(itemId);
  443. bool showAni = IsAction || DressUpMenuItemCfg1Array.Instance.CheckIsSceneType(itemId);
  444. DressUpUtil.AddItem(itemId, _sceneObj, _needSetMask, showAni, _roleObj);
  445. // FightDataManager.Instance.score += ItemDataManager.GetItemAdditionScore(itemId, InstanceZonesDataManager.currentScoreType, InstanceZonesDataManager.currentFightTags);
  446. }
  447. }
  448. private void Remove(int itemId)
  449. {
  450. if (_dressUpData.itemList == null)
  451. {
  452. return;
  453. }
  454. if (_dressUpData.itemList.Contains(itemId))
  455. {
  456. _dressUpData.itemList.Remove(itemId);
  457. DressUpUtil.RemoveItem(itemId, _sceneObj, _roleObj);
  458. // FightDataManager.Instance.score -= ItemDataManager.GetItemAdditionScore(itemId, InstanceZonesDataManager.currentScoreType, InstanceZonesDataManager.currentFightTags);
  459. }
  460. }
  461. //与身上的散件对比差异然后添加
  462. private void CompareAndAddItemList(bool oldIsAction, bool newIsAction, List<int> targetItemList)
  463. {
  464. bool actionStatusChanged = (oldIsAction && !newIsAction) || (!oldIsAction && newIsAction);
  465. foreach (int itemID in itemList)
  466. {
  467. if (!targetItemList.Contains(itemID))
  468. {
  469. //移除不穿的部件
  470. AddOrRemove(itemID, false, DressUpOption.Remove);
  471. }
  472. else if (actionStatusChanged)
  473. {
  474. //当动画形态切换时
  475. if (!DressUpMenuItemCfg1Array.Instance.CheckIsSceneType(itemID))
  476. {
  477. //非场景类型重穿
  478. AddOrRemove(itemID, false, DressUpOption.Remove);
  479. }
  480. }
  481. }
  482. foreach (int itemID in targetItemList)
  483. {
  484. AddOrRemove(itemID, false, DressUpOption.Add);
  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. }