PlazaRoomManager.cs 18 KB

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