PlazaRoomManager.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using LitJson;
  5. using Sfs2X.Core;
  6. using Sfs2X.Entities;
  7. using Sfs2X.Entities.Data;
  8. using Sfs2X.Requests;
  9. using Sfs2X.Entities.Variables;
  10. using UnityEngine.UI;
  11. public class PlazaRoomManager
  12. {
  13. public class PlazaRoomPlayer
  14. {
  15. public Player Player;
  16. public Transform NickName;
  17. public Transform MessageBox;
  18. public List<TweenRoot> TweenRoots;
  19. public PlazaRoomPlayer(Player player, string nickname)
  20. {
  21. Player = player;
  22. TweenRoots = new List<TweenRoot>();
  23. NickName = ManaReso.Get("NickName", Folder.UI, false, ManaReso.Get("W_HudParent"), false, ObjType.NickName);
  24. HudTarget hudTarget = NickName.GetComponent<HudTarget>();
  25. if (hudTarget == null)
  26. {
  27. hudTarget = NickName.AddComponent<HudTarget>();
  28. }
  29. hudTarget.PosTra = Player.ChildDic["NickName"];
  30. NickName.GetComponent<Text>().text = string.IsNullOrEmpty(nickname) ? Language.GetStr("UI", "未命名") : nickname;
  31. MessageBox = ManaReso.Get("MessageBox", Folder.UI, false, ManaReso.Get("W_HudParent"), false, ObjType.MessageBox);
  32. hudTarget = MessageBox.GetComponent<HudTarget>();
  33. if (hudTarget == null)
  34. {
  35. hudTarget = MessageBox.AddComponent<HudTarget>();
  36. }
  37. hudTarget.PosTra = Player.ChildDic["MessageBox"];
  38. MessageBox.CreateTweenCG(0, 1, 0.25f, true, true, Curve.EaseOutQuad).InOrigin = true;
  39. }
  40. public void Save()
  41. {
  42. foreach (var tweenRoot in TweenRoots)
  43. {
  44. tweenRoot.Pause();
  45. }
  46. ManaReso.Save(Player);
  47. ManaReso.Save(NickName);
  48. ManaReso.Save(MessageBox);
  49. }
  50. public bool IsMoving;
  51. public float Speed = 1f;
  52. public Vector3 Destination;
  53. public TweenRoot MoveTween;
  54. public void MoveTo(Vector3 destination)
  55. {
  56. if (destination.Equal(Player.transform.position))
  57. {
  58. return;
  59. }
  60. if (destination.x > Player.transform.position.x)
  61. {
  62. Player.Flip(PlayerDirection.Right);
  63. }
  64. else
  65. {
  66. Player.Flip(PlayerDirection.Left);
  67. }
  68. float duration = Mathf.Clamp(Vector3.Magnitude(destination - Player.transform.position) / 1.5f, 0.25f, 10f) / Speed;
  69. MoveTween = Player.transform.CreateTweenVec3D(destination, duration, false, true, true, Curve.EaseOutQuad);
  70. MoveTween.StartForward();
  71. MoveTween.AddEventOnetime(EventType.ForwardFinish, StopMove);
  72. TweenRoots.Add(MoveTween);
  73. IsMoving = true;
  74. Destination = destination;
  75. Player.PlayAnim(Player.WalkAnimationName);
  76. }
  77. public void StopMove()
  78. {
  79. IsMoving = false;
  80. Player.PlayAnim(Player.IdleAnimationName);
  81. }
  82. public float MessgeTime = 3f;
  83. public Coroutine MessgeCoroutine;
  84. public void ShowMessage(string message)
  85. {
  86. if (MessgeCoroutine != null)
  87. {
  88. Auxiliary.Instance.StopCoroutine(MessgeCoroutine);
  89. }
  90. MessageBox.GetChild(0).GetComponent<Text>().text = message;
  91. MessageBox.TweenForCG();
  92. MessgeCoroutine = Auxiliary.Instance.DelayCall(() => { MessageBox.TweenBacCG(); }, MessgeTime);
  93. }
  94. }
  95. #region Variable
  96. public GardenSmartFox GardenSmartFox;
  97. public User SelfUser;
  98. public PlazaRoomPlayer SelfInstance;
  99. public Room CurrentPlazaRoom;
  100. public bool InPlazaRoom
  101. {
  102. get { return CurrentPlazaRoom != null; }
  103. }
  104. public bool EnteringPlazaRoom;
  105. public bool IsBlackMaskFinish;
  106. public RequestStatus JoinRoomStatus;
  107. public Dictionary<User, PlazaRoomPlayer> UserInstanceDictionary = new Dictionary<User, PlazaRoomPlayer>();
  108. #endregion
  109. public void OnEnterPlazaButton()
  110. {
  111. IsBlackMaskFinish = false;
  112. EnteringPlazaRoom = true;
  113. ManaCenter.SceneSwitchLock = true;
  114. TweenRoot tweenRoot = ManaReso.Get("V_BlackMask").TweenBacCG();
  115. tweenRoot.AddEventOnetime(EventType.BackwardFinish,() =>{IsBlackMaskFinish = true;TryEnterPlazaRoom();});
  116. GardenSmartFox.ExecuteAfterCheckConection
  117. (
  118. () =>
  119. {
  120. GardenSmartFox.PlazaRoomManager.JoinRoomStatus = RequestStatus.Pending;
  121. GardenSmartFox.ExtensionManager.PlazaRoomExtension.JoinPlazaRoom();
  122. },
  123. (succeed, baseEvent) =>
  124. {
  125. if (!succeed)
  126. {
  127. JoinRoomStatus = RequestStatus.Failed;
  128. TryEnterPlazaRoom();
  129. }
  130. },
  131. (succeed, baseEvent) =>
  132. {
  133. if (!succeed)
  134. {
  135. JoinRoomStatus = RequestStatus.Failed;
  136. TryEnterPlazaRoom();
  137. }
  138. else
  139. {
  140. //GardenSmartFox.PlazaRoomManager.SyncClose();
  141. }
  142. }
  143. );
  144. }
  145. public void TryEnterPlazaRoom()
  146. {
  147. if (!IsBlackMaskFinish)
  148. {
  149. return;
  150. }
  151. if (!EnteringPlazaRoom)
  152. {
  153. return;
  154. }
  155. if (GardenSmartFox.PlazaRoomManager.JoinRoomStatus == RequestStatus.Failed)
  156. {
  157. QuitPlazaRoom();
  158. Bubble.Show(null, Language.GetStr("UI", "加入房间失败"), null, null, () => { ManaReso.Get("V_BlackMask").TweenForCG();},null,false);
  159. EnteringPlazaRoom = false;
  160. }
  161. else if (GardenSmartFox.PlazaRoomManager.JoinRoomStatus == RequestStatus.Succeed)
  162. {
  163. EnterPlazaRoom();
  164. EnteringPlazaRoom = false;
  165. }
  166. }
  167. public void EnterPlazaRoom()
  168. {
  169. GardenSmartFox.PlazaRoomManager.PlazaRoomCamera = ManaReso.Get<Camera>("MainCamera");
  170. GardenSmartFox.PlazaRoomManager.CameraOriginPosition = PlazaRoomCamera.transform.position;
  171. GardenSmartFox.PlazaRoomManager.CameraLeftBorder = ManaReso.Get("PlazaRoomCameraLeftBorder").position.x;
  172. GardenSmartFox.PlazaRoomManager.CameraRightBorder = ManaReso.Get("PlazaRoomCameraRightBorder").position.x;
  173. ManaReso.Get("V_BlackMask").TweenForCG();
  174. ManaReso.SetActive("C_Main", false);
  175. ManaReso.SetActive("Garden", false);
  176. ManaReso.SetActive("PlazaRoom", true);
  177. ManaReso.SetActive("W_HudParent", true);
  178. }
  179. public void ExitPlazaRoom()
  180. {
  181. QuitPlazaRoom();
  182. TweenRoot tweenRoot = ManaReso.Get("V_BlackMask").TweenBacCG();
  183. tweenRoot.AddEventOnetime
  184. (
  185. EventType.BackwardFinish,
  186. () =>
  187. {
  188. PlazaRoomCamera.transform.position = CameraOriginPosition;
  189. ManaReso.SetActive("C_Main", true);
  190. ManaReso.SetActive("Garden", true);
  191. ManaReso.SetActive("PlazaRoom", false);
  192. ManaReso.SetActive("W_HudParent", false);
  193. ManaReso.Get("V_BlackMask").TweenForCG();
  194. foreach (var kv in UserInstanceDictionary)
  195. {
  196. kv.Value.Save();
  197. }
  198. UserInstanceDictionary = new Dictionary<User, PlazaRoomPlayer>();
  199. }
  200. );
  201. }
  202. public void QuitPlazaRoom()
  203. {
  204. ManaCenter.SceneSwitchLock = false;
  205. }
  206. public PlazaRoomManager(GardenSmartFox gardenSmartFox)
  207. {
  208. GardenSmartFox = gardenSmartFox;
  209. GardenSmartFox.ExtensionManager.PlazaRoomExtension.onExitPlazaRoom += OnExitPlazaRoom;
  210. GardenSmartFox.ExtensionManager.PlazaRoomExtension.onJoinPlazaRoom += OnJoinPlazaRoom;
  211. GardenSmartFox.ExtensionManager.PlazaRoomExtension.onUserExitCurrentPlazaRoom += OnUserExitPlazaRoom;
  212. GardenSmartFox.ExtensionManager.PlazaRoomExtension.onUserEnterCurrentPlazaRoom += OnUserEnterPlazaRoom;
  213. GardenSmartFox.ExtensionManager.PlazaRoomExtension.onSynchronize += Synchronize;
  214. GardenSmartFox.ExtensionManager.PlazaRoomExtension.onReceivePublicMessage += ReceivePublicMessage;
  215. GardenSmartFox.ExtensionManager.PlazaRoomExtension.onInstantiate += Instantiate;
  216. GardenSmartFox.Connector.onConnectionLost += OnConectionLost;
  217. }
  218. public void Update()
  219. {
  220. if (!InPlazaRoom)
  221. return;
  222. KeepAliveThread();
  223. CameraControllThread();
  224. }
  225. public float KeepAliveTime = 30;
  226. public float KeepAliveTimer;
  227. public void KeepAliveThread()
  228. {
  229. KeepAliveTimer += Time.deltaTime;
  230. if (KeepAliveTimer > KeepAliveTime)
  231. {
  232. KeepAliveTimer = 0;
  233. GardenSmartFox.AddRequest(GardenSmartFox.ConstructCommandParameter(CommandID.KeepAlive.GetHashCode(), new SFSObject(), 0), RequestType.Immediate);
  234. }
  235. }
  236. public float CameraLeftBorder;
  237. public float CameraRightBorder;
  238. public Vector3 CameraOriginPosition;
  239. public Camera PlazaRoomCamera;
  240. public void CameraControllThread()
  241. {
  242. if (PlazaRoomCamera == null)
  243. {
  244. return;
  245. }
  246. float x;
  247. if (SelfInstance.Player.transform.position.x <= CameraLeftBorder)
  248. {
  249. x = Mathf.Lerp(PlazaRoomCamera.transform.position.x, CameraLeftBorder, Time.deltaTime);
  250. }
  251. else if (SelfInstance.Player.transform.position.x >= CameraRightBorder)
  252. {
  253. x = Mathf.Lerp(PlazaRoomCamera.transform.position.x, CameraRightBorder, Time.deltaTime);
  254. }
  255. else
  256. {
  257. x = Mathf.Lerp(PlazaRoomCamera.transform.position.x, SelfInstance.Player.transform.position.x, Time.deltaTime);
  258. }
  259. PlazaRoomCamera.transform.SetX(x);
  260. }
  261. public void OnConectionLost(BaseEvent baseEvent)
  262. {
  263. if (InPlazaRoom)
  264. {
  265. OnExitPlazaRoom();
  266. }
  267. }
  268. public void OnExitPlazaRoom()
  269. {
  270. CurrentPlazaRoom = null;
  271. ExitPlazaRoom();
  272. }
  273. public void OnJoinPlazaRoom(Room room)
  274. {
  275. CurrentPlazaRoom = room;
  276. SelfUser = GardenSmartFox.User;
  277. SelfInstance = InstantiatePlayer(SelfUser, ManaNickName.NickName, ManaReso.Get("PlazaRoomDefaultPosition").position, PlayerDirection.Left, ManaData.GetDressDataIDs(ManaPlayer.Player));
  278. UserInstanceDictionary.Add(SelfUser, SelfInstance);
  279. JoinRoomStatus = RequestStatus.Succeed;
  280. TryEnterPlazaRoom();
  281. SendInstantiateRequset(-1);
  282. }
  283. public void OnUserExitPlazaRoom(User user)
  284. {
  285. UserInstanceDictionary[user].Save();
  286. UserInstanceDictionary.Remove(user);
  287. }
  288. public void OnUserEnterPlazaRoom(User user)
  289. {
  290. SendInstantiateRequset(user);
  291. }
  292. public void Synchronize(User sender, SFSObject parameter)
  293. {
  294. if (!UserInstanceDictionary.ContainsKey(sender))
  295. return;
  296. if (sender.IsItMe)
  297. return;
  298. if (parameter.ContainsKey(DataID.Destination.GetHashString()))
  299. {
  300. Vector3 destination = parameter.GetText(DataID.Destination.GetHashString()).StringToVector();
  301. SynchronizeDestination(destination, sender);
  302. }
  303. if (parameter.ContainsKey(DataID.Close.GetHashString()))
  304. {
  305. List<int> closeIDs = parameter.GetIntArray(DataID.Destination.GetHashString()).ToList();
  306. SynchronizeClose(closeIDs, sender);
  307. }
  308. //Debug.LogWarning(sender.IsItMe + " : " + jsonData.ToJson());
  309. }
  310. public void SynchronizeClose(List<int> closeIDs, User sender)
  311. {
  312. Player player = UserInstanceDictionary[sender].Player;
  313. foreach (var closeID in closeIDs)
  314. {
  315. CloseUnit closeUnit = ManaPlayer.CloseUnitDic[closeID];
  316. player.ChangeClose(closeUnit.BodyPart, closeUnit.ArmatureName);
  317. }
  318. }
  319. public void SynchronizeDestination(Vector3 destination, User sender)
  320. {
  321. UserInstanceDictionary[sender].MoveTo(destination);
  322. }
  323. public void ReceivePublicMessage(string message, User sender)
  324. {
  325. UserInstanceDictionary[sender].ShowMessage(message);
  326. }
  327. public void Instantiate(User user, SFSObject parameter)
  328. {
  329. if (user.IsItMe)
  330. return;
  331. UserInstanceDictionary.Add(user, InstantiatePlayer(user, parameter));
  332. }
  333. public void SendInstantiateRequset(User receiver)
  334. {
  335. SendInstantiateRequset(receiver.Id);
  336. }
  337. public void SendInstantiateRequset(int receiverID)
  338. {
  339. SFSObject parameter = new SFSObject();
  340. parameter.PutInt(DataID.SenderID.GetHashString(), GardenSmartFox.User.Id);
  341. parameter.PutIntArray(DataID.Close.GetHashString(), ManaData.GetDressDataIDs(SelfInstance.Player).ToArray());
  342. parameter.PutText(DataID.Position.GetHashString(), SelfInstance.Player.transform.position.VectorToString());
  343. parameter.PutInt(DataID.PlayerDirection.GetHashString(), SelfInstance.Player.PlayerDirection.GetHashCode());
  344. parameter.PutText(DataID.NickName.GetHashString(), ManaNickName.NickName);
  345. GardenSmartFox.AddRequest(GardenSmartFox.ConstructCommandParameter(CommandID.Instantiate.GetHashCode(), parameter, receiverID), RequestType.Immediate);
  346. if (SelfInstance.IsMoving)
  347. {
  348. SendSynchronizeDestination(SelfInstance.Destination);
  349. }
  350. }
  351. public void SendSynchronizeDestination(Vector3 destination)
  352. {
  353. GardenSmartFoxManager.GardenSmartFox.ExtensionManager.PlazaRoomExtension.SendSynchronizeDestination(destination);
  354. }
  355. public PlazaRoomPlayer InstantiatePlayer(User user, string nickName, Vector3 position, PlayerDirection direction, List<int> dressDataIDs)
  356. {
  357. Transform tra = ManaReso.Get("Player", Folder.Scene, false, ManaReso.Get("PlazaRoom"), false, ObjType.Player);
  358. Player player = tra.GetComponent<Player>();
  359. if (player == null)
  360. {
  361. player = tra.AddScript<Player>();
  362. player.Build();
  363. }
  364. foreach (var id in dressDataIDs)
  365. {
  366. CloseUnit closeUnit = ManaPlayer.CloseUnitDic[id];
  367. closeUnit.ChangeDress(player);
  368. }
  369. player.SetAllCollider(false);
  370. player.transform.position = position;
  371. player.Flip(direction);
  372. tra.localScale = new Vector3(0.5f, 0.5f, 0.5f);
  373. return new PlazaRoomPlayer(player, nickName);
  374. }
  375. public PlazaRoomPlayer InstantiatePlayer(User user, SFSObject parameter)
  376. {
  377. List<int> dressDatas = ManaData.GetDressDataIDs(parameter);
  378. Vector3 position = parameter.GetText(DataID.Position.GetHashString()).StringToVector();
  379. PlayerDirection direction = (PlayerDirection) parameter.GetInt(DataID.PlayerDirection.GetHashString());
  380. string nickName = parameter.GetText(DataID.NickName.GetHashString());
  381. PlazaRoomPlayer plazaRoomPlayer = InstantiatePlayer(user, nickName, position, direction, dressDatas);
  382. return plazaRoomPlayer;
  383. }
  384. }