PlazaRoom.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using DragonBones;
  6. using UnityEngine;
  7. using UnityEngine.EventSystems;
  8. using Debug = UnityEngine.Debug;
  9. using Transform = UnityEngine.Transform;
  10. public class PlazaRoomLabel
  11. {
  12. public static string PlayerLeftDownBorder = "PlayerLeftDownBorder";
  13. public static string PlayerRightTopBorder = "PlayerRightTopBorder";
  14. public static string PlazaRoomDefaultPosition = "PlazaRoomDefaultPosition";
  15. public static string PlazaRoomSky = "PlazaRoomSky";
  16. public static string PlazaRoomCameraLeftBorder = "PlazaRoomCameraLeftBorder";
  17. public static string PlazaRoomCameraRightBorder = "PlazaRoomCameraRightBorder";
  18. public static string PlazaRoomChestPos = "PlazaRoomChestPos";
  19. public static string PlazaRoom = "PlazaRoom";
  20. public static string HeartSprite = "HeartSprite";
  21. public static string WelcomeSprite = "WelcomeSprite";
  22. public static string PhonoGraph = "PhonoGraph";
  23. public static string PlazaRoomCloud1 = "PlazaRoomCloud1";
  24. public static string PlazaRoomCloud2 = "PlazaRoomCloud2";
  25. public static string PlazaRoomCloud3 = "PlazaRoomCloud3";
  26. }
  27. public class PlazaRoom : MonoBehaviour, IPointerClickHandler
  28. {
  29. private class StarCreater
  30. {
  31. public float StarSpawnTime = 0;
  32. public float StarSpawnTimer = 0;
  33. public float MinStarSpawnTime = 0.1f;
  34. public float MaxStarSpawnTime = 0.3f;
  35. public float LeftStarBorder = 0;
  36. public float RightStarBorder = 16;
  37. public float UpStarBorder = 4.6f;
  38. public float DownStarBorder = 0;
  39. public Vector3 MinStarScale = new Vector3(0.5f, 0.5f, 0.5f);
  40. public Vector3 MaxStarScale = new Vector3(1, 1, 1);
  41. public void Update()
  42. {
  43. StarSpawnTimer += Time.deltaTime;
  44. if (StarSpawnTimer >= StarSpawnTime)
  45. {
  46. StarSpawnTime = Random.Range(MinStarSpawnTime, MaxStarSpawnTime);
  47. StarSpawnTimer = 0;
  48. Transform star = ResourceManager.Get(ResourceLabel.PlazaRoomStar, Folder.Scene, false, ResourceManager.Get(PlazaRoomLabel.PlazaRoom), false, ObjType.PlazaRoomStar, typeof(PlazaRoomStar));
  49. star.GetComponent<PlazaRoomStar>().Show();
  50. star.localScale = Vector3.Lerp(MinStarScale, MaxStarScale, Random.Range(0f, 1f));
  51. star.SetX(Random.Range(LeftStarBorder, RightStarBorder));
  52. star.SetY(Random.Range(DownStarBorder, UpStarBorder));
  53. }
  54. }
  55. }
  56. #region Config
  57. public static bool Initialized;
  58. private static string PhonoGraphAnimName = "newAnimation";
  59. private static Vector3 HitPosOffset = new Vector3(0, 0, -3);
  60. //private static Vector3 HitPosOffset = new Vector3(0, 0, -4.8f);
  61. private static List<StarCreater> StarCreaters = new List<StarCreater>();
  62. public static Dictionary<Transform, List<Transform>> ControllDepthDictionary = new Dictionary<Transform, List<Transform>>();
  63. #endregion
  64. public static void Initialize()
  65. {
  66. if (Initialized)
  67. return;
  68. else
  69. Initialized = true;
  70. LoadScene();
  71. InitCloud();
  72. }
  73. private static void InitCloud()
  74. {
  75. #region Cloud1
  76. Transform tra = ResourceManager.Get(PlazaRoomLabel.PlazaRoomCloud1);
  77. //tra.GetComponent<SpriteRenderer>().sprite = ManaReso.LoadSprite("云朵", Folder.Scene);
  78. TweenRoot tween = tra.CreateTweenVec2D(new Vector3(-21, tra.localPosition.y, tra.localPosition.z), 115f, true, false, true, Curve.Linear);
  79. tween.Repeat = true;
  80. tween.StartForward();
  81. #endregion
  82. #region Cloud2
  83. tra = ResourceManager.Get(PlazaRoomLabel.PlazaRoomCloud2);
  84. //tra.GetComponent<SpriteRenderer>().sprite = ManaReso.LoadSprite("云朵", Folder.Scene);
  85. tween = tra.CreateTweenVec2D(new Vector3(-21, tra.localPosition.y, tra.localPosition.z), 157.5f, true, false, true, Curve.Linear);
  86. tween.Repeat = true;
  87. tween.StartForward();
  88. #endregion
  89. #region Cloud3
  90. tra = ResourceManager.Get(PlazaRoomLabel.PlazaRoomCloud3);
  91. //tra.GetComponent<SpriteRenderer>().sprite = ManaReso.LoadSprite("云朵", Folder.Scene);
  92. tween = tra.CreateTweenVec2D(new Vector3(-21, tra.localPosition.y, tra.localPosition.z), 200f, true, false, true, Curve.Linear);
  93. tween.Repeat = true;
  94. tween.StartForward();
  95. #endregion
  96. }
  97. private static void LoadScene()
  98. {
  99. StarCreaters.Add(new StarCreater());
  100. StarCreaters.Add(new StarCreater());
  101. Transform tra = ResourceManager.Get(PlazaRoomLabel.PlazaRoom, Folder.Scene, true, null, true);
  102. tra.AddComponent<PlazaRoom>();
  103. UnityFactory.factory.LoadDragonBonesData(ResourceManager.Load<TextAsset>(ResourceLabel.PhonoGraphSke, Folder.Config));
  104. UnityFactory.factory.LoadTextureAtlasData(ResourceManager.Load<TextAsset>(ResourceLabel.PhonoGraphTex, Folder.Config), ResourceLabel.PhonoGraphTexture);
  105. UnityArmatureComponent uac = UnityFactory.factory.BuildArmatureComponent(PlazaRoomLabel.PhonoGraph);
  106. uac.transform.parent = ResourceManager.Get(PlazaRoomLabel.PhonoGraph);
  107. uac.anim.Play(PhonoGraphAnimName);
  108. uac.transform.localPosition = new Vector3();
  109. LanguageManager.OnLanguageChange += OnLanguageChange;
  110. OnLanguageChange(CurrentLanguage.Default, LanguageManager.CurrentLanguage);
  111. }
  112. public void Update()
  113. {
  114. CreateStarThread();
  115. ControllDepthThread();
  116. }
  117. public void CreateStarThread()
  118. {
  119. foreach (var starCreater in StarCreaters)
  120. {
  121. starCreater.Update();
  122. }
  123. }
  124. public void ControllDepthThread()
  125. {
  126. foreach (var v in SFSManager.GardenSmartFox.PlazaRoomController.UserInstanceDictionary.Values)
  127. {
  128. if (v.Player.transform.hasChanged)
  129. {
  130. ResetDepth();
  131. break;
  132. }
  133. }
  134. }
  135. public void ResetDepth()
  136. {
  137. Dictionary<Transform, List<Transform>> tempDictionary = new Dictionary<Transform, List<Transform>>();
  138. foreach (var plazaRoomPlayer in SFSManager.GardenSmartFox.PlazaRoomController.UserInstanceDictionary.Values)
  139. {
  140. tempDictionary.Add(plazaRoomPlayer.Player.transform, new List<Transform> {plazaRoomPlayer.MessageBox, plazaRoomPlayer.NickNameTransform});
  141. }
  142. foreach (var kv in ControllDepthDictionary)
  143. {
  144. tempDictionary.Add(kv.Key, kv.Value);
  145. }
  146. List<Transform> transforms = tempDictionary.Keys.ToList();
  147. transforms.MySort((transform0, transform1) => transform0.position.z > transform1.position.z);
  148. for (int i = 0; i < transforms.Count; i++)
  149. {
  150. for (int j = 0; j < tempDictionary[transforms[i]].Count; j++)
  151. {
  152. tempDictionary[transforms[i]][j].SetAsLastSibling();
  153. }
  154. }
  155. }
  156. private static void OnLanguageChange(CurrentLanguage fromLanguage, CurrentLanguage toLanguage)
  157. {
  158. if (toLanguage == CurrentLanguage.ChineseSimplified)
  159. {
  160. ResourceManager.SetActive(PlazaRoomLabel.WelcomeSprite, false);
  161. ResourceManager.SetActive(PlazaRoomLabel.HeartSprite, true);
  162. }
  163. else
  164. {
  165. ResourceManager.SetActive(PlazaRoomLabel.WelcomeSprite, true);
  166. ResourceManager.SetActive(PlazaRoomLabel.HeartSprite, false);
  167. }
  168. }
  169. public void OnPointerClick(PointerEventData eventData)
  170. {
  171. SFSManager.GardenSmartFox.PlazaRoomController.MoveTo(HitPositionToDestination(eventData.pointerCurrentRaycast.worldPosition));
  172. }
  173. public static Vector3 HitPositionToDestination(Vector3 hitPosition)
  174. {
  175. return hitPosition + HitPosOffset;
  176. }
  177. }