DressUpObj.cs 19 KB

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