SFSPlazaRoomManager.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. using System;
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using LitJson;
  6. using Sfs2X.Core;
  7. using Sfs2X.Entities;
  8. using Sfs2X.Entities.Data;
  9. using Sfs2X.Requests;
  10. using Sfs2X.Entities.Variables;
  11. using UnityEngine.UI;
  12. public enum JoinRoomResult
  13. {
  14. RoomFullError = 0,
  15. RoomExpireError = 1,
  16. Unknown = 2,
  17. Succeed = 3,
  18. }
  19. public class SFSPlazaRoomManager
  20. {
  21. public class PlazaRoomPlayer
  22. {
  23. #region Config
  24. public string NickName;
  25. public Player Player;
  26. public Transform NickNameTransform;
  27. public Transform MessageBox;
  28. public List<TweenRoot> TweenRoots;
  29. #endregion
  30. public PlazaRoomPlayer(Player player, string nickname)
  31. {
  32. Player = player;
  33. TweenRoots = new List<TweenRoot>();
  34. NickNameTransform = ManaReso.Get("NickName", Folder.UI, false, ManaReso.Get("W_HudParent"), false, ObjType.NickName);
  35. HudTarget hudTarget = NickNameTransform.GetComponent<HudTarget>();
  36. if (hudTarget == null)
  37. {
  38. hudTarget = NickNameTransform.AddComponent<HudTarget>();
  39. }
  40. hudTarget.PosTra = Player.ChildDic["NickName"];
  41. hudTarget.SetPosition();
  42. NickName = string.IsNullOrEmpty(nickname) ? Language.GetStr("UI", "未命名") : nickname;
  43. NickNameTransform.GetComponent<Text>().text = NickName;
  44. MessageBox = ManaReso.Get("MessageBox", Folder.UI, false, ManaReso.Get("W_HudParent"), false, ObjType.MessageBox);
  45. MessageBox.GetComponentInChildren<BestfitText>().VerticalMinSize = 12;
  46. MessageBox.GetComponentInChildren<BestfitText>().VerticalMaxSize = 20;
  47. hudTarget = MessageBox.GetComponent<HudTarget>();
  48. if (hudTarget == null)
  49. {
  50. hudTarget = MessageBox.AddComponent<HudTarget>();
  51. }
  52. hudTarget.PosTra = Player.ChildDic["MessageBox"];
  53. MessageBox.CreateTweenCG(0, 1, 0.25f, true, true, Curve.EaseOutQuad).InOrigin = true;
  54. }
  55. public void Save()
  56. {
  57. foreach (var tweenRoot in TweenRoots)
  58. {
  59. tweenRoot.Pause();
  60. }
  61. ManaReso.Save(Player);
  62. ManaReso.Save(NickNameTransform);
  63. ManaReso.Save(MessageBox);
  64. Player.PlayAnim(Player.IdleAnimationName);
  65. }
  66. public bool IsMoving;
  67. public float Speed = 1f;
  68. public Vector3 Destination;
  69. public TweenVec MoveTween;
  70. public void MoveTo(Vector3 destination)
  71. {
  72. if (destination.Equal(Player.transform.position))
  73. {
  74. return;
  75. }
  76. if (destination.x > Player.transform.position.x)
  77. {
  78. Player.Flip(PlayerDirection.Right);
  79. }
  80. else
  81. {
  82. Player.Flip(PlayerDirection.Left);
  83. }
  84. float duration = Mathf.Clamp(Vector3.Magnitude(destination - Player.transform.position) / 1.5f, 0.25f, 10f) / Speed;
  85. MoveTween = Player.transform.CreateTweenVec3D(destination, duration, false, true, true, Curve.EaseOutQuad);
  86. MoveTween.StartForward();
  87. MoveTween.AddEventOnetime(EventType.ForwardFinish, StopMove);
  88. TweenRoots.Add(MoveTween);
  89. IsMoving = true;
  90. Destination = destination;
  91. Player.PlayAnim(Player.WalkAnimationName);
  92. }
  93. public void StopMove()
  94. {
  95. IsMoving = false;
  96. Player.PlayAnim(Player.IdleAnimationName);
  97. }
  98. public float MessgeTime = 3f;
  99. public Coroutine MessgeCoroutine;
  100. public void ShowMessage(string message)
  101. {
  102. if (MessgeCoroutine != null)
  103. {
  104. Auxiliary.Instance.StopCoroutine(MessgeCoroutine);
  105. }
  106. MessageBox.GetChild(0).GetComponent<Text>().text = message;
  107. MessageBox.TweenForCG();
  108. MessgeCoroutine = Auxiliary.Instance.DelayCall(() => { MessageBox.TweenBacCG(); }, MessgeTime);
  109. }
  110. }
  111. #region Variable
  112. public GardenSmartFox GardenSmartFox;
  113. public PlazaRoomPlayer SelfInstance;
  114. public Room CurrentPlazaRoom;
  115. public bool JoinedPlazaRoom
  116. {
  117. get { return CurrentPlazaRoom != null; }
  118. }
  119. public bool InPlazaRoom;
  120. public bool IsBlackMaskFinish;
  121. public bool EnteringPlazaRoom;
  122. public JoinRoomResult JoinRoomResult;
  123. public Dictionary<int, PlazaRoomPlayer> UserInstanceDictionary = new Dictionary<int, PlazaRoomPlayer>();
  124. #endregion
  125. public void BeginEnterPlazaRoom()
  126. {
  127. IsBlackMaskFinish = false;
  128. JoinRoomResult = JoinRoomResult.Unknown;
  129. ManaReso.Get("B_SignIn0").TweenBacCG();
  130. ManaReso.Get("T_NickName").TweenBacCG();
  131. ManaCenter.SceneSwitchLock = true;
  132. TweenRoot tweenRoot = ManaReso.Get("V_BlackMask").TweenBacCG();
  133. tweenRoot.AddEventOnetime
  134. (
  135. EventType.BackwardFinish, () =>
  136. {
  137. IsBlackMaskFinish = true;
  138. TryEnterPlazaRoom();
  139. }
  140. );
  141. GardenSmartFox.ExecuteAfterCheckConection
  142. (
  143. null,
  144. (succeed, baseEvent) =>
  145. {
  146. if (!succeed)
  147. {
  148. JoinRoomResult = JoinRoomResult.Unknown;
  149. TryEnterPlazaRoom();
  150. }
  151. },
  152. (succeed, baseEvent) =>
  153. {
  154. if (!succeed)
  155. {
  156. JoinRoomResult = JoinRoomResult.Unknown;
  157. TryEnterPlazaRoom();
  158. }
  159. }
  160. );
  161. }
  162. public void TryEnterPlazaRoom()
  163. {
  164. if (!GardenSmartFox.PlazaRoomManager.IsBlackMaskFinish)
  165. return;
  166. if (JoinRoomResult == JoinRoomResult.Succeed)
  167. {
  168. EnterPlazaRoom();
  169. EnteringPlazaRoom = false;
  170. }
  171. else
  172. {
  173. QuitPlazaRoom();
  174. if (JoinRoomResult == JoinRoomResult.RoomFullError)
  175. {
  176. Bubble.Show(null, Language.GetStr("UI", "Z_RoomFull"), null, null, () => { ManaReso.Get("V_BlackMask").TweenForCG(); }, null, false);
  177. }
  178. else if (JoinRoomResult == JoinRoomResult.RoomExpireError)
  179. {
  180. Bubble.Show(null, Language.GetStr("UI", "Z_RoomExpire"), null, null, () => { ManaReso.Get("V_BlackMask").TweenForCG(); }, null, false);
  181. }
  182. else
  183. {
  184. Bubble.Show(null, Language.GetStr("UI", "Z_Unknown"), null, null, () => { ManaReso.Get("V_BlackMask").TweenForCG(); }, null, false);
  185. }
  186. EnteringPlazaRoom = false;
  187. }
  188. }
  189. private Vector3 PlayerDefaultPosition;
  190. public void EnterPlazaRoom()
  191. {
  192. PlazaRoom.Initialize();
  193. foreach (var kv in UserInstanceDictionary)
  194. {
  195. if (kv.Value.Player.transform.parent == null)
  196. kv.Value.Player.transform.parent = ManaReso.Get("PlazaRoom");
  197. }
  198. InPlazaRoom = true;
  199. PlazaRoomMge.ClosePanel();
  200. ManaGarden.RetrieveAllElf();
  201. GardenSmartFox.PlazaRoomManager.PlazaRoomSky = ManaReso.Get("PlazaRoomSky");
  202. SkyOriginPosition = GardenSmartFox.PlazaRoomManager.PlazaRoomSky.position;
  203. GardenSmartFox.PlazaRoomManager.PlazaRoomCamera = ManaReso.Get<Camera>("MainCamera");
  204. GardenSmartFox.PlazaRoomManager.CameraOriginPosition = GardenSmartFox.PlazaRoomManager.PlazaRoomCamera.transform.position;
  205. PlayerDefaultPosition = ManaReso.Get("PlazaRoomDefaultPosition").position;
  206. SelfInstance = InstantiatePlayer(ManaNickName.NickName, PlayerDefaultPosition, PlayerDirection.Left, ManaData.GetDressDataIDs(ManaPlayer.Player));
  207. SelfInstance.Player.transform.position += new Vector3(0, 0, -0.001f);
  208. UserInstanceDictionary.Add(SFSManager.GardenSmartFox.User.Id, SelfInstance);
  209. SendInstantiateRequset(-1);
  210. GardenSmartFox.PlazaRoomManager.CameraLeftBorder = ManaReso.Get("PlazaRoomCameraLeftBorder").position.x;
  211. GardenSmartFox.PlazaRoomManager.CameraRightBorder = ManaReso.Get("PlazaRoomCameraRightBorder").position.x;
  212. GardenSmartFox.PlazaRoomManager.PlazaRoomCamera.transform.SetX(GardenSmartFox.PlazaRoomManager.CameraLeftBorder);
  213. ManaReso.Get("V_BlackMask").TweenForCG();
  214. ManaReso.SetActive("C_Main2", false);
  215. ManaReso.SetActive("Garden", false);
  216. ManaReso.SetActive("PlazaRoom", true);
  217. ManaReso.SetActive("W_HudParent", true);
  218. ManaReso.SetActive("X_PlazaRoom", true);
  219. }
  220. public void ExitPlazaRoom()
  221. {
  222. QuitPlazaRoom();
  223. TweenRoot tweenRoot = ManaReso.Get("V_BlackMask").TweenBacCG();
  224. tweenRoot.AddEventOnetime
  225. (
  226. EventType.BackwardFinish,
  227. () =>
  228. {
  229. PlazaRoomSky.position = SkyOriginPosition;
  230. PlazaRoomCamera.transform.position = CameraOriginPosition;
  231. ManaReso.SetActive("C_Main2", true);
  232. ManaReso.SetActive("Garden", true);
  233. ManaReso.SetActive("PlazaRoom", false);
  234. ManaReso.SetActive("W_HudParent", false);
  235. ManaReso.SetActive("X_PlazaRoom", false);
  236. ManaReso.Get("V_BlackMask").TweenForCG();
  237. ManaReso.Get("B_SignIn0").TweenForCG();
  238. ManaReso.Get("T_NickName").TweenForCG();
  239. foreach (var kv in UserInstanceDictionary)
  240. {
  241. kv.Value.Save();
  242. }
  243. UserInstanceDictionary = new Dictionary<int, PlazaRoomPlayer>();
  244. }
  245. );
  246. }
  247. public void QuitPlazaRoom()
  248. {
  249. ManaCenter.SceneSwitchLock = false;
  250. InPlazaRoom = false;
  251. GardenSmartFox.SmartFox.Send(new LeaveRoomRequest(CurrentPlazaRoom));
  252. GardenSmartFox.PlazaRoomManager.CurrentPlazaRoom = null;
  253. }
  254. public SFSPlazaRoomManager(GardenSmartFox gardenSmartFox)
  255. {
  256. GardenSmartFox = gardenSmartFox;
  257. gardenSmartFox.Connector.onConnectionLost += OnConectionLost;
  258. }
  259. public void Update()
  260. {
  261. if (!JoinedPlazaRoom)
  262. return;
  263. if (EnteringPlazaRoom)
  264. return;
  265. //KeepAliveThread();
  266. CameraControllThread();
  267. }
  268. public float KeepAliveTime = 30;
  269. public float KeepAliveTimer;
  270. public void KeepAliveThread()
  271. {
  272. KeepAliveTimer += Time.deltaTime;
  273. if (KeepAliveTimer > KeepAliveTime)
  274. {
  275. KeepAliveTimer = 0;
  276. SFSObject parameter = new SFSObject();
  277. parameter.PutInt(Label.CommandID, PlazaRoomReq.KeepAlive.GetHashCode());
  278. GardenSmartFox.AddRequest(parameter, RequestType.Immediate);
  279. }
  280. }
  281. public float CameraLeftBorder;
  282. public float CameraRightBorder;
  283. public Vector3 CameraOriginPosition;
  284. public Camera PlazaRoomCamera;
  285. public void CameraControllThread()
  286. {
  287. if (PlazaRoomCamera == null)
  288. {
  289. return;
  290. }
  291. if (!InPlazaRoom)
  292. {
  293. return;
  294. }
  295. float x;
  296. if (SelfInstance.Player.transform.position.x <= CameraLeftBorder)
  297. {
  298. x = Mathf.Lerp(PlazaRoomCamera.transform.position.x, CameraLeftBorder, Time.deltaTime);
  299. if (SkyTween != null)
  300. SkyTween.Pause();
  301. }
  302. else if (SelfInstance.Player.transform.position.x >= CameraRightBorder)
  303. {
  304. x = Mathf.Lerp(PlazaRoomCamera.transform.position.x, CameraRightBorder, Time.deltaTime);
  305. if (SkyTween != null)
  306. SkyTween.Pause();
  307. }
  308. else
  309. {
  310. x = Mathf.Lerp(PlazaRoomCamera.transform.position.x, SelfInstance.Player.transform.position.x, Time.deltaTime);
  311. }
  312. PlazaRoomCamera.transform.SetX(x);
  313. }
  314. public void MoveTo(Vector3 destination)
  315. {
  316. SFSManager.GardenSmartFox.PlazaRoomManager.SelfInstance.MoveTo(destination);
  317. SFSManager.GardenSmartFox.PlazaRoomManager.SendSynchronizeDestination(destination);
  318. MoveSky(SelfInstance.MoveTween.Duration, SelfInstance.MoveTween.Delta);
  319. }
  320. private float SkyMoveRatio = 0.1f;
  321. private Vector3 SkyOriginPosition;
  322. private TweenRoot SkyTween;
  323. public Transform PlazaRoomSky;
  324. private void MoveSky(float duration, Vector3 offset)
  325. {
  326. offset *= -SkyMoveRatio;
  327. offset.y = 0;
  328. offset.z = 0;
  329. SkyTween = PlazaRoomSky.transform.CreateTweenVecOffset2D(offset, duration, false, true, true, Curve.Linear);
  330. SkyTween.StartForward();
  331. }
  332. public void OnConectionLost(BaseEvent baseEvent)
  333. {
  334. if (JoinedPlazaRoom)
  335. {
  336. OnExitPlazaRoom();
  337. }
  338. }
  339. public void OnExitPlazaRoom()
  340. {
  341. ExitPlazaRoom();
  342. }
  343. public void OnJoinPlazaRoom(Room room)
  344. {
  345. GardenSmartFox.PlazaRoomManager.CurrentPlazaRoom = room;
  346. JoinRoomResult = JoinRoomResult.Succeed;
  347. TryEnterPlazaRoom();
  348. }
  349. public void OnJoinPlazaRoomError(JoinRoomResult joinRoomResult)
  350. {
  351. this.JoinRoomResult = joinRoomResult;
  352. TryEnterPlazaRoom();
  353. }
  354. public void OnUserExitPlazaRoom(int userID)
  355. {
  356. UserInstanceDictionary[userID].Save();
  357. UserInstanceDictionary.Remove(userID);
  358. }
  359. public void OnUserEnterPlazaRoom(int senderID)
  360. {
  361. SendInstantiateRequset(senderID);
  362. }
  363. public void Synchronize(int senderID, ISFSObject parameter)
  364. {
  365. if (!UserInstanceDictionary.ContainsKey(senderID))
  366. return;
  367. if (SFSManager.GardenSmartFox.User.Id == senderID)
  368. return;
  369. if (parameter.ContainsKey(InfoLabel.Destination.GetHashString()))
  370. {
  371. Vector3 destination = parameter.GetUtfString(InfoLabel.Destination.GetHashString()).StringToVector();
  372. SynchronizeDestination(destination, senderID);
  373. }
  374. if (parameter.ContainsKey(InfoLabel.Close.GetHashString()))
  375. {
  376. List<int> closeIDs = parameter.GetIntArray(InfoLabel.Destination.GetHashString()).ToList();
  377. SynchronizeClose(closeIDs, senderID);
  378. }
  379. }
  380. public void SynchronizeClose(List<int> closeIDs, int senderID)
  381. {
  382. Player player = UserInstanceDictionary[senderID].Player;
  383. foreach (var closeID in closeIDs)
  384. {
  385. CloseUnit closeUnit = ManaPlayer.CloseUnitDic[closeID];
  386. player.ChangeClose(closeUnit.BodyPart, closeUnit.ArmatureName);
  387. }
  388. }
  389. public void SynchronizeDestination(Vector3 destination, int senderID)
  390. {
  391. UserInstanceDictionary[senderID].MoveTo(destination);
  392. }
  393. private float ExpressionDuration = 1;
  394. public void ReceiveExpression(int expressionID, int senderID)
  395. {
  396. PlazaRoomPlayer plazaRoomPlayer = UserInstanceDictionary[senderID];
  397. string expressionName = Enum.GetName(typeof(ExpressionID), expressionID);
  398. plazaRoomPlayer.Player.ChangeExpression(expressionName, ExpressionDuration);
  399. string message = $"{plazaRoomPlayer.NickName}:\u3000<({expressionName}按钮)>";
  400. ManaInfoBox.Show(InfoCategory.PlazaRoom, message, Mathf.Infinity, ManaReso.LoadSprite("Expression", Folder.Scene));
  401. ManaReso.SetText("X_CurrentInfoLab", message);
  402. }
  403. public void ReceivePublicMessage(string message, int senderID)
  404. {
  405. PlazaRoomPlayer plazaRoomPlayer = UserInstanceDictionary[senderID];
  406. plazaRoomPlayer.ShowMessage(message);
  407. message = $"{plazaRoomPlayer.NickName}:\u3000{message}";
  408. ManaInfoBox.Show(InfoCategory.PlazaRoom, message, Mathf.Infinity, ManaReso.LoadSprite("Expression", Folder.Scene));
  409. ManaReso.SetText("X_CurrentInfoLab", message);
  410. }
  411. public void OnInstantiate(int senderID, ISFSObject parameter)
  412. {
  413. if (SFSManager.GardenSmartFox.User.Id == senderID)
  414. return;
  415. UserInstanceDictionary.Add(senderID, InstantiatePlayer(parameter));
  416. }
  417. public void SendExpression(ExpressionID expressionID)
  418. {
  419. SFSManager.GardenSmartFox.EventManager.PlazaRoomEvent.SendExpression(expressionID);
  420. }
  421. public bool SendPublicMessage(string message)
  422. {
  423. if (string.IsNullOrEmpty(message))
  424. {
  425. Bubble.Show(null, Language.GetStr("UI", "内容不能为空"));
  426. return false;
  427. }
  428. else
  429. {
  430. message = StringFilter.GetFilteredString(message);
  431. SFSManager.GardenSmartFox.EventManager.PlazaRoomEvent.SendPublicMessage(message);
  432. return true;
  433. }
  434. }
  435. public void SendInstantiateRequset(int receiverID)
  436. {
  437. SFSObject parameter = new SFSObject();
  438. parameter.PutInt(InfoLabel.SenderID.GetHashString(), GardenSmartFox.User.Id);
  439. parameter.PutIntArray(InfoLabel.Close.GetHashString(), ManaData.GetDressDataIDs(SelfInstance.Player).ToArray());
  440. parameter.PutUtfString(InfoLabel.Position.GetHashString(), SelfInstance.Player.transform.position.VectorToString());
  441. parameter.PutInt(InfoLabel.PlayerDirection.GetHashString(), SelfInstance.Player.PlayerDirection.GetHashCode());
  442. parameter.PutUtfString(InfoLabel.NickName.GetHashString(), ManaNickName.NickName);
  443. GardenSmartFox.AddRequest(PlazaRoomEvent.WrapInfoRequest(CurrentPlazaRoom.Id, receiverID, InfoID.Instantiate, parameter), RequestType.Immediate);
  444. if (SelfInstance.IsMoving)
  445. {
  446. SendSynchronizeDestination(SelfInstance.Destination);
  447. }
  448. }
  449. public void SendSynchronizeDestination(Vector3 destination)
  450. {
  451. SFSManager.GardenSmartFox.EventManager.PlazaRoomEvent.SendSynchronizeDestination(destination);
  452. }
  453. public PlazaRoomPlayer InstantiatePlayer(string nickName, Vector3 position, PlayerDirection direction, List<int> dressDataIDs)
  454. {
  455. Transform parent = ManaReso.Get("PlazaRoom", false);
  456. Transform tra = ManaReso.Get("Player", Folder.Scene, false, parent, false, ObjType.Player);
  457. Player player = tra.GetComponent<Player>();
  458. if (player == null)
  459. {
  460. player = tra.AddScript<Player>();
  461. player.Build();
  462. }
  463. foreach (var id in dressDataIDs)
  464. {
  465. CloseUnit closeUnit = ManaPlayer.CloseUnitDic[id];
  466. closeUnit.ChangeDress(player);
  467. }
  468. player.SetAllCollider(false);
  469. player.transform.position = position;
  470. player.Flip(direction);
  471. tra.localScale = new Vector3(0.525f, 0.525f, 0.525f);
  472. return new PlazaRoomPlayer(player, nickName);
  473. }
  474. public PlazaRoomPlayer InstantiatePlayer(ISFSObject parameter)
  475. {
  476. List<int> dressDatas = ManaData.GetDressDataIDs(parameter);
  477. Vector3 position = parameter.GetUtfString(InfoLabel.Position.GetHashString()).StringToVector();
  478. PlayerDirection direction = (PlayerDirection) parameter.GetInt(InfoLabel.PlayerDirection.GetHashString());
  479. string nickName = parameter.GetUtfString(InfoLabel.NickName.GetHashString());
  480. PlazaRoomPlayer plazaRoomPlayer = InstantiatePlayer(nickName, position, direction, dressDatas);
  481. return plazaRoomPlayer;
  482. }
  483. }