DressUpObjDataCache.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using System;
  4. using System.Linq;
  5. using FairyGUI;
  6. namespace GFGGame
  7. {
  8. public class DressUpObjDataCache
  9. {
  10. private GameObject _sceneObj;
  11. private bool _needSetMask;
  12. public void setSceneObj(GameObject sceneObj, bool needSetMask = false)
  13. {
  14. _sceneObj = sceneObj;
  15. _needSetMask = needSetMask;
  16. }
  17. private int _bgId;
  18. public int bgId
  19. {
  20. get
  21. {
  22. return _bgId;
  23. }
  24. }
  25. private int _suitId;
  26. public int suitId
  27. {
  28. get
  29. {
  30. return _suitId;
  31. }
  32. }
  33. private bool _isPic;
  34. public int picStatus
  35. {
  36. get
  37. {
  38. return _isPic ? 1 : 0;
  39. }
  40. }
  41. private List<int> _equipDatas = new List<int>();
  42. public int[] equipDatas
  43. {
  44. get
  45. {
  46. return _equipDatas.ToArray();
  47. }
  48. }
  49. //角色基础分+部件基础分
  50. private int _score;
  51. public int score
  52. {
  53. get
  54. {
  55. return _score;
  56. }
  57. private set
  58. {
  59. _score = value;
  60. EventAgent.DispatchEvent(ConstMessage.DRESS_UP_SCORE_CHANGED, _score);
  61. }
  62. }
  63. //最终得分
  64. private int _totalScore;
  65. public int totalScore
  66. {
  67. get
  68. {
  69. return _totalScore;
  70. }
  71. set
  72. {
  73. _totalScore = value;
  74. }
  75. }
  76. //战斗对象最终得分
  77. private int _targetTotalScore;
  78. public int npcTotalScore
  79. {
  80. get
  81. {
  82. return _targetTotalScore;
  83. }
  84. set
  85. {
  86. _targetTotalScore = value;
  87. }
  88. }
  89. private bool _autoPlay = false;
  90. public bool autoPlay
  91. {
  92. get
  93. {
  94. return _autoPlay;
  95. }
  96. set
  97. {
  98. _autoPlay = value;
  99. }
  100. }
  101. public void Dispose()
  102. {
  103. _sceneObj = null;
  104. }
  105. private void Add(int value)
  106. {
  107. if (!_equipDatas.Contains(value))
  108. {
  109. _equipDatas.Add(value);
  110. DressUpUtil.AddItem(value, _sceneObj, _needSetMask);
  111. score += DressUpMenuItemDataManager.GetItemScore(value);
  112. }
  113. int dressSuitId = DressUpMenuSuitDataManager.CheckCurDressIsSuit();
  114. if (dressSuitId > 0) _suitId = dressSuitId;
  115. }
  116. private void Remove(int value)
  117. {
  118. if (_equipDatas == null)
  119. {
  120. return;
  121. }
  122. if (_equipDatas.Contains(value))
  123. {
  124. _equipDatas.Remove(value);
  125. DressUpUtil.RemoveItem(value, _sceneObj);
  126. score -= DressUpMenuItemDataManager.GetItemScore(value);
  127. }
  128. }
  129. /// <summary>
  130. /// 仅判断换装部件是否已穿着
  131. /// </summary>
  132. /// <param name="id"></param>
  133. /// <returns></returns>
  134. public bool CheckDressUpItemIsOn(int id)
  135. {
  136. if (id == _bgId)
  137. {
  138. return true;
  139. }
  140. return _equipDatas.Contains(id);
  141. }
  142. /// <summary>
  143. /// 仅判断套装是否穿上
  144. /// </summary>
  145. /// <param name="id"></param>
  146. /// <returns></returns>
  147. public bool CheckSuitIsOn(int id)
  148. {
  149. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(id);
  150. if (suitCfg == null)
  151. {
  152. return false;
  153. }
  154. int[] items = suitCfg.partsArr;
  155. foreach (int itemId in items)
  156. {
  157. bool isOn = CheckDressUpItemIsOn(itemId);
  158. if (!isOn)
  159. {
  160. return false;
  161. }
  162. }
  163. return true;
  164. }
  165. public void AddOrRemove(int value, bool checkDefault, bool isAdd = false, bool isRemove = false)
  166. {
  167. int subType = ItemUtil.GetItemSubType(value);
  168. if (subType == ConstDressUpItemType.BEI_JING)
  169. {
  170. _bgId = value;
  171. DressUpUtil.AddItem(_bgId, _sceneObj, _needSetMask);
  172. }
  173. else
  174. {
  175. if (!CheckDressUpItemIsOn(value))
  176. {
  177. if (!isRemove)
  178. {
  179. checkRemoveSameType(subType);
  180. Add(value);
  181. }
  182. }
  183. else
  184. {
  185. if (!isAdd)
  186. {
  187. Remove(value);
  188. }
  189. }
  190. if (checkDefault)
  191. {
  192. checkDefaultItem();
  193. }
  194. }
  195. }
  196. public void checkRemoveSameType(int type)
  197. {
  198. int count = 0;
  199. int firstTeshuId = 0;
  200. for (int i = 0; i < _equipDatas.Count; i++)
  201. {
  202. int itemID = (int)_equipDatas[i];
  203. int subType = ItemUtil.GetItemSubType(itemID);
  204. if (subType == type
  205. || (type == ConstDressUpItemType.LIAN_YI_QUN && (subType == ConstDressUpItemType.SHANG_YI || subType == ConstDressUpItemType.XIA_ZHUANG || subType == ConstDressUpItemType.NEI_DA))
  206. || (type == ConstDressUpItemType.SHANG_YI && subType == ConstDressUpItemType.LIAN_YI_QUN)
  207. || (type == ConstDressUpItemType.XIA_ZHUANG && subType == ConstDressUpItemType.LIAN_YI_QUN)
  208. || (type == ConstDressUpItemType.NEI_DA && subType == ConstDressUpItemType.LIAN_YI_QUN))
  209. {
  210. Remove(itemID);
  211. i--;
  212. }
  213. if (subType > ConstDressUpItemType.TE_SHU)
  214. {
  215. if (count == 0)
  216. {
  217. firstTeshuId = itemID;
  218. }
  219. count++;
  220. }
  221. }
  222. if (type > ConstDressUpItemType.TE_SHU && count >= 3)
  223. {
  224. //特殊饰品最多穿三件,第四件会自动顶掉第一件
  225. Remove(firstTeshuId);
  226. }
  227. }
  228. private void checkDefaultItem()
  229. {
  230. if (!IsSuitPic)
  231. {
  232. //检查默认资源
  233. //是否有头发
  234. bool has1 = false;
  235. //是否有连衣裙
  236. bool has2 = false;
  237. //是否有内搭
  238. bool has3 = false;
  239. //是否有上衣
  240. // bool has4 = false;
  241. //是否有下装
  242. bool has5 = false;
  243. //是否有默认内搭
  244. // bool has30000 = false;
  245. //是否有默认下装
  246. // bool has50000 = false;
  247. for (int i = 0; i < _equipDatas.Count; i++)
  248. {
  249. int itemID = (int)_equipDatas[i];
  250. int subType = ItemUtil.GetItemSubType(itemID);
  251. if (subType == (int)ConstDressUpItemType.FA_XING)
  252. {
  253. has1 = true;
  254. }
  255. else if (subType == ConstDressUpItemType.LIAN_YI_QUN)
  256. {
  257. has2 = true;
  258. }
  259. else if (subType == ConstDressUpItemType.NEI_DA)
  260. {
  261. has3 = true;
  262. }
  263. else if (subType == ConstDressUpItemType.XIA_ZHUANG)
  264. {
  265. has5 = true;
  266. }
  267. }
  268. if (!has1)
  269. {
  270. Add(10000);
  271. }
  272. if (!has2)
  273. {
  274. if (!has5)
  275. {
  276. Add(50000);
  277. }
  278. if (!has3)
  279. {
  280. Add(30000);
  281. }
  282. }
  283. }
  284. }
  285. private void UpdatePicAction()
  286. {
  287. if (IsSuitPic)
  288. {
  289. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(_suitId);
  290. DressUpUtil.UpdateBody(suitCfg.picRes, _sceneObj, !string.IsNullOrEmpty(suitCfg.aniRes), suitCfg.effRes);
  291. }
  292. else
  293. {
  294. DressUpUtil.UpdateBody(null, _sceneObj, false, null, _needSetMask);
  295. }
  296. }
  297. public void TakeOffAll(bool checkDefault = true)
  298. {
  299. _suitId = 0;
  300. _isPic = false;
  301. // AddOrRemove(propID, false, true);
  302. int[] tempList = equipDatas;
  303. foreach (int itemID in tempList)
  304. {
  305. AddOrRemove(itemID, false, false, true);
  306. }
  307. if (checkDefault)
  308. {
  309. checkDefaultItem();
  310. UpdatePicAction();
  311. }
  312. RoleLevelCfg roleLevelCfg = RoleLevelCfgArray.Instance.GetCfg(RoleDataManager.lvl);
  313. score = roleLevelCfg.baseScore;
  314. foreach (int itemId in _equipDatas)
  315. {
  316. score += DressUpMenuItemDataManager.GetItemScore(itemId);
  317. }
  318. }
  319. public void ChangeAction()
  320. {
  321. if (!HasSuitPicRes)
  322. {
  323. return;
  324. }
  325. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(_suitId);
  326. _isPic = !_isPic;
  327. if (_isPic)
  328. {
  329. int[] tempList = equipDatas;
  330. foreach (int itemID in tempList)
  331. {
  332. if (!DressUpMenuItemDataManager.CheckIsSceneType(itemID))
  333. {
  334. AddOrRemove(itemID, false, false, true);
  335. }
  336. }
  337. }
  338. else
  339. {
  340. int[] items = suitCfg.partsArr;
  341. foreach (int itemId in items)
  342. {
  343. if (!DressUpMenuItemDataManager.CheckIsSceneType(itemId))
  344. {
  345. AddOrRemove(itemId, false, true);
  346. }
  347. }
  348. }
  349. checkDefaultItem();
  350. UpdatePicAction();
  351. }
  352. public void TryCancelSuit(int itemID)
  353. {
  354. if (_suitId > 0)
  355. {
  356. if (!DressUpMenuItemDataManager.CheckIsSceneType(itemID))
  357. {
  358. if (_isPic)
  359. {
  360. ChangeAction();
  361. }
  362. _suitId = 0;
  363. }
  364. }
  365. }
  366. public bool IsSuitPic
  367. {
  368. get
  369. {
  370. return _suitId > 0 && _isPic;
  371. }
  372. }
  373. public bool HasSuitPicRes
  374. {
  375. get
  376. {
  377. if (_suitId > 0)
  378. {
  379. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(_suitId);
  380. if (suitCfg.picRes != null && suitCfg.picRes.Length > 0)
  381. {
  382. return true;
  383. }
  384. }
  385. return false;
  386. }
  387. }
  388. public void PutOnSuitCfg(int id, bool checkPic, bool noSceneType, int[] excludeType = null)
  389. {
  390. if (_suitId == id)
  391. {
  392. return;
  393. }
  394. TakeOffAll(false);
  395. _suitId = id;
  396. _isPic = HasSuitPicRes && checkPic;
  397. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(_suitId);
  398. List<int> items = new List<int>(suitCfg.partsArr);
  399. if (suitCfg.partsOptionalArr != null && suitCfg.partsOptionalArr.Length > 0)
  400. {
  401. items.AddRange(suitCfg.partsOptionalArr);
  402. }
  403. int subType = 0;
  404. foreach (int itemID in items)
  405. {
  406. if (DressUpMenuItemDataManager.CheckHasItem(itemID))
  407. {
  408. bool isSceneType = DressUpMenuItemDataManager.CheckIsSceneType(itemID);
  409. subType = ItemUtil.GetItemSubType(itemID);
  410. if (!_isPic || isSceneType)
  411. {
  412. if (!noSceneType || !isSceneType)
  413. {
  414. if (excludeType == null || Array.IndexOf(excludeType, subType) < 0)
  415. {
  416. AddOrRemove(itemID, false, true);
  417. }
  418. }
  419. }
  420. }
  421. }
  422. checkDefaultItem();
  423. UpdatePicAction();
  424. }
  425. public void PutOnSuitSaved(int index)
  426. {
  427. TakeOffAll(false);
  428. CustomSuitData suitSavedData = CustomSuitDataManager.GetSuitList(index);
  429. if (suitSavedData.bg > 0)
  430. {
  431. AddOrRemove(suitSavedData.bg, false);
  432. }
  433. foreach (int itemID in suitSavedData.equipDatas)
  434. {
  435. AddOrRemove(itemID, false, true);
  436. }
  437. _suitId = suitSavedData.suitId;
  438. _isPic = suitSavedData.pic > 0;
  439. checkDefaultItem();
  440. UpdatePicAction();
  441. }
  442. public void PutOnSuitSavedInFight(int index)
  443. {
  444. TakeOffAll(false);
  445. CustomSuitData suitSavedData = CustomSuitDataManager.GetSuitList(index);
  446. if (suitSavedData.suitId > 0)
  447. {
  448. PutOnSuitCfg(suitSavedData.suitId, false, true);
  449. }
  450. else
  451. {
  452. foreach (int itemID in suitSavedData.equipDatas)
  453. {
  454. if (!DressUpMenuItemDataManager.CheckIsSceneType(itemID))
  455. {
  456. AddOrRemove(itemID, false, true);
  457. }
  458. }
  459. checkDefaultItem();
  460. }
  461. }
  462. public void PutOnCurrentSuitSaved()
  463. {
  464. PutOnSuitSaved(CustomSuitDataManager.currentIndex);
  465. }
  466. public void PutOnDefaultSuitSaved(bool withBg = true)
  467. {
  468. TakeOffAll(false);
  469. CustomSuitData suitSavedData = CustomSuitDataManager.CreateDefaultSuitData(0);
  470. foreach (int itemID in suitSavedData.equipDatas)
  471. {
  472. AddOrRemove(itemID, false, true);
  473. }
  474. if (withBg)
  475. {
  476. if (suitSavedData.bg > 0)
  477. {
  478. AddOrRemove(suitSavedData.bg, false);
  479. }
  480. }
  481. checkDefaultItem();
  482. UpdatePicAction();
  483. }
  484. public void PutOnRecommendItems()
  485. {
  486. TakeOffAll(false);
  487. List<int> recommendList = DressUpMenuItemDataManager.GetRecommendItemList();
  488. StoryLevelCfg levelCfg = StoryLevelCfgArray.Instance.GetCfg(StoryDataManager.currentLevelID);
  489. StoryFightCfg fightCfg = StoryFightCfgArray.Instance.GetCfg(levelCfg.fightID);
  490. //推荐搭配自动穿必穿品
  491. if (fightCfg.needItemId > 0 && DressUpMenuItemDataManager.CheckHasItem(fightCfg.needItemId) && recommendList.IndexOf(fightCfg.needItemId) < 0)
  492. {
  493. recommendList.Add(fightCfg.needItemId);
  494. }
  495. else if (fightCfg.needSuitId > 0 && DressUpMenuSuitDataManager.CheckHaveSuit(fightCfg.needSuitId))
  496. {
  497. recommendList.Clear();
  498. SuitCfg cfg = SuitCfgArray.Instance.GetCfg(fightCfg.needSuitId);
  499. recommendList.AddRange(cfg.partsArr);
  500. }
  501. foreach (int itemID in recommendList)
  502. {
  503. AddOrRemove(itemID, false, true);
  504. }
  505. checkDefaultItem();
  506. UpdatePicAction();
  507. }
  508. public bool CheckCurrentScoreEnough()
  509. {
  510. return StoryDataManager.CheckCurrentScoreEnough(score);
  511. }
  512. public bool CheckEquipedFightNeeded()
  513. {
  514. StoryLevelCfg levelCfg = StoryLevelCfgArray.Instance.GetCfg(StoryDataManager.currentLevelID);
  515. StoryFightCfg fightCfg = StoryFightCfgArray.Instance.GetCfg(levelCfg.fightID);
  516. if (fightCfg.needItemId > 0)
  517. {
  518. return CheckDressUpItemIsOn(fightCfg.needItemId);
  519. }
  520. else if (fightCfg.needSuitId > 0)
  521. {
  522. return CheckSuitIsOn(fightCfg.needSuitId);
  523. }
  524. return true;
  525. }
  526. //根据位置原点和随机范围获取评分位置
  527. public void GetCirclePos(Vector2 pos, int range, out float x, out float y)
  528. {
  529. int numX = UnityEngine.Random.Range(0, 2);
  530. int signX = numX % 2 == 0 ? 1 : -1;
  531. float rangeX = UnityEngine.Random.Range(0, range);
  532. x = pos.x + signX * (rangeX);
  533. int numY = UnityEngine.Random.Range(0, 2);
  534. int signY = numY % 2 == 0 ? 1 : -1;
  535. float rangeY = UnityEngine.Random.Range(0, range);
  536. y = pos.y + signY * (rangeY);
  537. }
  538. }
  539. }