DressUpObjDataCache.cs 17 KB

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