少年修仙传客户端代码仓库
client_Wu Xijin
2019-06-13 033958214c0b16d7e7b93cc821b018c295251867
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.Collections;
using System.Collections.Generic;
 
using UnityEngine;
namespace Snxxz.UI
{
    [XLua.LuaCallCSharp]
    public class ChatBubbleModel : Model, IBeforePlayerDataInitialize, IPlayerLoginOk
    {
        public Dictionary<int, ChatBubble> chatBubbles = new Dictionary<int, ChatBubble>();
 
        public event Action chatBubbleStateRefresh;
 
        VipModel vipModel { get { return ModelCenter.Instance.GetModel<VipModel>(); } }
 
        bool serverInited = false;
        public override void Init()
        {
            ParseConfig();
            PlayerDatas.Instance.playerDataRefreshEvent += PlayerDataRefreshInfoEvent;
        }
 
        public override void UnInit()
        {
            PlayerDatas.Instance.playerDataRefreshEvent -= PlayerDataRefreshInfoEvent;
        }
 
        public void OnBeforePlayerDataInitialize()
        {
            bubbleState = 0;
            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;
                try
                {
                    got = MathUtility.GetBitValue(bubbleState, (ushort)id);
                }
                catch (ArgumentOutOfRangeException)
                {
                    DebugEx.LogError("目前不支持id超过31的聊天框:" + id);
                }
                if (!got && config.NeedLV != 0)
                {
                    return PlayerDatas.Instance.baseData.LV >= config.NeedLV;
                }
                return got;
            }
            return false;
        }
 
        #region 服务端数据
        public uint bubbleState { get; private set; }
        public void UpdateBubbleState(HA717_tagMCChatBubbleBoxState package)
        {
            List<int> list = null;
            if (serverInited)
            {
                list = new List<int>();
                foreach (var id in chatBubbles.Keys)
                {
                    if (!IsBubbleGot(id))
                    {
                        list.Add(id);
                    }
                }
            }
            bubbleState = package.BoxState;
            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;
                }
            }
        }
    }
}