DressUpObjDataCache.cs 17 KB

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