DressUpObj.cs 18 KB

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