DressUpObj.cs 22 KB

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