PlazaRoomManager.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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. }
  141. );
  142. }
  143. public void TryEnterPlazaRoom()
  144. {
  145. if (!GardenSmartFox.PlazaRoomManager.IsBlackMaskFinish)
  146. {
  147. return;
  148. }
  149. if (!GardenSmartFox.PlazaRoomManager.EnteringPlazaRoom)
  150. {
  151. return;
  152. }
  153. if (GardenSmartFox.PlazaRoomManager.JoinRoomStatus == RequestStatus.Pending)
  154. {
  155. return;
  156. }
  157. if (GardenSmartFox.PlazaRoomManager.JoinRoomStatus == RequestStatus.Failed)
  158. {
  159. QuitPlazaRoom();
  160. Bubble.Show(null, Language.GetStr("UI", "加入房间失败"), null, null, () => { ManaReso.Get("V_BlackMask").TweenForCG();},null,false);
  161. EnteringPlazaRoom = false;
  162. }
  163. else if (GardenSmartFox.PlazaRoomManager.JoinRoomStatus == RequestStatus.Succeed)
  164. {
  165. EnterPlazaRoom();
  166. EnteringPlazaRoom = false;
  167. }
  168. }
  169. public void EnterPlazaRoom()
  170. {
  171. GardenSmartFox.PlazaRoomManager.CameraLeftBorder = ManaReso.Get("PlazaRoomCameraLeftBorder").position.x;
  172. GardenSmartFox.PlazaRoomManager.CameraRightBorder = ManaReso.Get("PlazaRoomCameraRightBorder").position.x;
  173. GardenSmartFox.PlazaRoomManager.PlazaRoomCamera.transform.SetX(GardenSmartFox.PlazaRoomManager.CameraLeftBorder);
  174. ManaReso.Get("V_BlackMask").TweenForCG();
  175. ManaReso.SetActive("C_Main2", false);
  176. ManaReso.SetActive("Garden", false);
  177. ManaReso.SetActive("PlazaRoom", true);
  178. ManaReso.SetActive("W_HudParent", true);
  179. ManaReso.SetActive("X_PlazaRoom", true);
  180. }
  181. public void ExitPlazaRoom()
  182. {
  183. QuitPlazaRoom();
  184. TweenRoot tweenRoot = ManaReso.Get("V_BlackMask").TweenBacCG();
  185. tweenRoot.AddEventOnetime
  186. (
  187. EventType.BackwardFinish,
  188. () =>
  189. {
  190. PlazaRoomSky.position = SkyOriginPosition;
  191. PlazaRoomCamera.transform.position = CameraOriginPosition;
  192. ManaReso.SetActive("C_Main2", true);
  193. ManaReso.SetActive("Garden", true);
  194. ManaReso.SetActive("PlazaRoom", false);
  195. ManaReso.SetActive("W_HudParent", false);
  196. ManaReso.SetActive("X_PlazaRoom", false);
  197. ManaReso.Get("V_BlackMask").TweenForCG();
  198. foreach (var kv in UserInstanceDictionary)
  199. {
  200. kv.Value.Save();
  201. }
  202. UserInstanceDictionary = new Dictionary<User, PlazaRoomPlayer>();
  203. }
  204. );
  205. }
  206. public void QuitPlazaRoom()
  207. {
  208. ManaCenter.SceneSwitchLock = false;
  209. GardenSmartFox.SmartFox.Send(new LeaveRoomRequest(CurrentPlazaRoom));
  210. GardenSmartFox.PlazaRoomManager.CurrentPlazaRoom = null;
  211. }
  212. public PlazaRoomManager(GardenSmartFox gardenSmartFox)
  213. {
  214. GardenSmartFox = gardenSmartFox;
  215. GardenSmartFox.ExtensionManager.PlazaRoomExtension.onExitPlazaRoom += OnExitPlazaRoom;
  216. GardenSmartFox.ExtensionManager.PlazaRoomExtension.onJoinPlazaRoom += OnJoinPlazaRoom;
  217. GardenSmartFox.ExtensionManager.PlazaRoomExtension.onUserExitCurrentPlazaRoom += OnUserExitPlazaRoom;
  218. GardenSmartFox.ExtensionManager.PlazaRoomExtension.onUserEnterCurrentPlazaRoom += OnUserEnterPlazaRoom;
  219. GardenSmartFox.ExtensionManager.PlazaRoomExtension.onSynchronize += Synchronize;
  220. GardenSmartFox.ExtensionManager.PlazaRoomExtension.onReceivePublicMessage += ReceivePublicMessage;
  221. GardenSmartFox.ExtensionManager.PlazaRoomExtension.onInstantiate += Instantiate;
  222. gardenSmartFox.Connector.onConnectionLost += OnConectionLost;
  223. }
  224. public void Update()
  225. {
  226. if (!InPlazaRoom)
  227. return;
  228. if (EnteringPlazaRoom)
  229. return;
  230. KeepAliveThread();
  231. CameraControllThread();
  232. }
  233. public float KeepAliveTime = 30;
  234. public float KeepAliveTimer;
  235. public void KeepAliveThread()
  236. {
  237. KeepAliveTimer += Time.deltaTime;
  238. if (KeepAliveTimer > KeepAliveTime)
  239. {
  240. KeepAliveTimer = 0;
  241. GardenSmartFox.AddRequest(GardenSmartFox.ConstructCommandParameter(CommandID.KeepAlive.GetHashCode(), new SFSObject(), 0), RequestType.Immediate);
  242. }
  243. }
  244. public float CameraLeftBorder;
  245. public float CameraRightBorder;
  246. public Vector3 CameraOriginPosition;
  247. public Camera PlazaRoomCamera;
  248. public void CameraControllThread()
  249. {
  250. if (PlazaRoomCamera == null)
  251. {
  252. return;
  253. }
  254. float x;
  255. if (SelfInstance.Player.transform.position.x <= CameraLeftBorder)
  256. {
  257. x = Mathf.Lerp(PlazaRoomCamera.transform.position.x, CameraLeftBorder, Time.deltaTime);
  258. if (SkyTween != null)
  259. SkyTween.Pause();
  260. }
  261. else if (SelfInstance.Player.transform.position.x >= CameraRightBorder)
  262. {
  263. x = Mathf.Lerp(PlazaRoomCamera.transform.position.x, CameraRightBorder, Time.deltaTime);
  264. if (SkyTween != null)
  265. SkyTween.Pause();
  266. }
  267. else
  268. {
  269. x = Mathf.Lerp(PlazaRoomCamera.transform.position.x, SelfInstance.Player.transform.position.x, Time.deltaTime);
  270. }
  271. PlazaRoomCamera.transform.SetX(x);
  272. }
  273. public void MoveTo(Vector3 destination)
  274. {
  275. GardenSmartFoxManager.GardenSmartFox.PlazaRoomManager.SelfInstance.MoveTo(destination);
  276. GardenSmartFoxManager.GardenSmartFox.PlazaRoomManager.SendSynchronizeDestination(destination);
  277. MoveSky(SelfInstance.MoveTween.Duration, SelfInstance.MoveTween.Delta);
  278. }
  279. private float SkyMoveRatio = 0.1f;
  280. private Vector3 SkyOriginPosition;
  281. private TweenRoot SkyTween;
  282. public Transform PlazaRoomSky;
  283. private void MoveSky(float duration, Vector3 offset)
  284. {
  285. offset *= -SkyMoveRatio;
  286. offset.y = 0;
  287. offset.z = 0;
  288. SkyTween = PlazaRoomSky.transform.CreateTweenVecOffset2D(offset, duration, false, true, true, Curve.Linear);
  289. SkyTween.StartForward();
  290. }
  291. public void OnConectionLost(BaseEvent baseEvent)
  292. {
  293. if (InPlazaRoom)
  294. {
  295. OnExitPlazaRoom();
  296. }
  297. }
  298. public void OnExitPlazaRoom()
  299. {
  300. ExitPlazaRoom();
  301. }
  302. private Vector3 PlayerDefaultPosition;
  303. public void OnJoinPlazaRoom(Room room)
  304. {
  305. PlazaRoom.Initialize();
  306. GardenSmartFox.PlazaRoomManager.PlazaRoomSky = ManaReso.Get("PlazaRoomSky");
  307. SkyOriginPosition = GardenSmartFox.PlazaRoomManager.PlazaRoomSky.position;
  308. GardenSmartFox.PlazaRoomManager.PlazaRoomCamera = ManaReso.Get<Camera>("MainCamera");
  309. GardenSmartFox.PlazaRoomManager.CameraOriginPosition = GardenSmartFox.PlazaRoomManager.PlazaRoomCamera.transform.position;
  310. GardenSmartFox.PlazaRoomManager.CurrentPlazaRoom = room;
  311. SelfUser = GardenSmartFox.User;
  312. PlayerDefaultPosition = ManaReso.Get("PlazaRoomDefaultPosition").position;
  313. SelfInstance = InstantiatePlayer(SelfUser, ManaNickName.NickName, PlayerDefaultPosition, PlayerDirection.Left, ManaData.GetDressDataIDs(ManaPlayer.Player));
  314. SelfInstance.Player.transform.position += new Vector3(0, 0, -0.001f);
  315. UserInstanceDictionary.Add(SelfUser, SelfInstance);
  316. JoinRoomStatus = RequestStatus.Succeed;
  317. TryEnterPlazaRoom();
  318. SendInstantiateRequset(-1);
  319. }
  320. public void OnUserExitPlazaRoom(User user)
  321. {
  322. UserInstanceDictionary[user].Save();
  323. UserInstanceDictionary.Remove(user);
  324. }
  325. public void OnUserEnterPlazaRoom(User user)
  326. {
  327. SendInstantiateRequset(user);
  328. }
  329. public void Synchronize(User sender, SFSObject parameter)
  330. {
  331. if (!UserInstanceDictionary.ContainsKey(sender))
  332. return;
  333. if (sender.IsItMe)
  334. return;
  335. if (parameter.ContainsKey(DataID.Destination.GetHashString()))
  336. {
  337. Vector3 destination = parameter.GetUtfString(DataID.Destination.GetHashString()).StringToVector();
  338. SynchronizeDestination(destination, sender);
  339. }
  340. if (parameter.ContainsKey(DataID.Close.GetHashString()))
  341. {
  342. List<int> closeIDs = parameter.GetIntArray(DataID.Destination.GetHashString()).ToList();
  343. SynchronizeClose(closeIDs, sender);
  344. }
  345. //Debug.LogWarning(sender.IsItMe + " : " + jsonData.ToJson());
  346. }
  347. public void SynchronizeClose(List<int> closeIDs, User sender)
  348. {
  349. Player player = UserInstanceDictionary[sender].Player;
  350. foreach (var closeID in closeIDs)
  351. {
  352. CloseUnit closeUnit = ManaPlayer.CloseUnitDic[closeID];
  353. player.ChangeClose(closeUnit.BodyPart, closeUnit.ArmatureName);
  354. }
  355. }
  356. public void SynchronizeDestination(Vector3 destination, User sender)
  357. {
  358. UserInstanceDictionary[sender].MoveTo(destination);
  359. }
  360. public void ReceivePublicMessage(string message, User sender)
  361. {
  362. UserInstanceDictionary[sender].ShowMessage(message);
  363. }
  364. public void Instantiate(User user, SFSObject parameter)
  365. {
  366. if (user.IsItMe)
  367. return;
  368. UserInstanceDictionary.Add(user, InstantiatePlayer(user, parameter));
  369. }
  370. //private string LastPublicMessage;
  371. public bool SendPublicMessage(string message)
  372. {
  373. if (string.IsNullOrEmpty(message))
  374. {
  375. Bubble.Show(null, Language.GetStr("UI", "内容不能为空"));
  376. return false;
  377. }
  378. //else if (LastPublicMessage == message)
  379. //{
  380. // Bubble.Show(null, Language.GetStr("UI", "请勿重复发送"));
  381. // return false;
  382. //}
  383. else
  384. {
  385. //LastPublicMessage = message;
  386. message = StringFilter.GetFilteredString(message);
  387. GardenSmartFoxManager.GardenSmartFox.ExtensionManager.PlazaRoomExtension.SendPublicMessage(message);
  388. return true;
  389. }
  390. }
  391. public void SendInstantiateRequset(User receiver)
  392. {
  393. SendInstantiateRequset(receiver.Id);
  394. }
  395. public void SendInstantiateRequset(int receiverID)
  396. {
  397. SFSObject parameter = new SFSObject();
  398. parameter.PutInt(DataID.SenderID.GetHashString(), GardenSmartFox.User.Id);
  399. parameter.PutIntArray(DataID.Close.GetHashString(), ManaData.GetDressDataIDs(SelfInstance.Player).ToArray());
  400. parameter.PutUtfString(DataID.Position.GetHashString(), SelfInstance.Player.transform.position.VectorToString());
  401. parameter.PutInt(DataID.PlayerDirection.GetHashString(), SelfInstance.Player.PlayerDirection.GetHashCode());
  402. parameter.PutUtfString(DataID.NickName.GetHashString(), ManaNickName.NickName);
  403. GardenSmartFox.AddRequest(GardenSmartFox.ConstructCommandParameter(CommandID.Instantiate.GetHashCode(), parameter, receiverID), RequestType.Immediate);
  404. if (SelfInstance.IsMoving)
  405. {
  406. SendSynchronizeDestination(SelfInstance.Destination);
  407. }
  408. }
  409. public void SendSynchronizeDestination(Vector3 destination)
  410. {
  411. GardenSmartFoxManager.GardenSmartFox.ExtensionManager.PlazaRoomExtension.SendSynchronizeDestination(destination);
  412. }
  413. public PlazaRoomPlayer InstantiatePlayer(User user, string nickName, Vector3 position, PlayerDirection direction, List<int> dressDataIDs)
  414. {
  415. Transform tra = ManaReso.Get("Player", Folder.Scene, false, ManaReso.Get("PlazaRoom"), false, ObjType.Player);
  416. Player player = tra.GetComponent<Player>();
  417. if (player == null)
  418. {
  419. player = tra.AddScript<Player>();
  420. player.Build();
  421. }
  422. foreach (var id in dressDataIDs)
  423. {
  424. CloseUnit closeUnit = ManaPlayer.CloseUnitDic[id];
  425. closeUnit.ChangeDress(player);
  426. }
  427. player.SetAllCollider(false);
  428. player.transform.position = position;
  429. player.Flip(direction);
  430. tra.localScale = new Vector3(0.525f, 0.525f, 0.525f);
  431. return new PlazaRoomPlayer(player, nickName);
  432. }
  433. public PlazaRoomPlayer InstantiatePlayer(User user, SFSObject parameter)
  434. {
  435. List<int> dressDatas = ManaData.GetDressDataIDs(parameter);
  436. Vector3 position = parameter.GetUtfString(DataID.Position.GetHashString()).StringToVector();
  437. PlayerDirection direction = (PlayerDirection) parameter.GetInt(DataID.PlayerDirection.GetHashString());
  438. string nickName = parameter.GetUtfString(DataID.NickName.GetHashString());
  439. PlazaRoomPlayer plazaRoomPlayer = InstantiatePlayer(user, nickName, position, direction, dressDatas);
  440. return plazaRoomPlayer;
  441. }
  442. }