DressUpObj.cs 18 KB

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