using System; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace vnxbqy.UI { public class ChatBubbleModel : Model, IBeforePlayerDataInitialize, IPlayerLoginOk { public Dictionary chatBubbles = new Dictionary(); public event Action chatBubbleStateRefresh; VipModel vipModel { get { return ModelCenter.Instance.GetModel(); } } bool serverInited = false; public override void Init() { ParseConfig(); PlayerDatas.Instance.playerDataRefreshEvent += PlayerDataRefreshInfoEvent; } public override void UnInit() { PlayerDatas.Instance.playerDataRefreshEvent -= PlayerDataRefreshInfoEvent; } public void OnBeforePlayerDataInitialize() { bubblesIfo.Clear(); serverInited = false; } public void OnPlayerLoginOk() { serverInited = true; } private void PlayerDataRefreshInfoEvent(PlayerDataType refreshType) { if (refreshType == PlayerDataType.ExAttr10 && serverInited) { SysNotifyMgr.Instance.ShowTip("ChangeBubbleSuccess"); } } void ParseConfig() { var configs = ChatBubbleBoxConfig.GetValues(); for (int i = 0; i < configs.Count; i++) { var config = configs[i]; if (chatBubbles.ContainsKey(config.ID)) { continue; } var bubble = new ChatBubble(); bubble.id = config.ID; bubble.leftPadding = new RectOffset() { left = config.leftOffset.Length > 0 ? config.leftOffset[0] : 0, right = config.leftOffset.Length > 1 ? config.leftOffset[1] : 0, top = config.leftOffset.Length > 2 ? config.leftOffset[2] : 0, bottom = config.leftOffset.Length > 3 ? config.leftOffset[3] : 0, }; bubble.rifhtPadding = new RectOffset() { left = config.rightOffset.Length > 0 ? config.rightOffset[0] : 0, right = config.rightOffset.Length > 1 ? config.rightOffset[1] : 0, top = config.rightOffset.Length > 2 ? config.rightOffset[2] : 0, bottom = config.rightOffset.Length > 3 ? config.rightOffset[3] : 0, }; bubble.color = new Color32() { r = (byte)(config.color.Length > 0 ? config.color[0] : 0), g = (byte)(config.color.Length > 1 ? config.color[1] : 0), b = (byte)(config.color.Length > 2 ? config.color[2] : 0), a = (byte)(config.color.Length > 3 ? config.color[3] : 255), }; chatBubbles.Add(config.ID, bubble); } } public bool TryGetBubble(int id, out ChatBubble bubble) { return chatBubbles.TryGetValue(id, out bubble); } public bool IsBubbleGot(int id) { ChatBubble bubble; if (TryGetBubble(id, out bubble)) { var config = ChatBubbleBoxConfig.Get(id); bool got = false; if (bubblesIfo.ContainsKey(id) && bubblesIfo[id].State == 1) { got = true; } if (!got && config.NeedLV != 0) { return PlayerDatas.Instance.baseData.LV >= config.NeedLV; } return got; } return false; } #region 服务端数据 //聊天气泡框 public struct BubbleBox { public int State; //是否已激活 public int EndTime; //到期时间戳,0为永久 public int Star; //星级 } public Dictionary bubblesIfo = new Dictionary(); public void UpdateBubbleState(HA717_tagMCChatBubbleBoxState package) { List list = null; if (serverInited) { list = new List(); foreach (var id in chatBubbles.Keys) { if (!IsBubbleGot(id)) { list.Add(id); } } } for (int i = 0; i < package.Count; i++) { var info = package.BoxList[i]; bubblesIfo[info.BoxID] = new BubbleBox() { State = info.State, EndTime = (int)info.EndTime, Star = info.Star, }; } if (serverInited) { if (list != null) { for (int i = 0; i < list.Count; i++) { if (IsBubbleGot(list[i])) { SendUseBubble(list[i]); break; } } } } if (chatBubbleStateRefresh != null) { chatBubbleStateRefresh(); } } public void SendUseBubble(int id) { if (!IsBubbleGot(id)) { return; } if (id == PlayerDatas.Instance.baseData.bubbleId) { return; } CA230_tagCMSetChatBubbleBox pak = new CA230_tagCMSetChatBubbleBox(); pak.BubbleBoxType = (byte)id; GameNetSystem.Instance.SendInfo(pak); } #endregion public struct ChatBubble { public int id; public RectOffset leftPadding; public RectOffset rifhtPadding; public Color32 color; public string GetBubbleIcon(bool left, ref bool isFlip) { var config = ChatBubbleBoxConfig.Get(id); isFlip = false; if (left) { if (string.IsNullOrEmpty(config.leftBubbleIcon)) { isFlip = true; return config.rightBubbleIcon; } return config.leftBubbleIcon; } else { if (string.IsNullOrEmpty(config.rightBubbleIcon)) { isFlip = true; return config.leftBubbleIcon; } return config.rightBubbleIcon; } } } } }