Player.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. using DragonBones;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using UnityEngine.EventSystems;
  5. using System;
  6. using System.Xml;
  7. using System.Linq;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using Slot = DragonBones.Slot;
  11. using Random = UnityEngine.Random;
  12. using Transform = UnityEngine.Transform;
  13. public enum BodyPart
  14. {
  15. Eye,
  16. Top,
  17. Shoe,
  18. Head,
  19. Dress,
  20. Mouse,
  21. Headwear,
  22. }
  23. public class CloseUnit
  24. {
  25. public enum CloseType
  26. {
  27. Top,
  28. Hair,
  29. Wing,
  30. Dress,
  31. Decarator,
  32. }
  33. #region Var
  34. public int Index;
  35. public int BuyLevel;
  36. public string CloseName;
  37. public Text BtnLab;
  38. public Sprite Sprite;
  39. public Image Icon;
  40. public Button BuyBtn;
  41. public Button DressBtn;
  42. public BodyPart BodyPart;
  43. public Transform Transform;
  44. public CloseType Type;
  45. public double BuyAmt;
  46. public double BuyAdvanceAmt;
  47. public Current BuyCurrent;
  48. public Current BuyAdvanceCurrent;
  49. public Dictionary<string, Transform> ChildDic = new Dictionary<string, Transform>();
  50. #endregion
  51. public CloseUnit(XmlAttributeCollection attribute)
  52. {
  53. CloseName = attribute[2].Value;
  54. Sprite = SpriteParse(attribute[2].Value);
  55. Type = TypeParse(attribute[3].Value);
  56. Index = Auxiliary.IntParse(attribute[4].Value, -1);
  57. BodyPart = BodyPartParse(attribute[5].Value);
  58. BuyLevel = Auxiliary.IntParse(attribute[6].Value, 0);
  59. BuyCurrent = Auxiliary.CurrentParse(attribute[7].Value);
  60. BuyAmt = Auxiliary.DoubleParse(attribute[8].Value, 0);
  61. BuyAdvanceCurrent = Auxiliary.CurrentParse(attribute[9].Value);
  62. BuyAdvanceAmt = Auxiliary.DoubleParse(attribute[10].Value, 0);
  63. CreateItem();
  64. }
  65. protected void CreateItem()
  66. {
  67. Transform = ManaReso.Get("CloseItem", Folder.UI, false, ManaReso.Get("Canvas"), false);
  68. if (Type == CloseType.Top)
  69. {
  70. Transform.SetParent(ManaReso.Get("Pb_TopGrid"));
  71. }
  72. else if (Type == CloseType.Hair)
  73. {
  74. Transform.SetParent(ManaReso.Get("Pa_HairGrid"));
  75. }
  76. else if (Type == CloseType.Dress)
  77. {
  78. Transform.SetParent(ManaReso.Get("Pc_DressGrid"));
  79. }
  80. else if (Type == CloseType.Wing)
  81. {
  82. Transform.SetParent(ManaReso.Get("Pe_WingGrid"));
  83. }
  84. else if (Type == CloseType.Decarator)
  85. {
  86. Transform.SetParent(ManaReso.Get("Pd_DecaratorGrid"));
  87. }
  88. else
  89. {
  90. throw new Exception();
  91. }
  92. Transform.SetSiblingIndex(Index);
  93. Auxiliary.CompileDic(Transform, ChildDic);
  94. Icon = ChildDic["Icon"].GetComponent<Image>();
  95. DressBtn = ChildDic["CloseItem"].GetComponent<Button>();
  96. BuyBtn = ChildDic["BuyBtn"].GetComponent<Button>();
  97. BtnLab = ChildDic["BuyBtnLab"].GetComponent<Text>();
  98. Icon.sprite = Sprite;
  99. Icon.SetNativeSize();
  100. float newSize = 100 / Icon.sprite.rect.width;
  101. Icon.Resize(true, new Vector2(newSize, newSize)); //todo 把NewSize写到配置里
  102. }
  103. protected Sprite SpriteParse(string str)
  104. {
  105. return ManaPlayer.CloseDic[str];
  106. }
  107. protected BodyPart BodyPartParse(string str)
  108. {
  109. int type = Auxiliary.IntParse(str, -1);
  110. if (type == 1)
  111. {
  112. return BodyPart.Head;
  113. }
  114. else if (type == 2)
  115. {
  116. return BodyPart.Dress;
  117. }
  118. else if (type == 3)
  119. {
  120. return BodyPart.Shoe;
  121. }
  122. else if (type == 4)
  123. {
  124. return BodyPart.Headwear;
  125. }
  126. else if (type == 5)
  127. {
  128. return BodyPart.Top;
  129. }
  130. else
  131. {
  132. throw new Exception();
  133. }
  134. }
  135. protected CloseType TypeParse(string str)
  136. {
  137. int type = Auxiliary.IntParse(str, -1);
  138. if (type == 1)
  139. {
  140. return CloseType.Hair;
  141. }
  142. else if (type == 2)
  143. {
  144. return CloseType.Top;
  145. }
  146. else if (type == 3)
  147. {
  148. return CloseType.Dress;
  149. }
  150. else if (type == 4)
  151. {
  152. return CloseType.Decarator;
  153. }
  154. else if (type == 5)
  155. {
  156. return CloseType.Wing;
  157. }
  158. else
  159. {
  160. throw new Exception();
  161. }
  162. }
  163. }
  164. public class Player : Regist , IPointerClickHandler
  165. {
  166. #region 变量
  167. public static bool InDressRoom;
  168. public static float JumpFrequency;
  169. public bool AnimLock1;
  170. public bool AnimLock2;
  171. public float JumpTime;
  172. public float JumpTimer;
  173. public Dictionary<string, Transform> ChildDic = new Dictionary<string, Transform>();
  174. #region 换装
  175. private string Eye;
  176. private string Top;
  177. private string Shoe;
  178. private string Head;
  179. private string Dress;
  180. private string Mouse;
  181. private string HeadWear;
  182. private string TempClose;
  183. public DragonBones.Slot EyeSlot;
  184. public DragonBones.Slot TopSlot;
  185. public DragonBones.Slot HeadSlot;
  186. public DragonBones.Slot DressSlot;
  187. public DragonBones.Slot MouseSlot;
  188. public DragonBones.Slot LeftShoeSlot;
  189. public DragonBones.Slot RightShoeSlot;
  190. public DragonBones.Slot HeadWearSlot;
  191. public UnityArmatureComponent UAC;
  192. #endregion
  193. #endregion
  194. public override bool RegistImmed()
  195. {
  196. if (base.RegistImmed())
  197. {
  198. return true;
  199. }
  200. enabled = true;
  201. Auxiliary.CompileDic(transform, ChildDic);
  202. Vector3 bigShadowScale = new Vector3(1.820952f, 2.418199f, 1.820952f);
  203. Vector3 smallShadowScale = new Vector3(1.081191f, 1.435807f, 1.081191f);
  204. StreamScale streamScale = ChildDic["Shadow"].CreateStreamScale
  205. (
  206. new List<float>() {0, 0, 0},
  207. new List<float>() {0.33f, 0.33f, 0.33f, 0.33f},
  208. new List<VecPair>() {new VecPair(bigShadowScale, smallShadowScale), new VecPair(smallShadowScale, bigShadowScale), new VecPair(bigShadowScale, smallShadowScale), new VecPair(smallShadowScale, bigShadowScale)},
  209. true,
  210. true,
  211. Curve.EaseOutQuad
  212. );
  213. streamScale.AddEventOnetime
  214. (
  215. EventType.ForwardFinish,
  216. () =>
  217. {
  218. if (!InDressRoom)
  219. {
  220. ManaPlayer.Player.ChildDic["Shadow"].SetActive(false);
  221. }
  222. }
  223. );
  224. return false;
  225. }
  226. public void FixedUpdate()
  227. {
  228. if (InDressRoom)
  229. {
  230. JumpTimer += Time.fixedDeltaTime;
  231. if (JumpTimer > JumpTime)
  232. {
  233. if (!AnimLock1 && !AnimLock2)
  234. {
  235. PlayAnim("newAnimation1");
  236. }
  237. AnimLock2 = true;
  238. }
  239. if (JumpTimer > JumpFrequency)
  240. {
  241. AnimLock2 = false;
  242. JumpTime = Mathf.Lerp(0, JumpFrequency, Random.Range(0f, 1f));
  243. JumpTimer = 0;
  244. }
  245. }
  246. }
  247. public void OnStart(string str, EventObject eventObject)
  248. {
  249. if (eventObject.animationState.name == "newAnimation1")
  250. {
  251. ChildDic["Shadow"].StreamReForScale();
  252. AnimLock1 = true;
  253. TempClose = Eye;
  254. ChangeClose(BodyPart.Eye, "眼睛表情1");
  255. }
  256. else if (eventObject.animationState.name == "newAnimation")
  257. {
  258. AnimLock1 = false;
  259. ChangeClose(BodyPart.Eye, TempClose);
  260. }
  261. }
  262. public void PlayAnim(string animName)
  263. {
  264. if (AnimLock1)
  265. {
  266. return;
  267. }
  268. UAC.anim.Play(animName);
  269. }
  270. public void SetAllCollider(bool enable)
  271. {
  272. BoxCollider2D[] colliders = GetComponentsInChildren<BoxCollider2D>();
  273. for (int i = 0; i < colliders.Length; i++)
  274. {
  275. colliders[i].enabled = enable;
  276. }
  277. }
  278. public void OnPointerClick(PointerEventData eventData)
  279. {
  280. EnterDressRoom();
  281. }
  282. public void Save()
  283. {
  284. ExitDressRoom();
  285. }
  286. public void Reset()
  287. {
  288. }
  289. public void ExitDressRoom()
  290. {
  291. ManaCenter.SceneSwitchLock = false;
  292. TweenRoot tweenRoot = ManaReso.Get("I_BlackMask").TweenBacCG();
  293. tweenRoot.AddEventOnetime
  294. (
  295. EventType.BackwardFinish,
  296. () =>
  297. {
  298. ManaPlayer.Player.ChildDic["Shadow"].SetActive(false);
  299. ManaPlayer.Player.transform.position = ManaReso.Get("PlayerPosTra").position;
  300. ManaPlayer.Player.transform.localScale = ManaReso.Get("PlayerPosTra").lossyScale;
  301. ManaReso.Get("Garden").TweenForSr();
  302. ManaReso.Get("DressRoom").TweenBacSr();
  303. ManaReso.Get("C_Main").TweenForCG();
  304. ManaReso.Get("P_DressRoom").TweenBacCG();
  305. }
  306. );
  307. tweenRoot = ManaReso.Get("P_DressRoom").GetTweenCG();
  308. tweenRoot.AddEventOnetime
  309. (
  310. EventType.BackwardFinish,
  311. () =>
  312. {
  313. SetAllCollider(true);
  314. InDressRoom = false;
  315. ManaReso.Get("I_BlackMask").TweenForCG();
  316. }
  317. );
  318. }
  319. public void EnterDressRoom()
  320. {
  321. if (ManaCenter.SceneSwitchLock)
  322. {
  323. return;
  324. }
  325. InDressRoom = true;
  326. JumpTime = Mathf.Lerp(0, JumpFrequency, Random.Range(0.5f, 1f));
  327. ManaCenter.SceneSwitchLock = true;
  328. ManaReso.Get("C_Main").TweenBacCG();
  329. SetAllCollider(false);
  330. TweenRoot tweenRoot = ManaReso.Get("I_BlackMask").TweenBacCG();
  331. tweenRoot.AddEventOnetime
  332. (
  333. EventType.BackwardFinish,
  334. () =>
  335. {
  336. for (int i = 0; i < ManaGarden.PlantList.Count; i++)
  337. {
  338. ManaGarden.PlantList[i].Flower.RetrieveElf();
  339. }
  340. ManaPlayer.Player.ChildDic["Shadow"].SetActive(true);
  341. ManaPlayer.Player.transform.position = ManaReso.Get("DressRoomPos").position;
  342. ManaPlayer.Player.transform.localScale = ManaReso.Get("DressRoomPos").lossyScale;
  343. ManaReso.Get("Garden").TweenBacSr();
  344. ManaReso.Get("DressRoom").TweenForSr();
  345. ManaReso.Get("P_DressRoom").TweenForCG();
  346. }
  347. );
  348. tweenRoot = ManaReso.Get("P_DressRoom").GetTweenCG();
  349. tweenRoot.AddEventOnetime
  350. (
  351. EventType.ForwardFinish,
  352. () =>
  353. {
  354. ManaReso.Get("I_BlackMask").TweenForCG();
  355. }
  356. );
  357. }
  358. #region 换装
  359. public UnityArmatureComponent Build()
  360. {
  361. if (!ManaPlayer.Complete)
  362. {
  363. UnityFactory.factory.LoadDragonBonesData(ManaReso.Load<TextAsset>("stand_ske", Folder.Config));
  364. UnityFactory.factory.LoadDragonBonesData(ManaReso.Load<TextAsset>("Closet_ske", Folder.Config));
  365. UnityFactory.factory.LoadTextureAtlasData(ManaReso.Load<TextAsset>("stand_tex", Folder.Config), "stand_texture");
  366. UnityFactory.factory.LoadTextureAtlasData(ManaReso.Load<TextAsset>("Closet_tex", Folder.Config), "Closet_texture");
  367. ManaPlayer.Complete = true;
  368. }
  369. UAC = UnityFactory.factory.BuildArmatureComponent("Armature");
  370. UAC.transform.parent = transform;
  371. UAC.transform.localScale = new Vector3(1, 1, 1);
  372. UAC.transform.localPosition = new Vector3();
  373. UAC.anim.Play("newAnimation");
  374. UAC.AddEventListener(EventObject.START, OnStart);
  375. Eye = "眼睛1";
  376. Top = "上衣1";
  377. Shoe = "鞋子1";
  378. Head = "脑壳1";
  379. Dress = "裙子1";
  380. Mouse = "嘴巴1";
  381. HeadWear = "头饰品1";
  382. EyeSlot = UAC.armature.GetSlot("眼睛");
  383. TopSlot = UAC.armature.GetSlot("上衣");
  384. HeadSlot = UAC.armature.GetSlot("脑壳");
  385. DressSlot = UAC.armature.GetSlot("裙子");
  386. MouseSlot = UAC.armature.GetSlot("嘴巴");
  387. LeftShoeSlot = UAC.armature.GetSlot("鞋子左");
  388. RightShoeSlot = UAC.armature.GetSlot("鞋子右");
  389. HeadWearSlot = UAC.armature.GetSlot("头饰品");
  390. ChangeClose(BodyPart.Eye, "眼睛1", false);
  391. ChangeClose(BodyPart.Top, "上衣1", false);
  392. ChangeClose(BodyPart.Shoe, "鞋子1", false);
  393. ChangeClose(BodyPart.Head, "脑壳1", false);
  394. ChangeClose(BodyPart.Dress, "裙子1", false);
  395. ChangeClose(BodyPart.Mouse, "嘴巴1", false);
  396. ChangeClose(BodyPart.Headwear, "头饰品1", false);
  397. return UAC;
  398. }
  399. public UnityArmatureComponent BuildPink()
  400. {
  401. Build();
  402. ChangeClose(BodyPart.Eye, "眼睛3");
  403. ChangeClose(BodyPart.Top, "上衣3");
  404. ChangeClose(BodyPart.Shoe, "脑壳3");
  405. ChangeClose(BodyPart.Head, "裙子3");
  406. ChangeClose(BodyPart.Dress, "嘴巴3");
  407. ChangeClose(BodyPart.Mouse, "鞋子3");
  408. ChangeClose(BodyPart.Headwear, "头饰品3");
  409. return UAC;
  410. }
  411. public UnityArmatureComponent BuildBlond()
  412. {
  413. Build();
  414. ResetDepth();
  415. return UAC;
  416. }
  417. public UnityArmatureComponent BuildBrown()
  418. {
  419. Build();
  420. ChangeClose(BodyPart.Eye, "眼睛2");
  421. ChangeClose(BodyPart.Top, "上衣2");
  422. ChangeClose(BodyPart.Shoe, "脑壳2");
  423. ChangeClose(BodyPart.Head, "裙子2");
  424. ChangeClose(BodyPart.Dress, "嘴巴2");
  425. ChangeClose(BodyPart.Mouse, "鞋子2");
  426. ChangeClose(BodyPart.Headwear, "头饰品2");
  427. return UAC;
  428. }
  429. public void ResetDepth()
  430. {
  431. UAC.transform.SetLZ(2);
  432. transform.FindChild("Armature/" + Eye).SetLZ(-0.001f);
  433. transform.FindChild("Armature/" + Top).SetLZ(-0.003f);
  434. transform.FindChild("Armature/" + Head).SetLZ(0);
  435. transform.FindChild("Armature/" + Dress).SetLZ(-0.002f);
  436. transform.FindChild("Armature/" + Mouse).SetLZ(-0.001f);
  437. transform.FindChild("Armature/" + HeadWear).SetLZ(-0.001f);
  438. Transform shoeTra = transform.FindChild("Armature/" + Shoe);
  439. shoeTra.SetLZ(-0.001f);
  440. transform.FindChild("Armature").GetChild(shoeTra.GetSiblingIndex() + 1).SetLZ(-0.001f);
  441. transform.FindChild("Armature/左腿").SetLZ(0);
  442. transform.FindChild("Armature/右腿").SetLZ(0);
  443. transform.FindChild("Armature/脖子").SetLZ(0);
  444. transform.FindChild("Armature/左手").SetLZ(-0.001f);
  445. transform.FindChild("Armature/右手").SetLZ(-0.001f);
  446. }
  447. public void ChangeClose(BodyPart bodyPart, string armatureName, bool setDepth = true)
  448. {
  449. List<DragonBones.Slot> slotList = new List<DragonBones.Slot>();
  450. if (bodyPart == BodyPart.Eye)
  451. {
  452. Eye = armatureName;
  453. slotList.Add(EyeSlot);
  454. }
  455. else if (bodyPart == BodyPart.Top)
  456. {
  457. Top = armatureName;
  458. slotList.Add(TopSlot);
  459. }
  460. else if (bodyPart == BodyPart.Shoe)
  461. {
  462. Shoe = armatureName;
  463. slotList.Add(LeftShoeSlot);
  464. slotList.Add(RightShoeSlot);
  465. }
  466. else if (bodyPart == BodyPart.Head)
  467. {
  468. Head = armatureName;
  469. slotList.Add(HeadSlot);
  470. }
  471. else if (bodyPart == BodyPart.Dress)
  472. {
  473. Dress = armatureName;
  474. slotList.Add(DressSlot);
  475. }
  476. else if (bodyPart == BodyPart.Mouse)
  477. {
  478. Mouse = armatureName;
  479. slotList.Add(MouseSlot);
  480. }
  481. else if (bodyPart == BodyPart.Headwear)
  482. {
  483. HeadWear = armatureName;
  484. slotList.Add(HeadWearSlot);
  485. }
  486. else
  487. {
  488. throw new Exception();
  489. }
  490. ChangeClose(slotList, armatureName, setDepth);
  491. }
  492. public void ChangeClose(List<DragonBones.Slot> slotList, string armatureName, bool setDepth = true)
  493. {
  494. for (int i = 0; i < slotList.Count; i++)
  495. {
  496. slotList[i].childArmature = UnityFactory.factory.BuildArmature(armatureName);
  497. }
  498. if (setDepth)
  499. {
  500. ResetDepth();
  501. }
  502. }
  503. #endregion
  504. }