PlazaRoomManager.cs 19 KB

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