少年修仙传客户端代码仓库
hch
2023-06-14 f23c81d21c9cc4c9f06e8bed3ebb7ddbe7e15ac3
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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
 
public class TimeMgr : SingletonMonobehaviour<TimeMgr>
 
{
    private Dictionary<Component, TimeItem> timeItems = new Dictionary<Component, TimeItem>();
    private List<TimeItem> timeItemList = new List<TimeItem>();
    private int hourBuff = -1;
    private int minuteBuff = -1;
    public int dayBuff = -1;
    public event Action OnDayEvent;
    public event Action OnHourEvent;
    public event Action OnMinuteEvent;
 
    private void LateUpdate()
    {
        try
        {
            if (dayBuff != TimeUtility.ServerNow.Day)
            {
                if (OnDayEvent != null)
                {
                    OnDayEvent();
                }
                dayBuff = TimeUtility.ServerNow.Day;
            }
            if (hourBuff != TimeUtility.ServerNow.Hour)
            {
                if (OnHourEvent != null)
                {
                    OnHourEvent();
                }
                hourBuff = TimeUtility.ServerNow.Hour;
            }
            if (minuteBuff != TimeUtility.ServerNow.Minute)
            {
                if (OnMinuteEvent != null)
                {
                    OnMinuteEvent();
                }
                minuteBuff = TimeUtility.ServerNow.Minute;
            }
        }
        catch (Exception e)
        {
            DebugEx.Log(e.StackTrace);
        }
        for (int i = 0; i < syntonyList.Count; i++)
        {
            if ((TimeUtility.ServerNow - syntonyList[i].endTime).TotalSeconds > 0)
            {
                if (syntonyList[i].callback != null)
                {
                    syntonyList[i].callback();
                }
                var _type = syntonyList[i].type;
                syntonyList.RemoveAt(i);
                if (OnSyntonyEvent != null)
                {
                    OnSyntonyEvent(_type);
                }
                i--;
            }
        }
        if (timeItems.Count > 0)
        {
            timeItemList.RemoveRange(0, timeItemList.Count);
            foreach (Component item in timeItems.Keys)
            {
                if (item == null) continue;
                timeItemList.Add(timeItems[item]);
            }
            for (int i = 0; i < timeItemList.Count; i++)
            {
                timeItemList[i].Update();
            }
        }
    }
 
    public void Register(Component comp, Action<Component> func, float t, int loopCnt = 1)
    {
        if (comp == null) return;
        if (!timeItems.ContainsKey(comp))
        {
            TimeItem item = new TimeItem(t, comp, loopCnt, func);
            timeItems.Add(comp, item);
        }
    }
 
    public void SetInterval(Component comp, float t)
    {
        if (timeItems.ContainsKey(comp))
        {
            TimeItem item = timeItems[comp];
            item.time = t;
        }
    }
 
    public void UnRegister(Component comp)
    {
        if (comp == null) return;
        if (timeItems.ContainsKey(comp))
        {
            TimeItem item = timeItems[comp];
            timeItems.Remove(comp);
            item = null;
        }
    }
 
    public void Register(SyntonyType type, float _totalTime, Action _func = null)
    {
        int index;
        if (ContainsSyntony(type, out index))
        {
            syntonyList[index].SetTime(_totalTime, _func);
        }
        else
        {
            Syntony syntony = new Syntony();
            syntony.type = type;
            syntony.SetTime(_totalTime, _func);
            syntonyList.Add(syntony);
        }
    }
 
    public void UnRegister(SyntonyType type)
    {
        int index;
        if (ContainsSyntony(type, out index))
        {
            syntonyList.RemoveAt(index);
        }
    }
 
    public struct Syntony
    {
        public SyntonyType type;
        public DateTime endTime { get; private set; }
        public Action callback;
        public void SetTime(float _totalTime, Action _func = null)
        {
            endTime = TimeUtility.ServerNow.AddSeconds(_totalTime);
            callback = _func;
            DebugEx.LogFormat("{0}{1}", type, endTime);
        }
    }
 
    private List<Syntony> syntonyList = new List<Syntony>();
 
    public event Action<SyntonyType> OnSyntonyEvent;
 
    private bool ContainsSyntony(SyntonyType type, out int index)
    {
        index = 0;
        for (int i = 0; i < syntonyList.Count; i++)
        {
            if (syntonyList[i].type == type)
            {
                index = i;
                return true;
            }
        }
        return false;
    }
 
    public enum SyntonyType
    {
        PrayerRedpoint,
        BossShow,
        BossShowName,
        BossShowDialogue,
        BossShowSound,
        FairyLeaugeStage,
        VipExperirnceOverdue,
        HeavenBattleState,
        Realm,
        RealmSit,
        GuardHurtEffect,
        GetFairyInfo,
        Audio,
        OSRedEnvelope,
        AutoPlayVoice,
    }
}