Repository navigation
Expand file tree
/
Copy pathCManagerRandomTable.cs
More file actions
275 lines (222 loc) · 7.48 KB
/
Copy pathCManagerRandomTable.cs
File metadata and controls
275 lines (222 loc) · 7.48 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/* ============================================
Editor : Strix
Date : 2017-06-03 오후 11:48:05
Description :
Edit Log :
============================================ */
public interface IRandomItem
{
int IRandomItem_GetPercent();
}
public class CManagerRandomTable<CLASS_Resource> : CSingletonBase_Not_UnityComponent<CManagerRandomTable<CLASS_Resource>>
where CLASS_Resource : class, IRandomItem
{
/* const & readonly declaration */
/* enum & struct declaration */
public enum ERandomGetMode
{
Peek,
Delete,
}
/* public - Field declaration */
/* private - Field declaration */
private List<CLASS_Resource> _listRandomTable = new List<CLASS_Resource>();
private List<CLASS_Resource> _listRandomTable_Delete = new List<CLASS_Resource>();
private List<CLASS_Resource> _listRandomTable_Temp = new List<CLASS_Resource>();
private HashSet<CLASS_Resource> _setWinTable_OnDelete = new HashSet<CLASS_Resource>();
private ERandomGetMode _eRandomGetMode = ERandomGetMode.Peek;
private int _iTotalValue = 0;
private int _iTotalValue_Decrease_OnDelete = 0;
// ========================================================================== //
/* public - [Do] Function
* 외부 객체가 호출 */
public void DoClearRandomItemTable()
{
_listRandomTable.Clear();
}
public void DoAddRandomItem(CLASS_Resource pRandomItem)
{
_listRandomTable.Add( pRandomItem );
_listRandomTable.Sort( ComparerRandomItem );
int iPercent = pRandomItem.IRandomItem_GetPercent();
if (iPercent <= 0)
{
// Debug.LogWarning("Percent is less or equal to zero" + pRandomItem.ToString());
return;
}
_iTotalValue += iPercent;
//Debug.Log(pRandomItem.ToString() + iPercent);
if (_eRandomGetMode == ERandomGetMode.Delete)
{
_listRandomTable_Delete.Add(pRandomItem);
_listRandomTable_Delete.Sort(ComparerRandomItem);
}
}
public void DoAddRandomItem_Range(IEnumerable<CLASS_Resource> listRandomItem)
{
IEnumerator<CLASS_Resource> pIter = listRandomItem.GetEnumerator();
while(pIter.MoveNext())
{
DoAddRandomItem( pIter.Current );
}
}
public CLASS_Resource GetItem( string strItemName )
{
for (int i = 0; i < _listRandomTable.Count; i++)
{
MonoBehaviour pMono = _listRandomTable[i] as MonoBehaviour;
if(pMono != null)
{
if (pMono.name.Equals( strItemName ))
return _listRandomTable[i];
}
}
return null;
}
public CLASS_Resource GetRandomItem( List<CLASS_Resource> listRandomItem )
{
int iMaxValue = 0;
for (int i = 0; i < listRandomItem.Count; i++)
iMaxValue += listRandomItem[i].IRandomItem_GetPercent();
return ProcGetRandomItem( iMaxValue, listRandomItem );
}
public CLASS_Resource GetRandomItem( List<CLASS_Resource> listRandomItem, int iLimitValue )
{
int iMaxValue = 0;
for (int i = 0; i < listRandomItem.Count; i++)
iMaxValue += listRandomItem[i].IRandomItem_GetPercent();
ProcGetList_CutGreaterMaxValue_MoveToTempList( iLimitValue, listRandomItem );
return ProcGetRandomItem( iLimitValue, _listRandomTable_Temp );
}
public CLASS_Resource GetRandomItem()
{
if (_eRandomGetMode == ERandomGetMode.Peek)
return ProcGetRandomItem_Peek(_iTotalValue);
else
return ProcGetRandomItem_Delete(_iTotalValue - _iTotalValue_Decrease_OnDelete);
}
public CLASS_Resource GetRandomItem(int iMaxValue)
{
CLASS_Resource pRandomItem = null;
if(_eRandomGetMode == ERandomGetMode.Peek)
ProcGetList_CutGreaterMaxValue_MoveToTempList(iMaxValue, _listRandomTable);
else
ProcGetList_CutGreaterMaxValue_MoveToTempList(iMaxValue, _listRandomTable_Delete);
if (_listRandomTable_Temp.Count != 0)
{
int iRandomIndex = Random.Range(0, _listRandomTable_Temp.Count);
if (_eRandomGetMode == ERandomGetMode.Delete)
{
do
{
if (_setWinTable_OnDelete.Contains( _listRandomTable_Temp[iRandomIndex] ))
{
_listRandomTable_Temp.RemoveAt( iRandomIndex );
iRandomIndex = Random.Range( 0, _listRandomTable_Temp.Count );
continue;
}
else
break;
} while (_setWinTable_OnDelete.Contains( _listRandomTable_Temp[iRandomIndex] ) && _listRandomTable_Temp.Count != 0);
pRandomItem = _listRandomTable_Temp[iRandomIndex];
ProcAddDeleteItem( pRandomItem );
}
else
pRandomItem = ProcGetRandomItem( iMaxValue, _listRandomTable_Temp );
}
return pRandomItem;
}
public void DoSetRandomMode(ERandomGetMode eRandomMode)
{
_eRandomGetMode = eRandomMode;
}
public void DoReset_OnDeleteMode()
{
_setWinTable_OnDelete.Clear();
_iTotalValue_Decrease_OnDelete = 0;
_listRandomTable_Delete.Clear();
_listRandomTable_Delete.AddRange(_listRandomTable);
}
public float DoCalculatePercent(CLASS_Resource pItem, int iMaxValue = 0)
{
if (iMaxValue == 0)
iMaxValue = _iTotalValue;
int iItemPercentValue = pItem.IRandomItem_GetPercent();
float fPercent = (float)iItemPercentValue / (float)_iTotalValue;
return fPercent * 100;
}
/* public - [Event] Function
프랜드 객체가 호출 */
// ========================================================================== //
/* private - [Proc] Function
중요 로직을 처리 */
private CLASS_Resource ProcGetRandomItem_Peek(int iMaxValue)
{
if (iMaxValue > _iTotalValue)
iMaxValue = _iTotalValue;
return ProcGetRandomItem(iMaxValue, _listRandomTable);
}
private CLASS_Resource ProcGetRandomItem_Delete(int iMaxValue)
{
int iTotalValue = iMaxValue - _iTotalValue_Decrease_OnDelete;
if (iMaxValue > iTotalValue)
iMaxValue = iTotalValue;
CLASS_Resource pRandomItem = ProcGetRandomItem(iMaxValue, _listRandomTable_Delete);
ProcAddDeleteItem(pRandomItem);
return pRandomItem;
}
private CLASS_Resource ProcGetRandomItem(int iMaxValue, List<CLASS_Resource> listTable)
{
if (listTable.Count == 1)
return listTable[0];
CLASS_Resource pRandomItem = null;
int iRandomValue = Random.Range(0, iMaxValue);
int iCheckValue = 0;
for (int i = 0; i < listTable.Count; i++)
{
CLASS_Resource pRandomItemCurrent = listTable[i];
iCheckValue += pRandomItemCurrent.IRandomItem_GetPercent();
if (iRandomValue < iCheckValue)
{
pRandomItem = pRandomItemCurrent;
break;
}
}
return pRandomItem;
}
private void ProcGetList_CutGreaterMaxValue_MoveToTempList(int iMaxValue, List<CLASS_Resource> listTable)
{
_listRandomTable_Temp.Clear();
for (int i = 0; i < listTable.Count; i++)
{
CLASS_Resource pRandomItemCurrent = listTable[i];
if (pRandomItemCurrent.IRandomItem_GetPercent() <= iMaxValue)
_listRandomTable_Temp.Add(pRandomItemCurrent);
else
break;
}
}
/* private - Other[Find, Calculate] Function
찾기, 계산 등의 비교적 단순 로직 */
private int ComparerRandomItem(CLASS_Resource pResourceX, CLASS_Resource pResourceY)
{
int iValueX = pResourceX.IRandomItem_GetPercent();
int iValueY = pResourceY.IRandomItem_GetPercent();
if (iValueX < iValueY)
return -1;
else if (iValueX > iValueY)
return 1;
else
return 0;
}
private void ProcAddDeleteItem(CLASS_Resource pItem)
{
if (pItem == null) return;
_listRandomTable_Delete.Remove(pItem);
_iTotalValue_Decrease_OnDelete += pItem.IRandomItem_GetPercent();
_setWinTable_OnDelete.Add(pItem);
}
}