using UnityEngine; using System.Collections.Generic; using System.Linq; using LitJson; using Sfs2X.Core; using Sfs2X.Entities; using Sfs2X.Entities.Data; using Sfs2X.Requests; using Sfs2X.Entities.Variables; using UnityEngine.UI; public class PlazaRoomManager { public class PlazaRoomPlayer { public Player Player; public Transform NickName; public Transform MessageBox; public List TweenRoots; public PlazaRoomPlayer(Player player, string nickname) { Player = player; TweenRoots = new List(); NickName = ManaReso.Get("NickName", Folder.UI, false, ManaReso.Get("W_HudParent"), false, ObjType.NickName); HudTarget hudTarget = NickName.GetComponent(); if (hudTarget == null) { hudTarget = NickName.AddComponent(); } hudTarget.PosTra = Player.ChildDic["NickName"]; NickName.GetComponent().text = string.IsNullOrEmpty(nickname) ? Language.GetStr("UI", "未命名") : nickname; MessageBox = ManaReso.Get("MessageBox", Folder.UI, false, ManaReso.Get("W_HudParent"), false, ObjType.MessageBox); hudTarget = MessageBox.GetComponent(); if (hudTarget == null) { hudTarget = MessageBox.AddComponent(); } hudTarget.PosTra = Player.ChildDic["MessageBox"]; MessageBox.CreateTweenCG(0, 1, 0.25f, true, true, Curve.EaseOutQuad).InOrigin = true; } public void Save() { foreach (var tweenRoot in TweenRoots) { tweenRoot.Pause(); } ManaReso.Save(Player); ManaReso.Save(NickName); ManaReso.Save(MessageBox); } public bool IsMoving; public float Speed = 1f; public Vector3 Destination; public TweenRoot MoveTween; public void MoveTo(Vector3 destination) { if (destination.Equal(Player.transform.position)) { return; } if (destination.x > Player.transform.position.x) { Player.Flip(PlayerDirection.Right); } else { Player.Flip(PlayerDirection.Left); } float duration = Mathf.Clamp(Vector3.Magnitude(destination - Player.transform.position) / 1.5f, 0.25f, 10f) / Speed; MoveTween = Player.transform.CreateTweenVec3D(destination, duration, false, true, true, Curve.EaseOutQuad); MoveTween.StartForward(); MoveTween.AddEventOnetime(EventType.ForwardFinish, StopMove); TweenRoots.Add(MoveTween); IsMoving = true; Destination = destination; Player.PlayAnim(Player.WalkAnimationName); } public void StopMove() { IsMoving = false; Player.PlayAnim(Player.IdleAnimationName); } public float MessgeTime = 3f; public Coroutine MessgeCoroutine; public void ShowMessage(string message) { if (MessgeCoroutine != null) { Auxiliary.Instance.StopCoroutine(MessgeCoroutine); } MessageBox.GetChild(0).GetComponent().text = message; MessageBox.TweenForCG(); MessgeCoroutine = Auxiliary.Instance.DelayCall(() => { MessageBox.TweenBacCG(); }, MessgeTime); } } #region Variable public GardenSmartFox GardenSmartFox; public User SelfUser; public PlazaRoomPlayer SelfInstance; public Room CurrentPlazaRoom; public bool InPlazaRoom { get { return CurrentPlazaRoom != null; } } public bool EnteringPlazaRoom; public bool IsBlackMaskFinish; public RequestStatus JoinRoomStatus; public Dictionary UserInstanceDictionary = new Dictionary(); #endregion public void OnEnterPlazaButton() { IsBlackMaskFinish = false; EnteringPlazaRoom = true; ManaCenter.SceneSwitchLock = true; TweenRoot tweenRoot = ManaReso.Get("V_BlackMask").TweenBacCG(); tweenRoot.AddEventOnetime(EventType.BackwardFinish,() =>{IsBlackMaskFinish = true;TryEnterPlazaRoom();}); GardenSmartFox.ExecuteAfterCheckConection ( () => { GardenSmartFox.PlazaRoomManager.JoinRoomStatus = RequestStatus.Pending; GardenSmartFox.ExtensionManager.PlazaRoomExtension.JoinPlazaRoom(); }, (succeed, baseEvent) => { if (!succeed) { JoinRoomStatus = RequestStatus.Failed; TryEnterPlazaRoom(); } }, (succeed, baseEvent) => { if (!succeed) { JoinRoomStatus = RequestStatus.Failed; TryEnterPlazaRoom(); } else { //GardenSmartFox.PlazaRoomManager.SyncClose(); } } ); } public void TryEnterPlazaRoom() { if (!IsBlackMaskFinish) { return; } if (!EnteringPlazaRoom) { return; } if (GardenSmartFox.PlazaRoomManager.JoinRoomStatus == RequestStatus.Failed) { QuitPlazaRoom(); Bubble.Show(null, Language.GetStr("UI", "加入房间失败"), null, null, () => { ManaReso.Get("V_BlackMask").TweenForCG();},null,false); EnteringPlazaRoom = false; } else if (GardenSmartFox.PlazaRoomManager.JoinRoomStatus == RequestStatus.Succeed) { EnterPlazaRoom(); EnteringPlazaRoom = false; } } public void EnterPlazaRoom() { GardenSmartFox.PlazaRoomManager.PlazaRoomCamera = ManaReso.Get("MainCamera"); GardenSmartFox.PlazaRoomManager.CameraOriginPosition = PlazaRoomCamera.transform.position; GardenSmartFox.PlazaRoomManager.CameraLeftBorder = ManaReso.Get("PlazaRoomCameraLeftBorder").position.x; GardenSmartFox.PlazaRoomManager.CameraRightBorder = ManaReso.Get("PlazaRoomCameraRightBorder").position.x; ManaReso.Get("V_BlackMask").TweenForCG(); ManaReso.SetActive("C_Main", false); ManaReso.SetActive("Garden", false); ManaReso.SetActive("PlazaRoom", true); ManaReso.SetActive("W_HudParent", true); } public void ExitPlazaRoom() { QuitPlazaRoom(); TweenRoot tweenRoot = ManaReso.Get("V_BlackMask").TweenBacCG(); tweenRoot.AddEventOnetime ( EventType.BackwardFinish, () => { PlazaRoomCamera.transform.position = CameraOriginPosition; ManaReso.SetActive("C_Main", true); ManaReso.SetActive("Garden", true); ManaReso.SetActive("PlazaRoom", false); ManaReso.SetActive("W_HudParent", false); ManaReso.Get("V_BlackMask").TweenForCG(); foreach (var kv in UserInstanceDictionary) { kv.Value.Save(); } UserInstanceDictionary = new Dictionary(); } ); } public void QuitPlazaRoom() { ManaCenter.SceneSwitchLock = false; } public PlazaRoomManager(GardenSmartFox gardenSmartFox) { GardenSmartFox = gardenSmartFox; GardenSmartFox.ExtensionManager.PlazaRoomExtension.onExitPlazaRoom += OnExitPlazaRoom; GardenSmartFox.ExtensionManager.PlazaRoomExtension.onJoinPlazaRoom += OnJoinPlazaRoom; GardenSmartFox.ExtensionManager.PlazaRoomExtension.onUserExitCurrentPlazaRoom += OnUserExitPlazaRoom; GardenSmartFox.ExtensionManager.PlazaRoomExtension.onUserEnterCurrentPlazaRoom += OnUserEnterPlazaRoom; GardenSmartFox.ExtensionManager.PlazaRoomExtension.onSynchronize += Synchronize; GardenSmartFox.ExtensionManager.PlazaRoomExtension.onReceivePublicMessage += ReceivePublicMessage; GardenSmartFox.ExtensionManager.PlazaRoomExtension.onInstantiate += Instantiate; GardenSmartFox.Connector.onConnectionLost += OnConectionLost; } public void Update() { if (!InPlazaRoom) return; KeepAliveThread(); CameraControllThread(); } public float KeepAliveTime = 30; public float KeepAliveTimer; public void KeepAliveThread() { KeepAliveTimer += Time.deltaTime; if (KeepAliveTimer > KeepAliveTime) { KeepAliveTimer = 0; GardenSmartFox.AddRequest(GardenSmartFox.ConstructCommandParameter(CommandID.KeepAlive.GetHashCode(), new SFSObject(), 0), RequestType.Immediate); } } public float CameraLeftBorder; public float CameraRightBorder; public Vector3 CameraOriginPosition; public Camera PlazaRoomCamera; public void CameraControllThread() { if (PlazaRoomCamera == null) { return; } float x; if (SelfInstance.Player.transform.position.x <= CameraLeftBorder) { x = Mathf.Lerp(PlazaRoomCamera.transform.position.x, CameraLeftBorder, Time.deltaTime); } else if (SelfInstance.Player.transform.position.x >= CameraRightBorder) { x = Mathf.Lerp(PlazaRoomCamera.transform.position.x, CameraRightBorder, Time.deltaTime); } else { x = Mathf.Lerp(PlazaRoomCamera.transform.position.x, SelfInstance.Player.transform.position.x, Time.deltaTime); } PlazaRoomCamera.transform.SetX(x); } public void OnConectionLost(BaseEvent baseEvent) { if (InPlazaRoom) { OnExitPlazaRoom(); } } public void OnExitPlazaRoom() { CurrentPlazaRoom = null; ExitPlazaRoom(); } public void OnJoinPlazaRoom(Room room) { CurrentPlazaRoom = room; SelfUser = GardenSmartFox.User; SelfInstance = InstantiatePlayer(SelfUser, ManaNickName.NickName, ManaReso.Get("PlazaRoomDefaultPosition").position, PlayerDirection.Left, ManaData.GetDressDataIDs(ManaPlayer.Player)); UserInstanceDictionary.Add(SelfUser, SelfInstance); JoinRoomStatus = RequestStatus.Succeed; TryEnterPlazaRoom(); SendInstantiateRequset(-1); } public void OnUserExitPlazaRoom(User user) { UserInstanceDictionary[user].Save(); UserInstanceDictionary.Remove(user); } public void OnUserEnterPlazaRoom(User user) { SendInstantiateRequset(user); } public void Synchronize(User sender, SFSObject parameter) { if (!UserInstanceDictionary.ContainsKey(sender)) return; if (sender.IsItMe) return; if (parameter.ContainsKey(DataID.Destination.GetHashString())) { Vector3 destination = parameter.GetText(DataID.Destination.GetHashString()).StringToVector(); SynchronizeDestination(destination, sender); } if (parameter.ContainsKey(DataID.Close.GetHashString())) { List closeIDs = parameter.GetIntArray(DataID.Destination.GetHashString()).ToList(); SynchronizeClose(closeIDs, sender); } //Debug.LogWarning(sender.IsItMe + " : " + jsonData.ToJson()); } public void SynchronizeClose(List closeIDs, User sender) { Player player = UserInstanceDictionary[sender].Player; foreach (var closeID in closeIDs) { CloseUnit closeUnit = ManaPlayer.CloseUnitDic[closeID]; player.ChangeClose(closeUnit.BodyPart, closeUnit.ArmatureName); } } public void SynchronizeDestination(Vector3 destination, User sender) { UserInstanceDictionary[sender].MoveTo(destination); } public void ReceivePublicMessage(string message, User sender) { UserInstanceDictionary[sender].ShowMessage(message); } public void Instantiate(User user, SFSObject parameter) { if (user.IsItMe) return; UserInstanceDictionary.Add(user, InstantiatePlayer(user, parameter)); } public void SendInstantiateRequset(User receiver) { SendInstantiateRequset(receiver.Id); } public void SendInstantiateRequset(int receiverID) { SFSObject parameter = new SFSObject(); parameter.PutInt(DataID.SenderID.GetHashString(), GardenSmartFox.User.Id); parameter.PutIntArray(DataID.Close.GetHashString(), ManaData.GetDressDataIDs(SelfInstance.Player).ToArray()); parameter.PutText(DataID.Position.GetHashString(), SelfInstance.Player.transform.position.VectorToString()); parameter.PutInt(DataID.PlayerDirection.GetHashString(), SelfInstance.Player.PlayerDirection.GetHashCode()); parameter.PutText(DataID.NickName.GetHashString(), ManaNickName.NickName); GardenSmartFox.AddRequest(GardenSmartFox.ConstructCommandParameter(CommandID.Instantiate.GetHashCode(), parameter, receiverID), RequestType.Immediate); if (SelfInstance.IsMoving) { SendSynchronizeDestination(SelfInstance.Destination); } } public void SendSynchronizeDestination(Vector3 destination) { GardenSmartFoxManager.GardenSmartFox.ExtensionManager.PlazaRoomExtension.SendSynchronizeDestination(destination); } public PlazaRoomPlayer InstantiatePlayer(User user, string nickName, Vector3 position, PlayerDirection direction, List dressDataIDs) { Transform tra = ManaReso.Get("Player", Folder.Scene, false, ManaReso.Get("PlazaRoom"), false, ObjType.Player); Player player = tra.GetComponent(); if (player == null) { player = tra.AddScript(); player.Build(); } foreach (var id in dressDataIDs) { CloseUnit closeUnit = ManaPlayer.CloseUnitDic[id]; closeUnit.ChangeDress(player); } player.SetAllCollider(false); player.transform.position = position; player.Flip(direction); tra.localScale = new Vector3(0.5f, 0.5f, 0.5f); return new PlazaRoomPlayer(player, nickName); } public PlazaRoomPlayer InstantiatePlayer(User user, SFSObject parameter) { List dressDatas = ManaData.GetDressDataIDs(parameter); Vector3 position = parameter.GetText(DataID.Position.GetHashString()).StringToVector(); PlayerDirection direction = (PlayerDirection) parameter.GetInt(DataID.PlayerDirection.GetHashString()); string nickName = parameter.GetText(DataID.NickName.GetHashString()); PlazaRoomPlayer plazaRoomPlayer = InstantiatePlayer(user, nickName, position, direction, dressDatas); return plazaRoomPlayer; } }