-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathLevelFunc.cpp
191 lines (167 loc) · 4.6 KB
/
LevelFunc.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
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
#include "LevelFunc.h"
#include "LevelFuncMgr.h"
#include "Object.h"
#include <algorithm>
#include <cmath>
LevelFunc::LevelFunc(WorldCell *pWorldCell, int32_t nSector, int32_t nWall)
{
m_bActive = true;
m_fInterp = 0.0f;
m_nNextState = 0;
m_fScale = 1.0f;
m_fSpeed = 1.0f;
m_fDelta = 0.0f;
m_fAccel = 0.0f;
m_fVel = 0.0f;
m_nNextState = -1;
m_nDelay = 0;
m_nSector = nSector;
m_nWall = nWall;
m_pWorldCell = pWorldCell;
m_ActivateCB = nullptr;
m_SetValueCB = nullptr;
}
LevelFunc::~LevelFunc()
{
for (State *state : m_States)
{
xlDelete state;
}
for (ClientObject *cObj : m_ClientObjects)
{
xlDelete cObj;
}
}
void LevelFunc::AddState(float value, int32_t type, int32_t delay)
{
State *pState = xlNew State;
m_States.push_back( pState );
pState->value = value;
pState->type = type;
pState->delay = delay;
}
void LevelFunc::AddClientObj(Object *pObj, uint32_t uFlags)
{
ClientObject *pClientObj = xlNew ClientObject;
pObj->GetLoc( pClientObj->vInitialPos );
pClientObj->pObj = pObj;
pClientObj->uFlags = uFlags;
m_ClientObjects.push_back( pClientObj );
}
void LevelFunc::SendMessage(int msg, int param0, int param1)
{
if ( msg == IMSG_ACTIVATE )
{
//param0 = items, param1 = mask
if ( !m_bActive )
{
return;
}
if ( m_fInterp != 0.0f ) { return; }
if ( param1 != 0 || param0 != 0 )
{
if ( !(param0&m_nKeyNeeded) && m_nKeyNeeded != 0 ) { return; }
if ( param1 != 0 && (!(m_nEventMask¶m1)) ) { return; }
}
if ( m_States.size() > 0 )
{
m_nNextState = (m_nCurState+1)%m_States.size();
m_fDelta = m_fSpeed*m_fScale / fabsf(m_States[m_nNextState]->value - m_States[m_nCurState]->value);
}
else
{
m_fDelta = m_fSpeed*m_fScale;
}
m_fVel = (m_fAccel == 0.0f) ? 1.0f : 0.0f;
m_fInterp = 0.000001f;
m_nDelay = 0;
LevelFuncMgr::AddToActiveList(this);
}
}
void LevelFunc::Update()
{
if ( !m_bActive )
{
LevelFuncMgr::RemoveFromActiveList(this);
return;
}
if ( m_nDelay > 0 )
{
m_nDelay--;
return;
}
else if ( m_nDelay == 0 )
{
m_nDelay = -1;
}
if ( m_States.size() > 0 )
{
float fPrevInterp = m_fInterp;
if ( m_fSpeed == 0.0f )
{
m_fInterp = 1.0f;
}
else
{
m_fInterp += m_fDelta*m_fVel;
m_fVel = std::min(1.0f, m_fVel+m_fAccel);
}
if ( m_fInterp > 1.0f ) { m_fInterp = 1.0f; }
float value = (1.0f-m_fInterp)*m_States[m_nCurState]->value + m_fInterp*m_States[m_nNextState]->value;
SetValue(m_nSector, value, (m_fSpeed==0.0f)?true:false);
if ( m_fInterp >= 1.0f || m_fSpeed == 0.0f )
{
m_fInterp=0.0f;
m_nCurState = m_nNextState;
if ( m_States[m_nCurState]->type == ST_TIME )
{
m_nDelay = m_States[m_nCurState]->delay;
m_nNextState = (m_nCurState+1)%m_States.size();
m_fDelta = m_fSpeed*m_fScale / fabsf(m_States[m_nNextState]->value - m_States[m_nCurState]->value);
m_fVel = (m_fAccel == 0.0f) ? 1.0f : 0.0f;
}
else
{
if ( m_States[m_nCurState]->type == ST_TERMINATE )
{
m_bActive = false;
}
LevelFuncMgr::RemoveFromActiveList(this);
}
}
}
else
{
//continuous animation.
m_fInterp += m_fSpeed;
SetValue(m_nSector, m_fInterp, false);
}
}
void LevelFunc::SetInitialState(int32_t state, bool bStartOnInit/*=false*/)
{
if ( state == -1 )
state = 0;
m_nInitialState = state;
m_nCurState = state;
m_nNextState = state;
if ( m_States.size() > 0 )
{
SetValue(m_nSector, m_States[state]->value, true);
if ( m_States[state]->type != ST_HOLD || bStartOnInit )
{
m_nNextState = (m_nCurState+1)%m_States.size();
m_fDelta = m_fSpeed*m_fScale / fabsf(m_States[m_nNextState]->value - m_States[m_nCurState]->value);
m_fVel = (m_fAccel == 0.0f) ? 1.0f : 0.0f;
m_fInterp = 0.000001f;
m_nDelay = 0;
if ( m_bActive )
{
LevelFuncMgr::AddToActiveList(this);
}
}
}
else if ( m_bActive )
{
LevelFuncMgr::AddToActiveList(this);
}
}