-
Notifications
You must be signed in to change notification settings - Fork 1
/
task_manager.cpp
146 lines (121 loc) · 2.48 KB
/
task_manager.cpp
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
#include "task_manager.h"
CTaskManager *g_pTaskManager = NULL;
CTaskManager::CTaskProp::CTaskProp(edict_t * owner, task_handle_t handle, float time, task_types_t type, int tid, int repeat)
{
m_pOwner = owner;
m_pHandle = handle;
m_flCallTime = time;
m_flStartTime = gpGlobals->time;
m_iType = type;
m_iID = tid;
m_iRepeat = repeat;
}
void CTaskManager::SetTask(edict_t *owner, task_handle_t handle, float time, task_types_t type, int tid, int repeat)
{
//get unique index
tid += (owner ? ENTINDEX(owner) : 0);
auto pThisTask = new CTaskProp
(
owner,
handle,
time,
type,
tid,
repeat
);
m_pTaskVector.push_back(pThisTask);
}
void CTaskManager::RemoveTask(edict_t *owner, int tid)
{
//get unique index
tid += (owner ? ENTINDEX(owner) : 0);
auto it = m_pTaskVector.begin();
while (it != m_pTaskVector.end())
{
auto pThisTask = (*it);
if (pThisTask->getIndex() == tid)
{
delete pThisTask;
m_pTaskVector.erase(it);
break;
}
it++;
}
}
bool CTaskManager::IsExists(edict_t *owner, int tid)
{
//get unique index
tid += (owner ? ENTINDEX(owner) : 0);
for (auto it : m_pTaskVector)
{
if (it->getIndex() == tid)
return true;
}
return false;
}
void CTaskManager::ClearTaskByOwner(edict_t *owner)
{
auto it = m_pTaskVector.begin();
while (it != m_pTaskVector.end())
{
auto pThisTask = (*it);
if (pThisTask->getOwner() == owner)
{
delete pThisTask;
m_pTaskVector.erase(it);
break;
}
it++;
}
}
void CTaskManager::StartFrame(float time)
{
if (m_pTaskVector.empty() || m_flNextFrame > gpGlobals->time)
{
return;
}
m_flNextFrame = gpGlobals->time + time;
auto it = m_pTaskVector.begin();
while (it != m_pTaskVector.end())
{
auto pThisTask = (*it);
if (!pThisTask->isExpired())
{
it++;
continue;
}
switch (pThisTask->getType())
{
case TASK_NORMAL:
pThisTask->callHandle();
delete pThisTask;
it = m_pTaskVector.erase(it);
break;
case TASK_LOOP:
if (pThisTask->callHandle() == TASK_DESTROY)
{
delete pThisTask;
it = m_pTaskVector.erase(it);
}
else
{
pThisTask->updateCurrentTime();
it++;
}
break;
case TASK_REPEAT:
if (!pThisTask->isFinished())
{
pThisTask->callHandle();
pThisTask->updateCurrentTime();
it++;
}
else
{
delete pThisTask;
it = m_pTaskVector.erase(it);
}
break;
}
}
}